Use proper snippet syntax so placeholders work in vscode
This commit is contained in:
@@ -10,25 +10,58 @@
|
||||
*******************************************************************************/
|
||||
package org.springframework.ide.vscode.boot.java.snippets;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Respobsible for converting eclipse-like template string into lsp snippet text.
|
||||
* @author Kris De Volder
|
||||
*/
|
||||
public class JavaSnippetBuilder{
|
||||
|
||||
private Supplier<SnippetBuilder> snippetBuilderFactory;
|
||||
|
||||
private static final Pattern PLACE_HOLDER = Pattern.compile("\\$\\{(.+?)\\}");
|
||||
|
||||
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);
|
||||
edit.replace(query.getStart(), query.getEnd(), createSnippet(template));
|
||||
return edit;
|
||||
}
|
||||
|
||||
private String createSnippet(String template) {
|
||||
Matcher matcher = PLACE_HOLDER.matcher(template);
|
||||
int start = 0;
|
||||
SnippetBuilder snippet = snippetBuilderFactory.get();
|
||||
while (matcher.find(start)) {
|
||||
int matchStart = matcher.start();
|
||||
snippet.text(template.substring(start, matchStart));
|
||||
int matchEnd = matcher.end();
|
||||
String placeHolderImage = template.substring(matcher.start(1), matcher.end(1));
|
||||
int colon = placeHolderImage.indexOf(':');
|
||||
String id, value;
|
||||
if (colon>=0) {
|
||||
id = placeHolderImage.substring(0, colon);
|
||||
value = placeHolderImage.substring(colon+1);
|
||||
} else {
|
||||
id = placeHolderImage;
|
||||
value = id;
|
||||
}
|
||||
snippet.placeHolder(id, value);
|
||||
start = matchEnd;
|
||||
}
|
||||
snippet.text(template.substring(start));
|
||||
return snippet.build().toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user