use of org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits in project sts4 by spring-projects.
the class YTypeAssistContext method getCompletions.
@Override
public Collection<ICompletionProposal> getCompletions(YamlDocument doc, SNode node, int offset) throws Exception {
ISubCompletionEngine customContentAssistant = typeUtil.getCustomContentAssistant(type);
if (customContentAssistant != null) {
DocumentRegion region = getCustomAssistRegion(doc, node, offset);
if (region != null) {
return customContentAssistant.getCompletions(completionFactory(), region, region.toRelative(offset));
}
}
String query = getPrefix(doc, node, offset);
List<ICompletionProposal> completions = getValueCompletions(doc, node, offset, query);
if (completions.isEmpty()) {
TypeBasedSnippetProvider snippetProvider = typeUtil.getSnippetProvider();
if (snippetProvider != null) {
Collection<Snippet> snippets = snippetProvider.getSnippets(type);
YamlIndentUtil indenter = new YamlIndentUtil(doc);
for (Snippet snippet : snippets) {
String snippetName = snippet.getName();
double score = FuzzyMatcher.matchScore(query, snippetName);
if (score != 0.0 && snippet.isApplicable(getSchemaContext())) {
DocumentEdits edits = createEditFromSnippet(doc, node, offset, query, indenter, snippet);
completions.add(completionFactory().valueProposal(snippetName, query, snippetName, type, null, score, edits, typeUtil));
}
}
}
completions.addAll(getKeyCompletions(doc, node, offset, query));
}
if (typeUtil.isSequencable(type)) {
completions = new ArrayList<>(completions);
completions.addAll(getDashedCompletions(doc, node, offset));
}
return completions;
}
use of org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits in project sts4 by spring-projects.
the class YTypeAssistContext method getValueCompletions.
private List<ICompletionProposal> getValueCompletions(YamlDocument doc, SNode node, int offset, String query) {
PartialCollection<YValueHint> _values = typeUtil.getHintValues(type, getSchemaContext());
if (_values.getExplanation() != null && _values.getElements().isEmpty() && !Boolean.getBoolean("lsp.yaml.completions.errors.disable")) {
return ImmutableList.of(completionFactory().errorMessage(query, getMessage(_values.getExplanation())));
}
Collection<YValueHint> values = _values.getElements();
if (values != null) {
ArrayList<ICompletionProposal> completions = new ArrayList<>();
YamlIndentUtil indenter = new YamlIndentUtil(doc);
int referenceIndent;
try {
referenceIndent = getContextNode().getIndent();
} catch (Exception e) {
// Getting it from the node isn't always correct, but more often than not it is.
// So this fallback is better than nothing.
referenceIndent = node.getIndent();
}
for (YValueHint value : values) {
double score = FuzzyMatcher.matchScore(query, value.getValue());
if (score != 0 && value != null && !query.equals(value.getValue())) {
int queryStart = offset - query.length();
DocumentEdits edits = new DocumentEdits(doc.getDocument());
edits.delete(queryStart, offset);
if (!Character.isWhitespace(doc.getChar(queryStart - 1))) {
edits.insert(offset, " ");
}
edits.insert(offset, value.getValue());
String extraInsertion = value.getExtraInsertion();
if (extraInsertion != null) {
edits.insert(offset, indenter.applyIndentation(extraInsertion, referenceIndent));
}
completions.add(completionFactory().valueProposal(value.getValue(), query, value.getLabel(), type, value.getDocumentation(), score, edits, typeUtil));
}
}
return completions;
}
return Collections.emptyList();
}
use of org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits in project sts4 by spring-projects.
the class ValueCompletionProcessor method computeProposalsForStringLiteral.
private void computeProposalsForStringLiteral(ASTNode node, List<ICompletionProposal> completions, int offset, IDocument doc, FuzzyMap<PropertyInfo> index) throws BadLocationException {
String prefix = identifyPropertyPrefix(doc.get(node.getStartPosition() + 1, offset - (node.getStartPosition() + 1)), offset - (node.getStartPosition() + 1));
int startOffset = offset - prefix.length();
int endOffset = offset;
String prePrefix = doc.get(node.getStartPosition() + 1, offset - prefix.length() - node.getStartPosition() - 1);
String preCompletion;
if (prePrefix.endsWith("${")) {
preCompletion = "";
} else if (prePrefix.endsWith("$")) {
preCompletion = "{";
} else {
preCompletion = "${";
}
String fullNodeContent = doc.get(node.getStartPosition(), node.getLength());
String postCompletion = isClosingBracketMissing(fullNodeContent + preCompletion) ? "}" : "";
List<Match<PropertyInfo>> matches = findMatches(prefix, index);
for (Match<PropertyInfo> match : matches) {
DocumentEdits edits = new DocumentEdits(doc);
edits.replace(startOffset, endOffset, preCompletion + match.data.getId() + postCompletion);
ValuePropertyKeyProposal proposal = new ValuePropertyKeyProposal(edits, match.data.getId(), match.data.getName(), null);
completions.add(proposal);
}
}
use of org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits in project sts4 by spring-projects.
the class PropertiesCompletionProposalsCalculator method createPropertyProposals.
protected Collection<ICompletionProposal> createPropertyProposals(Type type, int navOffset, String prefix, List<TypedProperty> objectProperties) {
ArrayList<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>();
for (TypedProperty prop : objectProperties) {
double score = FuzzyMatcher.matchScore(prefix, prop.getName());
if (score != 0) {
Type valueType = prop.getType();
String postFix = propertyCompletionPostfix(typeUtil, valueType);
DocumentEdits edits = new DocumentEdits(doc);
edits.delete(navOffset + 1, offset);
edits.insert(offset, prop.getName() + postFix);
proposals.add(completionFactory.beanProperty(doc, null, type, prefix, prop, score, edits, typeUtil));
}
}
return proposals;
}
use of org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits in project sts4 by spring-projects.
the class PropertiesCompletionProposalsCalculator method getValueCompletions.
private Collection<ICompletionProposal> getValueCompletions(Value value) {
DocumentRegion valueRegion = createRegion(doc, value).trimStart(SPACES).trimEnd(SPACES);
String query = valuePrefixFinder.getPrefix(doc, offset, valueRegion.getStart());
int startOfValue = offset - query.length();
EnumCaseMode caseMode = caseMode(query);
// note: no need to skip whitespace backwards.
String propertyName = /*fuzzySearchPrefix.getPrefix(doc, pair.getOffset())*/
value.getParent().getKey().decode();
// because value partition includes whitespace around the assignment
if (propertyName != null) {
Collection<StsValueHint> valueCompletions = getValueHints(index, typeUtil, query, propertyName, caseMode);
if (valueCompletions != null && !valueCompletions.isEmpty()) {
ArrayList<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>();
for (StsValueHint hint : valueCompletions) {
String valueCandidate = hint.getValue();
double score = FuzzyMatcher.matchScore(query, valueCandidate);
if (score != 0) {
DocumentEdits edits = new DocumentEdits(doc);
edits.delete(startOfValue, offset);
edits.insert(offset, valueCandidate);
String valueTypeName = typeUtil.niceTypeName(getValueType(index, typeUtil, propertyName));
proposals.add(completionFactory.valueProposal(valueCandidate, query, valueTypeName, score, edits, ValueHintHoverInfo.create(hint)));
}
}
return proposals;
}
}
return Collections.emptyList();
}
Aggregations