Goto definition iin pipeline editor, working for resources

This commit is contained in:
Kris De Volder
2017-01-24 12:05:15 -08:00
parent 46818d3462
commit 099c19bba3
20 changed files with 515 additions and 69 deletions

View File

@@ -0,0 +1,87 @@
/*******************************************************************************
* 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.definition;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.TextDocumentPositionParams;
import org.springframework.ide.vscode.commons.languageserver.util.DefinitionHandler;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
import org.springframework.ide.vscode.commons.util.Log;
import org.springframework.ide.vscode.commons.util.text.TextDocument;
import reactor.core.publisher.Flux;
/**
* {@link SimpleDefinitionFinder} provides a 'dummy' implementation of
* @author Kris De Volder
*/
public class SimpleDefinitionFinder<T extends SimpleLanguageServer> implements DefinitionHandler {
protected final T server;
public SimpleDefinitionFinder(T server) {
this.server = server;
}
@Override
public CompletableFuture<List<Location>> handle(TextDocumentPositionParams position) {
return findDefinitions(position)
.collect(Collectors.toList())
.toFuture();
}
/**
* This is meant to be overridden by subclass. This method provides a simple implementation
* of 'goto definition' (which is not one you probably want to use in practice, but it
* might be usful just to test whether things are wired up correctly to make the
* 'goto definition' action in vscode work.
* <p>
* The implementation provided here simply looks for the first occurrence of the word
* currently pointed at in the current document using String.indexOf.
*/
protected Flux<Location> findDefinitions(TextDocumentPositionParams params) {
try {
TextDocument doc = server.getTextDocumentService().get(params);
int offset = doc.toOffset(params.getPosition());
int start = offset;
while (Character.isLetter(doc.getSafeChar(start))) {
start--;
}
start = start+1;
int end = offset;
while (Character.isLetter(doc.getSafeChar(end))) {
end++;
}
String word = doc.textBetween(start, end);
Log.log("Looking for definition of '"+word+"'");
String text = doc.get();
int def = text.indexOf(word);
if (def>=0) {
return Flux.just(
new Location(params.getTextDocument().getUri(),
doc.toRange(def, word.length())
)
)
.doOnNext((Location loc) -> {
Log.log("definition: "+loc);
});
}
} catch (Exception e) {
Log.log(e);
}
return Flux.empty();
}
}

View File

@@ -0,0 +1,22 @@
/*******************************************************************************
* 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.Location;
import org.eclipse.lsp4j.TextDocumentPositionParams;
@FunctionalInterface
public interface DefinitionHandler {
CompletableFuture<List<Location>> handle(TextDocumentPositionParams position);
}

View File

@@ -65,6 +65,13 @@ public class DocumentRegion implements CharSequence {
this.end = limitRange(end, start, doc.getLength());
}
/**
* Create {@link DocumentRegion} covering the whole document.
*/
public DocumentRegion(IDocument doc) {
this(doc, 0, doc.getLength());
}
private int limitRange(int offset, int min, int max) {
if (offset<min) {
return min;

View File

@@ -72,6 +72,8 @@ public class SimpleTextDocumentService implements TextDocumentService {
private CompletionResolveHandler completionResolveHandler = null;
private HoverHandler hoverHandler = null;
private DefinitionHandler definitionHandler;
public SimpleTextDocumentService(SimpleLanguageServer server) {
this.server = server;
}
@@ -91,6 +93,11 @@ public class SimpleTextDocumentService implements TextDocumentService {
this.completionResolveHandler = h;
}
public synchronized void onDefinition(DefinitionHandler h) {
Assert.isNull("A defintion handler is already set, multiple handlers not supported yet", definitionHandler);
this.definitionHandler = h;
}
/**
* Gets all documents this service is tracking, generally these are the documents that have been opened / changed,
* and not yet closed.
@@ -221,9 +228,15 @@ public class SimpleTextDocumentService implements TextDocumentService {
return Futures.of(null);
}
@SuppressWarnings({ "unchecked"})
@Override
public CompletableFuture<List<? extends Location>> definition(TextDocumentPositionParams position) {
return Futures.of(Collections.emptyList());
DefinitionHandler h = this.definitionHandler;
if (h!=null) {
Object r = h.handle(position); //YUCK!
return (CompletableFuture<List<? extends Location>>) r;
}
return CompletableFuture.completedFuture(Collections.emptyList());
}
@Override
@@ -294,4 +307,9 @@ public class SimpleTextDocumentService implements TextDocumentService {
return Futures.of(Collections.emptyList());
}
public void onDefinition(TextDocumentPositionParams h) {
// TODO Auto-generated method stub
}
}