Skip to content

Commit

Permalink
Fix NPE in CodeLens that displays grammar
Browse files Browse the repository at this point in the history
Closes eclipse#1396

Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 authored and angelozerr committed Dec 9, 2022
1 parent c1d2714 commit 74689af
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
//Use consistent indentation
"editor.detectIndentation": false,
"editor.tabSize": 4,
"editor.insertSpaces": false
"editor.insertSpaces": false,
"java.compile.nullAnalysis.mode": "automatic",
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.eclipse.lemminx.utils.XMLPositionUtility;
import org.eclipse.lsp4j.CodeLens;
import org.eclipse.lsp4j.Command;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextDocumentEdit;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
Expand Down Expand Up @@ -91,7 +92,14 @@ private void createReferencedGrammarLenses(ICodeLensRequest request, List<CodeLe
// The DOM document is bound with a schema/grammar, display the referenced
// grammars as Codelens.
boolean canSupportOpenUri = canSupportOpenUri(request);
Range range = XMLPositionUtility.createRange((DOMRange) document.getFirstChild());
Range range;
// If the grammar is bound using a file association, then the file could be empty
if (document.getFirstChild() == null) {
range = new Range();
range.setStart(new Position(0, 0));
} else {
range = XMLPositionUtility.createRange((DOMRange) document.getFirstChild());
}
range.setEnd(range.getStart());
Set<ReferencedGrammarInfo> referencedGrammarInfos = contentModelManager.getReferencedGrammarInfos(document);
for (ReferencedGrammarInfo info : referencedGrammarInfos) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,30 @@ public void referencedGrammarUsingFileAssociation() throws BadLocationException
cl(r(0, 0, 0, 0), " (with file association)", OPEN_URI));
}

@Test
public void referencedGrammarUsingFileAssociationEmptyDocument() throws BadLocationException {
Consumer<XMLLanguageService> configuration = ls -> {
ContentModelManager contentModelManager = ls.getComponent(ContentModelManager.class);
// Use root URI which ends with slash
contentModelManager.setRootURI("src/test/resources/xsd/");
contentModelManager.setFileAssociations(createXSDAssociationsNoNamespaceSchemaLocationLike(""));
};

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

testCodeLensFor(xml, "file:///test/resources.xml", new XMLLanguageService(), //
Collections.singletonList(CodeLensKind.OpenUri), //
configuration, //
cl(r(0, 0, 0, 0), " (with file association)", OPEN_URI));

xml = "";

testCodeLensFor(xml, "file:///test/resources.xml", new XMLLanguageService(), //
Collections.singletonList(CodeLensKind.OpenUri), //
configuration, //
cl(r(0, 0, 0, 0), " (with file association)", OPEN_URI));
}

private static XMLFileAssociation[] createXSDAssociationsNoNamespaceSchemaLocationLike(String baseSystemId) {
XMLFileAssociation resources = new XMLFileAssociation();
resources.setPattern("**/*resources*.xml");
Expand Down

0 comments on commit 74689af

Please sign in to comment.