-
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.
decoder: Implement semantic tokens for Keyword
- Loading branch information
1 parent
e9c1614
commit 1aaa58b
Showing
3 changed files
with
154 additions
and
8 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,31 @@ | ||
package decoder | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/hcl-lang/lang" | ||
"github.com/hashicorp/hcl/v2/hclsyntax" | ||
) | ||
|
||
func (kw Keyword) SemanticTokens(ctx context.Context) []lang.SemanticToken { | ||
eType, ok := kw.expr.(*hclsyntax.ScopeTraversalExpr) | ||
if !ok { | ||
return []lang.SemanticToken{} | ||
} | ||
|
||
if len(eType.Traversal) != 1 { | ||
return []lang.SemanticToken{} | ||
} | ||
|
||
if eType.Traversal.RootName() == kw.cons.Keyword { | ||
return []lang.SemanticToken{ | ||
{ | ||
Type: lang.TokenKeyword, | ||
Modifiers: []lang.SemanticTokenModifier{}, | ||
Range: eType.Range(), | ||
}, | ||
} | ||
} | ||
|
||
return []lang.SemanticToken{} | ||
} |
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,123 @@ | ||
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 TestSemanticTokens_exprKeyword(t *testing.T) { | ||
testCases := []struct { | ||
testName string | ||
attrSchema map[string]*schema.AttributeSchema | ||
cfg string | ||
expectedSemanticTokens []lang.SemanticToken | ||
}{ | ||
{ | ||
"mismatching expression type", | ||
map[string]*schema.AttributeSchema{ | ||
"attr": { | ||
Constraint: schema.Keyword{ | ||
Keyword: "foobar", | ||
}, | ||
}, | ||
}, | ||
`attr = "foobar"`, | ||
[]lang.SemanticToken{ | ||
{ | ||
Type: "hcl-attrName", | ||
Modifiers: lang.SemanticTokenModifiers{}, | ||
Range: hcl.Range{ | ||
Filename: "test.tf", | ||
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, | ||
End: hcl.Pos{Line: 1, Column: 5, Byte: 4}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
"mismatching keyword", | ||
map[string]*schema.AttributeSchema{ | ||
"attr": { | ||
Constraint: schema.Keyword{ | ||
Keyword: "foobar", | ||
}, | ||
}, | ||
}, | ||
`attr = barfoo`, | ||
[]lang.SemanticToken{ | ||
{ | ||
Type: "hcl-attrName", | ||
Modifiers: lang.SemanticTokenModifiers{}, | ||
Range: hcl.Range{ | ||
Filename: "test.tf", | ||
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, | ||
End: hcl.Pos{Line: 1, Column: 5, Byte: 4}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
"matching keyword", | ||
map[string]*schema.AttributeSchema{ | ||
"attr": { | ||
Constraint: schema.Keyword{ | ||
Keyword: "foobar", | ||
}, | ||
}, | ||
}, | ||
`attr = foobar`, | ||
[]lang.SemanticToken{ | ||
{ | ||
Type: "hcl-attrName", | ||
Modifiers: lang.SemanticTokenModifiers{}, | ||
Range: hcl.Range{ | ||
Filename: "test.tf", | ||
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, | ||
End: hcl.Pos{Line: 1, Column: 5, Byte: 4}, | ||
}, | ||
}, | ||
{ | ||
Type: "hcl-keyword", | ||
Modifiers: lang.SemanticTokenModifiers{}, | ||
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() | ||
tokens, err := d.SemanticTokensInFile(ctx, "test.tf") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if diff := cmp.Diff(tc.expectedSemanticTokens, tokens); diff != "" { | ||
t.Fatalf("unexpected tokens: %s", diff) | ||
} | ||
}) | ||
} | ||
} |