Add support for 'sts/highlight' protocol extension

Use it too highlight occurrences of the word 'foo' as an example of how to use this.
This commit is contained in:
Kris De Volder
2017-10-03 16:27:58 -07:00
parent 3133ec7e8e
commit cdf2e9fa44
10 changed files with 187 additions and 4 deletions

View File

@@ -92,6 +92,7 @@ public class BootJavaLanguageServer extends SimpleLanguageServer {
documents.onDidChangeContent(params -> {
TextDocument doc = params.getDocument();
validateWith(doc.getId(), reconcileEngine);
highlighWith(doc, new WordHighlighter(this, "foo"));
});
ICompletionEngine bootCompletionEngine = createCompletionEngine(javaProjectFinder, indexProvider);

View File

@@ -0,0 +1,58 @@
/*******************************************************************************
* 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;
import java.util.List;
import java.util.function.Function;
import org.eclipse.lsp4j.Range;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
import org.springframework.ide.vscode.commons.languageserver.util.TextDocumentContentChange;
import org.springframework.ide.vscode.commons.util.BadLocationException;
import org.springframework.ide.vscode.commons.util.Log;
import org.springframework.ide.vscode.commons.util.text.TextDocument;
import com.google.common.collect.ImmutableList;
/**
* Sample implementation of a 'highlighter' that can be used with {@link SimpleLanguageServer}.highlightWith.
* <p>
* Finds every occurence of a given word and highlights it.
*
* @author Kris De Volder
*/
public class WordHighlighter implements Function<TextDocument, List<Range>> {
private final SimpleLanguageServer server;
private final String word;
public WordHighlighter(SimpleLanguageServer server, String word) {
super();
this.server = server;
this.word = word;
}
@Override
public List<Range> apply(TextDocument doc) {
String text = doc.get();
int wordStart = text.indexOf(word);
ImmutableList.Builder<Range> highlights = ImmutableList.builder();
while (wordStart>=0) {
try {
highlights.add(doc.toRange(wordStart, word.length()));
} catch (BadLocationException e) {
Log.log(e);
}
wordStart = text.indexOf(word, wordStart+word.length());
}
return highlights.build();
}
}