Search in sources :

Example 11 with ICompletionProposal

use of org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal in project sts4 by spring-projects.

the class PropertiesCompletionProposalsCalculator method getFuzzyCompletions.

protected Collection<ICompletionProposal> getFuzzyCompletions() {
    final String prefix = fuzzySearchPrefix.getPrefix(doc, offset);
    if (prefix != null) {
        Collection<Match<PropertyInfo>> matches = findMatches(prefix);
        if (matches != null && !matches.isEmpty()) {
            ArrayList<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>(matches.size());
            for (final Match<PropertyInfo> match : matches) {
                DocumentEdits docEdits;
                try {
                    docEdits = LazyProposalApplier.from(() -> {
                        try {
                            Type type = TypeParser.parse(match.data.getType());
                            DocumentEdits edits = new DocumentEdits(doc);
                            edits.delete(offset - prefix.length(), offset);
                            edits.insert(offset, match.data.getId() + propertyCompletionPostfix(typeUtil, type));
                            return edits;
                        } catch (Throwable t) {
                            Log.log(t);
                            return new DocumentEdits(doc);
                        }
                    });
                    proposals.add(completionFactory.property(doc, docEdits, match, typeUtil));
                } catch (Throwable e) {
                    Log.log(e);
                }
            }
            return proposals;
        }
    }
    return Collections.emptyList();
}
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) PropertyInfo(org.springframework.ide.vscode.boot.metadata.PropertyInfo) Match(org.springframework.ide.vscode.commons.util.FuzzyMap.Match)

Example 12 with ICompletionProposal

use of org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal in project sts4 by spring-projects.

the class JavaSnippetManager method getCompletions.

public Collection<ICompletionProposal> getCompletions(IDocument doc, int offset, ASTNode node, CompilationUnit cu) {
    Collection<ICompletionProposal> completions = new ArrayList<>();
    DocumentRegion query = PREFIX_FINDER.getPrefixRegion(doc, offset);
    for (JavaSnippet javaSnippet : snippets) {
        if (FuzzyMatcher.matchScore(query.toString(), javaSnippet.getName()) != 0) {
            javaSnippet.generateCompletion(snippetBuilderFactory, query, node, cu).ifPresent((completion) -> completions.add(completion));
        }
    }
    return completions;
}
Also used : ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) ArrayList(java.util.ArrayList)

Example 13 with ICompletionProposal

use of org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal 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;
}
Also used : DocumentEdits(org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) Annotation(org.eclipse.jdt.core.dom.Annotation) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException) Match(org.springframework.ide.vscode.commons.util.FuzzyMap.Match) MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) PropertyInfo(org.springframework.ide.vscode.boot.metadata.PropertyInfo)

Example 14 with ICompletionProposal

use of org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal 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 15 with ICompletionProposal

use of org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal in project sts4 by spring-projects.

the class YamlCompletionEngine method fixIndentations.

protected Collection<? extends ICompletionProposal> fixIndentations(Collection<ICompletionProposal> completions, SNode currentNode, SNode contextNode, int baseIndent, double deempasizeBy) {
    if (!completions.isEmpty()) {
        int dashyIndent = getTargetIndent(contextNode, currentNode, true);
        int plainIndent = getTargetIndent(contextNode, currentNode, false);
        List<ICompletionProposal> transformed = new ArrayList<>();
        for (ICompletionProposal p : completions) {
            int targetIndent = p.getLabel().startsWith("- ") ? dashyIndent : plainIndent;
            ScoreableProposal p_fixed = indentFix((ScoreableProposal) p, targetIndent - baseIndent, currentNode, contextNode);
            if (p_fixed != null) {
                p_fixed.deemphasize(deempasizeBy);
                transformed.add(p_fixed);
            }
        }
        return transformed;
    }
    return Collections.emptyList();
}
Also used : ScoreableProposal(org.springframework.ide.vscode.commons.languageserver.completion.ScoreableProposal) ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) ArrayList(java.util.ArrayList)

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