Added interfaces for hover engine and adapter

This commit is contained in:
nsingh
2016-11-15 16:07:55 -08:00
parent 3e9f097c2f
commit 377040f2a0
8 changed files with 189 additions and 1 deletions

View File

@@ -1,3 +1,13 @@
/*******************************************************************************
* Copyright (c) 2016 Pivotal, Inc.
* 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
*
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.commons.languageserver.hover;
/**
@@ -5,4 +15,10 @@ package org.springframework.ide.vscode.commons.languageserver.hover;
*/
public interface HoverInfo {
String renderAsText();
String renderAsHtml();
String renderAsMarkdown();
}

View File

@@ -0,0 +1,19 @@
/*******************************************************************************
* Copyright (c) 2016 Pivotal, Inc.
* 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
*
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.commons.languageserver.hover;
import org.springframework.ide.vscode.commons.languageserver.util.IDocument;
public interface IHoverEngine {
HoverInfo getHover(IDocument document, int offset) throws Exception;
}

View File

@@ -0,0 +1,22 @@
/*******************************************************************************
* Copyright (c) 2016 Pivotal, Inc.
* 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
*
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.commons.languageserver.hover;
import java.util.concurrent.CompletableFuture;
import org.eclipse.lsp4j.Hover;
import org.eclipse.lsp4j.TextDocumentPositionParams;
public interface VscodeHoverEngine {
CompletableFuture<Hover> getHover(TextDocumentPositionParams params);
}

View File

@@ -0,0 +1,64 @@
/*******************************************************************************
* Copyright (c) 2016 Pivotal, Inc.
* 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
*
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.commons.languageserver.hover;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;
import org.eclipse.lsp4j.Hover;
import org.eclipse.lsp4j.TextDocumentPositionParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleTextDocumentService;
import org.springframework.ide.vscode.commons.languageserver.util.TextDocument;
import org.springframework.ide.vscode.commons.util.Futures;
public class VscodeHoverEngineAdapter implements VscodeHoverEngine {
private IHoverEngine engine;
private SimpleLanguageServer server;
final static Logger logger = LoggerFactory.getLogger(VscodeHoverEngineAdapter.class);
public VscodeHoverEngineAdapter(SimpleLanguageServer server, IHoverEngine engine) {
this.engine = engine;
this.server = server;
}
@Override
public CompletableFuture<Hover> getHover(TextDocumentPositionParams params) {
//TODO: This returns a CompletableFuture which suggests we should try to do expensive work asyncly.
// We are currently just doing all this in a blocking way and wrapping the already computed list into
// a trivial pre-resolved future.
try {
SimpleTextDocumentService documents = server.getTextDocumentService();
TextDocument doc = documents.get(params);
if (doc!=null) {
int offset = doc.toOffset(params.getPosition());
HoverInfo hoverInfo = engine.getHover(doc, offset);
if (hoverInfo != null) {
Hover hover = new Hover();
hover.setContents(Collections.singletonList(hoverInfo.renderAsMarkdown()));
return Futures.of(hover);
}
else{
return Futures.of(null);
}
}
} catch (Exception e) {
logger.error("error computing hover", e);
}
return SimpleTextDocumentService.NO_HOVER;
}
}

View File

@@ -167,6 +167,8 @@ public class SimpleTextDocumentService implements TextDocumentService {
public final static CompletableFuture<CompletionList> NO_COMPLETIONS = Futures.of(
new CompletionList(false, Collections.emptyList()));
public final static CompletableFuture<Hover> NO_HOVER = Futures.of(null);
@Override
public CompletableFuture<CompletionList> completion(TextDocumentPositionParams position) {

View File

@@ -9,6 +9,9 @@ import org.eclipse.lsp4j.ServerCapabilities;
import org.eclipse.lsp4j.TextDocumentSyncKind;
import org.springframework.ide.vscode.commons.languageserver.completion.VscodeCompletionEngine;
import org.springframework.ide.vscode.commons.languageserver.completion.VscodeCompletionEngineAdapter;
import org.springframework.ide.vscode.commons.languageserver.hover.IHoverEngine;
import org.springframework.ide.vscode.commons.languageserver.hover.VscodeHoverEngine;
import org.springframework.ide.vscode.commons.languageserver.hover.VscodeHoverEngineAdapter;
import org.springframework.ide.vscode.commons.languageserver.reconcile.IReconcileEngine;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleTextDocumentService;
@@ -40,7 +43,9 @@ public class ManifestYamlLanguageServer extends SimpleLanguageServer {
YamlAssistContextProvider contextProvider = new SchemaBasedYamlAssistContextProvider(schema);
YamlCompletionEngine yamlCompletionEngine = new YamlCompletionEngine(structureProvider, contextProvider);
VscodeCompletionEngine completionEngine = new VscodeCompletionEngineAdapter(this, yamlCompletionEngine);
IHoverEngine yamlHoverEngine = new ManifestYmlHoverEngine();
VscodeHoverEngine hoverEngine = new VscodeHoverEngineAdapter(this, yamlHoverEngine );
// SimpleWorkspaceService workspace = getWorkspaceService();
documents.onDidChangeContent(params -> {
TextDocument doc = params.getDocument();
@@ -60,6 +65,7 @@ public class ManifestYamlLanguageServer extends SimpleLanguageServer {
documents.onCompletion(completionEngine::getCompletions);
documents.onCompletionResolve(completionEngine::resolveCompletion);
documents.onHover(hoverEngine ::getHover);
}
protected IReconcileEngine getReconcileEngine() {

View File

@@ -0,0 +1,27 @@
/*******************************************************************************
* Copyright (c) 2016 Pivotal, Inc.
* 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
*
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.manifest.yaml;
import org.springframework.ide.vscode.commons.languageserver.hover.HoverInfo;
import org.springframework.ide.vscode.commons.languageserver.hover.IHoverEngine;
import org.springframework.ide.vscode.commons.languageserver.util.IDocument;
public class ManifestYmlHoverEngine implements IHoverEngine {
@Override
public HoverInfo getHover(IDocument document, int offset) throws Exception {
// TODO. Create the hover info based on AST
HoverInfo info = new YamlHoverInfo();
return info;
}
}

View File

@@ -0,0 +1,32 @@
/*******************************************************************************
* Copyright (c) 2016 Pivotal, Inc.
* 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
*
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.manifest.yaml;
import org.springframework.ide.vscode.commons.languageserver.hover.HoverInfo;
public class YamlHoverInfo implements HoverInfo {
@Override
public String renderAsText() {
return null;
}
@Override
public String renderAsHtml() {
return null;
}
@Override
public String renderAsMarkdown() {
return null;
}
}