Skip to content

Commit

Permalink
Support references in included files for codelens + diagnostics.
Browse files Browse the repository at this point in the history
Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Jan 13, 2023
1 parent a9473d4 commit b4c7d23
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public XMLReferencesCodeLensParticipant(XMLReferencesPlugin plugin) {
@Override
public void doCodeLens(ICodeLensRequest request, List<CodeLens> lenses, CancelChecker cancelChecker) {
DOMDocument document = request.getDocument();
Collection<ReferenceLink> links = SearchEngine.getInstance().searchLinks(document, plugin.getReferencesSettings(),
Collection<ReferenceLink> links = SearchEngine.getInstance().searchLinks(document,
plugin.getReferencesSettings(),
cancelChecker);
if (links.isEmpty()) {
return;
Expand All @@ -57,6 +58,10 @@ public void doCodeLens(ICodeLensRequest request, List<CodeLens> lenses, CancelCh
Map<DOMElement, CodeLens> cache = new HashMap<>();
for (ReferenceLink link : links) {
for (SearchNode to : link.getTos()) {
if (document != to.getOwnerDocument()) {
// The 'to' search node belongs to an included document, ignore it.
continue;
}
// Increment references count Codelens for the given target element
DOMNode toNode = to.getNode();
DOMElement toElement = toNode.isAttribute() ? ((DOMAttr) toNode).getOwnerElement()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public void doDiagnostics(DOMDocument document, List<Diagnostic> diagnostics,

for (ReferenceLink link : links) {
for (SearchNode from : link.getFroms()) {
if (document != from.getOwnerDocument()) {
// The 'from' search node belongs to an included document, ignore it.
continue;
}
// Validate the syntax of from node.
if (!from.isValid()) {
if (from.getValidationStatus() == ValidationStatus.INVALID_PREFIX) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public Collection<ReferenceLink> searchLinks(DOMDocument document, XMLReferences
SearchQuery query = SearchQueryFactory.createQuery(document,
settings, QueryDirection.BOTH);
if (query != null) {
// Search references in included files
query.setSearchInIncludedFiles(true);

final Map<XMLReferenceExpression, ReferenceLink> linksMap = new HashMap<>();
SearchEngine.getInstance().search(query,
(fromSearchNode, toSearchNode, expression) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static org.eclipse.lemminx.XMLAssert.r;
import static org.eclipse.lemminx.client.ClientCommands.SHOW_REFERENCES;

import java.io.File;
import java.util.Arrays;
import java.util.function.Consumer;

Expand Down Expand Up @@ -125,6 +126,32 @@ public void attrToText() throws BadLocationException {
cl(r(3, 7, 3, 13), "1 reference", SHOW_REFERENCES));
}

@Test
public void docbookWithoutInclude() throws BadLocationException {
String xml = "<docbook>\r\n"
+ " <xref linkend=\"s1\" />\r\n"
// [1 reference]
+ " <section id=\"s1\" />\r\n"
+ " <xref linkend=\"ch1\" />\r\n"
// + " <xi:include href=\"sub-book.xml\" />\r\n"
+ "</docbook>";
testCodeLensFor(xml, new File("src/test/resources/xml/docbook.xml").toURI().toString(), //
cl(r(2, 10, 2, 17), "1 reference", SHOW_REFERENCES));
}

@Test
public void docbookWithInclude() throws BadLocationException {
String xml = "<docbook>\r\n"
+ " <xref linkend=\"s1\" />\r\n"
// [3 references]
+ " <section id=\"s1\" />\r\n"
+ " <xref linkend=\"ch1\" />\r\n"
+ " <xi:include href=\"sub-book.xml\" />\r\n"
+ "</docbook>";
testCodeLensFor(xml, new File("src/test/resources/xml/docbook.xml").toURI().toString(), //
cl(r(2, 10, 2, 17), "3 references", SHOW_REFERENCES));
}

private static void testCodeLensFor(String value, String fileURI, CodeLens... expected) {
XMLLanguageService xmlLanguageService = new XMLLanguageService();
xmlLanguageService.getExtensions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import static org.eclipse.lemminx.XMLAssert.d;

import java.io.File;
import java.util.function.Consumer;

import org.eclipse.lemminx.AbstractCacheBasedTest;
Expand Down Expand Up @@ -64,7 +65,6 @@ public void invalidPrefix() throws BadLocationException {
"Undefined reference '#C': nothing that matches the expression '@xml:id' defines 'C'.",
"xml", DiagnosticSeverity.Warning));
}


@Test
public void noUndefinedReferences() throws BadLocationException {
Expand All @@ -76,6 +76,32 @@ public void noUndefinedReferences() throws BadLocationException {
testDiagnosticsFor(xml, "file:///test/foo.xml");
}

@Test
public void docbookWithoutInclude() throws BadLocationException {
String xml = "<docbook>\r\n"
+ " <xref linkend=\"s1\" />\r\n"
+ " <section id=\"s1\" />\r\n"
+ " <xref linkend=\"ch1\" />\r\n"
// + " <xi:include href=\"sub-book.xml\"
// xmlns:xi=\"http://www.w3.org/2001/XInclude\" />\r\n"
+ "</docbook>";
testDiagnosticsFor(xml, new File("src/test/resources/xml/docbook.xml").toURI().toString(),
d(3, 16, 3, 19, XMLReferencesErrorCode.UndefinedReference,
"Undefined reference 'ch1': nothing that matches the expression '@id' defines 'ch1'.",
"xml", DiagnosticSeverity.Warning));
}

@Test
public void docbookWithInclude() throws BadLocationException {
String xml = "<docbook>\r\n"
+ " <xref linkend=\"s1\" />\r\n"
+ " <section id=\"s1\" />\r\n"
+ " <xref linkend=\"ch1\" />\r\n"
+ " <xi:include href=\"sub-book.xml\" xmlns:xi=\"http://www.w3.org/2001/XInclude\" />\r\n"
+ "</docbook>";
testDiagnosticsFor(xml, new File("src/test/resources/xml/docbook.xml").toURI().toString());
}

public static void testDiagnosticsFor(String xml, String fileURI,
Diagnostic... expected) {
Consumer<XMLLanguageService> config = ls -> {
Expand Down
8 changes: 8 additions & 0 deletions org.eclipse.lemminx/src/test/resources/xml/sub-book.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<book>
<chapter id="ch1" />
<chapter id="ch2"/>
<xref linkend="s1" />
<xref linkend="s1" />
<xref linkend="ch1" />
<xref linkend="ch1" />
</book>

0 comments on commit b4c7d23

Please sign in to comment.