Skip to content

Commit

Permalink
schema: introduce SemanticTokenModifiers to AttrSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Mar 22, 2022
1 parent 4381384 commit 711ae33
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
15 changes: 10 additions & 5 deletions decoder/dependent_body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,22 @@ func TestBodySchema_DependentBodySchema_dependentAttr(t *testing.T) {
firstDepBody := &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"backend": {
Expr: schema.LiteralTypeOnly(cty.String),
IsDepKey: true,
Expr: schema.LiteralTypeOnly(cty.String),
IsDepKey: true,
SemanticTokenModifiers: lang.SemanticTokenModifiers{},
},
},
}
secondDepBody := &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"extra": {Expr: schema.LiteralTypeOnly(cty.Number)},
"extra": {
Expr: schema.LiteralTypeOnly(cty.Number),
SemanticTokenModifiers: lang.SemanticTokenModifiers{},
},
"backend": {
Expr: schema.LiteralTypeOnly(cty.String),
IsDepKey: true,
Expr: schema.LiteralTypeOnly(cty.String),
IsDepKey: true,
SemanticTokenModifiers: lang.SemanticTokenModifiers{},
},
},
}
Expand Down
8 changes: 5 additions & 3 deletions decoder/semantic_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ func (d *PathDecoder) tokensForBody(body *hclsyntax.Body, bodySchema *schema.Bod
attrSchema = bodySchema.AnyAttribute
}

modifiers := make([]lang.SemanticTokenModifier, 0)
attrModifiers := make([]lang.SemanticTokenModifier, 0)
attrModifiers = append(attrModifiers, parentModifiers...)
if isDependent {
modifiers = append(modifiers, lang.TokenModifierDependent)
attrModifiers = append(attrModifiers, lang.TokenModifierDependent)
}
attrModifiers = append(attrModifiers, attrSchema.SemanticTokenModifiers...)

tokens = append(tokens, lang.SemanticToken{
Type: lang.TokenAttrName,
Modifiers: modifiers,
Modifiers: attrModifiers,
Range: attr.NameRange,
})

Expand Down
12 changes: 8 additions & 4 deletions decoder/semantic_tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,8 +658,10 @@ resource "vault_auth_backend" "blah" {
},
},
{ // source
Type: lang.TokenAttrName,
Modifiers: []lang.SemanticTokenModifier{},
Type: lang.TokenAttrName,
Modifiers: []lang.SemanticTokenModifier{
lang.SemanticTokenModifier("module"),
},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{
Expand Down Expand Up @@ -692,8 +694,10 @@ resource "vault_auth_backend" "blah" {
},
},
{ // count
Type: lang.TokenAttrName,
Modifiers: []lang.SemanticTokenModifier{},
Type: lang.TokenAttrName,
Modifiers: []lang.SemanticTokenModifier{
lang.SemanticTokenModifier("module"),
},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{
Expand Down
26 changes: 16 additions & 10 deletions schema/attribute_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ type AttributeSchema struct {
// as an origin for another target (e.g. module inputs,
// or tfvars entires in Terraform)
OriginForTarget *PathTarget

// SemanticTokenModifiers represents the semantic token modifiers
// to report for the attribute name
// (in addition to any modifiers of any parent blocks)
SemanticTokenModifiers lang.SemanticTokenModifiers
}

type AttributeAddrSchema struct {
Expand Down Expand Up @@ -94,16 +99,17 @@ func (as *AttributeSchema) Copy() *AttributeSchema {
}

newAs := &AttributeSchema{
IsRequired: as.IsRequired,
IsOptional: as.IsOptional,
IsDeprecated: as.IsDeprecated,
IsComputed: as.IsComputed,
IsSensitive: as.IsSensitive,
IsDepKey: as.IsDepKey,
Description: as.Description,
Expr: as.Expr.Copy(),
Address: as.Address.Copy(),
OriginForTarget: as.OriginForTarget.Copy(),
IsRequired: as.IsRequired,
IsOptional: as.IsOptional,
IsDeprecated: as.IsDeprecated,
IsComputed: as.IsComputed,
IsSensitive: as.IsSensitive,
IsDepKey: as.IsDepKey,
Description: as.Description,
Expr: as.Expr.Copy(),
Address: as.Address.Copy(),
OriginForTarget: as.OriginForTarget.Copy(),
SemanticTokenModifiers: as.SemanticTokenModifiers.Copy(),
}

return newAs
Expand Down

0 comments on commit 711ae33

Please sign in to comment.