mirror of
https://github.com/sigmasternchen/xtext-core
synced 2025-03-15 08:18:55 +00:00
[#1372] update hover service to use MarkupContent
Signed-off-by: Christian Dietrich <christian.dietrich@itemis.de>
This commit is contained in:
parent
7050b2faba
commit
b13367970f
10 changed files with 94 additions and 70 deletions
|
@ -1,5 +1,5 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2016 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* Copyright (c) 2016, 2020 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
|
@ -28,7 +28,8 @@ class HoverTest extends AbstractTestLangLanguageServerTest {
|
|||
column = 'type F'.length
|
||||
expectedHover = '''
|
||||
[[3, 5] .. [3, 8]]
|
||||
Some documentation.
|
||||
kind: markdown
|
||||
value: Some documentation.
|
||||
'''
|
||||
]
|
||||
}
|
||||
|
@ -44,6 +45,10 @@ class HoverTest extends AbstractTestLangLanguageServerTest {
|
|||
'''
|
||||
line = 3
|
||||
column = '{'.length
|
||||
expectedHover = '''
|
||||
kind: markdown
|
||||
value:
|
||||
'''
|
||||
]
|
||||
}
|
||||
|
||||
|
@ -62,7 +67,8 @@ class HoverTest extends AbstractTestLangLanguageServerTest {
|
|||
column = ' F'.length
|
||||
expectedHover = '''
|
||||
[[4, 1] .. [4, 4]]
|
||||
Some documentation.
|
||||
kind: markdown
|
||||
value: Some documentation.
|
||||
'''
|
||||
]
|
||||
}
|
||||
|
@ -83,7 +89,8 @@ class HoverTest extends AbstractTestLangLanguageServerTest {
|
|||
column = 'type Bar extends F'.length
|
||||
expectedHover = '''
|
||||
[[5, 17] .. [5, 20]]
|
||||
Some documentation.
|
||||
kind: markdown
|
||||
value: Some documentation.
|
||||
'''
|
||||
]
|
||||
}
|
||||
|
@ -107,7 +114,8 @@ class HoverTest extends AbstractTestLangLanguageServerTest {
|
|||
column = 'type Bar extends F'.length
|
||||
expectedHover = '''
|
||||
[[0, 17] .. [0, 20]]
|
||||
Some documentation.
|
||||
kind: markdown
|
||||
value: Some documentation.
|
||||
'''
|
||||
]
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ import org.eclipse.lsp4j.DocumentSymbol;
|
|||
import org.eclipse.lsp4j.Hover;
|
||||
import org.eclipse.lsp4j.InsertTextFormat;
|
||||
import org.eclipse.lsp4j.Location;
|
||||
import org.eclipse.lsp4j.MarkupContent;
|
||||
import org.eclipse.lsp4j.Position;
|
||||
import org.eclipse.lsp4j.Range;
|
||||
import org.eclipse.lsp4j.SemanticHighlightingInformation;
|
||||
|
@ -366,6 +367,8 @@ public class CompletionTest extends AbstractTestLangLanguageServerTest {
|
|||
return _toExpectation((Hover)it);
|
||||
} else if (it instanceof Location) {
|
||||
return _toExpectation((Location)it);
|
||||
} else if (it instanceof MarkupContent) {
|
||||
return _toExpectation((MarkupContent)it);
|
||||
} else if (it instanceof Position) {
|
||||
return _toExpectation((Position)it);
|
||||
} else if (it instanceof Range) {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Copyright (c) 2016 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* Copyright (c) 2016, 2020 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
|
@ -43,7 +43,9 @@ public class HoverTest extends AbstractTestLangLanguageServerTest {
|
|||
StringConcatenation _builder_1 = new StringConcatenation();
|
||||
_builder_1.append("[[3, 5] .. [3, 8]]");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("Some documentation.");
|
||||
_builder_1.append("kind: markdown");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("value: Some documentation.");
|
||||
_builder_1.newLine();
|
||||
it.setExpectedHover(_builder_1.toString());
|
||||
};
|
||||
|
@ -67,6 +69,12 @@ public class HoverTest extends AbstractTestLangLanguageServerTest {
|
|||
it.setModel(_builder.toString());
|
||||
it.setLine(3);
|
||||
it.setColumn("{".length());
|
||||
StringConcatenation _builder_1 = new StringConcatenation();
|
||||
_builder_1.append("kind: markdown");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("value: ");
|
||||
_builder_1.newLine();
|
||||
it.setExpectedHover(_builder_1.toString());
|
||||
};
|
||||
this.testHover(_function);
|
||||
}
|
||||
|
@ -96,7 +104,9 @@ public class HoverTest extends AbstractTestLangLanguageServerTest {
|
|||
StringConcatenation _builder_1 = new StringConcatenation();
|
||||
_builder_1.append("[[4, 1] .. [4, 4]]");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("Some documentation.");
|
||||
_builder_1.append("kind: markdown");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("value: Some documentation.");
|
||||
_builder_1.newLine();
|
||||
it.setExpectedHover(_builder_1.toString());
|
||||
};
|
||||
|
@ -129,7 +139,9 @@ public class HoverTest extends AbstractTestLangLanguageServerTest {
|
|||
StringConcatenation _builder_1 = new StringConcatenation();
|
||||
_builder_1.append("[[5, 17] .. [5, 20]]");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("Some documentation.");
|
||||
_builder_1.append("kind: markdown");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("value: Some documentation.");
|
||||
_builder_1.newLine();
|
||||
it.setExpectedHover(_builder_1.toString());
|
||||
};
|
||||
|
@ -164,7 +176,9 @@ public class HoverTest extends AbstractTestLangLanguageServerTest {
|
|||
StringConcatenation _builder_2 = new StringConcatenation();
|
||||
_builder_2.append("[[0, 17] .. [0, 20]]");
|
||||
_builder_2.newLine();
|
||||
_builder_2.append("Some documentation.");
|
||||
_builder_2.append("kind: markdown");
|
||||
_builder_2.newLine();
|
||||
_builder_2.append("value: Some documentation.");
|
||||
_builder_2.newLine();
|
||||
it.setExpectedHover(_builder_2.toString());
|
||||
};
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2016, 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* Copyright (c) 2016, 2020 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
|
@ -7,15 +7,14 @@
|
|||
*******************************************************************************/
|
||||
package org.eclipse.xtext.ide.server.hover
|
||||
|
||||
import com.google.common.annotations.Beta
|
||||
import com.google.inject.Inject
|
||||
import com.google.inject.Singleton
|
||||
import java.util.List
|
||||
import org.eclipse.emf.ecore.EObject
|
||||
import org.eclipse.lsp4j.Hover
|
||||
import org.eclipse.lsp4j.MarkedString
|
||||
import org.eclipse.lsp4j.MarkupContent
|
||||
import org.eclipse.lsp4j.Range
|
||||
import org.eclipse.lsp4j.TextDocumentPositionParams
|
||||
import org.eclipse.lsp4j.jsonrpc.messages.Either
|
||||
import org.eclipse.xtend.lib.annotations.Accessors
|
||||
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor
|
||||
import org.eclipse.xtext.documentation.IEObjectDocumentationProvider
|
||||
|
@ -27,8 +26,6 @@ import org.eclipse.xtext.resource.XtextResource
|
|||
import org.eclipse.xtext.util.CancelIndicator
|
||||
import org.eclipse.xtext.util.ITextRegion
|
||||
|
||||
import static java.util.Collections.*
|
||||
|
||||
import static extension org.eclipse.xtext.nodemodel.util.NodeModelUtils.*
|
||||
|
||||
/**
|
||||
|
@ -36,6 +33,7 @@ import static extension org.eclipse.xtext.nodemodel.util.NodeModelUtils.*
|
|||
* @since 2.11
|
||||
*/
|
||||
@Singleton
|
||||
@Beta
|
||||
class HoverService implements IHoverService {
|
||||
|
||||
@Inject
|
||||
|
@ -88,7 +86,7 @@ class HoverService implements IHoverService {
|
|||
protected def Hover hover(HoverContext context) {
|
||||
if (context === null) return EMPTY_HOVER
|
||||
|
||||
val contents = context.contents
|
||||
val contents = context.markupContent
|
||||
if(contents === null) return EMPTY_HOVER
|
||||
|
||||
val range = context.range
|
||||
|
@ -103,33 +101,25 @@ class HoverService implements IHoverService {
|
|||
return resource.newRange(region)
|
||||
}
|
||||
|
||||
protected def List<Either<String, MarkedString>> getContents(HoverContext it) {
|
||||
val language = language
|
||||
return element.contents.map [ value |
|
||||
toContents(language, value)
|
||||
]
|
||||
protected def MarkupContent getMarkupContent(HoverContext it) {
|
||||
return toMarkupContent(kind, element.contents)
|
||||
}
|
||||
|
||||
protected def String getLanguage(HoverContext it) {
|
||||
return null
|
||||
protected def String getKind(HoverContext it) {
|
||||
return "markdown"
|
||||
}
|
||||
|
||||
protected def Either<String, MarkedString> toContents(String language, String value) {
|
||||
if (language === null) {
|
||||
return Either.forLeft(value)
|
||||
}
|
||||
return Either.forRight(new MarkedString(language, value))
|
||||
protected def MarkupContent toMarkupContent(String kind, String value) {
|
||||
return new MarkupContent(kind, value)
|
||||
}
|
||||
|
||||
def List<String> getContents(EObject element) {
|
||||
if(element === null) return emptyList
|
||||
def String getContents(EObject element) {
|
||||
if(element === null) return ""
|
||||
|
||||
val documentation = element.documentation
|
||||
if(documentation === null) return emptyList
|
||||
if(documentation === null) return ""
|
||||
|
||||
return #[
|
||||
documentation
|
||||
]
|
||||
return documentation
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* Copyright (c) 2017, 2020 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
|
@ -13,6 +13,7 @@ import org.eclipse.xtext.ide.server.Document
|
|||
import org.eclipse.xtext.resource.XtextResource
|
||||
import org.eclipse.xtext.util.CancelIndicator
|
||||
import com.google.inject.ImplementedBy
|
||||
import org.eclipse.lsp4j.MarkupContent
|
||||
|
||||
/**
|
||||
* @author Sven Efftinge - Initial contribution and API
|
||||
|
@ -20,7 +21,7 @@ import com.google.inject.ImplementedBy
|
|||
@ImplementedBy(HoverService)
|
||||
interface IHoverService {
|
||||
|
||||
public static val EMPTY_HOVER = new Hover(emptyList, null)
|
||||
public static val EMPTY_HOVER = new Hover(new MarkupContent("markdown", ""), null)
|
||||
|
||||
/**
|
||||
* callback for 'textDocument/hover' requests.
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Copyright (c) 2016, 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* Copyright (c) 2016, 2020 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Copyright (c) 2016, 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* Copyright (c) 2016, 2020 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
|
@ -7,16 +7,14 @@
|
|||
*/
|
||||
package org.eclipse.xtext.ide.server.hover;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.lsp4j.Hover;
|
||||
import org.eclipse.lsp4j.MarkedString;
|
||||
import org.eclipse.lsp4j.MarkupContent;
|
||||
import org.eclipse.lsp4j.Range;
|
||||
import org.eclipse.lsp4j.TextDocumentPositionParams;
|
||||
import org.eclipse.lsp4j.jsonrpc.messages.Either;
|
||||
import org.eclipse.xtext.documentation.IEObjectDocumentationProvider;
|
||||
import org.eclipse.xtext.ide.server.Document;
|
||||
import org.eclipse.xtext.ide.server.DocumentExtensions;
|
||||
|
@ -30,16 +28,14 @@ import org.eclipse.xtext.resource.ILocationInFileProvider;
|
|||
import org.eclipse.xtext.resource.XtextResource;
|
||||
import org.eclipse.xtext.util.CancelIndicator;
|
||||
import org.eclipse.xtext.util.ITextRegion;
|
||||
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
|
||||
import org.eclipse.xtext.xbase.lib.Extension;
|
||||
import org.eclipse.xtext.xbase.lib.Functions.Function1;
|
||||
import org.eclipse.xtext.xbase.lib.ListExtensions;
|
||||
|
||||
/**
|
||||
* @author kosyakov - Initial contribution and API
|
||||
* @since 2.11
|
||||
*/
|
||||
@Singleton
|
||||
@Beta
|
||||
@SuppressWarnings("all")
|
||||
public class HoverService implements IHoverService {
|
||||
@Inject
|
||||
|
@ -98,7 +94,7 @@ public class HoverService implements IHoverService {
|
|||
if ((context == null)) {
|
||||
return IHoverService.EMPTY_HOVER;
|
||||
}
|
||||
final List<Either<String, MarkedString>> contents = this.getContents(context);
|
||||
final MarkupContent contents = this.getMarkupContent(context);
|
||||
if ((contents == null)) {
|
||||
return IHoverService.EMPTY_HOVER;
|
||||
}
|
||||
|
@ -118,34 +114,26 @@ public class HoverService implements IHoverService {
|
|||
return this._documentExtensions.newRange(it.getResource(), it.getRegion());
|
||||
}
|
||||
|
||||
protected List<Either<String, MarkedString>> getContents(final HoverContext it) {
|
||||
final String language = this.getLanguage(it);
|
||||
final Function1<String, Either<String, MarkedString>> _function = (String value) -> {
|
||||
return this.toContents(language, value);
|
||||
};
|
||||
return ListExtensions.<String, Either<String, MarkedString>>map(this.getContents(it.getElement()), _function);
|
||||
protected MarkupContent getMarkupContent(final HoverContext it) {
|
||||
return this.toMarkupContent(this.getKind(it), this.getContents(it.getElement()));
|
||||
}
|
||||
|
||||
protected String getLanguage(final HoverContext it) {
|
||||
return null;
|
||||
protected String getKind(final HoverContext it) {
|
||||
return "markdown";
|
||||
}
|
||||
|
||||
protected Either<String, MarkedString> toContents(final String language, final String value) {
|
||||
if ((language == null)) {
|
||||
return Either.<String, MarkedString>forLeft(value);
|
||||
}
|
||||
MarkedString _markedString = new MarkedString(language, value);
|
||||
return Either.<String, MarkedString>forRight(_markedString);
|
||||
protected MarkupContent toMarkupContent(final String kind, final String value) {
|
||||
return new MarkupContent(kind, value);
|
||||
}
|
||||
|
||||
public List<String> getContents(final EObject element) {
|
||||
public String getContents(final EObject element) {
|
||||
if ((element == null)) {
|
||||
return Collections.<String>emptyList();
|
||||
return "";
|
||||
}
|
||||
final String documentation = this._iEObjectDocumentationProvider.getDocumentation(element);
|
||||
if ((documentation == null)) {
|
||||
return Collections.<String>emptyList();
|
||||
return "";
|
||||
}
|
||||
return Collections.<String>unmodifiableList(CollectionLiterals.<String>newArrayList(documentation));
|
||||
return documentation;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Copyright (c) 2017 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* Copyright (c) 2017, 2020 TypeFox GmbH (http://www.typefox.io) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
|
@ -9,14 +9,12 @@ package org.eclipse.xtext.ide.server.hover;
|
|||
|
||||
import com.google.inject.ImplementedBy;
|
||||
import org.eclipse.lsp4j.Hover;
|
||||
import org.eclipse.lsp4j.MarkedString;
|
||||
import org.eclipse.lsp4j.MarkupContent;
|
||||
import org.eclipse.lsp4j.TextDocumentPositionParams;
|
||||
import org.eclipse.lsp4j.jsonrpc.messages.Either;
|
||||
import org.eclipse.xtext.ide.server.Document;
|
||||
import org.eclipse.xtext.ide.server.hover.HoverService;
|
||||
import org.eclipse.xtext.resource.XtextResource;
|
||||
import org.eclipse.xtext.util.CancelIndicator;
|
||||
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
|
||||
|
||||
/**
|
||||
* @author Sven Efftinge - Initial contribution and API
|
||||
|
@ -24,7 +22,7 @@ import org.eclipse.xtext.xbase.lib.CollectionLiterals;
|
|||
@ImplementedBy(HoverService.class)
|
||||
@SuppressWarnings("all")
|
||||
public interface IHoverService {
|
||||
static final Hover EMPTY_HOVER = new Hover(CollectionLiterals.<Either<String, MarkedString>>emptyList(), null);
|
||||
static final Hover EMPTY_HOVER = new Hover(new MarkupContent("markdown", ""), null);
|
||||
|
||||
/**
|
||||
* callback for 'textDocument/hover' requests.
|
||||
|
|
|
@ -85,6 +85,7 @@ import org.junit.jupiter.api.BeforeEach
|
|||
import static extension org.eclipse.lsp4j.util.Ranges.containsRange
|
||||
import static extension org.eclipse.xtext.util.Strings.*
|
||||
import org.eclipse.lsp4j.SignatureHelpParams
|
||||
import org.eclipse.lsp4j.MarkupContent
|
||||
|
||||
/**
|
||||
* @author Sven Efftinge - Initial contribution and API
|
||||
|
@ -404,6 +405,11 @@ abstract class AbstractLanguageServerTest implements Endpoint {
|
|||
assertEquals(expectedCodeLensItems, result.toExpectation)
|
||||
}
|
||||
}
|
||||
|
||||
protected dispatch def String toExpectation(MarkupContent it) '''
|
||||
kind: «kind»
|
||||
value: «value»
|
||||
'''
|
||||
|
||||
protected dispatch def String toExpectation(Command it) '''
|
||||
command : «command»
|
||||
|
|
|
@ -56,6 +56,7 @@ import org.eclipse.lsp4j.InitializeParams;
|
|||
import org.eclipse.lsp4j.InitializeResult;
|
||||
import org.eclipse.lsp4j.Location;
|
||||
import org.eclipse.lsp4j.LocationLink;
|
||||
import org.eclipse.lsp4j.MarkupContent;
|
||||
import org.eclipse.lsp4j.Position;
|
||||
import org.eclipse.lsp4j.PublishDiagnosticsParams;
|
||||
import org.eclipse.lsp4j.Range;
|
||||
|
@ -867,6 +868,19 @@ public abstract class AbstractLanguageServerTest implements Endpoint {
|
|||
}
|
||||
}
|
||||
|
||||
protected String _toExpectation(final MarkupContent it) {
|
||||
StringConcatenation _builder = new StringConcatenation();
|
||||
_builder.append("kind: ");
|
||||
String _kind = it.getKind();
|
||||
_builder.append(_kind);
|
||||
_builder.newLineIfNotEmpty();
|
||||
_builder.append("value: ");
|
||||
String _value = it.getValue();
|
||||
_builder.append(_value);
|
||||
_builder.newLineIfNotEmpty();
|
||||
return _builder.toString();
|
||||
}
|
||||
|
||||
protected String _toExpectation(final Command it) {
|
||||
StringConcatenation _builder = new StringConcatenation();
|
||||
_builder.append("command : ");
|
||||
|
@ -1480,6 +1494,8 @@ public abstract class AbstractLanguageServerTest implements Endpoint {
|
|||
return _toExpectation((Hover)it);
|
||||
} else if (it instanceof Location) {
|
||||
return _toExpectation((Location)it);
|
||||
} else if (it instanceof MarkupContent) {
|
||||
return _toExpectation((MarkupContent)it);
|
||||
} else if (it instanceof Position) {
|
||||
return _toExpectation((Position)it);
|
||||
} else if (it instanceof Range) {
|
||||
|
|
Loading…
Reference in a new issue