Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decoder: Add support for parenthesis on LHS (map keys & attribute names) #367

Merged
merged 13 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion decoder/expr_any_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ func (a Any) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate
Elem: schema.AnyExpression{
OfType: typ.ElementType(),
},
AllowInterpolatedKeys: true,
}

return newExpression(a.pathCtx, expr, cons).CompletionAtPos(ctx, pos)
}

Expand All @@ -85,7 +87,8 @@ func (a Any) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate
}

cons := schema.Object{
Attributes: ctyObjectToObjectAttributes(typ),
Attributes: ctyObjectToObjectAttributes(typ),
AllowInterpolatedKeys: true,
}
return newExpression(a.pathCtx, expr, cons).CompletionAtPos(ctx, pos)
}
Expand Down
272 changes: 272 additions & 0 deletions decoder/expr_any_completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3609,6 +3609,278 @@ func TestCompletionAtPos_exprAny_parentheses(t *testing.T) {
},
}),
},
{
"empty parentheses as map key",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.AnyExpression{
OfType: cty.Map(cty.String),
},
},
},
reference.Targets{
{
Addr: lang.Address{
lang.RootStep{Name: "var"},
lang.AttrStep{Name: "foo"},
},
Type: cty.String,
},
},
`attr = {
() = "foo"
}
`,
hcl.Pos{Line: 2, Column: 4, Byte: 12},
lang.CompleteCandidates([]lang.Candidate{
{
Label: "var.foo",
Detail: "string",
Kind: lang.ReferenceCandidateKind,
TextEdit: lang.TextEdit{
NewText: "var.foo",
Snippet: "var.foo",
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 2, Column: 4, Byte: 12},
End: hcl.Pos{Line: 2, Column: 4, Byte: 12},
},
},
},
}),
},
{
"parentheses with prefix as map key",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.AnyExpression{
OfType: cty.Map(cty.String),
},
},
},
reference.Targets{
{
Addr: lang.Address{
lang.RootStep{Name: "var"},
lang.AttrStep{Name: "foo"},
},
Type: cty.String,
},
},
`attr = {
(var) = "foo"
}
`,
hcl.Pos{Line: 2, Column: 7, Byte: 15},
lang.CompleteCandidates([]lang.Candidate{
{
Label: "var.foo",
Detail: "string",
Kind: lang.ReferenceCandidateKind,
TextEdit: lang.TextEdit{
NewText: "var.foo",
Snippet: "var.foo",
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 2, Column: 4, Byte: 12},
End: hcl.Pos{Line: 2, Column: 7, Byte: 15},
},
},
},
}),
},
{
"empty parentheses as map key in static map",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.Map{
Elem: schema.LiteralType{Type: cty.String},
},
},
},
reference.Targets{
{
Addr: lang.Address{
lang.RootStep{Name: "var"},
lang.AttrStep{Name: "foo"},
},
Type: cty.String,
},
},
`attr = {
() = "foo"
}
`,
hcl.Pos{Line: 2, Column: 4, Byte: 12},
lang.CompleteCandidates([]lang.Candidate{}),
},
{
"parentheses with prefix as map key in static map",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.Map{
Elem: schema.LiteralType{Type: cty.String},
},
},
},
reference.Targets{
{
Addr: lang.Address{
lang.RootStep{Name: "var"},
lang.AttrStep{Name: "foo"},
},
Type: cty.String,
},
},
`attr = {
(var) = "foo"
}
`,
hcl.Pos{Line: 2, Column: 7, Byte: 15},
lang.CompleteCandidates([]lang.Candidate{}),
},
{
"empty parentheses as object attribute name",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.AnyExpression{
OfType: cty.Object(map[string]cty.Type{
"bar": cty.String,
}),
},
},
},
reference.Targets{
{
Addr: lang.Address{
lang.RootStep{Name: "var"},
lang.AttrStep{Name: "foo"},
},
Type: cty.String,
},
},
`attr = {
() = "foo"
}
`,
hcl.Pos{Line: 2, Column: 4, Byte: 12},
lang.CompleteCandidates([]lang.Candidate{
{
Label: "var.foo",
Detail: "string",
Kind: lang.ReferenceCandidateKind,
TextEdit: lang.TextEdit{
NewText: "var.foo",
Snippet: "var.foo",
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 2, Column: 4, Byte: 12},
End: hcl.Pos{Line: 2, Column: 4, Byte: 12},
},
},
},
}),
},
{
"parentheses with prefix as object attribute name",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.AnyExpression{
OfType: cty.Object(map[string]cty.Type{
"bar": cty.String,
}),
},
},
},
reference.Targets{
{
Addr: lang.Address{
lang.RootStep{Name: "var"},
lang.AttrStep{Name: "foo"},
},
Type: cty.String,
},
},
`attr = {
(var) = "foo"
}
`,
hcl.Pos{Line: 2, Column: 7, Byte: 15},
lang.CompleteCandidates([]lang.Candidate{
{
Label: "var.foo",
Detail: "string",
Kind: lang.ReferenceCandidateKind,
TextEdit: lang.TextEdit{
NewText: "var.foo",
Snippet: "var.foo",
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 2, Column: 4, Byte: 12},
End: hcl.Pos{Line: 2, Column: 7, Byte: 15},
},
},
},
}),
},
{
"empty parentheses as object attribute name in static object",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.Object{
Attributes: schema.ObjectAttributes{
"foo": &schema.AttributeSchema{
Constraint: schema.LiteralType{Type: cty.String},
},
},
},
},
},
reference.Targets{
{
Addr: lang.Address{
lang.RootStep{Name: "var"},
lang.AttrStep{Name: "foo"},
},
Type: cty.String,
},
},
`attr = {
() = "foo"
}
`,
hcl.Pos{Line: 2, Column: 4, Byte: 12},
lang.CompleteCandidates([]lang.Candidate{}),
},
{
"parentheses with prefix as map key in static map",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.Object{
Attributes: schema.ObjectAttributes{
"foo": &schema.AttributeSchema{
Constraint: schema.LiteralType{Type: cty.String},
},
},
},
},
},
reference.Targets{
{
Addr: lang.Address{
lang.RootStep{Name: "var"},
lang.AttrStep{Name: "foo"},
},
Type: cty.String,
},
},
`attr = {
(var) = "foo"
}
`,
hcl.Pos{Line: 2, Column: 7, Byte: 15},
lang.CompleteCandidates([]lang.Candidate{}),
},
}

for i, tc := range testCases {
Expand Down
4 changes: 3 additions & 1 deletion decoder/expr_any_hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func (a Any) HoverAtPos(ctx context.Context, pos hcl.Pos) *lang.HoverData {
Elem: schema.AnyExpression{
OfType: typ.ElementType(),
},
AllowInterpolatedKeys: true,
}
return newExpression(a.pathCtx, expr, cons).HoverAtPos(ctx, pos)
}
Expand All @@ -85,7 +86,8 @@ func (a Any) HoverAtPos(ctx context.Context, pos hcl.Pos) *lang.HoverData {
}

cons := schema.Object{
Attributes: ctyObjectToObjectAttributes(typ),
Attributes: ctyObjectToObjectAttributes(typ),
AllowInterpolatedKeys: true,
}
return newExpression(a.pathCtx, expr, cons).HoverAtPos(ctx, pos)
}
Expand Down
Loading