Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support references in included files for codelens + diagnostics. #1452

Merged
merged 1 commit into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -11,23 +11,18 @@
*******************************************************************************/
package org.eclipse.lemminx.utils;

import java.io.IOException;
import java.io.StringReader;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import org.apache.xerces.parsers.SAXParser;
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.dom.DOMElement;
import org.eclipse.lemminx.dom.DOMNode;
import org.eclipse.lemminx.dom.DOMParser;
import org.eclipse.lemminx.uriresolver.URIResolverExtensionManager;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;

Expand All @@ -37,8 +32,6 @@
*/
public class DOMUtils {

private static final Logger LOGGER = Logger.getLogger(DOMUtils.class.getName());

private static final String XSD_EXTENSION = ".xsd";

// DTD file extensions
Expand Down Expand Up @@ -217,7 +210,6 @@ public static DOMDocument loadDocument(String documentURI, URIResolverExtensionM
return DOMParser.getInstance().parse(IOUtils.convertStreamToString(new URL(documentURI).openStream()),
documentURI, resolverExtensionManager);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error while loading XML Schema '" + documentURI + "'.", e);
return null;
}
}
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>