Search in sources :

Example 1 with YamlIndentUtil

use of org.springframework.ide.vscode.commons.yaml.util.YamlIndentUtil 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 YamlIndentUtil

use of org.springframework.ide.vscode.commons.yaml.util.YamlIndentUtil 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 YamlIndentUtil

use of org.springframework.ide.vscode.commons.yaml.util.YamlIndentUtil 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();
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) YTypeUtil(org.springframework.ide.vscode.commons.yaml.schema.YTypeUtil) FuzzyMatcher(org.springframework.ide.vscode.commons.util.FuzzyMatcher) YamlIndentUtil(org.springframework.ide.vscode.commons.yaml.util.YamlIndentUtil) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) YamlPathSegmentType(org.springframework.ide.vscode.commons.yaml.path.YamlPathSegment.YamlPathSegmentType) ImmutableList(com.google.common.collect.ImmutableList) DEEMP_DEPRECATION(org.springframework.ide.vscode.commons.languageserver.completion.ScoreableProposal.DEEMP_DEPRECATION) Map(java.util.Map) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException) YamlPath(org.springframework.ide.vscode.commons.yaml.path.YamlPath) DynamicSchemaContext(org.springframework.ide.vscode.commons.yaml.schema.DynamicSchemaContext) SNodeType(org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SNodeType) YamlPathSegment(org.springframework.ide.vscode.commons.yaml.path.YamlPathSegment) Logger(org.slf4j.Logger) ExceptionUtil(org.springframework.ide.vscode.commons.util.ExceptionUtil) YTypedProperty(org.springframework.ide.vscode.commons.yaml.schema.YTypedProperty) Log(org.springframework.ide.vscode.commons.util.Log) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) ISubCompletionEngine(org.springframework.ide.vscode.commons.yaml.schema.ISubCompletionEngine) ScoreableProposal(org.springframework.ide.vscode.commons.languageserver.completion.ScoreableProposal) Collection(java.util.Collection) TypeBasedSnippetProvider(org.springframework.ide.vscode.commons.yaml.snippet.TypeBasedSnippetProvider) Set(java.util.Set) PartialCollection(org.springframework.ide.vscode.commons.util.PartialCollection) YamlDocument(org.springframework.ide.vscode.commons.yaml.structure.YamlDocument) DEEMP_DASH_PROPOSAL(org.springframework.ide.vscode.commons.languageserver.completion.ScoreableProposal.DEEMP_DASH_PROPOSAL) Collectors(java.util.stream.Collectors) YType(org.springframework.ide.vscode.commons.yaml.schema.YType) YPropertyInfoTemplates(org.springframework.ide.vscode.commons.yaml.hover.YPropertyInfoTemplates) List(java.util.List) CollectionUtil(org.springframework.ide.vscode.commons.util.CollectionUtil) DocumentEdits(org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits) ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) SNodeDynamicSchemaContext(org.springframework.ide.vscode.commons.yaml.schema.SNodeDynamicSchemaContext) SNode(org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SNode) YValueHint(org.springframework.ide.vscode.commons.yaml.schema.YValueHint) Snippet(org.springframework.ide.vscode.commons.yaml.snippet.Snippet) Renderable(org.springframework.ide.vscode.commons.util.Renderable) ValueProposal(org.springframework.ide.vscode.commons.yaml.completion.DefaultCompletionFactory.ValueProposal) Collections(java.util.Collections) DocumentEdits(org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits) ScoreableProposal(org.springframework.ide.vscode.commons.languageserver.completion.ScoreableProposal) ArrayList(java.util.ArrayList) DynamicSchemaContext(org.springframework.ide.vscode.commons.yaml.schema.DynamicSchemaContext) SNodeDynamicSchemaContext(org.springframework.ide.vscode.commons.yaml.schema.SNodeDynamicSchemaContext) Snippet(org.springframework.ide.vscode.commons.yaml.snippet.Snippet) YValueHint(org.springframework.ide.vscode.commons.yaml.schema.YValueHint) YamlIndentUtil(org.springframework.ide.vscode.commons.yaml.util.YamlIndentUtil) ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) TypeBasedSnippetProvider(org.springframework.ide.vscode.commons.yaml.snippet.TypeBasedSnippetProvider) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) YTypedProperty(org.springframework.ide.vscode.commons.yaml.schema.YTypedProperty) YType(org.springframework.ide.vscode.commons.yaml.schema.YType)

