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

Fix crash prefix completion with leading space #373

Merged
merged 5 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 30 additions & 0 deletions decoder/expr_any_completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,36 @@ func TestCompletionAtPos_exprAny_functions(t *testing.T) {
},
}),
},
{
"in front of prefix (with space)",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.AnyExpression{
OfType: cty.String,
},
},
},
reference.Targets{},
`attr = t
`,
hcl.Pos{Line: 1, Column: 7, Byte: 6},
lang.CompleteCandidates([]lang.Candidate{}),
},
{
"in front of non-empty [ ]",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.AnyExpression{
OfType: cty.String,
},
},
},
reference.Targets{},
`attr = t [x]
`,
hcl.Pos{Line: 1, Column: 9, Byte: 8},
lang.CompleteCandidates([]lang.Candidate{}),
},
}

for i, tc := range testCases {
Expand Down
2 changes: 1 addition & 1 deletion decoder/expr_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (fe functionExpr) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.

// There can be a single segment with trailing dot which cannot
// be a function anymore as functions cannot contain dots.
if prefixLen > len(rootName) {
if prefixLen < 0 || prefixLen > len(rootName) {
return []lang.Candidate{}
}

Expand Down
2 changes: 1 addition & 1 deletion decoder/expr_keyword_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (kw Keyword) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candi
}

prefixLen := pos.Byte - eType.Traversal.SourceRange().Start.Byte
if prefixLen > len(eType.Traversal.RootName()) {
if prefixLen < 0 || prefixLen > len(eType.Traversal.RootName()) {
// The user has probably typed an extra character, such as a
// period, that is not (yet) part of the expression. This prefix
// won't match anything, so we'll return early.
Expand Down
12 changes: 12 additions & 0 deletions decoder/expr_keyword_completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,19 @@ func TestCompletionAtPos_exprKeyword(t *testing.T) {
hcl.Pos{Line: 1, Column: 9, Byte: 8},
lang.CompleteCandidates([]lang.Candidate{}),
},
{
"in front of prefix (with space)",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.Keyword{Keyword: "foobar"},
},
},
`attr = f`,
hcl.Pos{Line: 1, Column: 7, Byte: 6},
lang.CompleteCandidates([]lang.Candidate{}),
},
}

for i, tc := range testCases {
t.Run(fmt.Sprintf("%d-%s", i, tc.testName), func(t *testing.T) {
bodySchema := &schema.BodySchema{
Expand Down
2 changes: 1 addition & 1 deletion decoder/expr_literal_type_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (lt LiteralType) completeBoolAtPos(ctx context.Context, pos hcl.Pos) []lang

case *hclsyntax.ScopeTraversalExpr:
prefixLen := pos.Byte - eType.Range().Start.Byte
if prefixLen > len(eType.Traversal.RootName()) {
if prefixLen < 0 || prefixLen > len(eType.Traversal.RootName()) {
// The user has probably typed an extra character, such as a
// period, that is not (yet) part of the expression. This prefix
// won't match anything, so we'll return early.
Expand Down
14 changes: 14 additions & 0 deletions decoder/expr_literal_type_completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,20 @@ func TestCompletionAtPos_exprLiteralType(t *testing.T) {
},
}),
},
{
"in front of prefix (with space)",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.LiteralType{
Type: cty.Bool,
},
},
},
`attr = f
`,
hcl.Pos{Line: 1, Column: 7, Byte: 6},
lang.CompleteCandidates([]lang.Candidate{}),
},
}

for i, tc := range testCases {
Expand Down
2 changes: 1 addition & 1 deletion decoder/expr_literal_value_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (lv LiteralValue) completeBoolAtPos(ctx context.Context, pos hcl.Pos) []lan

case *hclsyntax.ScopeTraversalExpr:
prefixLen := pos.Byte - eType.Range().Start.Byte
if prefixLen > len(eType.Traversal.RootName()) {
if prefixLen < 0 || prefixLen > len(eType.Traversal.RootName()) {
// The user has probably typed an extra character, such as a
// period, that is not (yet) part of the expression. This prefix
// won't match anything, so we'll return early.
Expand Down
14 changes: 14 additions & 0 deletions decoder/expr_literal_value_completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,20 @@ func TestCompletionAtPos_exprLiteralValue(t *testing.T) {
},
}),
},
{
"in front of prefix (with space)",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.LiteralValue{
Value: cty.BoolVal(true),
},
},
},
`attr = t
`,
hcl.Pos{Line: 1, Column: 7, Byte: 6},
lang.CompleteCandidates([]lang.Candidate{}),
},
}

for i, tc := range testCases {
Expand Down
2 changes: 1 addition & 1 deletion decoder/expr_type_declaration_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (td TypeDeclaration) CompletionAtPos(ctx context.Context, pos hcl.Pos) []la
}

prefixLen := pos.Byte - eType.Range().Start.Byte
if prefixLen > len(eType.Traversal.RootName()) {
if prefixLen < 0 || prefixLen > len(eType.Traversal.RootName()) {
// The user has probably typed an extra character, such as a
// period, that is not (yet) part of the expression. This prefix
// won't match anything, so we'll return early.
Expand Down
14 changes: 13 additions & 1 deletion decoder/expr_type_declaration_completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestCompletionAtPos_exprTypeDeclaration(t *testing.T) {
}),
},
{
"partial name with dot",
"partial name with dot",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.TypeDeclaration{},
Expand Down Expand Up @@ -1014,6 +1014,18 @@ func TestCompletionAtPos_exprTypeDeclaration(t *testing.T) {
hcl.Pos{Line: 2, Column: 4, Byte: 19},
lang.CompleteCandidates([]lang.Candidate{}),
},
{
"in front of prefix (with space)",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.TypeDeclaration{},
},
},
`attr = st
`,
hcl.Pos{Line: 1, Column: 7, Byte: 6},
lang.CompleteCandidates([]lang.Candidate{}),
},
}

for i, tc := range testCases {
Expand Down
Loading