Skip to content

Commit

Permalink
schema: Only prefill required fields if enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Mar 14, 2023
1 parent 0b9bc90 commit 02495a1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
4 changes: 4 additions & 0 deletions schema/constraint_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func (o Object) EmptyCompletionData(ctx context.Context, placeholder int, nestin
TriggerSuggest: triggerSuggest,
}

if !prefillRequiredFields(ctx) {
return emptyObjectData
}

attrData, ok := o.attributesCompletionData(ctx, placeholder, nestingLevel)
if !ok {
return emptyObjectData
Expand Down
59 changes: 57 additions & 2 deletions schema/constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,15 @@ tomap({

func TestConstraint_EmptyCompletionData(t *testing.T) {
testCases := []struct {
cons Constraint
expectedCompData CompletionData
cons Constraint
prefillRequiredFields bool
expectedCompData CompletionData
}{
{
LiteralType{
Type: cty.String,
},
false,
CompletionData{
NewText: `"value"`,
Snippet: `"${1:value}"`,
Expand All @@ -344,6 +346,7 @@ func TestConstraint_EmptyCompletionData(t *testing.T) {
Type: cty.String,
},
},
false,
CompletionData{
NewText: `[ "value" ]`,
Snippet: `[ "${1:value}" ]`,
Expand All @@ -354,6 +357,7 @@ func TestConstraint_EmptyCompletionData(t *testing.T) {
LiteralType{
Type: cty.List(cty.String),
},
false,
CompletionData{
NewText: `[ "value" ]`,
Snippet: `[ "${1:value}" ]`,
Expand All @@ -366,6 +370,7 @@ func TestConstraint_EmptyCompletionData(t *testing.T) {
Type: cty.String,
},
},
false,
CompletionData{
NewText: `[ "value" ]`,
Snippet: `[ "${1:value}" ]`,
Expand All @@ -376,6 +381,7 @@ func TestConstraint_EmptyCompletionData(t *testing.T) {
LiteralType{
Type: cty.Set(cty.String),
},
false,
CompletionData{
NewText: `[ "value" ]`,
Snippet: `[ "${1:value}" ]`,
Expand All @@ -390,6 +396,7 @@ func TestConstraint_EmptyCompletionData(t *testing.T) {
"baz": cty.List(cty.String),
}),
},
true,
CompletionData{
NewText: `{
bar = 0
Expand All @@ -415,6 +422,7 @@ func TestConstraint_EmptyCompletionData(t *testing.T) {
}),
}),
},
true,
CompletionData{
NewText: `{
bar = 0
Expand All @@ -439,6 +447,7 @@ func TestConstraint_EmptyCompletionData(t *testing.T) {
LiteralValue{
Value: cty.StringVal("foobar"),
},
false,
CompletionData{
NewText: `"foobar"`,
Snippet: `"foobar"`,
Expand All @@ -449,6 +458,7 @@ func TestConstraint_EmptyCompletionData(t *testing.T) {
LiteralValue{
Value: cty.StringVal("foo\nbar"),
},
false,
CompletionData{
NewText: `<<<STRING
foo
Expand All @@ -467,6 +477,7 @@ STRING
LiteralValue{
Value: cty.NumberIntVal(42),
},
false,
CompletionData{
NewText: "42",
Snippet: "42",
Expand All @@ -481,6 +492,7 @@ STRING
"baz": cty.ListVal([]cty.Value{cty.StringVal("toot")}),
}),
},
true,
CompletionData{
NewText: `{
bar = 42
Expand All @@ -502,6 +514,7 @@ STRING
"bar": cty.StringVal("boo"),
}),
},
false,
CompletionData{
NewText: `{
"bar" = "boo"
Expand All @@ -525,6 +538,7 @@ STRING
}),
}),
},
false,
CompletionData{
NewText: `{
"bar" = {
Expand Down Expand Up @@ -556,6 +570,7 @@ STRING
}),
}),
},
true,
CompletionData{
NewText: `{
bar = 43
Expand Down Expand Up @@ -587,6 +602,7 @@ STRING
}),
}),
},
true,
CompletionData{
NewText: `{
bar = 43
Expand All @@ -613,6 +629,7 @@ STRING
Keyword: "kw",
},
},
false,
CompletionData{
NewText: "[ ]",
Snippet: "[ ${1} ]",
Expand All @@ -626,6 +643,7 @@ STRING
Keyword: "kw",
},
},
false,
CompletionData{
NewText: "[ ]",
Snippet: "[ ${1} ]",
Expand All @@ -641,6 +659,7 @@ STRING
},
},
},
false,
CompletionData{
NewText: "[ ]",
Snippet: "[ ${1} ]",
Expand All @@ -654,6 +673,7 @@ STRING
Keyword: "kw",
},
},
false,
CompletionData{
NewText: `{
Expand All @@ -673,6 +693,7 @@ STRING
},
},
},
false,
CompletionData{
NewText: `{
"name" = {
Expand All @@ -696,6 +717,7 @@ STRING
},
},
},
false,
CompletionData{
NewText: `{
"name" = {
Expand All @@ -720,6 +742,7 @@ STRING
},
},
},
false,
CompletionData{
NewText: `{
Expand Down Expand Up @@ -748,6 +771,7 @@ STRING
},
},
},
true,
CompletionData{
NewText: `{
bar = "value"
Expand All @@ -760,6 +784,35 @@ STRING
NextPlaceholder: 3,
},
},
{
Object{
Attributes: map[string]*AttributeSchema{
"foo": {
Constraint: LiteralType{
Type: cty.Bool,
},
IsRequired: true,
},
"bar": {
Constraint: LiteralType{
Type: cty.String,
},
IsRequired: true,
},
},
},
false,
CompletionData{
NewText: `{
}`,
Snippet: `{
${1}
}`,
NextPlaceholder: 2,
TriggerSuggest: true,
},
},
{
Object{
Attributes: map[string]*AttributeSchema{
Expand All @@ -784,6 +837,7 @@ STRING
},
},
},
true,
CompletionData{
NewText: `{
bar = {
Expand All @@ -804,6 +858,7 @@ STRING
for i, tc := range testCases {
t.Run(fmt.Sprintf("%2d", i), func(t *testing.T) {
ctx := context.Background()
ctx = WithPrefillRequiredFields(ctx, tc.prefillRequiredFields)
data := tc.cons.EmptyCompletionData(ctx, 1, 0)
if diff := cmp.Diff(tc.expectedCompData, data); diff != "" {
t.Fatalf("unexpected completion data: %s", diff)
Expand Down

0 comments on commit 02495a1

Please sign in to comment.