hcl-lang icon indicating copy to clipboard operation
hcl-lang copied to clipboard

Improve completion within objects and maps with incomplete configuration

Open dbanck opened this issue 1 year ago • 0 comments

Context

While implementing template expressions in #322, we discovered some edge cases for template expressions inside objects and maps. In the past, we ran into similar problems with completion inside objects and maps with incomplete configuration.

Nested ObjectConsExpr

Completion currently fails for nested objects where the inner object is incomplete

attr = {
  foo = {
    bar
  }
}

Nested TemplateExpr

Completion currently fails for empty template expressions inside objects

default = {
  foo = "foo-${}"
}

Example test case for maps:

{
	"expression within map",
	map[string]*schema.AttributeSchema{
		"attr": {
			Constraint: schema.Map{
				Elem: schema.AnyExpression{OfType: cty.String},
			},
		},
	},
	reference.Targets{
		{
			Addr: lang.Address{
				lang.RootStep{Name: "var"},
				lang.AttrStep{Name: "bar"},
			},
			RangePtr: &hcl.Range{
				Filename: "variables.tf",
				Start:    hcl.Pos{Line: 2, Column: 1, Byte: 17},
				End:      hcl.Pos{Line: 2, Column: 3, Byte: 19},
			},
			Type: cty.String,
		},
	},
	`attr = {
foo = "foo-${}"
}
`,
	hcl.Pos{Line: 2, Column: 16, Byte: 24},
	lang.CompleteCandidates([]lang.Candidate{
		{
			Label:  "var.bar",
			Detail: "string",
			Kind:   lang.TraversalCandidateKind,
			TextEdit: lang.TextEdit{
				NewText: "var.bar",
				Snippet: "var.bar",
				Range: hcl.Range{
					Filename: "test.tf",
					Start:    hcl.Pos{Line: 2, Column: 16, Byte: 24},
					End:      hcl.Pos{Line: 2, Column: 16, Byte: 24},
				},
			},
		},
	}),
},

Proposal

We have filed two upstream issues in HCL that need to be addressed first and will likely fix the problem already:

  • https://github.com/hashicorp/hcl/issues/597
  • https://github.com/hashicorp/hcl/issues/641

We should add/update the tests for objects, maps, and template expression to make sure the feature works as expected.

dbanck avatar Nov 17 '23 13:11 dbanck