org.eclipse.lsp4j.MarkupContent Java Examples
The following examples show how to use
org.eclipse.lsp4j.MarkupContent.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ContentModelHoverParticipant.java From lemminx with Eclipse Public License 2.0 | 6 votes |
@Override public Hover onTag(IHoverRequest hoverRequest) throws Exception { try { ContentModelManager contentModelManager = hoverRequest.getComponent(ContentModelManager.class); DOMElement element = (DOMElement) hoverRequest.getNode(); Collection<CMDocument> cmDocuments = contentModelManager.findCMDocument(element); if (cmDocuments.isEmpty()) { // no bound grammar -> no documentation return null; } // Compute element declaration documentation from bound grammars List<MarkupContent> contentValues = new ArrayList<>(); for (CMDocument cmDocument : cmDocuments) { CMElementDeclaration cmElement = cmDocument.findCMElement(element); if (cmElement != null) { MarkupContent content = XMLGenerator.createMarkupContent(cmElement, hoverRequest); fillHoverContent(content, contentValues); } } return createHover(contentValues); } catch (CacheResourceDownloadingException e) { return getCacheWarningHover(e, hoverRequest); } }
Example #2
Source File: ContentModelCompletionParticipant.java From lemminx with Eclipse Public License 2.0 | 6 votes |
private static void addCompletionItem(CMElementDeclaration elementDeclaration, DOMElement parentElement, String defaultPrefix, boolean forceUseOfPrefix, ICompletionRequest request, ICompletionResponse response, XMLGenerator generator, Set<String> tags) { String prefix = forceUseOfPrefix ? defaultPrefix : (parentElement != null ? parentElement.getPrefix(elementDeclaration.getNamespace()) : null); String label = elementDeclaration.getName(prefix); if (tags != null) { if (tags.contains(label)) { return; } else { tags.add(label); } } CompletionItem item = new CompletionItem(label); item.setFilterText(request.getFilterForStartTagName(label)); item.setKind(CompletionItemKind.Property); MarkupContent documentation = XMLGenerator.createMarkupContent(elementDeclaration, request); item.setDocumentation(documentation); String xml = generator.generate(elementDeclaration, prefix, isGenerateEndTag(request.getNode(), request.getOffset(), label)); item.setTextEdit(new TextEdit(request.getReplaceRange(), xml)); item.setInsertTextFormat(InsertTextFormat.Snippet); response.addCompletionItem(item, true); }
Example #3
Source File: SnippetUtilsTest.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
@Test public void testMultipleVariablesInput() { ClientPreferences mockCapabilies = mock(ClientPreferences.class); when(mockCapabilies.isSupportsCompletionDocumentationMarkdown()).thenReturn(Boolean.FALSE); when(preferenceManager.getClientPreferences()).thenReturn(mockCapabilies); //@formatter:off String raw = "for (${1:int} ${2:i} = ${3:0}; ${2:i} < ${4:args.length}; ${2:i}++) {\n" + "\t${0}\n" + "}"; //@formatter:on Either<String, MarkupContent> result = SnippetUtils.beautifyDocument(raw); //@formatter:off String expected = "for (int i = 0; i < args.length; i++) {\n" + "\t\n" + "}"; //@formatter:on assertEquals(result.getLeft(), expected); }
Example #4
Source File: SnippetUtilsTest.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
@Test public void testSelectedTextPlaceholder() { ClientPreferences mockCapabilies = mock(ClientPreferences.class); when(mockCapabilies.isSupportsCompletionDocumentationMarkdown()).thenReturn(Boolean.FALSE); when(preferenceManager.getClientPreferences()).thenReturn(mockCapabilies); //@formatter:off String raw = "for (${1:int} ${2:i} = ${3:0}; ${2:i} < ${4:args.length}; ${2:i}++) {\n" + "\t$TM_SELECTED_TEXT${0}\n" + "}"; //@formatter:on Either<String, MarkupContent> result = SnippetUtils.beautifyDocument(raw); //@formatter:off String expected = "for (int i = 0; i < args.length; i++) {\n" + "\t\n" + "}"; //@formatter:on assertEquals(result.getLeft(), expected); }
Example #5
Source File: ContentModelCompletionParticipant.java From lemminx with Eclipse Public License 2.0 | 6 votes |
private void fillAttributesWithCMAttributeDeclarations(DOMElement parentElement, Range fullRange, CMElementDeclaration cmElement, boolean canSupportSnippet, boolean generateValue, ICompletionRequest request, ICompletionResponse response) { Collection<CMAttributeDeclaration> attributes = cmElement.getAttributes(); if (attributes == null) { return; } for (CMAttributeDeclaration cmAttribute : attributes) { String attrName = cmAttribute.getName(); if (!parentElement.hasAttribute(attrName)) { CompletionItem item = new AttributeCompletionItem(attrName, canSupportSnippet, fullRange, generateValue, cmAttribute.getDefaultValue(), cmAttribute.getEnumerationValues(), request.getSharedSettings()); MarkupContent documentation = XMLGenerator.createMarkupContent(cmAttribute, cmElement, request); item.setDocumentation(documentation); response.addCompletionAttribute(item); } } }
Example #6
Source File: EntitiesCompletionParticipant.java From lemminx with Eclipse Public License 2.0 | 6 votes |
/** * Collect external entities. * * @param document the DOM document. * @param entityRange the entity range. * @param markdown true if the documentation can be formatted as markdown and * false otherwise. * @param request the completion request. * @param response the completion response. */ private static void collectExternalEntityProposals(DOMDocument document, Range entityRange, boolean markdown, ICompletionRequest request, ICompletionResponse response) { ContentModelManager contentModelManager = request.getComponent(ContentModelManager.class); Collection<CMDocument> cmDocuments = contentModelManager.findCMDocument(document, null, false); for (CMDocument cmDocument : cmDocuments) { List<Entity> entities = cmDocument.getEntities(); for (Entity entity : entities) { if (entity.getNodeName() != null) { // provide completion for the external declared entity MarkupContent documentation = EntitiesDocumentationUtils.getDocumentation((DTDEntityDecl) entity, EntityOriginType.EXTERNAL, markdown); fillCompletion(entity.getNodeName(), documentation, entityRange, response); } } } }
Example #7
Source File: EntitiesCompletionParticipant.java From lemminx with Eclipse Public License 2.0 | 6 votes |
/** * Collect local entities declared in the DOCTYPE. * * @param document the DOM document. * @param entityRange the entity range. * @param markdown true if the documentation can be formatted as markdown and * false otherwise. * @param response the completion response. */ private static void collectLocalEntityProposals(DOMDocument document, Range entityRange, boolean markdown, ICompletionResponse response) { DOMDocumentType docType = document.getDoctype(); if (docType == null) { return; } NamedNodeMap entities = docType.getEntities(); for (int i = 0; i < entities.getLength(); i++) { Entity entity = (Entity) entities.item(i); if (entity.getNodeName() != null) { // provide completion for the locally declared entity MarkupContent documentation = EntitiesDocumentationUtils.getDocumentation((DTDEntityDecl) entity, EntityOriginType.LOCAL, markdown); fillCompletion(entity.getNodeName(), documentation, entityRange, response); } } }
Example #8
Source File: EntitiesHoverParticipant.java From lemminx with Eclipse Public License 2.0 | 6 votes |
/** * Returns the markup content of the given entity name in the external entities * and null otherwise. * * @param entityName the entity name to search. * @param entityRange the hovered range. * @param document the DOM document * @param request the hover request. * @return the markup content of the given entity name in the external entities * and null otherwise. */ private static MarkupContent searchInExternalEntities(String entityName, Range entityRange, DOMDocument document, IHoverRequest request) { ContentModelManager contentModelManager = request.getComponent(ContentModelManager.class); Collection<CMDocument> cmDocuments = contentModelManager.findCMDocument(document, null, false); for (CMDocument cmDocument : cmDocuments) { List<Entity> entities = cmDocument.getEntities(); for (Entity ent : entities) { DTDEntityDecl entity = (DTDEntityDecl) ent; if (entityName.equals(entity.getName())) { boolean markdown = request.canSupportMarkupKind(MarkupKind.MARKDOWN); return EntitiesDocumentationUtils.getDocumentation(entity, EntityOriginType.EXTERNAL, markdown); } } } return null; }
Example #9
Source File: EntitiesHoverParticipant.java From lemminx with Eclipse Public License 2.0 | 6 votes |
/** * Returns the markup content of the given entity name in the local entities and * null otherwise. * * @param entityName the entity name to search. * @param entityRange the hovered range. * @param document the DOM document * @param request the hover request. * @return the markup content of the given entity name in the local entities and * null otherwise. */ private static MarkupContent searchInLocalEntities(String entityName, Range entityRange, DOMDocument document, IHoverRequest request) { DOMDocumentType docType = document.getDoctype(); if (docType == null) { return null; } // Loop for entities declared in the DOCTYPE of the document NamedNodeMap entities = docType.getEntities(); for (int i = 0; i < entities.getLength(); i++) { DTDEntityDecl entity = (DTDEntityDecl) entities.item(i); if (entityName.equals(entity.getName())) { boolean markdown = request.canSupportMarkupKind(MarkupKind.MARKDOWN); return EntitiesDocumentationUtils.getDocumentation(entity, EntityOriginType.LOCAL, markdown); } } return null; }
Example #10
Source File: ContentModelCompletionParticipant.java From lemminx with Eclipse Public License 2.0 | 6 votes |
private void fillAttributeValuesWithCMAttributeDeclarations(CMElementDeclaration cmElement, ICompletionRequest request, ICompletionResponse response) { String attributeName = request.getCurrentAttributeName(); CMAttributeDeclaration cmAttribute = cmElement.findCMAttribute(attributeName); if (cmAttribute != null) { Range fullRange = request.getReplaceRange(); cmAttribute.getEnumerationValues().forEach(value -> { CompletionItem item = new CompletionItem(); item.setLabel(value); String insertText = request.getInsertAttrValue(value); item.setLabel(value); item.setKind(CompletionItemKind.Value); item.setFilterText(insertText); item.setTextEdit(new TextEdit(fullRange, insertText)); MarkupContent documentation = XMLGenerator.createMarkupContent(cmAttribute, value, cmElement, request); item.setDocumentation(documentation); response.addCompletionItem(item); }); } }
Example #11
Source File: SnippetUtils.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
public static Either<String, MarkupContent> beautifyDocument(String raw) { // remove the placeholder for the plain cursor like: ${0}, ${1:variable} String escapedString = raw.replaceAll("\\$\\{\\d:?(.*?)\\}", "$1"); // Replace the reserved variable with empty string. // See: https://github.com/eclipse/eclipse.jdt.ls/issues/1220 escapedString = escapedString.replaceAll(TM_SELECTED_TEXT, ""); if (JavaLanguageServerPlugin.getPreferencesManager() != null && JavaLanguageServerPlugin.getPreferencesManager().getClientPreferences() != null && JavaLanguageServerPlugin.getPreferencesManager().getClientPreferences().isSupportsCompletionDocumentationMarkdown()) { MarkupContent markupContent = new MarkupContent(); markupContent.setKind(MarkupKind.MARKDOWN); markupContent.setValue(String.format("```%s\n%s\n```", MARKDOWN_LANGUAGE, escapedString)); return Either.forRight(markupContent); } else { return Either.forLeft(escapedString); } }
Example #12
Source File: EntitiesHoverParticipant.java From lemminx with Eclipse Public License 2.0 | 6 votes |
@Override public Hover onText(IHoverRequest request) throws Exception { DOMNode node = request.getNode(); if (!node.isText()) { return null; } // Hover is done in a text node, check if it's a entity reference DOMDocument document = request.getXMLDocument(); int offset = request.getOffset(); EntityReferenceRange entityRange = XMLPositionUtility.selectEntityReference(offset, document); if (entityRange == null) { return null; } // The hovered text follows the entity reference syntax (ex : &) String entityName = entityRange.getName(); Range range = entityRange.getRange(); // Try to find the entity MarkupContent entityContents = searchInEntities(entityName, range, document, request); if (entityContents != null) { return new Hover(entityContents, range); } return null; }
Example #13
Source File: HoverTypeAdapter.java From lsp4j with Eclipse Public License 2.0 | 6 votes |
protected Either<List<Either<String, MarkedString>>, MarkupContent> readContents(final JsonReader in) throws IOException { final JsonToken nextToken = in.peek(); boolean _equals = Objects.equal(nextToken, JsonToken.STRING); if (_equals) { final List<Either<String, MarkedString>> value = CollectionLiterals.<Either<String, MarkedString>>newArrayList(Either.<String, MarkedString>forLeft(in.nextString())); return Either.<List<Either<String, MarkedString>>, MarkupContent>forLeft(value); } else { boolean _equals_1 = Objects.equal(nextToken, JsonToken.BEGIN_ARRAY); if (_equals_1) { final List<Either<String, MarkedString>> value_1 = this.gson.<List<Either<String, MarkedString>>>fromJson(in, HoverTypeAdapter.LIST_STRING_MARKEDSTRING.getType()); return Either.<List<Either<String, MarkedString>>, MarkupContent>forLeft(value_1); } else { JsonElement _parse = new JsonParser().parse(in); final JsonObject object = ((JsonObject) _parse); boolean _has = object.has("language"); if (_has) { final List<Either<String, MarkedString>> value_2 = CollectionLiterals.<Either<String, MarkedString>>newArrayList(Either.<String, MarkedString>forRight(this.gson.<MarkedString>fromJson(object, MarkedString.class))); return Either.<List<Either<String, MarkedString>>, MarkupContent>forLeft(value_2); } else { return Either.<List<Either<String, MarkedString>>, MarkupContent>forRight(this.gson.<MarkupContent>fromJson(object, MarkupContent.class)); } } } }
Example #14
Source File: MarkupContentFactory.java From lemminx with Eclipse Public License 2.0 | 6 votes |
/** * Create the content. * * @param values the list of documentation values * @param markupKind the markup kind. * @return the content. */ private static String createContent(List<MarkupContent> values, String markupKind) { StringBuilder content = new StringBuilder(); for (MarkupContent value : values) { if (!StringUtils.isEmpty(value.getValue())) { if (content.length() > 0) { if (markupKind.equals(MarkupKind.MARKDOWN)) { content.append(System.lineSeparator()); content.append(System.lineSeparator()); content.append(MARKDOWN_SEPARATOR); } content.append(System.lineSeparator()); content.append(System.lineSeparator()); } content.append(value.getValue()); } } return content.toString(); }
Example #15
Source File: MarkupContentFactory.java From lemminx with Eclipse Public License 2.0 | 6 votes |
/** * Create the hover from the given markup content list and range. * * @param values the list of documentation values * @param defaultRange the default range. * @return the hover from the given markup content list and range. */ public static Hover createHover(List<MarkupContent> values, Range defaultRange) { if (values.isEmpty()) { return null; } if (values.size() == 1) { return new Hover(values.get(0), defaultRange); } // Markup kind boolean hasMarkdown = values.stream() // .anyMatch(contents -> MarkupKind.MARKDOWN.equals(contents.getKind())); String markupKind = hasMarkdown ? MarkupKind.MARKDOWN : MarkupKind.PLAINTEXT; // Contents String content = createContent(values, markupKind); // Range Range range = defaultRange; return new Hover(new MarkupContent(markupKind, content), range); }
Example #16
Source File: MarkupContentFactory.java From lemminx with Eclipse Public License 2.0 | 6 votes |
/** * Create the markup content according the given markup kind and the capability * of the client. * * @param value the documentation value * @param preferredKind the preferred markup kind * @return the markup content according the given markup kind and the capability * of the client. */ public static MarkupContent createMarkupContent(String value, String preferredKind, ISharedSettingsRequest support) { if (value == null) { return null; } MarkupContent content = new MarkupContent(); if (MarkupKind.MARKDOWN.equals(preferredKind) && support.canSupportMarkupKind(preferredKind)) { String markdown = MarkdownConverter.convert(value); content.setValue(markdown); content.setKind(MarkupKind.MARKDOWN); } else { content.setValue(value); content.setKind(MarkupKind.PLAINTEXT); } return content; }
Example #17
Source File: SnippetRegistry.java From lemminx with Eclipse Public License 2.0 | 6 votes |
private static MarkupContent createDocumentation(Snippet snippet, Map<String, String> model, boolean canSupportMarkdown, String lineDelimiter) { StringBuilder doc = new StringBuilder(); if (canSupportMarkdown) { doc.append(System.lineSeparator()); doc.append("```"); String scope = snippet.getScope(); if (scope != null) { doc.append(scope); } doc.append(System.lineSeparator()); } String insertText = getInsertText(snippet, model, true, lineDelimiter); doc.append(insertText); if (canSupportMarkdown) { doc.append(System.lineSeparator()); doc.append("```"); doc.append(System.lineSeparator()); } return new MarkupContent(canSupportMarkdown ? MarkupKind.MARKDOWN : MarkupKind.PLAINTEXT, doc.toString()); }
Example #18
Source File: ParameterInformation.java From lsp4j with Eclipse Public License 2.0 | 5 votes |
public void setDocumentation(final MarkupContent documentation) { if (documentation == null) { this.documentation = null; return; } this.documentation = Either.forRight(documentation); }
Example #19
Source File: HoverTypeAdapter.java From lsp4j with Eclipse Public License 2.0 | 5 votes |
protected void writeContents(final JsonWriter out, final Either<List<Either<String, MarkedString>>, MarkupContent> contents) throws IOException { boolean _isLeft = contents.isLeft(); if (_isLeft) { final List<Either<String, MarkedString>> list = contents.getLeft(); int _size = list.size(); boolean _equals = (_size == 1); if (_equals) { this.gson.toJson(list.get(0), HoverTypeAdapter.STRING_MARKEDSTRING.getType(), out); } else { this.gson.toJson(list, HoverTypeAdapter.LIST_STRING_MARKEDSTRING.getType(), out); } } else { this.gson.toJson(contents.getRight(), MarkupContent.class, out); } }
Example #20
Source File: SignatureInformation.java From lsp4j with Eclipse Public License 2.0 | 5 votes |
public void setDocumentation(final MarkupContent documentation) { if (documentation == null) { this.documentation = null; return; } this.documentation = Either.forRight(documentation); }
Example #21
Source File: Hover.java From lsp4j with Eclipse Public License 2.0 | 5 votes |
public void setContents(final MarkupContent contents) { if (contents == null) { Preconditions.checkNotNull(contents, "contents"); this.contents = null; return; } this.contents = Either.forRight(contents); }
Example #22
Source File: HoverService.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
protected Hover hover(HoverContext context) { if (context == null) { return IHoverService.EMPTY_HOVER; } MarkupContent contents = getMarkupContent(context); if (contents == null) { return IHoverService.EMPTY_HOVER; } Range range = getRange(context); if (range == null) { return IHoverService.EMPTY_HOVER; } return new Hover(contents, range); }
Example #23
Source File: XMLAssert.java From lemminx with Eclipse Public License 2.0 | 5 votes |
private static String getHoverLabel(Hover hover) { Either<List<Either<String, MarkedString>>, MarkupContent> contents = hover != null ? hover.getContents() : null; if (contents == null) { return null; } return contents.getRight().getValue(); }
Example #24
Source File: CustomContentAssistService.java From solidity-ide with Eclipse Public License 1.0 | 5 votes |
@Override protected CompletionItem toCompletionItem(ContentAssistEntry entry, int caretOffset, Position caretPosition, Document document) { CompletionItem completionItem = super.toCompletionItem(entry, caretOffset, caretPosition, document); Either<String, MarkupContent> documentation = completionItem.getDocumentation(); if (documentation != null && documentation.getLeft() == null && documentation.getRight()==null) { completionItem.setDocumentation((Either<String,MarkupContent>)null); } return completionItem; }
Example #25
Source File: CompletionItemBuilder.java From syndesis with Apache License 2.0 | 5 votes |
public static Either<String, MarkupContent> beautifyDocument(String raw) { // remove the placeholder for the plain cursor like: ${0}, ${1:variable} String escapedString = raw.replaceAll("\\$\\{\\d:?(.*?)\\}", "$1"); MarkupContent markupContent = new MarkupContent(); markupContent.setKind(MarkupKind.MARKDOWN); markupContent.setValue(String.format("```%s%n%s%n```", "java", escapedString)); return Either.forRight(markupContent); }
Example #26
Source File: StringLSP4J.java From n4js with Eclipse Public License 1.0 | 5 votes |
/** @return string for given element */ public String toString(MarkupContent markupContent) { if (markupContent == null) { return ""; } return "[" + markupContent.getKind() + "] " + markupContent.getValue(); }
Example #27
Source File: StringLSP4J.java From n4js with Eclipse Public License 1.0 | 5 votes |
/** @return string for given element */ public String toString7(Either<String, MarkupContent> documentation) { if (documentation == null) { return ""; } if (documentation.isLeft()) { return documentation.getLeft(); } else { return toString(documentation.getRight()); } }
Example #28
Source File: StringLSP4J.java From n4js with Eclipse Public License 1.0 | 5 votes |
/** @return string for given element */ public String toString5(Either<String, MarkupContent> strOrMarkupContent) { if (strOrMarkupContent == null) { return ""; } if (strOrMarkupContent.isLeft()) { return strOrMarkupContent.getLeft(); } else { return toString(strOrMarkupContent.getRight()); } }
Example #29
Source File: StringLSP4J.java From n4js with Eclipse Public License 1.0 | 5 votes |
/** @return string for given element */ public String toString1(Either<List<Either<String, MarkedString>>, MarkupContent> contents) { if (contents == null) { return ""; } if (contents.isLeft()) { List<Either<String, MarkedString>> markedStrings = contents.getLeft(); return Strings.toString(this::toString2, markedStrings); } else { return toString(contents.getRight()); } }
Example #30
Source File: MicroProfileConfigHoverParticipant.java From intellij-quarkus with Eclipse Public License 2.0 | 5 votes |
/** * Returns documentation about the provided <code>propertyKey</code>'s value, * <code>propertyValue</code> * * @param propertyKey the property key * @param propertyValue the property key's value * @param documentFormat documentation format (markdown/text) * @param insertSpacing true if spacing should be inserted around the equals * sign and false otherwise * @return */ public static MarkupContent getDocumentation(String propertyKey, String propertyValue, DocumentFormat documentFormat, boolean insertSpacing) { boolean markdown = DocumentFormat.Markdown.equals(documentFormat); StringBuilder content = new StringBuilder(); if (markdown) { content.append("`"); } content.append(propertyKey); if (propertyValue == null) { if (markdown) { content.append("`"); } content.append(" is not set."); } else { if (insertSpacing) { content.append(" = "); } else { content.append("="); } content.append(propertyValue); if (markdown) { content.append("`"); } } return new MarkupContent(markdown ? MarkupKind.MARKDOWN : MarkupKind.PLAINTEXT, content.toString()); }