Skip to content

Commit

Permalink
Search references in included files for codelens + diagnostics.
Browse files Browse the repository at this point in the history
Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Jan 13, 2023
1 parent 8948c15 commit 37891f9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
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 @@ -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,30 @@ 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"
// + " <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"
// [2 references]
+ " <section id=\"s1\" />\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
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 37891f9

Please sign in to comment.