Search in sources :

Example 1 with SnippetBuilder

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

the class SchemaBasedYamlASTReconciler method checkRequiredProperties.

private void checkRequiredProperties(Node parent, MappingNode map, YType type, Map<String, YTypedProperty> beanProperties, DynamicSchemaContext dc) {
    Set<String> foundProps = NodeUtil.getScalarKeys(map);
    boolean allPropertiesKnown = beanProperties.keySet().containsAll(foundProps);
    // Don't check for missing properties if some properties look like they might be spelled incorrectly.
    if (allPropertiesKnown) {
        // Check for missing required properties:
        List<YTypedProperty> missingProps = beanProperties.values().stream().filter(YTypedProperty::isRequired).filter(prop -> !foundProps.contains(prop.getName())).collect(CollectorUtil.toImmutableList());
        Set<String> missingPropNames = missingProps.stream().map(YTypedProperty::getName).collect(Collectors.toCollection(TreeSet::new));
        if (!missingPropNames.isEmpty()) {
            String message;
            if (missingPropNames.size() == 1) {
                // slightly more specific message when only one missing property
                String missing = missingPropNames.stream().findFirst().get();
                message = "Property '" + missing + "' is required for '" + type + "'";
            } else {
                message = "Properties " + missingPropNames + " are required for '" + type + "'";
            }
            SchemaBasedSnippetGenerator snippetProvider = new SchemaBasedSnippetGenerator(typeUtil, SnippetBuilder::gimped);
            Snippet snippet = snippetProvider.getSnippet(missingProps);
            problems.accept(YamlSchemaProblems.missingProperties(message, dc, missingPropNames, snippet.getSnippet(), snippet.getPlaceHolder(1).getOffset(), parent, map, quickfixes.MISSING_PROP_FIX));
        }
    }
}
Also used : ReconcileException(org.springframework.ide.vscode.commons.languageserver.reconcile.ReconcileException) SequenceNode(org.yaml.snakeyaml.nodes.SequenceNode) IDocument(org.springframework.ide.vscode.commons.util.text.IDocument) QuickfixData(org.springframework.ide.vscode.commons.languageserver.quickfix.Quickfix.QuickfixData) SchemaContextAware(org.springframework.ide.vscode.commons.yaml.schema.SchemaContextAware) NodeUtil(org.springframework.ide.vscode.commons.yaml.ast.NodeUtil) YTypeUtil(org.springframework.ide.vscode.commons.yaml.schema.YTypeUtil) ProblemType(org.springframework.ide.vscode.commons.languageserver.reconcile.ProblemType) TreeSet(java.util.TreeSet) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Node(org.yaml.snakeyaml.nodes.Node) StringUtil(org.springframework.ide.vscode.commons.util.StringUtil) YamlQuickfixes(org.springframework.ide.vscode.commons.yaml.quickfix.YamlQuickfixes) YamlFileAST(org.springframework.ide.vscode.commons.yaml.ast.YamlFileAST) NodeId(org.yaml.snakeyaml.nodes.NodeId) Map(java.util.Map) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException) IProblemCollector(org.springframework.ide.vscode.commons.languageserver.reconcile.IProblemCollector) YamlPath(org.springframework.ide.vscode.commons.yaml.path.YamlPath) ValueParser(org.springframework.ide.vscode.commons.util.ValueParser) DynamicSchemaContext(org.springframework.ide.vscode.commons.yaml.schema.DynamicSchemaContext) SnippetBuilder(org.springframework.ide.vscode.commons.languageserver.util.SnippetBuilder) ASTDynamicSchemaContext(org.springframework.ide.vscode.commons.yaml.schema.ASTDynamicSchemaContext) NodeUtil.asScalar(org.springframework.ide.vscode.commons.yaml.ast.NodeUtil.asScalar) YamlPathSegment(org.springframework.ide.vscode.commons.yaml.path.YamlPathSegment) ExceptionUtil(org.springframework.ide.vscode.commons.util.ExceptionUtil) 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) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) ReconcileProblemImpl(org.springframework.ide.vscode.commons.languageserver.reconcile.ReconcileProblemImpl) YamlSchema(org.springframework.ide.vscode.commons.yaml.schema.YamlSchema) Set(java.util.Set) ReplacementQuickfix(org.springframework.ide.vscode.commons.languageserver.reconcile.ReplacementQuickfix) Constraint(org.springframework.ide.vscode.commons.yaml.schema.constraints.Constraint) IntegerRange(org.springframework.ide.vscode.commons.util.IntegerRange) Collectors(java.util.stream.Collectors) YType(org.springframework.ide.vscode.commons.yaml.schema.YType) SchemaBasedSnippetGenerator(org.springframework.ide.vscode.commons.yaml.snippet.SchemaBasedSnippetGenerator) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple) List(java.util.List) ProblemTypeProvider(org.springframework.ide.vscode.commons.languageserver.reconcile.ProblemTypeProvider) ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) ExceptionUtil.getSimpleError(org.springframework.ide.vscode.commons.util.ExceptionUtil.getSimpleError) Snippet(org.springframework.ide.vscode.commons.yaml.snippet.Snippet) Pattern(java.util.regex.Pattern) SnippetBuilder(org.springframework.ide.vscode.commons.languageserver.util.SnippetBuilder) SchemaBasedSnippetGenerator(org.springframework.ide.vscode.commons.yaml.snippet.SchemaBasedSnippetGenerator) Snippet(org.springframework.ide.vscode.commons.yaml.snippet.Snippet) YTypedProperty(org.springframework.ide.vscode.commons.yaml.schema.YTypedProperty)

