Skip to content

Commit 521a8c0

Browse files
authored
schema: Make modifiers plural in LabelSchema (#110)
* schema: Make modifiers plural in LabelSchema * schema: Make modifiers plural in BlockSchema * schema: introduce SemanticTokenModifiers to AttrSchema * avoid auto-assigning hcl-dependent semantic modifier
1 parent 71f927e commit 521a8c0

File tree

7 files changed

+116
-90
lines changed

7 files changed

+116
-90
lines changed

decoder/dependent_body_test.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,22 @@ func TestBodySchema_DependentBodySchema_dependentAttr(t *testing.T) {
111111
firstDepBody := &schema.BodySchema{
112112
Attributes: map[string]*schema.AttributeSchema{
113113
"backend": {
114-
Expr: schema.LiteralTypeOnly(cty.String),
115-
IsDepKey: true,
114+
Expr: schema.LiteralTypeOnly(cty.String),
115+
IsDepKey: true,
116+
SemanticTokenModifiers: lang.SemanticTokenModifiers{},
116117
},
117118
},
118119
}
119120
secondDepBody := &schema.BodySchema{
120121
Attributes: map[string]*schema.AttributeSchema{
121-
"extra": {Expr: schema.LiteralTypeOnly(cty.Number)},
122+
"extra": {
123+
Expr: schema.LiteralTypeOnly(cty.Number),
124+
SemanticTokenModifiers: lang.SemanticTokenModifiers{},
125+
},
122126
"backend": {
123-
Expr: schema.LiteralTypeOnly(cty.String),
124-
IsDepKey: true,
127+
Expr: schema.LiteralTypeOnly(cty.String),
128+
IsDepKey: true,
129+
SemanticTokenModifiers: lang.SemanticTokenModifiers{},
125130
},
126131
},
127132
}

decoder/semantic_tokens.go

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (d *PathDecoder) SemanticTokensInFile(filename string) ([]lang.SemanticToke
2828
return []lang.SemanticToken{}, nil
2929
}
3030

31-
tokens := d.tokensForBody(body, d.pathCtx.Schema, false, []lang.SemanticTokenModifier{})
31+
tokens := d.tokensForBody(body, d.pathCtx.Schema, []lang.SemanticTokenModifier{})
3232

3333
sort.Slice(tokens, func(i, j int) bool {
3434
return tokens[i].Range.Start.Byte < tokens[j].Range.Start.Byte
@@ -37,9 +37,7 @@ func (d *PathDecoder) SemanticTokensInFile(filename string) ([]lang.SemanticToke
3737
return tokens, nil
3838
}
3939

40-
func (d *PathDecoder) tokensForBody(body *hclsyntax.Body, bodySchema *schema.BodySchema,
41-
isDependent bool, parentModifiers []lang.SemanticTokenModifier) []lang.SemanticToken {
42-
40+
func (d *PathDecoder) tokensForBody(body *hclsyntax.Body, bodySchema *schema.BodySchema, parentModifiers []lang.SemanticTokenModifier) []lang.SemanticToken {
4341
tokens := make([]lang.SemanticToken, 0)
4442

4543
if bodySchema == nil {
@@ -56,14 +54,13 @@ func (d *PathDecoder) tokensForBody(body *hclsyntax.Body, bodySchema *schema.Bod
5654
attrSchema = bodySchema.AnyAttribute
5755
}
5856

59-
modifiers := make([]lang.SemanticTokenModifier, 0)
60-
if isDependent {
61-
modifiers = append(modifiers, lang.TokenModifierDependent)
62-
}
57+
attrModifiers := make([]lang.SemanticTokenModifier, 0)
58+
attrModifiers = append(attrModifiers, parentModifiers...)
59+
attrModifiers = append(attrModifiers, attrSchema.SemanticTokenModifiers...)
6360

6461
tokens = append(tokens, lang.SemanticToken{
6562
Type: lang.TokenAttrName,
66-
Modifiers: modifiers,
63+
Modifiers: attrModifiers,
6764
Range: attr.NameRange,
6865
})
6966

@@ -80,13 +77,7 @@ func (d *PathDecoder) tokensForBody(body *hclsyntax.Body, bodySchema *schema.Bod
8077

8178
blockModifiers := make([]lang.SemanticTokenModifier, 0)
8279
blockModifiers = append(blockModifiers, parentModifiers...)
83-
84-
if isDependent {
85-
blockModifiers = append(blockModifiers, lang.TokenModifierDependent)
86-
}
87-
if blockSchema.SemanticTokenModifier != "" {
88-
blockModifiers = append(blockModifiers, blockSchema.SemanticTokenModifier)
89-
}
80+
blockModifiers = append(blockModifiers, blockSchema.SemanticTokenModifiers...)
9081

9182
tokens = append(tokens, lang.SemanticToken{
9283
Type: lang.TokenBlockType,
@@ -104,15 +95,8 @@ func (d *PathDecoder) tokensForBody(body *hclsyntax.Body, bodySchema *schema.Bod
10495

10596
labelModifiers := make([]lang.SemanticTokenModifier, 0)
10697
labelModifiers = append(labelModifiers, parentModifiers...)
107-
if labelSchema.IsDepKey {
108-
labelModifiers = append(labelModifiers, lang.TokenModifierDependent)
109-
}
110-
if blockSchema.SemanticTokenModifier != "" {
111-
labelModifiers = append(labelModifiers, blockSchema.SemanticTokenModifier)
112-
}
113-
if labelSchema.SemanticTokenModifier != "" {
114-
labelModifiers = append(labelModifiers, labelSchema.SemanticTokenModifier)
115-
}
98+
labelModifiers = append(labelModifiers, blockSchema.SemanticTokenModifiers...)
99+
labelModifiers = append(labelModifiers, labelSchema.SemanticTokenModifiers...)
116100

117101
tokens = append(tokens, lang.SemanticToken{
118102
Type: lang.TokenBlockLabel,
@@ -122,12 +106,12 @@ func (d *PathDecoder) tokensForBody(body *hclsyntax.Body, bodySchema *schema.Bod
122106
}
123107

124108
if block.Body != nil {
125-
tokens = append(tokens, d.tokensForBody(block.Body, blockSchema.Body, false, blockModifiers)...)
109+
tokens = append(tokens, d.tokensForBody(block.Body, blockSchema.Body, blockModifiers)...)
126110
}
127111

128112
depSchema, _, ok := NewBlockSchema(blockSchema).DependentBodySchema(block.AsHCLBlock())
129113
if ok {
130-
tokens = append(tokens, d.tokensForBody(block.Body, depSchema, true, []lang.SemanticTokenModifier{})...)
114+
tokens = append(tokens, d.tokensForBody(block.Body, depSchema, []lang.SemanticTokenModifier{})...)
131115
}
132116
}
133117

decoder/semantic_tokens_test.go

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,22 @@ func TestDecoder_SemanticTokensInFile_basic(t *testing.T) {
106106
"source": {
107107
Expr: schema.LiteralTypeOnly(cty.String),
108108
IsDeprecated: true,
109+
SemanticTokenModifiers: lang.SemanticTokenModifiers{
110+
lang.TokenModifierDependent,
111+
},
109112
},
110113
},
111114
},
112115
},
113116
"resource": {
114117
Labels: []*schema.LabelSchema{
115-
{Name: "type", IsDepKey: true},
118+
{
119+
Name: "type",
120+
IsDepKey: true,
121+
SemanticTokenModifiers: lang.SemanticTokenModifiers{
122+
lang.TokenModifierDependent,
123+
},
124+
},
116125
{Name: "name"},
117126
},
118127
},
@@ -164,8 +173,10 @@ resource "vault_auth_backend" "blah" {
164173
},
165174
},
166175
{ // source
167-
Type: lang.TokenAttrName,
168-
Modifiers: []lang.SemanticTokenModifier{},
176+
Type: lang.TokenAttrName,
177+
Modifiers: []lang.SemanticTokenModifier{
178+
lang.TokenModifierDependent,
179+
},
169180
Range: hcl.Range{
170181
Filename: "test.tf",
171182
Start: hcl.Pos{
@@ -297,7 +308,13 @@ func TestDecoder_SemanticTokensInFile_dependentSchema(t *testing.T) {
297308
Blocks: map[string]*schema.BlockSchema{
298309
"resource": {
299310
Labels: []*schema.LabelSchema{
300-
{Name: "type", IsDepKey: true},
311+
{
312+
Name: "type",
313+
IsDepKey: true,
314+
SemanticTokenModifiers: lang.SemanticTokenModifiers{
315+
lang.TokenModifierDependent,
316+
},
317+
},
301318
{Name: "name"},
302319
},
303320
DependentBody: map[schema.SchemaKey]*schema.BodySchema{
@@ -457,10 +474,8 @@ resource "aws_instance" "beta" {
457474
},
458475
},
459476
{ // instance_type
460-
Type: lang.TokenAttrName,
461-
Modifiers: []lang.SemanticTokenModifier{
462-
lang.TokenModifierDependent,
463-
},
477+
Type: lang.TokenAttrName,
478+
Modifiers: []lang.SemanticTokenModifier{},
464479
Range: hcl.Range{
465480
Filename: "test.tf",
466481
Start: hcl.Pos{
@@ -493,10 +508,8 @@ resource "aws_instance" "beta" {
493508
},
494509
},
495510
{ // deprecated
496-
Type: lang.TokenAttrName,
497-
Modifiers: []lang.SemanticTokenModifier{
498-
lang.TokenModifierDependent,
499-
},
511+
Type: lang.TokenAttrName,
512+
Modifiers: []lang.SemanticTokenModifier{},
500513
Range: hcl.Range{
501514
Filename: "test.tf",
502515
Start: hcl.Pos{
@@ -540,11 +553,11 @@ func TestDecoder_SemanticTokensInFile_customModifiers(t *testing.T) {
540553
bodySchema := &schema.BodySchema{
541554
Blocks: map[string]*schema.BlockSchema{
542555
"module": {
543-
SemanticTokenModifier: lang.SemanticTokenModifier("module"),
556+
SemanticTokenModifiers: lang.SemanticTokenModifiers{"module"},
544557
Labels: []*schema.LabelSchema{
545558
{
546-
Name: "name",
547-
SemanticTokenModifier: lang.SemanticTokenModifier("name"),
559+
Name: "name",
560+
SemanticTokenModifiers: lang.SemanticTokenModifiers{"name"},
548561
},
549562
},
550563
Body: &schema.BodySchema{
@@ -553,33 +566,34 @@ func TestDecoder_SemanticTokensInFile_customModifiers(t *testing.T) {
553566
Expr: schema.LiteralTypeOnly(cty.Number),
554567
},
555568
"source": {
556-
Expr: schema.LiteralTypeOnly(cty.String),
557-
IsDeprecated: true,
569+
Expr: schema.LiteralTypeOnly(cty.String),
570+
IsDeprecated: true,
571+
SemanticTokenModifiers: lang.SemanticTokenModifiers{lang.TokenModifierDependent},
558572
},
559573
},
560574
},
561575
},
562576
"resource": {
563-
SemanticTokenModifier: lang.SemanticTokenModifier("resource"),
577+
SemanticTokenModifiers: lang.SemanticTokenModifiers{"resource"},
564578
Labels: []*schema.LabelSchema{
565579
{
566-
Name: "type",
567-
IsDepKey: true,
568-
SemanticTokenModifier: lang.SemanticTokenModifier("type"),
580+
Name: "type",
581+
IsDepKey: true,
582+
SemanticTokenModifiers: lang.SemanticTokenModifiers{"type", lang.TokenModifierDependent},
569583
},
570584
{
571-
Name: "name",
572-
SemanticTokenModifier: lang.SemanticTokenModifier("name"),
585+
Name: "name",
586+
SemanticTokenModifiers: lang.SemanticTokenModifiers{"name"},
573587
},
574588
},
575589
Body: &schema.BodySchema{
576590
Blocks: map[string]*schema.BlockSchema{
577591
"provisioner": {
578-
SemanticTokenModifier: lang.SemanticTokenModifier("provisioner"),
592+
SemanticTokenModifiers: lang.SemanticTokenModifiers{"provisioner"},
579593
Labels: []*schema.LabelSchema{
580594
{
581-
Name: "type",
582-
SemanticTokenModifier: lang.SemanticTokenModifier("type"),
595+
Name: "type",
596+
SemanticTokenModifiers: lang.SemanticTokenModifiers{"type"},
583597
},
584598
},
585599
},
@@ -658,8 +672,11 @@ resource "vault_auth_backend" "blah" {
658672
},
659673
},
660674
{ // source
661-
Type: lang.TokenAttrName,
662-
Modifiers: []lang.SemanticTokenModifier{},
675+
Type: lang.TokenAttrName,
676+
Modifiers: []lang.SemanticTokenModifier{
677+
lang.SemanticTokenModifier("module"),
678+
lang.TokenModifierDependent,
679+
},
663680
Range: hcl.Range{
664681
Filename: "test.tf",
665682
Start: hcl.Pos{
@@ -692,8 +709,10 @@ resource "vault_auth_backend" "blah" {
692709
},
693710
},
694711
{ // count
695-
Type: lang.TokenAttrName,
696-
Modifiers: []lang.SemanticTokenModifier{},
712+
Type: lang.TokenAttrName,
713+
Modifiers: []lang.SemanticTokenModifier{
714+
lang.SemanticTokenModifier("module"),
715+
},
697716
Range: hcl.Range{
698717
Filename: "test.tf",
699718
Start: hcl.Pos{
@@ -747,9 +766,9 @@ resource "vault_auth_backend" "blah" {
747766
{ // vault_auth_backend
748767
Type: lang.TokenBlockLabel,
749768
Modifiers: []lang.SemanticTokenModifier{
750-
lang.TokenModifierDependent,
751769
lang.SemanticTokenModifier("resource"),
752770
lang.SemanticTokenModifier("type"),
771+
lang.TokenModifierDependent,
753772
},
754773
Range: hcl.Range{
755774
Filename: "test.tf",

lang/semantic_token.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
type SemanticToken struct {
88
Type SemanticTokenType
9-
Modifiers []SemanticTokenModifier
9+
Modifiers SemanticTokenModifiers
1010
Range hcl.Range
1111
}
1212

@@ -49,6 +49,18 @@ var SupportedSemanticTokenTypes = SemanticTokenTypes{
4949

5050
type SemanticTokenModifier string
5151

52+
type SemanticTokenModifiers []SemanticTokenModifier
53+
54+
func (stm SemanticTokenModifiers) Copy() SemanticTokenModifiers {
55+
if stm == nil {
56+
return nil
57+
}
58+
59+
modifiersCopy := make(SemanticTokenModifiers, len(stm))
60+
copy(modifiersCopy, stm)
61+
return modifiersCopy
62+
}
63+
5264
const (
5365
TokenModifierDependent = SemanticTokenModifier("hcl-dependent")
5466
)

schema/attribute_schema.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ type AttributeSchema struct {
3131
// as an origin for another target (e.g. module inputs,
3232
// or tfvars entires in Terraform)
3333
OriginForTarget *PathTarget
34+
35+
// SemanticTokenModifiers represents the semantic token modifiers
36+
// to report for the attribute name
37+
// (in addition to any modifiers of any parent blocks)
38+
SemanticTokenModifiers lang.SemanticTokenModifiers
3439
}
3540

3641
type AttributeAddrSchema struct {
@@ -94,16 +99,17 @@ func (as *AttributeSchema) Copy() *AttributeSchema {
9499
}
95100

96101
newAs := &AttributeSchema{
97-
IsRequired: as.IsRequired,
98-
IsOptional: as.IsOptional,
99-
IsDeprecated: as.IsDeprecated,
100-
IsComputed: as.IsComputed,
101-
IsSensitive: as.IsSensitive,
102-
IsDepKey: as.IsDepKey,
103-
Description: as.Description,
104-
Expr: as.Expr.Copy(),
105-
Address: as.Address.Copy(),
106-
OriginForTarget: as.OriginForTarget.Copy(),
102+
IsRequired: as.IsRequired,
103+
IsOptional: as.IsOptional,
104+
IsDeprecated: as.IsDeprecated,
105+
IsComputed: as.IsComputed,
106+
IsSensitive: as.IsSensitive,
107+
IsDepKey: as.IsDepKey,
108+
Description: as.Description,
109+
Expr: as.Expr.Copy(),
110+
Address: as.Address.Copy(),
111+
OriginForTarget: as.OriginForTarget.Copy(),
112+
SemanticTokenModifiers: as.SemanticTokenModifiers.Copy(),
107113
}
108114

109115
return newAs

schema/block_schema.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ type BlockSchema struct {
1414
Labels []*LabelSchema
1515
Type BlockType
1616

17-
// SemanticTokenModifier represents the semantic token modifier
17+
// SemanticTokenModifiers represents the semantic token modifiers
1818
// to report for the block's type and labels
1919
// (in addition to any modifiers of any parent blocks)
20-
SemanticTokenModifier lang.SemanticTokenModifier
20+
SemanticTokenModifiers lang.SemanticTokenModifiers
2121

2222
// Body represents the body within block
2323
// such as attributes and nested blocks
@@ -167,14 +167,14 @@ func (bs *BlockSchema) Copy() *BlockSchema {
167167
}
168168

169169
newBs := &BlockSchema{
170-
Type: bs.Type,
171-
SemanticTokenModifier: bs.SemanticTokenModifier,
172-
IsDeprecated: bs.IsDeprecated,
173-
MinItems: bs.MinItems,
174-
MaxItems: bs.MaxItems,
175-
Description: bs.Description,
176-
Body: bs.Body.Copy(),
177-
Address: bs.Address.Copy(),
170+
Type: bs.Type,
171+
SemanticTokenModifiers: bs.SemanticTokenModifiers.Copy(),
172+
IsDeprecated: bs.IsDeprecated,
173+
MinItems: bs.MinItems,
174+
MaxItems: bs.MaxItems,
175+
Description: bs.Description,
176+
Body: bs.Body.Copy(),
177+
Address: bs.Address.Copy(),
178178
}
179179

180180
if bs.Labels != nil {

0 commit comments

Comments
 (0)