Add support to include imports through additional edits
This commit is contained in:
@@ -15,11 +15,9 @@ import java.util.Optional;
|
||||
|
||||
import org.eclipse.jdt.core.dom.ASTNode;
|
||||
import org.eclipse.lsp4j.CompletionItemKind;
|
||||
import org.springframework.ide.vscode.boot.java.handlers.SimpleCompletionFactory;
|
||||
import org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal;
|
||||
import org.springframework.ide.vscode.commons.languageserver.util.DocumentRegion;
|
||||
import org.springframework.ide.vscode.commons.languageserver.util.SnippetBuilder;
|
||||
import org.springframework.ide.vscode.commons.util.Renderables;
|
||||
import org.springframework.ide.vscode.commons.util.text.IDocument;
|
||||
|
||||
import com.google.common.base.Supplier;
|
||||
|
||||
@@ -46,15 +44,14 @@ public class JavaSnippet {
|
||||
}
|
||||
|
||||
public Optional<ICompletionProposal> generateCompletion(Supplier<SnippetBuilder> snippetBuilderFactory,
|
||||
IDocument doc, int offset, ASTNode node, String query) {
|
||||
DocumentRegion query, ASTNode node) {
|
||||
|
||||
if (context.appliesTo(node)) {
|
||||
return Optional.of(
|
||||
SimpleCompletionFactory.simpleProposal(doc,
|
||||
offset,
|
||||
new JavaSnippetCompletion(snippetBuilderFactory,
|
||||
query,
|
||||
kind, template, "Snippet", Renderables.NO_DESCRIPTION
|
||||
).setLabel(name)
|
||||
this
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -65,4 +62,16 @@ public class JavaSnippet {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public String getTemplate() {
|
||||
return this.template;
|
||||
}
|
||||
|
||||
public List<String> getImports() {
|
||||
return this.imports;
|
||||
}
|
||||
|
||||
public CompletionItemKind getKind() {
|
||||
return kind;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/*******************************************************************************
|
||||
* 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.snippets;
|
||||
|
||||
import org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits;
|
||||
import org.springframework.ide.vscode.commons.languageserver.util.DocumentRegion;
|
||||
import org.springframework.ide.vscode.commons.languageserver.util.SnippetBuilder;
|
||||
|
||||
import com.google.common.base.Supplier;
|
||||
|
||||
public class JavaSnippetBuilder{
|
||||
|
||||
private Supplier<SnippetBuilder> snippetBuilderFactory;
|
||||
|
||||
public JavaSnippetBuilder(Supplier<SnippetBuilder> snippetBuilderFactory) {
|
||||
this.snippetBuilderFactory = snippetBuilderFactory;
|
||||
}
|
||||
|
||||
public DocumentEdits createEdit(DocumentRegion query, String template) {
|
||||
DocumentEdits edit = new DocumentEdits(query.getDocument());
|
||||
|
||||
edit.replace(query.getStart(), query.getEnd(), template);
|
||||
return edit;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*******************************************************************************
|
||||
* 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.snippets;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.eclipse.lsp4j.CompletionItemKind;
|
||||
import org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits;
|
||||
import org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal;
|
||||
import org.springframework.ide.vscode.commons.languageserver.util.DocumentRegion;
|
||||
import org.springframework.ide.vscode.commons.languageserver.util.SnippetBuilder;
|
||||
import org.springframework.ide.vscode.commons.util.Renderable;
|
||||
import org.springframework.ide.vscode.commons.util.Renderables;
|
||||
|
||||
import com.google.common.base.Supplier;
|
||||
|
||||
public class JavaSnippetCompletion implements ICompletionProposal{
|
||||
|
||||
private DocumentRegion query;
|
||||
private JavaSnippet javaSnippet;
|
||||
private Supplier<SnippetBuilder> snippetBuilderFactory;
|
||||
|
||||
public JavaSnippetCompletion(Supplier<SnippetBuilder> snippetBuilderFactory, DocumentRegion query, JavaSnippet javaSnippet) {
|
||||
this.snippetBuilderFactory = snippetBuilderFactory;
|
||||
this.query = query;
|
||||
this.javaSnippet = javaSnippet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLabel() {
|
||||
return javaSnippet.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletionItemKind getKind() {
|
||||
return javaSnippet.getKind();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DocumentEdits getTextEdit() {
|
||||
return new JavaSnippetBuilder(snippetBuilderFactory).createEdit(query, javaSnippet.getTemplate());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDetail() {
|
||||
return "Snippet";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Renderable getDocumentation() {
|
||||
return Renderables.NO_DESCRIPTION;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<DocumentEdits> getAdditionalEdit() {
|
||||
DocumentEdits edit = new DocumentEdits(query.getDocument());
|
||||
edit.insert(0, "import my.stuff;\n");
|
||||
return Optional.of(edit );
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import java.util.List;
|
||||
|
||||
import org.eclipse.jdt.core.dom.ASTNode;
|
||||
import org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal;
|
||||
import org.springframework.ide.vscode.commons.languageserver.util.DocumentRegion;
|
||||
import org.springframework.ide.vscode.commons.languageserver.util.PrefixFinder;
|
||||
import org.springframework.ide.vscode.commons.languageserver.util.SnippetBuilder;
|
||||
import org.springframework.ide.vscode.commons.util.FuzzyMatcher;
|
||||
@@ -48,11 +49,11 @@ public class JavaSnippetManager {
|
||||
public Collection<ICompletionProposal> getCompletions(IDocument doc, int offset, ASTNode node) {
|
||||
Collection<ICompletionProposal> completions = new ArrayList<>();
|
||||
|
||||
String query = PREFIX_FINDER.getPrefix(doc, offset);
|
||||
DocumentRegion query = PREFIX_FINDER.getPrefixRegion(doc, offset);
|
||||
|
||||
for (JavaSnippet javaSnippet : snippets) {
|
||||
if (FuzzyMatcher.matchScore(query, javaSnippet.getName()) != 0) {
|
||||
javaSnippet.generateCompletion(snippetBuilderFactory, doc, offset, node, query)
|
||||
if (FuzzyMatcher.matchScore(query.toString(), javaSnippet.getName()) != 0) {
|
||||
javaSnippet.generateCompletion(snippetBuilderFactory, query, node)
|
||||
.ifPresent((completion) -> completions.add(completion));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user