Search in sources :

Example 1 with DocumentEdits

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;
}
Also used : DocumentEdits(org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits) YamlIndentUtil(org.springframework.ide.vscode.commons.yaml.util.YamlIndentUtil) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) TypeBasedSnippetProvider(org.springframework.ide.vscode.commons.yaml.snippet.TypeBasedSnippetProvider) Snippet(org.springframework.ide.vscode.commons.yaml.snippet.Snippet) ISubCompletionEngine(org.springframework.ide.vscode.commons.yaml.schema.ISubCompletionEngine)

Example 2 with DocumentEdits

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();
}
Also used : DocumentEdits(org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits) YamlIndentUtil(org.springframework.ide.vscode.commons.yaml.util.YamlIndentUtil) ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) ArrayList(java.util.ArrayList) YValueHint(org.springframework.ide.vscode.commons.yaml.schema.YValueHint) YValueHint(org.springframework.ide.vscode.commons.yaml.schema.YValueHint) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException)

Example 3 with DocumentEdits

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);
    }
}
Also used : DocumentEdits(org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits) PropertyInfo(org.springframework.ide.vscode.boot.metadata.PropertyInfo) Match(org.springframework.ide.vscode.commons.util.FuzzyMap.Match)

Example 4 with DocumentEdits

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;
}
Also used : CommonLanguageTools.getValueType(org.springframework.ide.vscode.boot.common.CommonLanguageTools.getValueType) Type(org.springframework.ide.vscode.boot.metadata.types.Type) DocumentEdits(org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits) ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) ArrayList(java.util.ArrayList) TypedProperty(org.springframework.ide.vscode.boot.metadata.types.TypedProperty)

Example 5 with DocumentEdits

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();
}
Also used : EnumCaseMode(org.springframework.ide.vscode.boot.metadata.types.TypeUtil.EnumCaseMode) DocumentEdits(org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits) StsValueHint(org.springframework.ide.vscode.boot.metadata.hints.StsValueHint) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) ArrayList(java.util.ArrayList) StsValueHint(org.springframework.ide.vscode.boot.metadata.hints.StsValueHint)

Aggregations

DocumentEdits (org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits)18 ICompletionProposal (org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal)8 ArrayList (java.util.ArrayList)7 DocumentRegion (org.springframework.ide.vscode.commons.util.text.DocumentRegion)5 YamlIndentUtil (org.springframework.ide.vscode.commons.yaml.util.YamlIndentUtil)5 PropertyInfo (org.springframework.ide.vscode.boot.metadata.PropertyInfo)4 Match (org.springframework.ide.vscode.commons.util.FuzzyMap.Match)4 ScoreableProposal (org.springframework.ide.vscode.commons.languageserver.completion.ScoreableProposal)3 YValueHint (org.springframework.ide.vscode.commons.yaml.schema.YValueHint)3 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 CommonLanguageTools.getValueType (org.springframework.ide.vscode.boot.common.CommonLanguageTools.getValueType)2 Type (org.springframework.ide.vscode.boot.metadata.types.Type)2 Renderable (org.springframework.ide.vscode.commons.util.Renderable)2 Strings (com.google.common.base.Strings)1 Supplier (com.google.common.base.Supplier)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Map (java.util.Map)1 Optional (java.util.Optional)1