use of org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits in project sts4 by spring-projects.
the class ValueCompletionProcessor method provideCompletions.
@Override
public Collection<ICompletionProposal> provideCompletions(ASTNode node, Annotation annotation, ITypeBinding type, int offset, IDocument doc) {
List<ICompletionProposal> result = new ArrayList<>();
try {
FuzzyMap<PropertyInfo> index = indexProvider.getIndex(doc);
// case: @Value(<*>)
if (node == annotation && doc.get(offset - 1, 2).endsWith("()")) {
List<Match<PropertyInfo>> matches = findMatches("", index);
for (Match<PropertyInfo> match : matches) {
DocumentEdits edits = new DocumentEdits(doc);
edits.replace(offset, offset, "\"${" + match.data.getId() + "}\"");
ValuePropertyKeyProposal proposal = new ValuePropertyKeyProposal(edits, match.data.getId(), match.data.getName(), null);
result.add(proposal);
}
} else // case: @Value(prefix<*>)
if (node instanceof SimpleName && node.getParent() instanceof Annotation) {
computeProposalsForSimpleName(node, result, offset, doc, index);
} else // case: @Value(value=<*>)
if (node instanceof SimpleName && node.getParent() instanceof MemberValuePair && "value".equals(((MemberValuePair) node.getParent()).getName().toString())) {
computeProposalsForSimpleName(node, result, offset, doc, index);
} else // case: @Value("prefix<*>")
if (node instanceof StringLiteral && node.getParent() instanceof Annotation) {
if (node.toString().startsWith("\"") && node.toString().endsWith("\"")) {
computeProposalsForStringLiteral(node, result, offset, doc, index);
}
} else // case: @Value(value="prefix<*>")
if (node instanceof StringLiteral && node.getParent() instanceof MemberValuePair && "value".equals(((MemberValuePair) node.getParent()).getName().toString())) {
if (node.toString().startsWith("\"") && node.toString().endsWith("\"")) {
computeProposalsForStringLiteral(node, result, offset, doc, index);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
use of org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits in project sts4 by spring-projects.
the class ImportRewrite method createEdit.
public DocumentEdits createEdit(IDocument doc) {
DocumentEdits edits = null;
StringBuffer buffer = new StringBuffer();
String[] createdImprts = getAddedImports();
if (createdImprts != null && createdImprts.length > 0) {
edits = new DocumentEdits(doc);
buffer.append('\n');
for (String imp : createdImprts) {
buffer.append("import ");
buffer.append(imp);
buffer.append(';');
buffer.append('\n');
}
edits.insert(getAddedImportsInsertLocation(), buffer.toString());
}
return edits;
}
use of org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits in project sts4 by spring-projects.
the class YTypeAssistContext method createEditFromSnippet.
private DocumentEdits createEditFromSnippet(YamlDocument doc, SNode node, int offset, String query, YamlIndentUtil indenter, Snippet _snippet) throws Exception {
DocumentEdits edits = new DocumentEdits(doc.getDocument());
int start = offset - query.length();
edits.delete(start, query);
int referenceIndent = doc.getColumn(start);
boolean needNewline = node.getNodeType() == SNodeType.KEY;
String snippet = _snippet.getSnippet();
if (needNewline) {
snippet = "\n" + snippet;
referenceIndent = YamlIndentUtil.getNewChildKeyIndent(node);
} else if (start > 0 && !Character.isWhitespace(doc.getChar(start - 1))) {
referenceIndent++;
edits.insert(start, " ");
}
edits.insert(start, indenter.applyIndentation(snippet, referenceIndent));
return edits;
}
use of org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits in project sts4 by spring-projects.
the class YTypeAssistContext method getKeyCompletions.
public List<ICompletionProposal> getKeyCompletions(YamlDocument doc, SNode node, int offset, String query) throws Exception {
int queryOffset = offset - query.length();
DynamicSchemaContext dynamicCtxt = getSchemaContext();
List<YTypedProperty> allProperties = typeUtil.getProperties(type);
if (CollectionUtil.hasElements(allProperties)) {
List<List<YTypedProperty>> tieredProperties = sortIntoTiers(allProperties);
Set<String> definedProps = dynamicCtxt.getDefinedProperties();
List<ICompletionProposal> proposals = new ArrayList<>();
boolean suggestDeprecated = typeUtil.suggestDeprecatedProperties();
YamlIndentUtil indenter = new YamlIndentUtil(doc);
for (List<YTypedProperty> thisTier : tieredProperties) {
List<YTypedProperty> undefinedProps = thisTier.stream().filter(p -> !definedProps.contains(p.getName()) && (suggestDeprecated || !p.isDeprecated())).collect(Collectors.toList());
if (!undefinedProps.isEmpty()) {
for (YTypedProperty p : undefinedProps) {
String name = p.getName();
double score = FuzzyMatcher.matchScore(query, name);
if (score != 0) {
TypeBasedSnippetProvider snippetProvider = typeUtil.getSnippetProvider();
DocumentEdits edits;
if (snippetProvider != null) {
// Generate edits from snippet
Snippet snippet = snippetProvider.getSnippet(p);
edits = createEditFromSnippet(doc, node, offset, query, indenter, snippet);
} else {
// Generate edits the old-fashioned way
edits = new DocumentEdits(doc.getDocument());
YType YType = p.getType();
edits.delete(queryOffset, query);
int referenceIndent = doc.getColumn(queryOffset);
boolean needNewline = node.getNodeType() == SNodeType.KEY;
StringBuilder snippet = new StringBuilder();
if (needNewline) {
snippet.append("\n");
referenceIndent = YamlIndentUtil.getNewChildKeyIndent(node);
} else if (queryOffset > 0 && !Character.isWhitespace(doc.getChar(queryOffset - 1))) {
// See https://www.pivotaltracker.com/story/show/137722057
snippet.append(" ");
referenceIndent++;
}
snippet.append(p.getName());
snippet.append(":");
snippet.append(appendTextFor(YType));
edits.insert(queryOffset, indenter.applyIndentation(snippet.toString(), referenceIndent));
}
ICompletionProposal completion = completionFactory().beanProperty(doc.getDocument(), contextPath.toPropString(), getType(), query, p, score, edits, typeUtil);
if (p.isDeprecated() && completion instanceof ScoreableProposal) {
completion.deemphasize(DEEMP_DEPRECATION);
}
proposals.add(completion);
}
}
}
// We should only move on to the next tier if all required properties in this tier are defined.
if (undefinedProps.stream().anyMatch(p -> p.isRequired())) {
// stop here, take no more from next tier!
return proposals;
}
}
return proposals;
}
return Collections.emptyList();
}
use of org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits in project sts4 by spring-projects.
the class YamlCompletionEngine method indented.
public ScoreableProposal indented(ICompletionProposal proposal, String indentStr) {
int numArrows = (indentStr.length() + 1) / 2;
ScoreableProposal transformed = new TransformedCompletion(proposal) {
@Override
public String tranformLabel(String originalLabel) {
return Strings.repeat(Unicodes.RIGHT_ARROW + " ", numArrows) + originalLabel;
}
@Override
public DocumentEdits transformEdit(DocumentEdits originalEdit) {
// originalEdit.indentFirstEdit(indentStr);
YamlIndentUtil indenter = new YamlIndentUtil("\n");
if (originalEdit.hasRelativeIndents()) {
originalEdit.transformFirstNonWhitespaceEdit((Integer offset, String insertText) -> {
String prefix = insertText.substring(0, offset);
String target = insertText.substring(offset);
return prefix + indentStr + indenter.applyIndentation(target, indentStr);
});
} else {
originalEdit.transformFirstNonWhitespaceEdit((Integer offset, String insertText) -> {
String prefix = insertText.substring(0, offset);
String target = insertText.substring(offset);
return prefix + indentStr + target;
});
}
return originalEdit;
}
};
transformed.deemphasize(numArrows * DEEMP_INDENTED_PROPOSAL);
return transformed;
}
Aggregations