-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* schema: Implement EmptyCompletionData for Keyword * decoder: Implement completion for Keyword * decoder: Implement hover for Keyword * decoder: Implement semantic tokens for Keyword
- Loading branch information
1 parent
31ff628
commit 053e555
Showing
8 changed files
with
510 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package decoder | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"github.com/hashicorp/hcl-lang/lang" | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/hclsyntax" | ||
) | ||
|
||
func (kw Keyword) CompletionAtPos(ctx context.Context, pos hcl.Pos) []lang.Candidate { | ||
if isEmptyExpression(kw.expr) { | ||
return []lang.Candidate{ | ||
{ | ||
Label: kw.cons.Keyword, | ||
Detail: kw.cons.FriendlyName(), | ||
Description: kw.cons.Description, | ||
Kind: lang.KeywordCandidateKind, | ||
TextEdit: lang.TextEdit{ | ||
NewText: kw.cons.Keyword, | ||
Snippet: kw.cons.Keyword, | ||
Range: hcl.Range{ | ||
Filename: kw.expr.Range().Filename, | ||
Start: pos, | ||
End: pos, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
eType, ok := kw.expr.(*hclsyntax.ScopeTraversalExpr) | ||
if !ok { | ||
return []lang.Candidate{} | ||
} | ||
|
||
if len(eType.Traversal) != 1 { | ||
return []lang.Candidate{} | ||
} | ||
|
||
prefixLen := pos.Byte - eType.Traversal.SourceRange().Start.Byte | ||
prefix := eType.Traversal.RootName()[0:prefixLen] | ||
|
||
if strings.HasPrefix(kw.cons.Keyword, prefix) { | ||
return []lang.Candidate{ | ||
{ | ||
Label: kw.cons.Keyword, | ||
Detail: kw.cons.FriendlyName(), | ||
Description: kw.cons.Description, | ||
Kind: lang.KeywordCandidateKind, | ||
TextEdit: lang.TextEdit{ | ||
NewText: kw.cons.Keyword, | ||
Snippet: kw.cons.Keyword, | ||
Range: eType.Range(), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
return []lang.Candidate{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package decoder | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/hashicorp/hcl-lang/lang" | ||
"github.com/hashicorp/hcl-lang/schema" | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/hclsyntax" | ||
) | ||
|
||
func TestCompletionAtPos_exprKeyword(t *testing.T) { | ||
testCases := []struct { | ||
testName string | ||
attrSchema map[string]*schema.AttributeSchema | ||
cfg string | ||
pos hcl.Pos | ||
expectedCandidates lang.Candidates | ||
}{ | ||
{ | ||
"no expression", | ||
map[string]*schema.AttributeSchema{ | ||
"attr": { | ||
Constraint: schema.Keyword{Keyword: "foobar"}, | ||
}, | ||
}, | ||
`attr = `, | ||
hcl.Pos{Line: 1, Column: 8, Byte: 7}, | ||
lang.CompleteCandidates([]lang.Candidate{ | ||
{ | ||
Label: "foobar", | ||
Detail: "keyword", | ||
Kind: lang.KeywordCandidateKind, | ||
TextEdit: lang.TextEdit{ | ||
NewText: "foobar", | ||
Snippet: "foobar", | ||
Range: hcl.Range{ | ||
Filename: "test.tf", | ||
Start: hcl.Pos{Line: 1, Column: 8, Byte: 7}, | ||
End: hcl.Pos{Line: 1, Column: 8, Byte: 7}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
}, | ||
{ | ||
"matching prefix", | ||
map[string]*schema.AttributeSchema{ | ||
"attr": { | ||
Constraint: schema.Keyword{Keyword: "foobar"}, | ||
}, | ||
}, | ||
`attr = f`, | ||
hcl.Pos{Line: 1, Column: 9, Byte: 8}, | ||
lang.CompleteCandidates([]lang.Candidate{ | ||
{ | ||
Label: "foobar", | ||
Detail: "keyword", | ||
Kind: lang.KeywordCandidateKind, | ||
TextEdit: lang.TextEdit{ | ||
NewText: "foobar", | ||
Snippet: "foobar", | ||
Range: hcl.Range{ | ||
Filename: "test.tf", | ||
Start: hcl.Pos{Line: 1, Column: 8, Byte: 7}, | ||
End: hcl.Pos{Line: 1, Column: 9, Byte: 8}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
}, | ||
{ | ||
"matching prefix in the middle", | ||
map[string]*schema.AttributeSchema{ | ||
"attr": { | ||
Constraint: schema.Keyword{Keyword: "foobar"}, | ||
}, | ||
}, | ||
`attr = foo`, | ||
hcl.Pos{Line: 1, Column: 9, Byte: 8}, | ||
lang.CompleteCandidates([]lang.Candidate{ | ||
{ | ||
Label: "foobar", | ||
Detail: "keyword", | ||
Kind: lang.KeywordCandidateKind, | ||
TextEdit: lang.TextEdit{ | ||
NewText: "foobar", | ||
Snippet: "foobar", | ||
Range: hcl.Range{ | ||
Filename: "test.tf", | ||
Start: hcl.Pos{Line: 1, Column: 8, Byte: 7}, | ||
End: hcl.Pos{Line: 1, Column: 11, Byte: 10}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
}, | ||
{ | ||
"mismatching prefix", | ||
map[string]*schema.AttributeSchema{ | ||
"attr": { | ||
Constraint: schema.Keyword{Keyword: "foobar"}, | ||
}, | ||
}, | ||
`attr = x`, | ||
hcl.Pos{Line: 1, Column: 9, Byte: 8}, | ||
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{ | ||
Attributes: tc.attrSchema, | ||
} | ||
|
||
f, _ := hclsyntax.ParseConfig([]byte(tc.cfg), "test.tf", hcl.InitialPos) | ||
d := testPathDecoder(t, &PathContext{ | ||
Schema: bodySchema, | ||
Files: map[string]*hcl.File{ | ||
"test.tf": f, | ||
}, | ||
}) | ||
|
||
ctx := context.Background() | ||
candidates, err := d.CandidatesAtPos(ctx, "test.tf", tc.pos) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if diff := cmp.Diff(tc.expectedCandidates, candidates); diff != "" { | ||
t.Fatalf("unexpected candidates: %s", diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package decoder | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/hcl-lang/lang" | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/hclsyntax" | ||
) | ||
|
||
func (kw Keyword) HoverAtPos(ctx context.Context, pos hcl.Pos) *lang.HoverData { | ||
eType, ok := kw.expr.(*hclsyntax.ScopeTraversalExpr) | ||
if !ok { | ||
return nil | ||
} | ||
|
||
if len(eType.Traversal) != 1 { | ||
return nil | ||
} | ||
|
||
if eType.Traversal.RootName() == kw.cons.Keyword { | ||
content := fmt.Sprintf("`%s` _%s_", kw.cons.Keyword, kw.cons.FriendlyName()) | ||
if kw.cons.Description.Value != "" { | ||
content += "\n\n" + kw.cons.Description.Value | ||
} | ||
|
||
return &lang.HoverData{ | ||
Content: lang.Markdown(content), | ||
Range: eType.SrcRange, | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package decoder | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/hashicorp/hcl-lang/lang" | ||
"github.com/hashicorp/hcl-lang/schema" | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/hclsyntax" | ||
) | ||
|
||
func TestHoverAtPos_exprKeyword(t *testing.T) { | ||
testCases := []struct { | ||
testName string | ||
attrSchema map[string]*schema.AttributeSchema | ||
cfg string | ||
pos hcl.Pos | ||
expectedHoverData *lang.HoverData | ||
}{ | ||
{ | ||
"mismatching expression type", | ||
map[string]*schema.AttributeSchema{ | ||
"attr": { | ||
Constraint: schema.Keyword{ | ||
Keyword: "foobar", | ||
}, | ||
}, | ||
}, | ||
`attr = "foobar"`, | ||
hcl.Pos{Line: 1, Column: 12, Byte: 11}, | ||
nil, | ||
}, | ||
{ | ||
"mismatching keyword", | ||
map[string]*schema.AttributeSchema{ | ||
"attr": { | ||
Constraint: schema.Keyword{ | ||
Keyword: "foobar", | ||
}, | ||
}, | ||
}, | ||
`attr = barfoo`, | ||
hcl.Pos{Line: 1, Column: 12, Byte: 11}, | ||
nil, | ||
}, | ||
{ | ||
"matching keyword", | ||
map[string]*schema.AttributeSchema{ | ||
"attr": { | ||
Constraint: schema.Keyword{ | ||
Keyword: "foobar", | ||
}, | ||
}, | ||
}, | ||
`attr = foobar`, | ||
hcl.Pos{Line: 1, Column: 12, Byte: 11}, | ||
&lang.HoverData{ | ||
Content: lang.Markdown("`foobar` _keyword_"), | ||
Range: hcl.Range{ | ||
Filename: "test.tf", | ||
Start: hcl.Pos{Line: 1, Column: 8, Byte: 7}, | ||
End: hcl.Pos{Line: 1, Column: 14, Byte: 13}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
"matching keyword with all metadata", | ||
map[string]*schema.AttributeSchema{ | ||
"attr": { | ||
Constraint: schema.Keyword{ | ||
Keyword: "foobar", | ||
Name: "custom name", | ||
Description: lang.Markdown("custom _description_"), | ||
}, | ||
}, | ||
}, | ||
`attr = foobar`, | ||
hcl.Pos{Line: 1, Column: 12, Byte: 11}, | ||
&lang.HoverData{ | ||
Content: lang.Markdown("`foobar` _custom name_\n\ncustom _description_"), | ||
Range: hcl.Range{ | ||
Filename: "test.tf", | ||
Start: hcl.Pos{Line: 1, Column: 8, Byte: 7}, | ||
End: hcl.Pos{Line: 1, Column: 14, Byte: 13}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for i, tc := range testCases { | ||
t.Run(fmt.Sprintf("%d-%s", i, tc.testName), func(t *testing.T) { | ||
bodySchema := &schema.BodySchema{ | ||
Attributes: tc.attrSchema, | ||
} | ||
|
||
f, _ := hclsyntax.ParseConfig([]byte(tc.cfg), "test.tf", hcl.InitialPos) | ||
d := testPathDecoder(t, &PathContext{ | ||
Schema: bodySchema, | ||
Files: map[string]*hcl.File{ | ||
"test.tf": f, | ||
}, | ||
}) | ||
|
||
ctx := context.Background() | ||
hoverData, err := d.HoverAtPos(ctx, "test.tf", tc.pos) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if diff := cmp.Diff(tc.expectedHoverData, hoverData); diff != "" { | ||
t.Fatalf("unexpected hover data: %s", diff) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.