use of org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal 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.ICompletionProposal 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.ICompletionProposal in project sts4 by spring-projects.
the class YamlCompletionEngine method getCompletions.
@Override
public Collection<ICompletionProposal> getCompletions(TextDocument _doc, int offset) throws Exception {
YamlDocument doc = new YamlDocument(_doc, structureProvider);
if (!doc.isCommented(offset)) {
SRootNode root = doc.getStructure();
SNode current = root.find(offset);
int cursorIndent = doc.getColumn(offset);
int nodeIndent = current.getIndent();
int baseIndent = YamlIndentUtil.minIndent(cursorIndent, nodeIndent);
List<SNode> contextNodes = getContextNodes(doc, current, offset, baseIndent);
if (current.getNodeType() == SNodeType.RAW) {
// relaxed indentation
List<ICompletionProposal> completions = new ArrayList<>();
double deempasizeBy = 0.0;
for (SNode contextNode : contextNodes) {
completions.addAll(getRelaxedCompletions(offset, doc, current, contextNode, baseIndent, deempasizeBy));
deempasizeBy += ScoreableProposal.DEEMP_NEXT_CONTEXT;
}
return completions;
} else {
// precise indentation only
Assert.isLegal(contextNodes.size() <= 1);
for (SNode contextNode : contextNodes) {
return getBaseCompletions(offset, doc, current, contextNode);
}
}
}
return Collections.emptyList();
}
use of org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal 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.ICompletionProposal in project sts4 by spring-projects.
the class PropertiesCompletionProposalsCalculator method getNavigationProposals.
private Collection<ICompletionProposal> getNavigationProposals() {
String navPrefix = navigationPrefixFinder.getPrefix(doc, offset);
try {
if (navPrefix != null) {
// offset of 'nav' operator char (i.e. '.' or ']').
int navOffset = offset - navPrefix.length() - 1;
navPrefix = fuzzySearchPrefix.getPrefix(doc, navOffset);
if (navPrefix != null && !navPrefix.isEmpty()) {
PropertyInfo prop = findLongestValidProperty(index, navPrefix);
if (prop != null) {
int regionStart = navOffset - navPrefix.length();
Collection<ICompletionProposal> hintProposals = getKeyHintProposals(prop, navOffset);
if (CollectionUtil.hasElements(hintProposals)) {
return hintProposals;
}
PropertyNavigator navigator = new PropertyNavigator(doc, null, typeUtil, new DocumentRegion(doc, regionStart, navOffset));
Type type = navigator.navigate(regionStart + prop.getId().length(), TypeParser.parse(prop.getType()));
if (type != null) {
return getNavigationProposals(type, navOffset);
}
}
}
}
} catch (Exception e) {
Log.log(e);
}
return Collections.emptyList();
}
Aggregations