Search in sources :

Example 6 with PropertyInfo

use of org.springframework.ide.vscode.boot.metadata.PropertyInfo 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 7 with PropertyInfo

use of org.springframework.ide.vscode.boot.metadata.PropertyInfo in project sts4 by spring-projects.

the class SpringPropertiesReconcileEngine method reconcile.

@Override
public void reconcile(IDocument doc, IProblemCollector problemCollector) {
    FuzzyMap<PropertyInfo> index = fIndexProvider.getIndex(doc);
    problemCollector.beginCollecting();
    try {
        ParseResults results = parser.parse(doc.get());
        DuplicateNameChecker duplicateNameChecker = new DuplicateNameChecker(problemCollector);
        results.syntaxErrors.forEach(syntaxError -> {
            problemCollector.accept(problem(PROP_SYNTAX_ERROR, syntaxError.getMessage(), syntaxError.getOffset(), syntaxError.getLength()));
        });
        if (index == null || index.isEmpty()) {
            // some problem putting information about properties into the index.
            return;
        }
        results.ast.getNodes(KeyValuePair.class).forEach(pair -> {
            try {
                DocumentRegion propertyNameRegion = createRegion(doc, pair.getKey());
                String keyName = PropertiesFileEscapes.unescape(propertyNameRegion.toString());
                duplicateNameChecker.check(propertyNameRegion);
                PropertyInfo validProperty = SpringPropertyIndex.findLongestValidProperty(index, keyName);
                if (validProperty != null) {
                    // in PropertyNavigator (probably these changes are also for the better making it simpler as well)
                    if (validProperty.isDeprecated()) {
                        problemCollector.accept(problemDeprecated(propertyNameRegion, validProperty));
                    }
                    int offset = validProperty.getId().length() + propertyNameRegion.getStart();
                    PropertyNavigator navigator = new PropertyNavigator(doc, problemCollector, typeUtilProvider.getTypeUtil(doc), propertyNameRegion);
                    Type valueType = navigator.navigate(offset, TypeParser.parse(validProperty.getType()));
                    if (valueType != null) {
                        reconcileType(doc, valueType, pair.getValue(), problemCollector);
                    }
                } else {
                    // validProperty==null
                    // The name is invalid, with no 'prefix' of the name being a valid property name.
                    PropertyInfo similarEntry = index.findLongestCommonPrefixEntry(propertyNameRegion.toString());
                    CharSequence validPrefix = commonPrefix(similarEntry.getId(), keyName);
                    problemCollector.accept(problemUnkownProperty(propertyNameRegion, similarEntry, validPrefix));
                }
            // end: validProperty==null
            } catch (Exception e) {
                Log.log(e);
            }
        });
    } catch (Throwable e2) {
        Log.log(e2);
    } finally {
        problemCollector.endCollecting();
    }
}
Also used : KeyValuePair(org.springframework.ide.vscode.java.properties.parser.PropertiesAst.KeyValuePair) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException) Type(org.springframework.ide.vscode.boot.metadata.types.Type) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) ParseResults(org.springframework.ide.vscode.java.properties.parser.ParseResults) PropertyInfo(org.springframework.ide.vscode.boot.metadata.PropertyInfo)

Example 8 with PropertyInfo

use of org.springframework.ide.vscode.boot.metadata.PropertyInfo in project sts4 by spring-projects.

the class ApplicationYamlASTReconciler method reconcile.

private void reconcile(YamlFileAST root, NodeTuple entry, IndexNavigator nav) {
    Node keyNode = entry.getKeyNode();
    String key = asScalar(keyNode);
    if (key == null) {
        expectScalar(keyNode);
    } else {
        IndexNavigator subNav = nav.selectSubProperty(key);
        PropertyInfo match = subNav.getExactMatch();
        PropertyInfo extension = subNav.getExtensionCandidate();
        if (match == null && extension == null) {
            // nothing found for this key. Maybe user is using camelCase variation of the key?
            String keyAlias = StringUtil.camelCaseToHyphens(key);
            IndexNavigator subNavAlias = nav.selectSubProperty(keyAlias);
            match = subNavAlias.getExactMatch();
            extension = subNavAlias.getExtensionCandidate();
            if (match != null || extension != null) {
                // Got something for the alias, so use that instead.
                // Note: do not swap for alias unless we actually found something.
                // This gives more logical errors (in terms of user's key, not its canonical alias)
                subNav = subNavAlias;
            }
        }
        if (match != null && extension != null) {
            // This ambiguity is hard to deal with and we choose not to do so for now
            return;
        } else if (match != null) {
            Type type = TypeParser.parse(match.getType());
            if (match.isDeprecated()) {
                deprecatedProperty(match, keyNode);
            }
            reconcile(root, entry.getValueNode(), type);
        } else if (extension != null) {
            // We don't really care about the extension only about the fact that it
            // exists and so it is meaningful to continue checking...
            Node valueNode = entry.getValueNode();
            reconcile(root, valueNode, subNav);
        } else {
            // both are null, this means there's no valid property with the current prefix
            // whether exact or extending it with further navigation
            unkownProperty(keyNode, subNav.getPrefix(), entry);
        }
    }
}
Also used : Type(org.springframework.ide.vscode.boot.metadata.types.Type) SequenceNode(org.yaml.snakeyaml.nodes.SequenceNode) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) Node(org.yaml.snakeyaml.nodes.Node) ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) PropertyInfo(org.springframework.ide.vscode.boot.metadata.PropertyInfo) IndexNavigator(org.springframework.ide.vscode.boot.metadata.IndexNavigator)

Example 9 with PropertyInfo

use of org.springframework.ide.vscode.boot.metadata.PropertyInfo 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 10 with PropertyInfo

use of org.springframework.ide.vscode.boot.metadata.PropertyInfo 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)12 Type (org.springframework.ide.vscode.boot.metadata.types.Type)5 DocumentEdits (org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits)4 Match (org.springframework.ide.vscode.commons.util.FuzzyMap.Match)4 DocumentRegion (org.springframework.ide.vscode.commons.util.text.DocumentRegion)4 ArrayList (java.util.ArrayList)3 StsValueHint (org.springframework.ide.vscode.boot.metadata.hints.StsValueHint)3 ICompletionProposal (org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal)3 BadLocationException (org.springframework.ide.vscode.commons.util.BadLocationException)3 CommonLanguageTools.getValueType (org.springframework.ide.vscode.boot.common.CommonLanguageTools.getValueType)2 PropertyNavigator (org.springframework.ide.vscode.boot.properties.reconcile.PropertyNavigator)2 Collection (java.util.Collection)1 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 Test (org.junit.Test)1 AbstractPropsEditorTest (org.springframework.ide.vscode.boot.editor.harness.AbstractPropsEditorTest)1 IndexNavigator (org.springframework.ide.vscode.boot.metadata.IndexNavigator)1 HintProvider (org.springframework.ide.vscode.boot.metadata.hints.HintProvider)1