Search in sources :

Example 1 with YValueHint

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

the class YTypeAssistContext method getValueCompletions.

private List<ICompletionProposal> getValueCompletions(YamlDocument doc, SNode node, int offset, String query) {
    PartialCollection<YValueHint> _values = typeUtil.getHintValues(type, getSchemaContext());
    if (_values.getExplanation() != null && _values.getElements().isEmpty() && !Boolean.getBoolean("lsp.yaml.completions.errors.disable")) {
        return ImmutableList.of(completionFactory().errorMessage(query, getMessage(_values.getExplanation())));
    }
    Collection<YValueHint> values = _values.getElements();
    if (values != null) {
        ArrayList<ICompletionProposal> completions = new ArrayList<>();
        YamlIndentUtil indenter = new YamlIndentUtil(doc);
        int referenceIndent;
        try {
            referenceIndent = getContextNode().getIndent();
        } catch (Exception e) {
            // Getting it from the node isn't always correct, but more often than not it is.
            // So this fallback is better than nothing.
            referenceIndent = node.getIndent();
        }
        for (YValueHint value : values) {
            double score = FuzzyMatcher.matchScore(query, value.getValue());
            if (score != 0 && value != null && !query.equals(value.getValue())) {
                int queryStart = offset - query.length();
                DocumentEdits edits = new DocumentEdits(doc.getDocument());
                edits.delete(queryStart, offset);
                if (!Character.isWhitespace(doc.getChar(queryStart - 1))) {
                    edits.insert(offset, " ");
                }
                edits.insert(offset, value.getValue());
                String extraInsertion = value.getExtraInsertion();
                if (extraInsertion != null) {
                    edits.insert(offset, indenter.applyIndentation(extraInsertion, referenceIndent));
                }
                completions.add(completionFactory().valueProposal(value.getValue(), query, value.getLabel(), type, value.getDocumentation(), score, edits, typeUtil));
            }
        }
        return completions;
    }
    return Collections.emptyList();
}
Also used : DocumentEdits(org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits) YamlIndentUtil(org.springframework.ide.vscode.commons.yaml.util.YamlIndentUtil) ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) ArrayList(java.util.ArrayList) YValueHint(org.springframework.ide.vscode.commons.yaml.schema.YValueHint) YValueHint(org.springframework.ide.vscode.commons.yaml.schema.YValueHint) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException)

Example 2 with YValueHint

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

the class ConcourseModel method addExtraInsertion.

private YValueHint addExtraInsertion(YValueHint h, DynamicSchemaContext dc) {
    return new BasicYValueHint(h.getValue(), h.getLabel()).setExtraInsertion(() -> {
        String resourceTypeName = h.getValue();
        AbstractType sourceType = (AbstractType) resourceTypes.getSourceType(resourceTypeName);
        if (sourceType != null && getParentPropertyNode("source", dc) == null) {
            // don't auto insert what's already there!
            List<YTypedProperty> requiredProps = sourceType.getProperties().stream().filter(p -> p.isRequired()).collect(Collectors.toList());
            if (!requiredProps.isEmpty()) {
                SnippetBuilder snippet = snippetBuilderFactory.get();
                snippet.text("\nsource:");
                for (YTypedProperty p : requiredProps) {
                    snippet.text("\n  " + p.getName() + ": ");
                    snippet.placeHolder();
                }
                return snippet.toString();
            }
        }
        return null;
    });
}
Also used : AbstractType(org.springframework.ide.vscode.commons.yaml.schema.YTypeFactory.AbstractType) Arrays(java.util.Arrays) IDocument(org.springframework.ide.vscode.commons.util.text.IDocument) YamlPathSegment.valueAt(org.springframework.ide.vscode.commons.yaml.path.YamlPathSegment.valueAt) ASTRootCursor(org.springframework.ide.vscode.commons.yaml.path.ASTRootCursor) NodeTypes(org.springframework.ide.vscode.commons.yaml.reconcile.ASTTypeCache.NodeTypes) YamlSchemaProblems(org.springframework.ide.vscode.commons.yaml.reconcile.YamlSchemaProblems) Multiset(com.google.common.collect.Multiset) NodeUtil(org.springframework.ide.vscode.commons.yaml.ast.NodeUtil) NodeCursor(org.springframework.ide.vscode.commons.yaml.path.NodeCursor) Supplier(com.google.common.base.Supplier) YamlAstCache(org.springframework.ide.vscode.commons.yaml.ast.YamlAstCache) Function(java.util.function.Function) ASTTypeCache(org.springframework.ide.vscode.commons.yaml.reconcile.ASTTypeCache) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) Node(org.yaml.snakeyaml.nodes.Node) StringUtil(org.springframework.ide.vscode.commons.util.StringUtil) YamlTraversal(org.springframework.ide.vscode.commons.yaml.path.YamlTraversal) YTypeFactory(org.springframework.ide.vscode.commons.yaml.schema.YTypeFactory) BasicYValueHint(org.springframework.ide.vscode.commons.yaml.schema.BasicYValueHint) YamlFileAST(org.springframework.ide.vscode.commons.yaml.ast.YamlFileAST) ImmutableMultiset(com.google.common.collect.ImmutableMultiset) IProblemCollector(org.springframework.ide.vscode.commons.languageserver.reconcile.IProblemCollector) YamlPath(org.springframework.ide.vscode.commons.yaml.path.YamlPath) YamlPathSegment.anyChild(org.springframework.ide.vscode.commons.yaml.path.YamlPathSegment.anyChild) YamlPathSegment.keyAt(org.springframework.ide.vscode.commons.yaml.path.YamlPathSegment.keyAt) DynamicSchemaContext(org.springframework.ide.vscode.commons.yaml.schema.DynamicSchemaContext) SnippetBuilder(org.springframework.ide.vscode.commons.languageserver.util.SnippetBuilder) YBeanUnionType(org.springframework.ide.vscode.commons.yaml.schema.YTypeFactory.YBeanUnionType) YamlPathSegment(org.springframework.ide.vscode.commons.yaml.path.YamlPathSegment) YTypedProperty(org.springframework.ide.vscode.commons.yaml.schema.YTypedProperty) CollectorUtil(org.springframework.ide.vscode.commons.util.CollectorUtil) Log(org.springframework.ide.vscode.commons.util.Log) Builder(com.google.common.collect.ImmutableMultiset.Builder) Collection(java.util.Collection) Set(java.util.Set) Constraint(org.springframework.ide.vscode.commons.yaml.schema.constraints.Constraint) Collectors(java.util.stream.Collectors) Assert(org.springframework.ide.vscode.commons.util.Assert) YType(org.springframework.ide.vscode.commons.yaml.schema.YType) List(java.util.List) Stream(java.util.stream.Stream) Streams(org.springframework.ide.vscode.commons.yaml.util.Streams) Entry(java.util.Map.Entry) Optional(java.util.Optional) YValueHint(org.springframework.ide.vscode.commons.yaml.schema.YValueHint) SimpleLanguageServer(org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer) YAMLException(org.yaml.snakeyaml.error.YAMLException) BasicYValueHint(org.springframework.ide.vscode.commons.yaml.schema.BasicYValueHint) AbstractType(org.springframework.ide.vscode.commons.yaml.schema.YTypeFactory.AbstractType) SnippetBuilder(org.springframework.ide.vscode.commons.languageserver.util.SnippetBuilder) YTypedProperty(org.springframework.ide.vscode.commons.yaml.schema.YTypedProperty)

