Skip to content

Commit

Permalink
No hyperlink to DTD source on hover
Browse files Browse the repository at this point in the history
Fixes eclipse#693

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed May 13, 2020
1 parent 25cf8e4 commit 31e5fae
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private CMDocument findCMDocument(String uri, String publicId, String systemId,
try {
Path file = cacheResolverExtension.getCachedResource(resolvedUri);
if (file != null) {
cmDocument = modelProvider.createCMDocument(file.toFile().getPath());
cmDocument = modelProvider.createCMDocument(file.toUri().toString());
}
} catch (CacheResourceDownloadingException e) {
// the DTD/XML Schema is downloading
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -97,7 +96,7 @@ public class XMLAssert {
public static final int NEW_XML_SNIPPETS = 7;

public static final int NEW_XSD_SNIPPETS = 1;

public static final int PROLOG_SNIPPETS = 2;

public static final int REGION_SNIPPETS = 2;
Expand Down Expand Up @@ -577,13 +576,21 @@ public static void assertHover(String value) throws BadLocationException {
assertHover(value, null, null);
}

public static void assertHover(String value, String expectedHoverLabel, Integer expectedHoverOffset)
public static void assertHover(String value, String expectedHoverLabel, Range expectedHoverRange)
throws BadLocationException {
assertHover(new XMLLanguageService(), value, null, null, expectedHoverLabel, expectedHoverOffset);
assertHover(new XMLLanguageService(), value, null, null, expectedHoverLabel, expectedHoverRange);
}

public static void assertHover(XMLLanguageService xmlLanguageService, String value, String catalogPath,
String fileURI, String expectedHoverLabel, Integer expectedHoverOffset) throws BadLocationException {
String fileURI, String expectedHoverLabel, Range expectedHoverRange) throws BadLocationException {
ContentModelSettings settings = new ContentModelSettings();
settings.setUseCache(false);
assertHover(xmlLanguageService, value, catalogPath, fileURI, expectedHoverLabel, expectedHoverRange, settings);
}

public static void assertHover(XMLLanguageService xmlLanguageService, String value, String catalogPath,
String fileURI, String expectedHoverLabel, Range expectedHoverRange, ContentModelSettings settings)
throws BadLocationException {
int offset = value.indexOf("|");
value = value.substring(0, offset) + value.substring(offset + 1);

Expand All @@ -592,8 +599,6 @@ public static void assertHover(XMLLanguageService xmlLanguageService, String val
Position position = document.positionAt(offset);

DOMDocument htmlDoc = DOMParser.getInstance().parse(document, xmlLanguageService.getResolverExtensionManager());
ContentModelSettings settings = new ContentModelSettings();
settings.setUseCache(false);
// Configure XML catalog for XML schema
if (catalogPath != null) {
settings.setCatalogs(new String[] { catalogPath });
Expand All @@ -609,10 +614,8 @@ public static void assertHover(XMLLanguageService xmlLanguageService, String val
} else {
String actualHoverLabel = getHoverLabel(hover);
assertEquals(expectedHoverLabel, actualHoverLabel);
if (expectedHoverOffset != null) {
assertNotNull(hover.getRange());
assertNotNull(hover.getRange().getStart());
assertEquals(expectedHoverOffset.intValue(), hover.getRange().getStart().getCharacter());
if (expectedHoverRange != null) {
assertEquals(hover.getRange(), expectedHoverRange);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
* Seiphon Wang <[email protected]>
*/
package org.eclipse.lemminx.extensions.contentmodel;

import static org.eclipse.lemminx.XMLAssert.r;
import org.apache.xerces.impl.XMLEntityManager;
import org.apache.xerces.util.URI.MalformedURIException;
import org.eclipse.lemminx.XMLAssert;
import org.eclipse.lemminx.commons.BadLocationException;
import org.eclipse.lemminx.extensions.contentmodel.settings.ContentModelSettings;
import org.eclipse.lemminx.services.XMLLanguageService;
import org.eclipse.lemminx.uriresolver.CacheResourcesManager;
import org.eclipse.lsp4j.Range;
import org.junit.jupiter.api.Test;

public class DTDHoverExtensionsTest {
Expand All @@ -29,14 +32,14 @@ public void testTagHover() throws BadLocationException, MalformedURIException {
"<!DOCTYPE service-builder PUBLIC \"-//Liferay//DTD Service Builder 7.2.0//EN\" \"http://www.liferay.com/dtd/liferay-service-builder_7_2_0.dtd\">"
+ "<service-builder dependency-injector=\"ds\" package-path=\"testSB\"></servi|ce-builder>";
assertHover(xml,"The service-builder element is the root of the deployment descriptor for" + //
" a Service Builder descriptor that is used to generate services available to" +
" portlets. The Service Builder saves the developer time by generating Spring" +
" utilities, SOAP utilities, and Hibernate persistence classes to ease the" +
" a Service Builder descriptor that is used to generate services available to" + //
" portlets. The Service Builder saves the developer time by generating Spring" + //
" utilities, SOAP utilities, and Hibernate persistence classes to ease the" + //
" development of services."
+ //
System.lineSeparator() + //
System.lineSeparator() + "Source: [liferay-service-builder_7_2_0.dtd](" + dtdURI + ")",
206);
r(1, 206, 1, 221));
}

@Test
Expand All @@ -48,15 +51,40 @@ public void testAttributeNameHover() throws BadLocationException, MalformedURIEx
assertHover(xml,
"The package-path value specifies the package of the generated code."
+ //

System.lineSeparator() + //
System.lineSeparator() + "Source: [liferay-service-builder_7_2_0.dtd](" + dtdURI + ")",
null);
}

private static void assertHover(String value, String expectedHoverLabel, Integer expectedHoverOffset)
@Test
public void webXML() throws Exception {
String dtdURI = CacheResourcesManager.getResourceCachePath("http://java.sun.com/dtd/web-app_2_3.dtd").toUri()
.toString().replace("file:///", "file:/");
String xml = "<!DOCTYPE web-app PUBLIC\n" + //
" \"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN\"\n" + //
" \"http://java.sun.com/dtd/web-app_2_3.dtd\" >\n" + //
"\n" + //
"<web-a|pp>\n" + //
" <display-name>Servlet 2.3 Web Application</display-name>\n" + //
"</web-app>";
assertHoverWithCache(xml,
"The web-app element is the root of the deployment descriptor for a web application." + //
System.lineSeparator() + //
System.lineSeparator() + "Source: [web-app_2_3.dtd](" + dtdURI + ")",
r(4, 1, 4, 8));
}

private static void assertHover(String value, String expectedHoverLabel, Range expectedHoverRange)
throws BadLocationException {
XMLAssert.assertHover(new XMLLanguageService(), value, "src/test/resources/catalogs/catalog-liferay.xml", null,
expectedHoverLabel, expectedHoverOffset);
expectedHoverLabel, expectedHoverRange);
}

private static void assertHoverWithCache(String value, String expectedHoverLabel, Range expectedHoverRange) throws BadLocationException {
ContentModelSettings settings = new ContentModelSettings();
settings.setUseCache(true);
XMLAssert.assertHover(new XMLLanguageService(), value, null, null, expectedHoverLabel, expectedHoverRange, settings);
}

private static String getDTDFileURI(String dtdURI) throws MalformedURIException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
*/
package org.eclipse.lemminx.extensions.contentmodel;

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

import org.apache.xerces.impl.XMLEntityManager;
import org.apache.xerces.util.URI.MalformedURIException;
import org.eclipse.lemminx.XMLAssert;
import org.eclipse.lemminx.commons.BadLocationException;
import org.eclipse.lemminx.extensions.xsi.XSISchemaModel;
import org.eclipse.lemminx.services.XMLLanguageService;
import org.eclipse.lsp4j.Range;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -37,7 +40,7 @@ public void testTagHover() throws BadLocationException, MalformedURIException {
+ //
System.lineSeparator() + //
System.lineSeparator() + "Source: [spring-beans-3.0.xsd](" + schemaURI + ")",
2);
r(2, 2, 2, 6));
};

@Test
Expand Down Expand Up @@ -222,10 +225,10 @@ public void hoverCacheBug() throws BadLocationException, MalformedURIException {

}

private static void assertHover(String value, String expectedHoverLabel, Integer expectedHoverOffset)
private static void assertHover(String value, String expectedHoverLabel, Range expectedHoverRange)
throws BadLocationException {
XMLAssert.assertHover(new XMLLanguageService(), value, "src/test/resources/catalogs/catalog.xml", null,
expectedHoverLabel, expectedHoverOffset);
expectedHoverLabel, expectedHoverRange);
}

private static String getXMLSchemaFileURI(String schemaURI) throws MalformedURIException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
*/
package org.eclipse.lemminx.services.extensions;


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

import org.apache.xerces.impl.XMLEntityManager;
import org.apache.xerces.util.URI.MalformedURIException;
import org.eclipse.lemminx.XMLAssert;
import org.eclipse.lemminx.commons.BadLocationException;
import org.eclipse.lemminx.services.XMLLanguageService;
import org.eclipse.lsp4j.Range;
import org.junit.jupiter.api.Test;

/**
Expand Down Expand Up @@ -45,7 +49,7 @@ public void testTagHover() throws BadLocationException, MalformedURIException {
System.lineSeparator() + "Source: [spring-beans-3.0.xsd](" + schemaURI + ")" + HOVER_SEPARATOR +
System.lineSeparator() +
System.lineSeparator(),
2);
r(2, 2, 2, 6));
};

@Test
Expand All @@ -67,7 +71,7 @@ public void testAttributeNameHover() throws BadLocationException, MalformedURIEx
null);
};

private static void assertHover(String value, String expectedHoverLabel, Integer expectedHoverOffset)
private static void assertHover(String value, String expectedHoverLabel, Range expectedHoverOffset)
throws BadLocationException {
XMLAssert.assertHover(new AggregatedHoverLanguageService(), value, "src/test/resources/catalogs/catalog.xml", null,
expectedHoverLabel, expectedHoverOffset);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2018 Angelo ZERR
* Copyright (c) 201r(0,8,0, 12)-2020 Angelo ZERR
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand All @@ -12,9 +12,11 @@
*/
package org.eclipse.lemminx.services.extensions;

import static org.eclipse.lemminx.XMLAssert.r;
import org.eclipse.lemminx.XMLAssert;
import org.eclipse.lemminx.commons.BadLocationException;
import org.eclipse.lemminx.services.XMLLanguageService;
import org.eclipse.lsp4j.Range;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -27,28 +29,28 @@ public class HTMLHoverExtensionsTest {
@Test
public void testSingle() throws BadLocationException {
assertHover("|<html></html>");
assertHover("<|html></html>", "<html>", 1);
assertHover("<h|tml></html>", "<html>", 1);
assertHover("<htm|l></html>", "<html>", 1);
assertHover("<html|></html>", "<html>", 1);
assertHover("<|html></html>", "<html>", r(0, 1, 0, 5));
assertHover("<h|tml></html>", "<html>", r(0, 1, 0, 5));
assertHover("<htm|l></html>", "<html>", r(0, 1, 0, 5));
assertHover("<html|></html>", "<html>", r(0, 1, 0, 5));
assertHover("<html>|</html>");
assertHover("<html><|/html>");
assertHover("<html></|html>", "</html>", 8);
assertHover("<html></h|tml>", "</html>", 8);
assertHover("<html></ht|ml>", "</html>", 8);
assertHover("<html></htm|l>", "</html>", 8);
assertHover("<html></html|>", "</html>", 8);
assertHover("<html></|html>", "</html>", r(0, 8, 0, 12));
assertHover("<html></h|tml>", "</html>", r(0, 8, 0, 12));
assertHover("<html></ht|ml>", "</html>", r(0, 8, 0, 12));
assertHover("<html></htm|l>", "</html>", r(0, 8, 0, 12));
assertHover("<html></html|>", "</html>", r(0, 8, 0, 12));
assertHover("<html></html>|");
assertHover("<html>hover|Text</html>", "hoverText", 6);
assertHover("<html>h|overText</html>", "hoverText", 6);
assertHover("<html> |</html>", " ", 6);
assertHover("<html>hover|Text</html>", "hoverText", r(0, 6, 0, 15));
assertHover("<html>h|overText</html>", "hoverText", r(0, 6, 0, 15));
assertHover("<html> |</html>", " ", r(0, 6, 0, 7));
};

private static void assertHover(String value) throws BadLocationException {
assertHover(value, null, null);
}

private static void assertHover(String value, String expectedHoverLabel, Integer expectedHoverOffset)
private static void assertHover(String value, String expectedHoverLabel, Range expectedHoverOffset)
throws BadLocationException {
XMLAssert.assertHover(new HTMLLanguageService(), value, null, null, expectedHoverLabel, expectedHoverOffset);
}
Expand All @@ -68,6 +70,7 @@ public String onTag(IHoverRequest request) {
String tagLabel = request.isOpen() ? "<" + tag + ">" : "</" + tag + ">";
return tagLabel;
}

@Override
public String onText(IHoverRequest request) throws Exception {
return request.getNode().getTextContent();
Expand Down

0 comments on commit 31e5fae

Please sign in to comment.