diff --git a/schema/constraint_object.go b/schema/constraint_object.go index 9ac19f81..6c1afafe 100644 --- a/schema/constraint_object.go +++ b/schema/constraint_object.go @@ -60,54 +60,62 @@ func prefillRequiredFields(ctx context.Context) bool { } func (o Object) EmptyCompletionData(ctx context.Context, placeholder int, nestingLevel int) CompletionData { - rootNesting := strings.Repeat(" ", nestingLevel) + nesting := strings.Repeat(" ", nestingLevel) attrNesting := strings.Repeat(" ", nestingLevel+1) + emptyObjectData := CompletionData{ + NewText: fmt.Sprintf("{\n%s\n%s}", attrNesting, nesting), + Snippet: fmt.Sprintf("{\n%s${%d}\n%s}", attrNesting, placeholder, nesting), + NextPlaceholder: placeholder + 1, + TriggerSuggest: true, + } - if len(o.Attributes) == 0 { - return CompletionData{ - NewText: fmt.Sprintf("{\n%s\n%s}", attrNesting, rootNesting), - Snippet: fmt.Sprintf("{\n%s${%d}\n%s}", attrNesting, placeholder, rootNesting), - NextPlaceholder: placeholder + 1, - } + attrData, ok := o.attributesCompletionData(ctx, placeholder, nestingLevel) + if !ok { + return emptyObjectData } - newText := "{\n" - snippet := "{\n" + return CompletionData{ + NewText: fmt.Sprintf("{\n%s%s}", attrData.NewText, nesting), + Snippet: fmt.Sprintf("{\n%s%s}", attrData.Snippet, nesting), + NextPlaceholder: attrData.NextPlaceholder, + } +} - lastPlaceholder := placeholder +func (o Object) attributesCompletionData(ctx context.Context, placeholder, nestingLevel int) (CompletionData, bool) { + newText, snippet := "", "" + anyRequiredFields := false + attrNesting := strings.Repeat(" ", nestingLevel+1) + nextPlaceholder := placeholder attrNames := sortedObjectExprAttrNames(o.Attributes) for _, name := range attrNames { attr := o.Attributes[name] - cData := attr.Constraint.EmptyCompletionData(ctx, lastPlaceholder, nestingLevel+1) - if cData.NewText == "" || cData.Snippet == "" { - return CompletionData{ - NewText: fmt.Sprintf("{\n%s\n%s}", attrNesting, rootNesting), - Snippet: fmt.Sprintf("{\n%s${%d}\n%s}", attrNesting, placeholder, rootNesting), - TriggerSuggest: cData.TriggerSuggest, - NextPlaceholder: placeholder + 1, - } + attrData := attr.Constraint.EmptyCompletionData(ctx, nextPlaceholder, nestingLevel+1) + if attrData.NewText == "" || attrData.Snippet == "" { + return CompletionData{}, false } - newText += fmt.Sprintf("%s%s = %s\n", attrNesting, name, cData.NewText) - snippet += fmt.Sprintf("%s%s = %s\n", attrNesting, name, cData.Snippet) - lastPlaceholder = cData.NextPlaceholder - } + if attr.IsRequired { + anyRequiredFields = true + } else { + continue + } - if nestingLevel > 0 { - newText += fmt.Sprintf("%s}", strings.Repeat(" ", nestingLevel)) - snippet += fmt.Sprintf("%s}", strings.Repeat(" ", nestingLevel)) - } else { - newText += "}" - snippet += "}" + newText += fmt.Sprintf("%s%s = %s\n", attrNesting, name, attrData.NewText) + snippet += fmt.Sprintf("%s%s = %s\n", attrNesting, name, attrData.Snippet) + nextPlaceholder = attrData.NextPlaceholder } - return CompletionData{ - NewText: newText, - Snippet: snippet, - NextPlaceholder: lastPlaceholder, + if anyRequiredFields { + return CompletionData{ + NewText: newText, + Snippet: snippet, + NextPlaceholder: nextPlaceholder, + }, true } + + return CompletionData{}, false } func (o Object) EmptyHoverData(nestingLevel int) *HoverData { diff --git a/schema/constraint_test.go b/schema/constraint_test.go index 4fd590b9..a94f87d6 100644 --- a/schema/constraint_test.go +++ b/schema/constraint_test.go @@ -738,11 +738,13 @@ STRING Constraint: LiteralType{ Type: cty.Bool, }, + IsRequired: true, }, "bar": { Constraint: LiteralType{ Type: cty.String, }, + IsRequired: true, }, }, }, @@ -765,6 +767,7 @@ STRING Constraint: LiteralType{ Type: cty.Bool, }, + IsRequired: true, }, "bar": { Constraint: Object{ @@ -773,9 +776,11 @@ STRING Constraint: LiteralType{ Type: cty.String, }, + IsRequired: true, }, }, }, + IsRequired: true, }, }, }, @@ -797,7 +802,7 @@ STRING }, } for i, tc := range testCases { - t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { + t.Run(fmt.Sprintf("%2d", i), func(t *testing.T) { ctx := context.Background() data := tc.cons.EmptyCompletionData(ctx, 1, 0) if diff := cmp.Diff(tc.expectedCompData, data); diff != "" {