Search in sources :

Example 1 with ICompletionProposal

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;
}
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 ICompletionProposal

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();
}
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 ICompletionProposal

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();
}
Also used : SRootNode(org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SRootNode) SNode(org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SNode) YamlDocument(org.springframework.ide.vscode.commons.yaml.structure.YamlDocument) ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) ArrayList(java.util.ArrayList)

Example 4 with ICompletionProposal

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;
}
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 ICompletionProposal

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();
}
Also used : CommonLanguageTools.getValueType(org.springframework.ide.vscode.boot.common.CommonLanguageTools.getValueType) Type(org.springframework.ide.vscode.boot.metadata.types.Type) ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) PropertyNavigator(org.springframework.ide.vscode.boot.properties.reconcile.PropertyNavigator) PropertyInfo(org.springframework.ide.vscode.boot.metadata.PropertyInfo) StsValueHint(org.springframework.ide.vscode.boot.metadata.hints.StsValueHint) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException)

Aggregations

ICompletionProposal (org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal)16 ArrayList (java.util.ArrayList)14 DocumentEdits (org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits)7 DocumentRegion (org.springframework.ide.vscode.commons.util.text.DocumentRegion)7 CommonLanguageTools.getValueType (org.springframework.ide.vscode.boot.common.CommonLanguageTools.getValueType)3 PropertyInfo (org.springframework.ide.vscode.boot.metadata.PropertyInfo)3 Type (org.springframework.ide.vscode.boot.metadata.types.Type)3 Annotation (org.eclipse.jdt.core.dom.Annotation)2 MemberValuePair (org.eclipse.jdt.core.dom.MemberValuePair)2 SimpleName (org.eclipse.jdt.core.dom.SimpleName)2 StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)2 StsValueHint (org.springframework.ide.vscode.boot.metadata.hints.StsValueHint)2 ScoreableProposal (org.springframework.ide.vscode.commons.languageserver.completion.ScoreableProposal)2 BadLocationException (org.springframework.ide.vscode.commons.util.BadLocationException)2 Match (org.springframework.ide.vscode.commons.util.FuzzyMap.Match)2 ValueParseException (org.springframework.ide.vscode.commons.util.ValueParseException)2 ISubCompletionEngine (org.springframework.ide.vscode.commons.yaml.schema.ISubCompletionEngine)2 YValueHint (org.springframework.ide.vscode.commons.yaml.schema.YValueHint)2 YamlIndentUtil (org.springframework.ide.vscode.commons.yaml.util.YamlIndentUtil)2 Strings (com.google.common.base.Strings)1