Example 4 with YamlIndentUtil

use of org.springframework.ide.vscode.commons.yaml.util.YamlIndentUtil 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;
}
Also used : DocumentEdits(org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits) ScoreableProposal(org.springframework.ide.vscode.commons.languageserver.completion.ScoreableProposal) YamlIndentUtil(org.springframework.ide.vscode.commons.yaml.util.YamlIndentUtil)

Example 5 with YamlIndentUtil

use of org.springframework.ide.vscode.commons.yaml.util.YamlIndentUtil in project sts4 by spring-projects.

the class YamlCompletionEngine method dedented.

public ScoreableProposal dedented(ICompletionProposal proposal, int numSpacesToRemove, IDocument doc) {
    Assert.isLegal(numSpacesToRemove > 0);
    int spacesEnd = proposal.getTextEdit().getFirstEditStart();
    int spacesStart = spacesEnd - numSpacesToRemove;
    int numArrows = numSpacesToRemove / YamlIndentUtil.INDENT_BY;
    String spaces = new DocumentRegion(doc, spacesStart, spacesEnd).toString();
    YamlIndentUtil indenter = new YamlIndentUtil(doc);
    if (spaces.length() == numSpacesToRemove && SPACES.matcher(spaces).matches()) {
        ScoreableProposal transformed = new TransformedCompletion(proposal) {

            @Override
            public String tranformLabel(String originalLabel) {
                return Strings.repeat(Unicodes.LEFT_ARROW + " ", numArrows) + originalLabel;
            }

            @Override
            public DocumentEdits transformEdit(DocumentEdits originalEdit) {
                originalEdit.firstDelete(spacesStart, spacesEnd);
                originalEdit.transformFirstNonWhitespaceEdit((offset, insertText) -> {
                    String prefix = insertText.substring(0, offset);
                    String dedented = indenter.applyIndentation(insertText.substring(offset), -numSpacesToRemove);
                    return prefix + dedented;
                });
                return originalEdit;
            }

            @Override
            public String getFilterText() {
                // to replace. Since we are replacing these removed spaces, they must be part of the filtertext
                return spaces + super.getFilterText();
            }
        };
        transformed.deemphasize(DEEMP_DEDENTED_PROPOSAL * numArrows);
        return transformed;
    }
    // in our attempt to de-dent.)
    return null;
}
Also used : DocumentEdits(org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits) YamlIndentUtil(org.springframework.ide.vscode.commons.yaml.util.YamlIndentUtil) ScoreableProposal(org.springframework.ide.vscode.commons.languageserver.completion.ScoreableProposal) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion)

Aggregations

DocumentEdits (org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits)5 YamlIndentUtil (org.springframework.ide.vscode.commons.yaml.util.YamlIndentUtil)5 ICompletionProposal (org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal)3 ScoreableProposal (org.springframework.ide.vscode.commons.languageserver.completion.ScoreableProposal)3 DocumentRegion (org.springframework.ide.vscode.commons.util.text.DocumentRegion)3 ArrayList (java.util.ArrayList)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 Snippet (org.springframework.ide.vscode.commons.yaml.snippet.Snippet)2 TypeBasedSnippetProvider (org.springframework.ide.vscode.commons.yaml.snippet.TypeBasedSnippetProvider)2 Strings (com.google.common.base.Strings)1 ImmutableList (com.google.common.collect.ImmutableList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Logger (org.slf4j.Logger)1