Example 2 with SnippetBuilder

use of org.springframework.ide.vscode.commons.languageserver.util.SnippetBuilder 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 SnippetBuilder

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

the class JavaSnippetBuilder method createSnippet.

private String createSnippet(String template) {
    Matcher matcher = PLACE_HOLDER.matcher(template);
    int start = 0;
    SnippetBuilder snippet = snippetBuilderFactory.get();
    while (matcher.find(start)) {
        int matchStart = matcher.start();
        snippet.text(template.substring(start, matchStart));
        int matchEnd = matcher.end();
        String placeHolderImage = template.substring(matcher.start(1), matcher.end(1));
        int colon = placeHolderImage.indexOf(':');
        String id, value;
        if (colon >= 0) {
            id = placeHolderImage.substring(0, colon);
            value = placeHolderImage.substring(colon + 1);
        } else {
            id = placeHolderImage;
            value = id;
        }
        snippet.placeHolder(id, value);
        start = matchEnd;
    }
    snippet.text(template.substring(start));
    return snippet.build().toString();
}
Also used : Matcher(java.util.regex.Matcher) SnippetBuilder(org.springframework.ide.vscode.commons.languageserver.util.SnippetBuilder)

Example 4 with SnippetBuilder

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

the class SchemaBasedSnippetGenerator method generateFullSnippet.

private Snippet generateFullSnippet(YType type, int indent) {
    if (typeUtil.isBean(type)) {
        SnippetBuilder builder = snippetBuilderFactory.get();
        List<YTypedProperty> requiredProps = typeUtil.getProperties(type).stream().filter(p -> p.isRequired()).collect(CollectorUtil.toImmutableList());
        if (!requiredProps.isEmpty()) {
            generateBeanSnippet(requiredProps, builder, indent, maxNesting);
        }
        if (builder.getPlaceholderCount() >= 2) {
            return new Snippet(typeUtil.niceTypeName(type) + " Snippet", builder.build(), (dc) -> requiredProps.stream().noneMatch(p -> dc.getDefinedProperties().contains(p.getName())));
        }
    }
    return null;
}
Also used : List(java.util.List) YTypedProperty(org.springframework.ide.vscode.commons.yaml.schema.YTypedProperty) CollectorUtil(org.springframework.ide.vscode.commons.util.CollectorUtil) ImmutableList(com.google.common.collect.ImmutableList) Collection(java.util.Collection) PlaceHolderString(org.springframework.ide.vscode.commons.languageserver.util.PlaceHolderString) YTypeUtil(org.springframework.ide.vscode.commons.yaml.schema.YTypeUtil) Supplier(com.google.common.base.Supplier) YamlIndentUtil(org.springframework.ide.vscode.commons.yaml.util.YamlIndentUtil) Collectors(java.util.stream.Collectors) YType(org.springframework.ide.vscode.commons.yaml.schema.YType) SnippetBuilder(org.springframework.ide.vscode.commons.languageserver.util.SnippetBuilder) SnippetBuilder(org.springframework.ide.vscode.commons.languageserver.util.SnippetBuilder) YTypedProperty(org.springframework.ide.vscode.commons.yaml.schema.YTypedProperty)

