Search in sources :

Example 1 with MarkedStringDTO

use of org.eclipse.che.api.languageserver.shared.lsapi.MarkedStringDTO in project che by eclipse.

the class HoverProvider method computeHover.

@Override
public JsPromise<OrionHoverOverlay> computeHover(OrionHoverContextOverlay context) {
    EditorPartPresenter activeEditor = editorAgent.getActiveEditor();
    if (activeEditor == null || !(activeEditor instanceof TextEditor)) {
        return null;
    }
    TextEditor editor = ((TextEditor) activeEditor);
    if (!(editor.getConfiguration() instanceof LanguageServerEditorConfiguration)) {
        return null;
    }
    LanguageServerEditorConfiguration configuration = (LanguageServerEditorConfiguration) editor.getConfiguration();
    if (configuration.getServerCapabilities().isHoverProvider() == null || !configuration.getServerCapabilities().isHoverProvider()) {
        return null;
    }
    Document document = editor.getDocument();
    TextDocumentPositionParamsDTO paramsDTO = helper.createTDPP(document, context.getOffset());
    Promise<HoverDTO> promise = client.hover(paramsDTO);
    Promise<OrionHoverOverlay> then = promise.then(new Function<HoverDTO, OrionHoverOverlay>() {

        @Override
        public OrionHoverOverlay apply(HoverDTO arg) throws FunctionException {
            OrionHoverOverlay hover = OrionHoverOverlay.create();
            hover.setType("markdown");
            String content = renderContent(arg);
            //do not show hover with only white spaces
            if (StringUtils.isNullOrWhitespace(content)) {
                return null;
            }
            hover.setContent(content);
            return hover;
        }

        private String renderContent(HoverDTO hover) {
            List<String> contents = new ArrayList<String>();
            for (MarkedStringDTO dto : hover.getContents()) {
                String lang = dto.getLanguage();
                if (lang == null || MarkedString.PLAIN_STRING.equals(lang)) {
                    // plain markdown text
                    contents.add(dto.getValue());
                } else {
                    // markdown code block
                    contents.add("```" + lang + "\n" + dto.getValue() + "\n```");
                }
            }
            return Joiner.on("\n\n").join(contents);
        }
    });
    return (JsPromise<OrionHoverOverlay>) then;
}
Also used : TextDocumentPositionParamsDTO(org.eclipse.che.api.languageserver.shared.lsapi.TextDocumentPositionParamsDTO) FunctionException(org.eclipse.che.api.promises.client.FunctionException) HoverDTO(org.eclipse.che.api.languageserver.shared.lsapi.HoverDTO) MarkedString(io.typefox.lsapi.MarkedString) JsPromise(org.eclipse.che.api.promises.client.js.JsPromise) Document(org.eclipse.che.ide.api.editor.document.Document) TextEditor(org.eclipse.che.ide.api.editor.texteditor.TextEditor) MarkedStringDTO(org.eclipse.che.api.languageserver.shared.lsapi.MarkedStringDTO) LanguageServerEditorConfiguration(org.eclipse.che.plugin.languageserver.ide.editor.LanguageServerEditorConfiguration) ArrayList(java.util.ArrayList) List(java.util.List) EditorPartPresenter(org.eclipse.che.ide.api.editor.EditorPartPresenter) OrionHoverOverlay(org.eclipse.che.ide.editor.orion.client.jso.OrionHoverOverlay)

Aggregations

MarkedString (io.typefox.lsapi.MarkedString)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 HoverDTO (org.eclipse.che.api.languageserver.shared.lsapi.HoverDTO)1 MarkedStringDTO (org.eclipse.che.api.languageserver.shared.lsapi.MarkedStringDTO)1 TextDocumentPositionParamsDTO (org.eclipse.che.api.languageserver.shared.lsapi.TextDocumentPositionParamsDTO)1 FunctionException (org.eclipse.che.api.promises.client.FunctionException)1 JsPromise (org.eclipse.che.api.promises.client.js.JsPromise)1 EditorPartPresenter (org.eclipse.che.ide.api.editor.EditorPartPresenter)1 Document (org.eclipse.che.ide.api.editor.document.Document)1 TextEditor (org.eclipse.che.ide.api.editor.texteditor.TextEditor)1 OrionHoverOverlay (org.eclipse.che.ide.editor.orion.client.jso.OrionHoverOverlay)1 LanguageServerEditorConfiguration (org.eclipse.che.plugin.languageserver.ide.editor.LanguageServerEditorConfiguration)1