Skip to content

Commit 29f92cc

Browse files
committed
fixed #782: NPE in hover info when declaration was not found
1 parent 5d38af8 commit 29f92cc

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/languageserver/requests/HoverInfo.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ private static String nearestScopeName(Element n) {
121121
}
122122

123123
public List<Either<String, MarkedString>> description(NameDef n) {
124+
if (n == null) {
125+
return Collections.emptyList();
126+
}
124127
List<Either<String, MarkedString>> result = new ArrayList<>();
125128
String comment = n.attrComment();
126129
if (comment != null && !comment.isEmpty()) {

de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/GetDefinitionTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ public void superConstructorCall() {
6060
testGetDef(testData, "3:4-3:13");
6161
}
6262

63+
@Test
64+
public void nonexistantModule() {
65+
CompletionTestData testData = input(
66+
"package test",
67+
"class A",
68+
" use B|lub"
69+
);
70+
71+
testGetDef(testData, Collections.emptyList());
72+
}
73+
6374

6475
private void testGetDef(CompletionTestData testData, String... expectedPositions) {
6576
testGetDef(testData, Arrays.asList(expectedPositions));
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package tests.wurstscript.tests;
2+
3+
import de.peeeq.wurstio.languageserver.BufferManager;
4+
import de.peeeq.wurstio.languageserver.ModelManager;
5+
import de.peeeq.wurstio.languageserver.ModelManagerImpl;
6+
import de.peeeq.wurstio.languageserver.WFile;
7+
import de.peeeq.wurstio.languageserver.requests.HoverInfo;
8+
import org.eclipse.lsp4j.*;
9+
import org.testng.annotations.Test;
10+
11+
import java.io.File;
12+
import java.util.Collections;
13+
import java.util.List;
14+
import java.util.stream.Collectors;
15+
16+
import static org.testng.Assert.assertEquals;
17+
18+
/**
19+
* tests the get definition functionality.
20+
* <p>
21+
* the position of the cursor is denoted by a bar "|" in the test cases
22+
*/
23+
public class HoverTests extends WurstLanguageServerTest {
24+
25+
26+
@Test
27+
public void nonexistantModule() {
28+
CompletionTestData testData = input(
29+
"package test",
30+
"class A",
31+
" use B|lub"
32+
);
33+
34+
List<String> text = testHoverText(testData);
35+
assertEquals(Collections.emptyList(), text);
36+
}
37+
38+
39+
private List<String> testHoverText(CompletionTestData testData) {
40+
Hover result = getHoverInfo(testData);
41+
if (result.getContents().isLeft()) {
42+
return result.getContents().getLeft()
43+
.stream()
44+
.map(s -> {
45+
if (s.isLeft()) {
46+
return s.getLeft();
47+
} else {
48+
return s.getRight().getValue();
49+
}
50+
})
51+
.collect(Collectors.toList());
52+
} else {
53+
MarkupContent right = result.getContents().getRight();
54+
return Collections.singletonList(right.getValue());
55+
}
56+
}
57+
58+
private Hover getHoverInfo(CompletionTestData testData) {
59+
BufferManager bufferManager = new BufferManager();
60+
File projectPath = new File("./test-output").getAbsoluteFile();
61+
ModelManager modelManager = new ModelManagerImpl(projectPath, bufferManager);
62+
String uri = projectPath.toURI().toString() + "/wurst/test.wurst";
63+
bufferManager.updateFile(WFile.create(uri), testData.buffer);
64+
TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
65+
Position pos = new Position(testData.line, testData.column);
66+
CompletionParams position = new CompletionParams(textDocument, pos);
67+
HoverInfo hoverInfo = new HoverInfo(position, bufferManager);
68+
69+
return hoverInfo.execute(modelManager);
70+
}
71+
72+
73+
}

0 commit comments

Comments
 (0)