Skip to content

Commit

Permalink
Hover markup kind response ignores the hover client capability
Browse files Browse the repository at this point in the history
Fix #525

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Jul 22, 2019
1 parent a89cc52 commit 20c3819
Show file tree
Hide file tree
Showing 23 changed files with 200 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
import org.eclipse.lsp4xml.logs.LogHelper;
import org.eclipse.lsp4xml.services.IXMLDocumentProvider;
import org.eclipse.lsp4xml.services.XMLLanguageService;
import org.eclipse.lsp4xml.services.extensions.CompletionSettings;
import org.eclipse.lsp4xml.settings.AllXMLSettings;
import org.eclipse.lsp4xml.settings.XMLCompletionSettings;
import org.eclipse.lsp4xml.settings.InitializationOptionsSettings;
import org.eclipse.lsp4xml.settings.LogsSettings;
import org.eclipse.lsp4xml.settings.ServerSettings;
Expand Down Expand Up @@ -141,7 +141,7 @@ public synchronized void updateSettings(Object initializationOptionsSettings) {
xmlTextDocumentService.getSharedFormattingSettings().merge(formatterSettings);
}

CompletionSettings newCompletions = xmlClientSettings.getCompletion();
XMLCompletionSettings newCompletions = xmlClientSettings.getCompletion();
if (newCompletions != null) {
xmlTextDocumentService.updateCompletionSettings(newCompletions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@
import org.eclipse.lsp4xml.dom.DOMParser;
import org.eclipse.lsp4xml.extensions.contentmodel.settings.XMLValidationSettings;
import org.eclipse.lsp4xml.services.XMLLanguageService;
import org.eclipse.lsp4xml.services.extensions.CompletionSettings;
import org.eclipse.lsp4xml.services.extensions.save.AbstractSaveContext;
import org.eclipse.lsp4xml.settings.SharedSettings;
import org.eclipse.lsp4xml.settings.XMLCodeLensSettings;
import org.eclipse.lsp4xml.settings.XMLCompletionSettings;
import org.eclipse.lsp4xml.settings.XMLFormattingOptions;
import org.eclipse.lsp4xml.settings.XMLSymbolSettings;
import org.eclipse.lsp4xml.utils.XMLPositionUtility;
Expand Down Expand Up @@ -148,8 +148,9 @@ public void updateClientCapabilities(ClientCapabilities capabilities,
ExtendedClientCapabilities extendedClientCapabilities) {
TextDocumentClientCapabilities textDocumentClientCapabilities = capabilities.getTextDocument();
if (textDocumentClientCapabilities != null) {
// Completion settings
sharedSettings.getCompletionSettings().setCapabilities(textDocumentClientCapabilities.getCompletion());
sharedSettings.getFoldingSettings().setCapabilities(textDocumentClientCapabilities.getFoldingRange());
sharedSettings.getHoverSettings().setCapabilities(textDocumentClientCapabilities.getHover());
codeActionLiteralSupport = textDocumentClientCapabilities.getCodeAction() != null
&& textDocumentClientCapabilities.getCodeAction().getCodeActionLiteralSupport() != null;
hierarchicalDocumentSymbolSupport = textDocumentClientCapabilities.getDocumentSymbol() != null
Expand Down Expand Up @@ -180,7 +181,8 @@ public CompletableFuture<Either<List<CompletionItem>, CompletionList>> completio
@Override
public CompletableFuture<Hover> hover(TextDocumentPositionParams params) {
return computeDOMAsync(params.getTextDocument(), (cancelChecker, xmlDocument) -> {
return getXMLLanguageService().doHover(xmlDocument, params.getPosition(), cancelChecker);
return getXMLLanguageService().doHover(xmlDocument, params.getPosition(), sharedSettings.getHoverSettings(),
cancelChecker);
});
}

Expand Down Expand Up @@ -438,7 +440,7 @@ private XMLLanguageService getXMLLanguageService() {
return xmlLanguageServer.getXMLLanguageService();
}

public void updateCompletionSettings(CompletionSettings newCompletion) {
public void updateCompletionSettings(XMLCompletionSettings newCompletion) {
sharedSettings.getCompletionSettings().merge(newCompletion);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.eclipse.lsp4xml.services.extensions.HoverParticipantAdapter;
import org.eclipse.lsp4xml.services.extensions.IHoverRequest;
import org.eclipse.lsp4xml.uriresolver.CacheResourceDownloadingException;
import org.eclipse.lsp4xml.utils.MarkdownConverter;

/**
* Extension to support XML hover based on content model (XML Schema completion,
Expand All @@ -39,22 +38,19 @@ public Hover onTag(IHoverRequest hoverRequest) throws Exception {
if (cmElement != null) {
String doc = cmElement.getDocumentation();
if (doc != null && doc.length() > 0) {
String markdown = MarkdownConverter.convert(doc);
MarkupContent content = new MarkupContent();
content.setKind(MarkupKind.MARKDOWN);
content.setValue(markdown);
MarkupContent content = hoverRequest.createMarkupContent(doc, MarkupKind.MARKDOWN);
return new Hover(content, hoverRequest.getTagRange());
}
}
} catch (CacheResourceDownloadingException e) {
return getCacheWarningHover(e);
return getCacheWarningHover(e, hoverRequest);
}
return null;
}

@Override
public Hover onAttributeName(IHoverRequest hoverRequest) throws Exception {

DOMAttr attribute = (DOMAttr) hoverRequest.getNode();

try {
Expand All @@ -66,15 +62,13 @@ public Hover onAttributeName(IHoverRequest hoverRequest) throws Exception {
if (cmAttribute != null) {
String doc = cmAttribute.getDocumentation();
if (doc != null && doc.length() > 0) {
MarkupContent content = new MarkupContent();
content.setKind(MarkupKind.PLAINTEXT);
content.setValue(doc);
MarkupContent content = hoverRequest.createMarkupContent(doc, MarkupKind.MARKDOWN);
return new Hover(content);
}
}
}
} catch (CacheResourceDownloadingException e) {
return getCacheWarningHover(e);
return getCacheWarningHover(e, hoverRequest);
}
return null;
}
Expand All @@ -83,44 +77,42 @@ public Hover onAttributeName(IHoverRequest hoverRequest) throws Exception {
public Hover onAttributeValue(IHoverRequest hoverRequest) throws Exception {
DOMAttr attribute = (DOMAttr) hoverRequest.getNode();

//Attempts to compute specifically for XSI related attributes since
//the XSD itself does not have enough information. Should create a mock XSD eventually.
// Attempts to compute specifically for XSI related attributes since
// the XSD itself does not have enough information. Should create a mock XSD
// eventually.
Hover temp = XSISchemaModel.computeHoverResponse(attribute, hoverRequest);
if(temp != null) {
if (temp != null) {
return temp;
}

try {
ContentModelManager contentModelManager = hoverRequest.getComponent(ContentModelManager.class);

CMElementDeclaration cmElement = contentModelManager.findCMElement(attribute.getOwnerElement());
if (cmElement != null) {
String attributeName = attribute.getName();
CMAttributeDeclaration cmAttribute = cmElement.findCMAttribute(attributeName);

String attributeValue = attribute.getValue();
if (cmAttribute != null) {
String doc = cmAttribute.getValueDocumentation(attributeValue);
if (doc != null && doc.length() > 0) {
String markdown = MarkdownConverter.convert(doc);
MarkupContent content = new MarkupContent();
content.setKind(MarkupKind.MARKDOWN);
content.setValue(markdown);
MarkupContent content = hoverRequest.createMarkupContent(doc, MarkupKind.MARKDOWN);
return new Hover(content);
}
}
}
} catch (CacheResourceDownloadingException e) {
return getCacheWarningHover(e);
return getCacheWarningHover(e, hoverRequest);
}
return null;
}

private Hover getCacheWarningHover(CacheResourceDownloadingException e) {
private static Hover getCacheWarningHover(CacheResourceDownloadingException e, IHoverRequest hoverRequest) {
// Here cache is enabled and some XML Schema, DTD, etc are loading
MarkupContent content = new MarkupContent();
content.setKind(MarkupKind.MARKDOWN);
content.setValue("Cannot process " + (e.isDTD() ? "DTD" : "XML Schema") + " hover: " + e.getMessage());
MarkupContent content = hoverRequest.createMarkupContent(
"Cannot process " + (e.isDTD() ? "DTD" : "XML Schema") + " hover: " + e.getMessage(),
MarkupKind.MARKDOWN);
return new Hover(content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
import org.eclipse.lsp4xml.dom.DOMDocument;
import org.eclipse.lsp4xml.dom.DOMNode;
import org.eclipse.lsp4xml.extensions.contentmodel.utils.XMLGenerator;
import org.eclipse.lsp4xml.services.extensions.CompletionSettings;
import org.eclipse.lsp4xml.services.extensions.ICompletionRequest;
import org.eclipse.lsp4xml.services.extensions.XMLExtensionsRegistry;
import org.eclipse.lsp4xml.settings.XMLCompletionSettings;
import org.eclipse.lsp4xml.settings.SharedSettings;
import org.eclipse.lsp4xml.settings.XMLFormattingOptions;
import org.eclipse.lsp4xml.utils.StringUtils;
Expand All @@ -29,7 +29,7 @@
*/
class CompletionRequest extends AbstractPositionRequest implements ICompletionRequest {

private final CompletionSettings completionSettings;
private final XMLCompletionSettings completionSettings;

private final XMLFormattingOptions formattingSettings;

Expand Down Expand Up @@ -62,7 +62,7 @@ public XMLFormattingOptions getFormattingSettings() {
}

@Override
public CompletionSettings getCompletionSettings() {
public XMLCompletionSettings getCompletionSettings() {
return completionSettings;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
package org.eclipse.lsp4xml.services;

import org.eclipse.lsp4j.MarkupContent;
import org.eclipse.lsp4j.MarkupKind;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4xml.commons.BadLocationException;
Expand All @@ -18,22 +20,27 @@
import org.eclipse.lsp4xml.dom.DOMNode;
import org.eclipse.lsp4xml.services.extensions.IHoverRequest;
import org.eclipse.lsp4xml.services.extensions.XMLExtensionsRegistry;
import org.eclipse.lsp4xml.settings.XMLHoverSettings;
import org.eclipse.lsp4xml.utils.MarkdownConverter;

/**
* Hover request implementation.
*
*/
class HoverRequest extends AbstractPositionRequest implements IHoverRequest {

private final XMLHoverSettings settings;

private final XMLExtensionsRegistry extensionsRegistry;

private Range tagRange;

private boolean open;

public HoverRequest(DOMDocument xmlDocument, Position position, XMLExtensionsRegistry extensionsRegistry)
throws BadLocationException {
public HoverRequest(DOMDocument xmlDocument, Position position, XMLHoverSettings settings,
XMLExtensionsRegistry extensionsRegistry) throws BadLocationException {
super(xmlDocument, position);
this.settings = settings;
this.extensionsRegistry = extensionsRegistry;
}

Expand Down Expand Up @@ -71,4 +78,25 @@ public void setOpen(boolean open) {
public <T> T getComponent(Class clazz) {
return extensionsRegistry.getComponent(clazz);
}

@Override
public boolean canSupportMarkupKind(String kind) {
return settings != null && settings.getCapabilities() != null
&& settings.getCapabilities().getContentFormat() != null
&& settings.getCapabilities().getContentFormat().contains(kind);
}

@Override
public MarkupContent createMarkupContent(String value, String kind) {
MarkupContent content = new MarkupContent();
if (MarkupKind.MARKDOWN.equals(kind) && canSupportMarkupKind(kind)) {
String markdown = MarkdownConverter.convert(value);
content.setValue(markdown);
content.setKind(MarkupKind.MARKDOWN);
} else {
content.setValue(value);
content.setKind(MarkupKind.PLAINTEXT);
}
return content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
import org.eclipse.lsp4xml.dom.parser.TokenType;
import org.eclipse.lsp4xml.dom.parser.XMLScanner;
import org.eclipse.lsp4xml.extensions.prolog.PrologModel;
import org.eclipse.lsp4xml.services.extensions.CompletionSettings;
import org.eclipse.lsp4xml.services.extensions.ICompletionParticipant;
import org.eclipse.lsp4xml.services.extensions.ICompletionRequest;
import org.eclipse.lsp4xml.services.extensions.ICompletionResponse;
import org.eclipse.lsp4xml.services.extensions.XMLExtensionsRegistry;
import org.eclipse.lsp4xml.settings.XMLCompletionSettings;
import org.eclipse.lsp4xml.settings.SharedSettings;
import org.eclipse.lsp4xml.utils.StringUtils;

Expand Down Expand Up @@ -513,7 +513,7 @@ private void collectOpenTagSuggestions(boolean hasOpenBracket, Range replaceRang
xml.append(" />");
} else {
xml.append(">");
CompletionSettings completionSettings = completionRequest.getCompletionSettings();
XMLCompletionSettings completionSettings = completionRequest.getCompletionSettings();

if (completionSettings.isCompletionSnippetsSupported()) {
xml.append("$0");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.regex.Pattern;

import org.eclipse.lsp4j.FoldingRange;
import org.eclipse.lsp4j.FoldingRangeCapabilities;
import org.eclipse.lsp4j.FoldingRangeKind;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
import org.eclipse.lsp4xml.commons.BadLocationException;
Expand All @@ -32,6 +31,7 @@
import org.eclipse.lsp4xml.dom.parser.TokenType;
import org.eclipse.lsp4xml.dom.parser.XMLScanner;
import org.eclipse.lsp4xml.services.extensions.XMLExtensionsRegistry;
import org.eclipse.lsp4xml.settings.XMLFoldingSettings;

/**
* XML folding support.
Expand Down Expand Up @@ -59,7 +59,8 @@ public TagInfo(int startLine, String tagName) {
}
}

public List<FoldingRange> getFoldingRanges(TextDocument document, FoldingRangeCapabilities context, CancelChecker cancelChecker) {
public List<FoldingRange> getFoldingRanges(TextDocument document, XMLFoldingSettings context,
CancelChecker cancelChecker) {
Scanner scanner = XMLScanner.createScanner(document.getText());
TokenType token = scanner.scan();
List<FoldingRange> ranges = new ArrayList<>();
Expand Down Expand Up @@ -153,8 +154,7 @@ public List<FoldingRange> getFoldingRanges(TextDocument document, FoldingRangeCa
}
} catch (BadLocationException e) {
LOGGER.log(Level.SEVERE, "Foldings received a BadLocation while scanning the document", e);
}
catch(StackOverflowError e) {
} catch (StackOverflowError e) {
// This exception occurs with large file, why?
// For the moment we catch it.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.lsp4xml.dom.parser.XMLScanner;
import org.eclipse.lsp4xml.services.extensions.IHoverParticipant;
import org.eclipse.lsp4xml.services.extensions.XMLExtensionsRegistry;
import org.eclipse.lsp4xml.settings.XMLHoverSettings;

/**
* XML hover support.
Expand All @@ -42,10 +43,11 @@ public XMLHover(XMLExtensionsRegistry extensionsRegistry) {
this.extensionsRegistry = extensionsRegistry;
}

public Hover doHover(DOMDocument xmlDocument, Position position, CancelChecker cancelChecker) {
public Hover doHover(DOMDocument xmlDocument, Position position, XMLHoverSettings settings,
CancelChecker cancelChecker) {
HoverRequest hoverRequest = null;
try {
hoverRequest = new HoverRequest(xmlDocument, position, extensionsRegistry);
hoverRequest = new HoverRequest(xmlDocument, position, settings, extensionsRegistry);
} catch (BadLocationException e) {
LOGGER.log(Level.SEVERE, "Failed creating HoverRequest", e);
return null;
Expand All @@ -72,7 +74,7 @@ public Hover doHover(DOMDocument xmlDocument, Position position, CancelChecker c
}
} else if (node.isAttribute()) {
DOMAttr attr = (DOMAttr) node;
if(attr.valueContainsOffset(offset)) {
if (attr.valueContainsOffset(offset)) {
return getAttrValueHover(hoverRequest, null);
}
// Attribute is hover
Expand Down Expand Up @@ -129,12 +131,12 @@ private Range getTagNameRange(TokenType tokenType, int startOffset, int offset,
* Returns the LSP hover from the hovered attribute.
*
* @param hoverRequest the hover request.
* @param attrRange the attribute range
* @param attrRange the attribute range
* @return the LSP hover from the hovered attribute.
*/
private Hover getAttrNameHover(HoverRequest hoverRequest, Range attrRange) {
//hoverRequest.setTagRange(tagRange);
//hoverRequest.setOpen(open);
// hoverRequest.setTagRange(tagRange);
// hoverRequest.setOpen(open);
for (IHoverParticipant participant : extensionsRegistry.getHoverParticipants()) {
try {
Hover hover = participant.onAttributeName(hoverRequest);
Expand All @@ -152,12 +154,12 @@ private Hover getAttrNameHover(HoverRequest hoverRequest, Range attrRange) {
* Returns the LSP hover from the hovered attribute.
*
* @param hoverRequest the hover request.
* @param attrRange the attribute range
* @param attrRange the attribute range
* @return the LSP hover from the hovered attribute.
*/
private Hover getAttrValueHover(HoverRequest hoverRequest, Range attrRange) {
//hoverRequest.setTagRange(tagRange);
//hoverRequest.setOpen(open);
// hoverRequest.setTagRange(tagRange);
// hoverRequest.setOpen(open);
for (IHoverParticipant participant : extensionsRegistry.getHoverParticipants()) {
try {
Hover hover = participant.onAttributeValue(hoverRequest);
Expand Down
Loading

0 comments on commit 20c3819

Please sign in to comment.