Skip to content
This repository has been archived by the owner on Apr 21, 2023. It is now read-only.

Commit

Permalink
[#1372] update hover service to use MarkupContent
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Dietrich <[email protected]>
  • Loading branch information
cdietrich committed Feb 3, 2020
1 parent 7050b2f commit b133679
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -28,7 +28,8 @@ class HoverTest extends AbstractTestLangLanguageServerTest {
column = 'type F'.length
expectedHover = '''
[[3, 5] .. [3, 8]]
Some documentation.
kind: markdown
value: Some documentation.
'''
]
}
Expand All @@ -44,6 +45,10 @@ class HoverTest extends AbstractTestLangLanguageServerTest {
'''
line = 3
column = '{'.length
expectedHover = '''
kind: markdown
value:
'''
]
}

Expand All @@ -62,7 +67,8 @@ class HoverTest extends AbstractTestLangLanguageServerTest {
column = ' F'.length
expectedHover = '''
[[4, 1] .. [4, 4]]
Some documentation.
kind: markdown
value: Some documentation.
'''
]
}
Expand All @@ -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.
'''
]
}
Expand All @@ -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.
'''
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -366,6 +367,8 @@ protected String toExpectation(final Object it) {
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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -43,7 +43,9 @@ public void testHover_01() {
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());
};
Expand All @@ -67,6 +69,12 @@ public void testHover_02() {
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);
}
Expand Down Expand Up @@ -96,7 +104,9 @@ public void testHover_03() {
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());
};
Expand Down Expand Up @@ -129,7 +139,9 @@ public void testHover_04() {
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());
};
Expand Down Expand Up @@ -164,7 +176,9 @@ public void testHover_05() {
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());
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
/*******************************************************************************
* 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
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
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
Expand All @@ -27,15 +26,14 @@ 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.*

/**
* @author kosyakov - Initial contribution and API
* @since 2.11
*/
@Singleton
@Beta
class HoverService implements IHoverService {

@Inject
Expand Down Expand Up @@ -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
Expand All @@ -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
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,14 +13,15 @@ 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
*/
@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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
/**
* 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
* http://www.eclipse.org/legal/epl-v10.html
*/
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;
Expand All @@ -30,16 +28,14 @@
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
Expand Down Expand Up @@ -98,7 +94,7 @@ protected Hover hover(final HoverContext context) {
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;
}
Expand All @@ -118,34 +114,26 @@ protected Range getRange(final HoverContext it) {
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;
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,22 +9,20 @@

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
*/
@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.
Expand Down
Loading

0 comments on commit b133679

Please sign in to comment.