Skip to content

Commit

Permalink
Completion should support markdown for documentation
Browse files Browse the repository at this point in the history
Fix #526

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Jul 22, 2019
1 parent a89cc52 commit 386141e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionItemKind;
import org.eclipse.lsp4j.InsertTextFormat;
import org.eclipse.lsp4j.MarkupKind;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4xml.commons.BadLocationException;
Expand All @@ -31,6 +32,7 @@
import org.eclipse.lsp4xml.services.extensions.ICompletionResponse;
import org.eclipse.lsp4xml.settings.XMLFormattingOptions;
import org.eclipse.lsp4xml.uriresolver.CacheResourceDownloadingException;
import org.eclipse.lsp4xml.utils.StringUtils;

/**
* Extension to support XML completion based on content model (XML Schema
Expand Down Expand Up @@ -68,8 +70,9 @@ public void onTagOpen(ICompletionRequest request, ICompletionResponse response)

if (cmElement != null) {
defaultPrefix = parentElement.getPrefix();
fillWithChildrenElementDeclaration(parentElement, cmElement.getPossibleElements(parentElement, request.getOffset()),
defaultPrefix, false, request, response, schemaURI);
fillWithChildrenElementDeclaration(parentElement,
cmElement.getPossibleElements(parentElement, request.getOffset()), defaultPrefix, false,
request, response, schemaURI);
}
if (parentElement.isDocumentElement()) {
// completion on root document element
Expand All @@ -96,8 +99,8 @@ public void onTagOpen(ICompletionRequest request, ICompletionResponse response)
if (cmInternalElement != null) {
defaultPrefix = parentElement.getPrefix();
fillWithChildrenElementDeclaration(parentElement,
cmInternalElement.getPossibleElements(parentElement, request.getOffset()), defaultPrefix, false, request,
response, schemaURI);
cmInternalElement.getPossibleElements(parentElement, request.getOffset()), defaultPrefix, false,
request, response, schemaURI);
}
} catch (CacheResourceDownloadingException e) {
// XML Schema, DTD is loading, ignore this error
Expand All @@ -114,10 +117,8 @@ private void fillWithChildrenElementDeclaration(DOMElement element, Collection<C
CompletionItem item = new CompletionItem(label);
item.setFilterText(request.getFilterForStartTagName(label));
item.setKind(CompletionItemKind.Property);
String detail = XMLGenerator.generateDocumentation(child.getDocumentation(), schemaURI);
if (detail != null) {
item.setDetail(detail);
}
String documentation = XMLGenerator.generateDocumentation(child.getDocumentation(), schemaURI);
updateDocumentation(item, documentation, request);
String xml = generator.generate(child, prefix);
item.setTextEdit(new TextEdit(request.getReplaceRange(), xml));
item.setInsertTextFormat(InsertTextFormat.Snippet);
Expand All @@ -141,19 +142,19 @@ public void onAttributeName(boolean generateValue, ICompletionRequest request, I
// Completion on attribute based on external grammar
CMElementDeclaration cmElement = contentModelManager.findCMElement(parentElement);
fillAttributesWithCMAttributeDeclarations(parentElement, fullRange, cmElement, canSupportSnippet,
generateValue, response, formattingSettings);
generateValue, request, response, formattingSettings);
// Completion on attribute based on internal grammar
cmElement = contentModelManager.findInternalCMElement(parentElement);
fillAttributesWithCMAttributeDeclarations(parentElement, fullRange, cmElement, canSupportSnippet,
generateValue, response, formattingSettings);
generateValue, request, response, formattingSettings);
} catch (CacheResourceDownloadingException e) {
// XML Schema, DTD is loading, ignore this error
}
}

private void fillAttributesWithCMAttributeDeclarations(DOMElement parentElement, Range fullRange,
CMElementDeclaration cmElement, boolean canSupportSnippet, boolean generateValue,
ICompletionResponse response, XMLFormattingOptions formattingOptions) {
ICompletionRequest request, ICompletionResponse response, XMLFormattingOptions formattingOptions) {
if (cmElement == null) {
return;
}
Expand All @@ -167,9 +168,7 @@ private void fillAttributesWithCMAttributeDeclarations(DOMElement parentElement,
CompletionItem item = new AttributeCompletionItem(attrName, canSupportSnippet, fullRange, generateValue,
cmAttribute.getDefaultValue(), cmAttribute.getEnumerationValues(), formattingOptions);
String documentation = cmAttribute.getDocumentation();
if (documentation != null) {
item.setDetail(documentation);
}
updateDocumentation(item, documentation, request);
response.addCompletionAttribute(item);
}
}
Expand Down Expand Up @@ -217,4 +216,10 @@ private void fillAttributeValuesWithCMAttributeDeclarations(CMElementDeclaration
}
}

private static void updateDocumentation(CompletionItem item, String documentation, ICompletionRequest request) {
if (StringUtils.isEmpty(documentation)) {
return;
}
item.setDocumentation(request.createMarkupContent(documentation, MarkupKind.MARKDOWN));
}
}
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 @@ -21,6 +23,7 @@
import org.eclipse.lsp4xml.services.extensions.XMLExtensionsRegistry;
import org.eclipse.lsp4xml.settings.SharedSettings;
import org.eclipse.lsp4xml.settings.XMLFormattingOptions;
import org.eclipse.lsp4xml.utils.MarkdownConverter;
import org.eclipse.lsp4xml.utils.StringUtils;

/**
Expand Down Expand Up @@ -122,4 +125,27 @@ private String getQuotation() {
String quotation = formattingSettings != null ? formattingSettings.getQuotationAsString() : null;
return StringUtils.isEmpty(quotation) ? XMLFormattingOptions.DEFAULT_QUOTATION : quotation;
}

@Override
public boolean canSupportMarkupKind(String kind) {
return completionSettings != null && completionSettings.getCompletionCapabilities() != null
&& completionSettings.getCompletionCapabilities().getCompletionItem() != null
&& completionSettings.getCompletionCapabilities().getCompletionItem().getDocumentationFormat() != null
&& completionSettings.getCompletionCapabilities().getCompletionItem().getDocumentationFormat()
.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 @@ -10,6 +10,8 @@
*/
package org.eclipse.lsp4xml.services.extensions;

import org.eclipse.lsp4j.MarkupContent;
import org.eclipse.lsp4j.MarkupKind;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4xml.commons.BadLocationException;
import org.eclipse.lsp4xml.extensions.contentmodel.utils.XMLGenerator;
Expand All @@ -33,4 +35,24 @@ public interface ICompletionRequest extends IPositionRequest {

String getInsertAttrValue(String value);

/**
* Returns <code>true</code> if client can support the given Markup kind for
* documentation and <code>false</code> otherwise.
*
* @param kind the markup kind
* @return <code>true</code> if client can support the given Markup kind for
* documentation and <code>false</code> otherwise.
*/
boolean canSupportMarkupKind(String kind);

/**
* Create the markup content according the given markup kind and the capability
* of the client.
*
* @param value the documentation value
* @param kind the {@link MarkupKind}
* @return the markup content according the given markup kind and the capability
* of the client.
*/
MarkupContent createMarkupContent(String value, String kind);
}

0 comments on commit 386141e

Please sign in to comment.