Example 3 with YValueHint

use of org.springframework.ide.vscode.commons.yaml.schema.YValueHint 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)

Example 4 with YValueHint

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

the class ConcourseModel method getResourceTypeNameHints.

public Collection<YValueHint> getResourceTypeNameHints(DynamicSchemaContext dc) {
    IDocument doc = dc.getDocument();
    Multiset<String> userDefined = getStringsFromAst(doc, RESOURCE_TYPE_NAMES_PATH);
    if (userDefined != null) {
        Builder<YValueHint> builder = ImmutableMultiset.builder();
        builder.addAll(YTypeFactory.hints(userDefined));
        builder.addAll(Arrays.stream(PipelineYmlSchema.BUILT_IN_RESOURCE_TYPES).map(h -> addExtraInsertion(h, dc)).collect(Collectors.toList()));
        return builder.build();
    }
    return null;
}
Also used : IDocument(org.springframework.ide.vscode.commons.util.text.IDocument) BasicYValueHint(org.springframework.ide.vscode.commons.yaml.schema.BasicYValueHint) YValueHint(org.springframework.ide.vscode.commons.yaml.schema.YValueHint)

Example 5 with YValueHint

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

the class AbstractCFHintsProvider method getHints.

/**
 * @return non-null list of hints. Return empty if no hints available
 */
protected Collection<YValueHint> getHints(List<CFTarget> targets) throws Exception {
    if (targets == null || targets.isEmpty()) {
        // this "don't know" value will suppress bogus warnings in the reconciler.
        return null;
    }
    List<YValueHint> hints = new ArrayList<>();
    boolean validTargetsPresent = false;
    for (CFTarget cfTarget : targets) {
        try {
            // TODO: check if duplicate proposals can be the list of all hints. Duplicates don't seem to cause duplicate proposals. Verify this!
            getHints(cfTarget).stream().filter(hint -> !hints.contains(hint)).forEach(hint -> hints.add(hint));
            validTargetsPresent = true;
        } catch (Exception e) {
        // Drop individual target error
        }
    }
    if (validTargetsPresent) {
        return hints;
    } else {
        throw new ConnectionException(targetCache.getCfClientConfig().getClientParamsProvider().getMessages().noNetworkConnection());
    }
}
Also used : ExceptionUtil(org.springframework.ide.vscode.commons.util.ExceptionUtil) Collection(java.util.Collection) Callable(java.util.concurrent.Callable) CFTarget(org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CFTarget) Assert(org.springframework.ide.vscode.commons.util.Assert) NoTargetsException(org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.NoTargetsException) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException) ConnectionException(org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.ConnectionException) CFTargetCache(org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CFTargetCache) YValueHint(org.springframework.ide.vscode.commons.yaml.schema.YValueHint) CFTarget(org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CFTarget) ArrayList(java.util.ArrayList) YValueHint(org.springframework.ide.vscode.commons.yaml.schema.YValueHint) NoTargetsException(org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.NoTargetsException) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException) ConnectionException(org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.ConnectionException) ConnectionException(org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.ConnectionException)

Aggregations

YValueHint (org.springframework.ide.vscode.commons.yaml.schema.YValueHint)5 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)2 List (java.util.List)2 ICompletionProposal (org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal)2 Assert (org.springframework.ide.vscode.commons.util.Assert)2 ValueParseException (org.springframework.ide.vscode.commons.util.ValueParseException)2 IDocument (org.springframework.ide.vscode.commons.util.text.IDocument)2 Supplier (com.google.common.base.Supplier)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMultiset (com.google.common.collect.ImmutableMultiset)1 Builder (com.google.common.collect.ImmutableMultiset.Builder)1 Multiset (com.google.common.collect.Multiset)1 Arrays (java.util.Arrays)1 Entry (java.util.Map.Entry)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Callable (java.util.concurrent.Callable)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1