Search in sources :

Example 1 with SchemaBasedSnippetGenerator

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

Aggregations

ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 QuickfixData (org.springframework.ide.vscode.commons.languageserver.quickfix.Quickfix.QuickfixData)1 IProblemCollector (org.springframework.ide.vscode.commons.languageserver.reconcile.IProblemCollector)1 ProblemType (org.springframework.ide.vscode.commons.languageserver.reconcile.ProblemType)1 ProblemTypeProvider (org.springframework.ide.vscode.commons.languageserver.reconcile.ProblemTypeProvider)1 ReconcileException (org.springframework.ide.vscode.commons.languageserver.reconcile.ReconcileException)1 ReconcileProblemImpl (org.springframework.ide.vscode.commons.languageserver.reconcile.ReconcileProblemImpl)1 ReplacementQuickfix (org.springframework.ide.vscode.commons.languageserver.reconcile.ReplacementQuickfix)1 SnippetBuilder (org.springframework.ide.vscode.commons.languageserver.util.SnippetBuilder)1 CollectorUtil (org.springframework.ide.vscode.commons.util.CollectorUtil)1 ExceptionUtil (org.springframework.ide.vscode.commons.util.ExceptionUtil)1 ExceptionUtil.getSimpleError (org.springframework.ide.vscode.commons.util.ExceptionUtil.getSimpleError)1 IntegerRange (org.springframework.ide.vscode.commons.util.IntegerRange)1