Search in sources :

Example 6 with ICompletionProposal

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

Example 7 with ICompletionProposal

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

the class ScopeCompletionProcessor method provideCompletions.

@Override
public Collection<ICompletionProposal> provideCompletions(ASTNode node, Annotation annotation, ITypeBinding type, int offset, IDocument doc) {
    List<ICompletionProposal> result = new ArrayList<>();
    try {
        if (node instanceof SimpleName && node.getParent() instanceof MemberValuePair) {
            MemberValuePair memberPair = (MemberValuePair) node.getParent();
            // case: @Scope(value=<*>)
            if ("value".equals(memberPair.getName().toString()) && memberPair.getValue().toString().equals("$missing$")) {
                for (ScopeNameCompletion completion : ScopeNameCompletionProposal.COMPLETIONS) {
                    ICompletionProposal proposal = new ScopeNameCompletionProposal(completion, doc, offset, offset, "");
                    result.add(proposal);
                }
            }
        } else // case: @Scope(<*>)
        if (node == annotation && doc.get(offset - 1, 2).endsWith("()")) {
            for (ScopeNameCompletion completion : ScopeNameCompletionProposal.COMPLETIONS) {
                ICompletionProposal proposal = new ScopeNameCompletionProposal(completion, doc, offset, offset, "");
                result.add(proposal);
            }
        } else if (node instanceof StringLiteral && node.getParent() instanceof Annotation) {
            // case: @Scope("...")
            if (node.toString().startsWith("\"") && node.toString().endsWith("\"")) {
                String prefix = doc.get(node.getStartPosition(), offset - node.getStartPosition());
                for (ScopeNameCompletion completion : ScopeNameCompletionProposal.COMPLETIONS) {
                    if (completion.getValue().startsWith(prefix)) {
                        ICompletionProposal proposal = new ScopeNameCompletionProposal(completion, doc, node.getStartPosition(), node.getStartPosition() + node.getLength(), prefix);
                        result.add(proposal);
                    }
                }
            }
        } else if (node instanceof StringLiteral && node.getParent() instanceof MemberValuePair) {
            MemberValuePair memberPair = (MemberValuePair) node.getParent();
            // case: @Scope(value=<*>)
            if ("value".equals(memberPair.getName().toString()) && node.toString().startsWith("\"") && node.toString().endsWith("\"")) {
                String prefix = doc.get(node.getStartPosition(), offset - node.getStartPosition());
                for (ScopeNameCompletion completion : ScopeNameCompletionProposal.COMPLETIONS) {
                    if (completion.getValue().startsWith(prefix)) {
                        ICompletionProposal proposal = new ScopeNameCompletionProposal(completion, doc, node.getStartPosition(), node.getStartPosition() + node.getLength(), prefix);
                        result.add(proposal);
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
Also used : MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) Annotation(org.eclipse.jdt.core.dom.Annotation)

Example 8 with ICompletionProposal

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

the class GithubRepoContentAssistant method getCompletions.

@Override
public List<ICompletionProposal> getCompletions(CompletionFactory f, DocumentRegion region, int offset) {
    DocumentRegion query = region.subSequence(0, offset);
    // If uri prefix is already there, we provide CA for owner / repo
    for (String uriPrefix : URI_PREFIXES) {
        if (query.startsWith(uriPrefix)) {
            return getOwnerOrRepoCompletions(f, query.subSequence(uriPrefix.length()));
        }
    }
    // If uri prefix is not yet there, maybe we can suggest it (if it matches the query)
    List<ICompletionProposal> proposals = new ArrayList<>(URI_PREFIXES.length);
    for (String uriPrefix : URI_PREFIXES) {
        if (FuzzyMatcher.matchScore(query, uriPrefix) != 0.0) {
            proposals.add(SimpleCompletionFactory.simpleProposal(query, CompletionItemKind.Text, uriPrefix, null, null));
        }
    }
    return proposals;
}
Also used : DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) ArrayList(java.util.ArrayList)

Example 9 with ICompletionProposal

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

the class BootJavaCompletionEngine method getCompletions.

@Override
public Collection<ICompletionProposal> getCompletions(TextDocument document, int offset) throws Exception {
    return server.getCompilationUnitCache().withCompilationUnit(document, cu -> {
        if (cu != null) {
            ASTNode node = NodeFinder.perform(cu, offset, 0);
            if (node != null) {
                Collection<ICompletionProposal> completions = new ArrayList<ICompletionProposal>();
                completions.addAll(collectCompletionsForAnnotations(node, offset, document));
                completions.addAll(snippets.getCompletions(document, offset, node, cu));
                return completions;
            }
        }
        return Collections.emptyList();
    });
}
Also used : ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList)

Example 10 with ICompletionProposal

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

the class RouteContentAssistant method getCompletions.

@Override
public List<ICompletionProposal> getCompletions(CompletionFactory f, DocumentRegion region, int offset) {
    // offset = 2 means: "ab<*>c"
    try {
        region = chopEnd(region, offset);
        // So offset > 3 means we are 'our of bounds for this content assistant
        if (offset <= region.length()) {
            String[] queries = getQueries(region.subSequence(0, offset));
            Collection<YValueHint> domains = domainsProvider.call();
            List<ICompletionProposal> proposals = new ArrayList<>();
            for (YValueHint domain : domains) {
                for (String query : queries) {
                    double score = FuzzyMatcher.matchScore(query, domain.getValue());
                    if (score != 0.0) {
                        proposals.add(createProposal(f, region, offset, query, score, domain));
                        // break here so we select the first (i.e. longest) query that matches
                        break;
                    }
                }
            }
            return proposals;
        }
    } catch (Exception e) {
        Log.log(e);
    // Ignore. This is somewhat expected. Stuff can go wrong resolving the domains
    // and CA engine just doesn't provide CA in that case.
    }
    return ImmutableList.of();
}
Also used : ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) ArrayList(java.util.ArrayList) YValueHint(org.springframework.ide.vscode.commons.yaml.schema.YValueHint)

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