Search in sources :

Example 1 with YamlDocument

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

the class YamlCompletionEngine method getCompletions.

@Override
public Collection<ICompletionProposal> getCompletions(TextDocument _doc, int offset) throws Exception {
    YamlDocument doc = new YamlDocument(_doc, structureProvider);
    if (!doc.isCommented(offset)) {
        SRootNode root = doc.getStructure();
        SNode current = root.find(offset);
        int cursorIndent = doc.getColumn(offset);
        int nodeIndent = current.getIndent();
        int baseIndent = YamlIndentUtil.minIndent(cursorIndent, nodeIndent);
        List<SNode> contextNodes = getContextNodes(doc, current, offset, baseIndent);
        if (current.getNodeType() == SNodeType.RAW) {
            // relaxed indentation
            List<ICompletionProposal> completions = new ArrayList<>();
            double deempasizeBy = 0.0;
            for (SNode contextNode : contextNodes) {
                completions.addAll(getRelaxedCompletions(offset, doc, current, contextNode, baseIndent, deempasizeBy));
                deempasizeBy += ScoreableProposal.DEEMP_NEXT_CONTEXT;
            }
            return completions;
        } else {
            // precise indentation only
            Assert.isLegal(contextNodes.size() <= 1);
            for (SNode contextNode : contextNodes) {
                return getBaseCompletions(offset, doc, current, contextNode);
            }
        }
    }
    return Collections.emptyList();
}
Also used : SRootNode(org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SRootNode) SNode(org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SNode) YamlDocument(org.springframework.ide.vscode.commons.yaml.structure.YamlDocument) ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) ArrayList(java.util.ArrayList)

Example 2 with YamlDocument

use of org.springframework.ide.vscode.commons.yaml.structure.YamlDocument 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 3 with YamlDocument

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

the class YamlHoverInfoProvider method getHoverInfo.

@Override
public Tuple2<Renderable, IRegion> getHoverInfo(IDocument doc, int offset) throws Exception {
    YamlFileAST ast = getAst(doc);
    if (ast != null) {
        IRegion region = getHoverRegion(ast, offset);
        if (region != null) {
            YamlDocument ymlDoc = new YamlDocument(doc, structureProvider);
            YamlAssistContext assistContext = assistContextProvider.getGlobalAssistContext(ymlDoc);
            if (assistContext != null) {
                List<NodeRef<?>> astPath = ast.findPath(offset);
                final YamlPath path = YamlPath.fromASTPath(astPath);
                if (path != null) {
                    YamlPath assistPath = path;
                    if (assistPath.pointsAtKey()) {
                        // When a path points at a key we must tramsform it to a
                        // 'value-terminating path'
                        // to be able to reuse the 'getHoverInfo' method on
                        // YamlAssistContext (as navigation
                        // into 'key' is not defined for YamlAssistContext.
                        String key = path.getLastSegment().toPropString();
                        assistPath = path.dropLast().append(YamlPathSegment.valueAt(key));
                    }
                    assistContext = assistPath.traverse(assistContext);
                    if (assistContext != null) {
                        Renderable info = path.pointsAtValue() ? assistContext.getValueHoverInfo(ymlDoc, new DocumentRegion(doc, region)) : assistContext.getHoverInfo();
                        // Fix for: PT 134914895. If assist context cannot provide an info, then don't return a Tuple.
                        if (info != null) {
                            return Tuples.of(info, region);
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : NodeRef(org.springframework.ide.vscode.commons.yaml.ast.NodeRef) YamlPath(org.springframework.ide.vscode.commons.yaml.path.YamlPath) Renderable(org.springframework.ide.vscode.commons.util.Renderable) YamlFileAST(org.springframework.ide.vscode.commons.yaml.ast.YamlFileAST) YamlDocument(org.springframework.ide.vscode.commons.yaml.structure.YamlDocument) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) YamlAssistContext(org.springframework.ide.vscode.commons.yaml.completion.YamlAssistContext) IRegion(org.springframework.ide.vscode.commons.util.text.IRegion)

Example 4 with YamlDocument

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

the class MockYamlEditor method parseStructure.

public SRootNode parseStructure() throws Exception {
    YamlStructureProvider sp = YamlStructureProvider.DEFAULT;
    TextDocument _doc = new TextDocument(null, getLanguageId());
    _doc.setText(text);
    YamlDocument doc = new YamlDocument(_doc, sp);
    return sp.getStructure(doc);
}
Also used : TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) YamlDocument(org.springframework.ide.vscode.commons.yaml.structure.YamlDocument) YamlStructureProvider(org.springframework.ide.vscode.commons.yaml.structure.YamlStructureProvider)

Aggregations

YamlDocument (org.springframework.ide.vscode.commons.yaml.structure.YamlDocument)4 ArrayList (java.util.ArrayList)2 ICompletionProposal (org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal)2 Renderable (org.springframework.ide.vscode.commons.util.Renderable)2 DocumentRegion (org.springframework.ide.vscode.commons.util.text.DocumentRegion)2 YamlPath (org.springframework.ide.vscode.commons.yaml.path.YamlPath)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 LoggerFactory (org.slf4j.LoggerFactory)1 DocumentEdits (org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits)1 ScoreableProposal (org.springframework.ide.vscode.commons.languageserver.completion.ScoreableProposal)1 DEEMP_DASH_PROPOSAL (org.springframework.ide.vscode.commons.languageserver.completion.ScoreableProposal.DEEMP_DASH_PROPOSAL)1 DEEMP_DEPRECATION (org.springframework.ide.vscode.commons.languageserver.completion.ScoreableProposal.DEEMP_DEPRECATION)1