added support for code lenses
This commit is contained in:
@@ -13,6 +13,7 @@ package org.springframework.ide.vscode.boot.java;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.ide.vscode.boot.java.handlers.BootJavaCodeLensEngine;
|
||||
import org.springframework.ide.vscode.boot.java.handlers.BootJavaCompletionEngine;
|
||||
import org.springframework.ide.vscode.boot.java.handlers.BootJavaDocumentSymbolHandler;
|
||||
import org.springframework.ide.vscode.boot.java.handlers.BootJavaHoverProvider;
|
||||
@@ -89,6 +90,10 @@ public class BootJavaLanguageServer extends SimpleLanguageServer {
|
||||
documents.onReferences(referencesHandler);
|
||||
documents.onDocumentSymbol(createDocumentSymbolHandler(this, javaProjectFinder));
|
||||
|
||||
BootJavaCodeLensEngine codeLensHandler = createCodeLensEngine(this, javaProjectFinder);
|
||||
documents.onCodeLens(codeLensHandler::createCodeLenses);
|
||||
documents.onCodeLensResolve(codeLensHandler::resolveCodeLens);
|
||||
|
||||
workspaceService.onWorkspaceSymbol(createWorkspaceSymbolHandler(this, javaProjectFinder));
|
||||
}
|
||||
|
||||
@@ -148,4 +153,9 @@ public class BootJavaLanguageServer extends SimpleLanguageServer {
|
||||
return new BootJavaReferencesHandler(server, projectFinder, providers);
|
||||
}
|
||||
|
||||
protected BootJavaCodeLensEngine createCodeLensEngine(SimpleLanguageServer server, JavaProjectFinder projectFinder) {
|
||||
return new BootJavaCodeLensEngine(server, projectFinder);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2017 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.boot.java.handlers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import org.eclipse.lsp4j.CodeLens;
|
||||
import org.eclipse.lsp4j.CodeLensParams;
|
||||
import org.eclipse.lsp4j.Command;
|
||||
import org.eclipse.lsp4j.Position;
|
||||
import org.eclipse.lsp4j.Range;
|
||||
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder;
|
||||
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
|
||||
import org.springframework.ide.vscode.commons.languageserver.util.SimpleTextDocumentService;
|
||||
import org.springframework.ide.vscode.commons.util.text.TextDocument;
|
||||
|
||||
/**
|
||||
* @author Martin Lippert
|
||||
*/
|
||||
public class BootJavaCodeLensEngine {
|
||||
|
||||
private final SimpleLanguageServer server;
|
||||
private final JavaProjectFinder projectFinder;
|
||||
|
||||
public BootJavaCodeLensEngine(SimpleLanguageServer server, JavaProjectFinder projectFinder) {
|
||||
this.server = server;
|
||||
this.projectFinder = projectFinder;
|
||||
}
|
||||
|
||||
public CompletableFuture<List<? extends CodeLens>> createCodeLenses(CodeLensParams params) {
|
||||
SimpleTextDocumentService documents = server.getTextDocumentService();
|
||||
String docURI = params.getTextDocument().getUri();
|
||||
|
||||
if (documents.get(docURI) != null) {
|
||||
TextDocument doc = documents.get(docURI).copy();
|
||||
try {
|
||||
CompletableFuture<List<? extends CodeLens>> codeLensesResult = provideCodeLenses(doc);
|
||||
if (codeLensesResult != null) {
|
||||
return codeLensesResult;
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
return SimpleTextDocumentService.NO_CODELENS;
|
||||
}
|
||||
|
||||
private CompletableFuture<List<? extends CodeLens>> provideCodeLenses(TextDocument doc) {
|
||||
List<CodeLens> result = new ArrayList<>();
|
||||
|
||||
CodeLens codeLens = new CodeLens();
|
||||
|
||||
Range range = new Range();
|
||||
range.setStart(new Position(5, 0));
|
||||
range.setEnd(new Position(5, 10));
|
||||
codeLens.setRange(range);
|
||||
|
||||
Command command = new Command("my first awesome code lens", "my first code lens command");
|
||||
codeLens.setCommand(command);
|
||||
|
||||
codeLens.setData("some data");
|
||||
|
||||
result.add(codeLens);
|
||||
return CompletableFuture.completedFuture(result);
|
||||
}
|
||||
|
||||
public CompletableFuture<CodeLens> resolveCodeLens(CodeLens unresolved) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2017 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.util;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import org.eclipse.lsp4j.CodeLens;
|
||||
import org.eclipse.lsp4j.CodeLensParams;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface CodeLensHandler {
|
||||
|
||||
CompletableFuture<List<? extends CodeLens>> handle(CodeLensParams params);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2017 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.util;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import org.eclipse.lsp4j.CodeLens;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface CodeLensResolveHandler {
|
||||
CompletableFuture<CodeLens> handle(CodeLens unresolved);
|
||||
}
|
||||
@@ -20,6 +20,7 @@ import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import org.eclipse.lsp4j.ApplyWorkspaceEditParams;
|
||||
import org.eclipse.lsp4j.ApplyWorkspaceEditResponse;
|
||||
import org.eclipse.lsp4j.CodeLensOptions;
|
||||
import org.eclipse.lsp4j.CompletionOptions;
|
||||
import org.eclipse.lsp4j.Diagnostic;
|
||||
import org.eclipse.lsp4j.DiagnosticSeverity;
|
||||
@@ -243,6 +244,11 @@ public abstract class SimpleLanguageServer implements LanguageServer, LanguageCl
|
||||
if (hasDocumentSymbolHandler()) {
|
||||
c.setDocumentSymbolProvider(true);
|
||||
}
|
||||
if (hasCodeLensHandler()) {
|
||||
CodeLensOptions codeLensOptions = new CodeLensOptions();
|
||||
codeLensOptions.setResolveProvider(hasCodeLensResolveProvider());
|
||||
c.setCodeLensProvider(codeLensOptions );
|
||||
}
|
||||
if (hasExecuteCommandSupport && hasQuickFixes()) {
|
||||
c.setExecuteCommandProvider(new ExecuteCommandOptions(ImmutableList.of(
|
||||
CODE_ACTION_COMMAND_ID
|
||||
@@ -263,6 +269,14 @@ public abstract class SimpleLanguageServer implements LanguageServer, LanguageCl
|
||||
return getTextDocumentService().hasDocumentSymbolHandler();
|
||||
}
|
||||
|
||||
private boolean hasCodeLensHandler() {
|
||||
return getTextDocumentService().hasCodeLensHandler();
|
||||
}
|
||||
|
||||
private boolean hasCodeLensResolveProvider() {
|
||||
return getTextDocumentService().hasCodeLensResolveProvider();
|
||||
}
|
||||
|
||||
private boolean hasReferencesHandler() {
|
||||
return getTextDocumentService().hasReferencesHandler();
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
* Contributors:
|
||||
* Pivotal, Inc. - initial API and implementation
|
||||
*******************************************************************************/
|
||||
|
||||
package org.springframework.ide.vscode.commons.languageserver.util;
|
||||
|
||||
import java.util.Collection;
|
||||
@@ -71,14 +70,18 @@ public class SimpleTextDocumentService implements TextDocumentService {
|
||||
final private SimpleLanguageServer server;
|
||||
private Map<String, TrackedDocument> documents = new HashMap<>();
|
||||
private ListenerList<TextDocumentContentChange> documentChangeListeners = new ListenerList<>();
|
||||
|
||||
private CompletionHandler completionHandler = null;
|
||||
private CompletionResolveHandler completionResolveHandler = null;
|
||||
private HoverHandler hoverHandler = null;
|
||||
|
||||
private HoverHandler hoverHandler = null;
|
||||
private DefinitionHandler definitionHandler;
|
||||
private ReferencesHandler referencesHandler;
|
||||
|
||||
private DocumentSymbolHandler documentSymbolHandler;
|
||||
|
||||
private CodeLensHandler codeLensHandler;
|
||||
private CodeLensResolveHandler codeLensResolveHandler;
|
||||
|
||||
private Consumer<TextDocumentSaveChange> documentSaveListener;
|
||||
|
||||
public SimpleTextDocumentService(SimpleLanguageServer server) {
|
||||
@@ -90,6 +93,16 @@ public class SimpleTextDocumentService implements TextDocumentService {
|
||||
this.hoverHandler = h;
|
||||
}
|
||||
|
||||
public synchronized void onCodeLens(CodeLensHandler h) {
|
||||
Assert.isNull("A code lens handler is already set, multiple handlers not supported yet", codeLensHandler);
|
||||
this.codeLensHandler = h;
|
||||
}
|
||||
|
||||
public synchronized void onCodeLensResolve(CodeLensResolveHandler h) {
|
||||
Assert.isNull("A code lens resolve handler is already set, multiple handlers not supported yet", codeLensResolveHandler);
|
||||
this.codeLensResolveHandler = h;
|
||||
}
|
||||
|
||||
public synchronized void onDocumentSymbol(DocumentSymbolHandler h) {
|
||||
Assert.isNull("A DocumentSymbolHandler is already set, multiple handlers not supported yet", documentSymbolHandler);
|
||||
this.documentSymbolHandler = h;
|
||||
@@ -234,6 +247,7 @@ public class SimpleTextDocumentService implements TextDocumentService {
|
||||
public final static CompletableFuture<Hover> NO_HOVER = CompletableFuture.completedFuture(new Hover(ImmutableList.of(), null));
|
||||
public final static CompletableFuture<List<? extends Location>> NO_REFERENCES = CompletableFuture.completedFuture(ImmutableList.of());
|
||||
public final static List<? extends SymbolInformation> NO_SYMBOLS = ImmutableList.of();
|
||||
public final static CompletableFuture<List<? extends CodeLens>> NO_CODELENS = CompletableFuture.completedFuture(ImmutableList.of());
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Either<List<CompletionItem>, CompletionList>> completion(TextDocumentPositionParams position) {
|
||||
@@ -319,11 +333,19 @@ public class SimpleTextDocumentService implements TextDocumentService {
|
||||
|
||||
@Override
|
||||
public CompletableFuture<List<? extends CodeLens>> codeLens(CodeLensParams params) {
|
||||
CodeLensHandler handler = this.codeLensHandler;
|
||||
if (handler != null) {
|
||||
return handler.handle(params);
|
||||
}
|
||||
return CompletableFuture.completedFuture(Collections.emptyList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<CodeLens> resolveCodeLens(CodeLens unresolved) {
|
||||
CodeLensResolveHandler handler = this.codeLensResolveHandler;
|
||||
if (handler != null) {
|
||||
return handler.handle(unresolved);
|
||||
}
|
||||
return CompletableFuture.completedFuture(null);
|
||||
}
|
||||
|
||||
@@ -409,4 +431,12 @@ public class SimpleTextDocumentService implements TextDocumentService {
|
||||
return this.documentSymbolHandler!=null;
|
||||
}
|
||||
|
||||
public boolean hasCodeLensHandler() {
|
||||
return this.codeLensHandler != null;
|
||||
}
|
||||
|
||||
public boolean hasCodeLensResolveProvider() {
|
||||
return this.codeLensResolveHandler != null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user