Skip to content

Commit

Permalink
schema: Refactor & Limit Object.EmptyCompletionData to required attri…
Browse files Browse the repository at this point in the history
…butes
  • Loading branch information
radeksimko committed Mar 14, 2023
1 parent 300e600 commit 0c5c6ca
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 33 deletions.
72 changes: 40 additions & 32 deletions schema/constraint_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion schema/constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -738,11 +738,13 @@ STRING
Constraint: LiteralType{
Type: cty.Bool,
},
IsRequired: true,
},
"bar": {
Constraint: LiteralType{
Type: cty.String,
},
IsRequired: true,
},
},
},
Expand All @@ -765,6 +767,7 @@ STRING
Constraint: LiteralType{
Type: cty.Bool,
},
IsRequired: true,
},
"bar": {
Constraint: Object{
Expand All @@ -773,9 +776,11 @@ STRING
Constraint: LiteralType{
Type: cty.String,
},
IsRequired: true,
},
},
},
IsRequired: true,
},
},
},
Expand All @@ -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 != "" {
Expand Down

0 comments on commit 0c5c6ca

Please sign in to comment.