Search in sources :

Example 1 with Match

use of org.springframework.ide.vscode.commons.util.FuzzyMap.Match 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 2 with Match

use of org.springframework.ide.vscode.commons.util.FuzzyMap.Match 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 3 with Match

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

the class ValueCompletionProcessor method computeProposalsForSimpleName.

private void computeProposalsForSimpleName(ASTNode node, List<ICompletionProposal> completions, int offset, IDocument doc, FuzzyMap<PropertyInfo> index) {
    String prefix = identifyPropertyPrefix(node.toString(), offset - node.getStartPosition());
    int startOffset = node.getStartPosition();
    int endOffset = node.getStartPosition() + node.getLength();
    String proposalPrefix = "\"";
    String proposalPostfix = "\"";
    List<Match<PropertyInfo>> matches = findMatches(prefix, index);
    for (Match<PropertyInfo> match : matches) {
        DocumentEdits edits = new DocumentEdits(doc);
        edits.replace(startOffset, endOffset, proposalPrefix + "${" + match.data.getId() + "}" + proposalPostfix);
        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 Match

use of org.springframework.ide.vscode.commons.util.FuzzyMap.Match 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)

Aggregations

PropertyInfo (org.springframework.ide.vscode.boot.metadata.PropertyInfo)4 DocumentEdits (org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits)4 Match (org.springframework.ide.vscode.commons.util.FuzzyMap.Match)4 ArrayList (java.util.ArrayList)2 ICompletionProposal (org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal)2 Annotation (org.eclipse.jdt.core.dom.Annotation)1 MemberValuePair (org.eclipse.jdt.core.dom.MemberValuePair)1 SimpleName (org.eclipse.jdt.core.dom.SimpleName)1 StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)1 CommonLanguageTools.getValueType (org.springframework.ide.vscode.boot.common.CommonLanguageTools.getValueType)1 Type (org.springframework.ide.vscode.boot.metadata.types.Type)1 BadLocationException (org.springframework.ide.vscode.commons.util.BadLocationException)1