Example 5 with SnippetBuilder

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

the class SchemaBasedSnippetGenerator method getSnippet.

@Override
public Snippet getSnippet(List<YTypedProperty> props) {
    SnippetBuilder builder = snippetBuilderFactory.get();
    generateBeanSnippet(props, builder, 0, maxNesting);
    String snippetName;
    if (props.size() == 1) {
        snippetName = props.get(0).getName();
    } else {
        snippetName = props.stream().map(p -> p.getName()).collect(Collectors.toList()).toString() + " Snippet";
    }
    return new Snippet(snippetName, builder.build(), (dc) -> props.stream().allMatch(p -> !dc.getDefinedProperties().contains(p.getName())));
}
Also used : List(java.util.List) YTypedProperty(org.springframework.ide.vscode.commons.yaml.schema.YTypedProperty) CollectorUtil(org.springframework.ide.vscode.commons.util.CollectorUtil) ImmutableList(com.google.common.collect.ImmutableList) Collection(java.util.Collection) PlaceHolderString(org.springframework.ide.vscode.commons.languageserver.util.PlaceHolderString) YTypeUtil(org.springframework.ide.vscode.commons.yaml.schema.YTypeUtil) Supplier(com.google.common.base.Supplier) YamlIndentUtil(org.springframework.ide.vscode.commons.yaml.util.YamlIndentUtil) Collectors(java.util.stream.Collectors) YType(org.springframework.ide.vscode.commons.yaml.schema.YType) SnippetBuilder(org.springframework.ide.vscode.commons.languageserver.util.SnippetBuilder) SnippetBuilder(org.springframework.ide.vscode.commons.languageserver.util.SnippetBuilder) PlaceHolderString(org.springframework.ide.vscode.commons.languageserver.util.PlaceHolderString)

Aggregations

SnippetBuilder (org.springframework.ide.vscode.commons.languageserver.util.SnippetBuilder)5 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 CollectorUtil (org.springframework.ide.vscode.commons.util.CollectorUtil)4 YType (org.springframework.ide.vscode.commons.yaml.schema.YType)4 YTypedProperty (org.springframework.ide.vscode.commons.yaml.schema.YTypedProperty)4 Supplier (com.google.common.base.Supplier)3 Collection (java.util.Collection)3 ImmutableList (com.google.common.collect.ImmutableList)2 Set (java.util.Set)2 IProblemCollector (org.springframework.ide.vscode.commons.languageserver.reconcile.IProblemCollector)2 PlaceHolderString (org.springframework.ide.vscode.commons.languageserver.util.PlaceHolderString)2 Log (org.springframework.ide.vscode.commons.util.Log)2 StringUtil (org.springframework.ide.vscode.commons.util.StringUtil)2 IDocument (org.springframework.ide.vscode.commons.util.text.IDocument)2 NodeUtil (org.springframework.ide.vscode.commons.yaml.ast.NodeUtil)2 YamlFileAST (org.springframework.ide.vscode.commons.yaml.ast.YamlFileAST)2 YamlPath (org.springframework.ide.vscode.commons.yaml.path.YamlPath)2 YamlPathSegment (org.springframework.ide.vscode.commons.yaml.path.YamlPathSegment)2 DynamicSchemaContext (org.springframework.ide.vscode.commons.yaml.schema.DynamicSchemaContext)2