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

schema: Make modifiers plural in LabelSchema #110

Merged
merged 4 commits into from
Mar 22, 2022
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
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
38 changes: 11 additions & 27 deletions decoder/semantic_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (d *PathDecoder) SemanticTokensInFile(filename string) ([]lang.SemanticToke
return []lang.SemanticToken{}, nil
}

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

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

func (d *PathDecoder) tokensForBody(body *hclsyntax.Body, bodySchema *schema.BodySchema,
isDependent bool, parentModifiers []lang.SemanticTokenModifier) []lang.SemanticToken {

func (d *PathDecoder) tokensForBody(body *hclsyntax.Body, bodySchema *schema.BodySchema, parentModifiers []lang.SemanticTokenModifier) []lang.SemanticToken {
tokens := make([]lang.SemanticToken, 0)

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

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

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

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

blockModifiers := make([]lang.SemanticTokenModifier, 0)
blockModifiers = append(blockModifiers, parentModifiers...)

if isDependent {
blockModifiers = append(blockModifiers, lang.TokenModifierDependent)
}
if blockSchema.SemanticTokenModifier != "" {
blockModifiers = append(blockModifiers, blockSchema.SemanticTokenModifier)
}
blockModifiers = append(blockModifiers, blockSchema.SemanticTokenModifiers...)

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

labelModifiers := make([]lang.SemanticTokenModifier, 0)
labelModifiers = append(labelModifiers, parentModifiers...)
if labelSchema.IsDepKey {
labelModifiers = append(labelModifiers, lang.TokenModifierDependent)
}
if blockSchema.SemanticTokenModifier != "" {
labelModifiers = append(labelModifiers, blockSchema.SemanticTokenModifier)
}
if labelSchema.SemanticTokenModifier != "" {
labelModifiers = append(labelModifiers, labelSchema.SemanticTokenModifier)
}
labelModifiers = append(labelModifiers, blockSchema.SemanticTokenModifiers...)
labelModifiers = append(labelModifiers, labelSchema.SemanticTokenModifiers...)

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

if block.Body != nil {
tokens = append(tokens, d.tokensForBody(block.Body, blockSchema.Body, false, blockModifiers)...)
tokens = append(tokens, d.tokensForBody(block.Body, blockSchema.Body, blockModifiers)...)
}

depSchema, _, ok := NewBlockSchema(blockSchema).DependentBodySchema(block.AsHCLBlock())
if ok {
tokens = append(tokens, d.tokensForBody(block.Body, depSchema, true, []lang.SemanticTokenModifier{})...)
tokens = append(tokens, d.tokensForBody(block.Body, depSchema, []lang.SemanticTokenModifier{})...)
}
}

Expand Down
81 changes: 50 additions & 31 deletions decoder/semantic_tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,22 @@ func TestDecoder_SemanticTokensInFile_basic(t *testing.T) {
"source": {
Expr: schema.LiteralTypeOnly(cty.String),
IsDeprecated: true,
SemanticTokenModifiers: lang.SemanticTokenModifiers{
lang.TokenModifierDependent,
},
},
},
},
},
"resource": {
Labels: []*schema.LabelSchema{
{Name: "type", IsDepKey: true},
{
Name: "type",
IsDepKey: true,
SemanticTokenModifiers: lang.SemanticTokenModifiers{
lang.TokenModifierDependent,
},
},
{Name: "name"},
},
},
Expand Down Expand Up @@ -164,8 +173,10 @@ resource "vault_auth_backend" "blah" {
},
},
{ // source
Type: lang.TokenAttrName,
Modifiers: []lang.SemanticTokenModifier{},
Type: lang.TokenAttrName,
Modifiers: []lang.SemanticTokenModifier{
lang.TokenModifierDependent,
},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{
Expand Down Expand Up @@ -297,7 +308,13 @@ func TestDecoder_SemanticTokensInFile_dependentSchema(t *testing.T) {
Blocks: map[string]*schema.BlockSchema{
"resource": {
Labels: []*schema.LabelSchema{
{Name: "type", IsDepKey: true},
{
Name: "type",
IsDepKey: true,
SemanticTokenModifiers: lang.SemanticTokenModifiers{
lang.TokenModifierDependent,
},
},
{Name: "name"},
},
DependentBody: map[schema.SchemaKey]*schema.BodySchema{
Expand Down Expand Up @@ -457,10 +474,8 @@ resource "aws_instance" "beta" {
},
},
{ // instance_type
Type: lang.TokenAttrName,
Modifiers: []lang.SemanticTokenModifier{
lang.TokenModifierDependent,
},
Type: lang.TokenAttrName,
Modifiers: []lang.SemanticTokenModifier{},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{
Expand Down Expand Up @@ -493,10 +508,8 @@ resource "aws_instance" "beta" {
},
},
{ // deprecated
Type: lang.TokenAttrName,
Modifiers: []lang.SemanticTokenModifier{
lang.TokenModifierDependent,
},
Type: lang.TokenAttrName,
Modifiers: []lang.SemanticTokenModifier{},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{
Expand Down Expand Up @@ -540,11 +553,11 @@ func TestDecoder_SemanticTokensInFile_customModifiers(t *testing.T) {
bodySchema := &schema.BodySchema{
Blocks: map[string]*schema.BlockSchema{
"module": {
SemanticTokenModifier: lang.SemanticTokenModifier("module"),
SemanticTokenModifiers: lang.SemanticTokenModifiers{"module"},
Labels: []*schema.LabelSchema{
{
Name: "name",
SemanticTokenModifier: lang.SemanticTokenModifier("name"),
Name: "name",
SemanticTokenModifiers: lang.SemanticTokenModifiers{"name"},
},
},
Body: &schema.BodySchema{
Expand All @@ -553,33 +566,34 @@ func TestDecoder_SemanticTokensInFile_customModifiers(t *testing.T) {
Expr: schema.LiteralTypeOnly(cty.Number),
},
"source": {
Expr: schema.LiteralTypeOnly(cty.String),
IsDeprecated: true,
Expr: schema.LiteralTypeOnly(cty.String),
IsDeprecated: true,
SemanticTokenModifiers: lang.SemanticTokenModifiers{lang.TokenModifierDependent},
},
},
},
},
"resource": {
SemanticTokenModifier: lang.SemanticTokenModifier("resource"),
SemanticTokenModifiers: lang.SemanticTokenModifiers{"resource"},
Labels: []*schema.LabelSchema{
{
Name: "type",
IsDepKey: true,
SemanticTokenModifier: lang.SemanticTokenModifier("type"),
Name: "type",
IsDepKey: true,
SemanticTokenModifiers: lang.SemanticTokenModifiers{"type", lang.TokenModifierDependent},
},
{
Name: "name",
SemanticTokenModifier: lang.SemanticTokenModifier("name"),
Name: "name",
SemanticTokenModifiers: lang.SemanticTokenModifiers{"name"},
},
},
Body: &schema.BodySchema{
Blocks: map[string]*schema.BlockSchema{
"provisioner": {
SemanticTokenModifier: lang.SemanticTokenModifier("provisioner"),
SemanticTokenModifiers: lang.SemanticTokenModifiers{"provisioner"},
Labels: []*schema.LabelSchema{
{
Name: "type",
SemanticTokenModifier: lang.SemanticTokenModifier("type"),
Name: "type",
SemanticTokenModifiers: lang.SemanticTokenModifiers{"type"},
},
},
},
Expand Down Expand Up @@ -658,8 +672,11 @@ resource "vault_auth_backend" "blah" {
},
},
{ // source
Type: lang.TokenAttrName,
Modifiers: []lang.SemanticTokenModifier{},
Type: lang.TokenAttrName,
Modifiers: []lang.SemanticTokenModifier{
lang.SemanticTokenModifier("module"),
lang.TokenModifierDependent,
},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{
Expand Down Expand Up @@ -692,8 +709,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 Expand Up @@ -747,9 +766,9 @@ resource "vault_auth_backend" "blah" {
{ // vault_auth_backend
Type: lang.TokenBlockLabel,
Modifiers: []lang.SemanticTokenModifier{
lang.TokenModifierDependent,
lang.SemanticTokenModifier("resource"),
lang.SemanticTokenModifier("type"),
lang.TokenModifierDependent,
},
Range: hcl.Range{
Filename: "test.tf",
Expand Down
14 changes: 13 additions & 1 deletion lang/semantic_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

type SemanticToken struct {
Type SemanticTokenType
Modifiers []SemanticTokenModifier
Modifiers SemanticTokenModifiers
Range hcl.Range
}

Expand Down Expand Up @@ -49,6 +49,18 @@ var SupportedSemanticTokenTypes = SemanticTokenTypes{

type SemanticTokenModifier string

type SemanticTokenModifiers []SemanticTokenModifier

func (stm SemanticTokenModifiers) Copy() SemanticTokenModifiers {
if stm == nil {
return nil
}

modifiersCopy := make(SemanticTokenModifiers, len(stm))
copy(modifiersCopy, stm)
return modifiersCopy
}

const (
TokenModifierDependent = SemanticTokenModifier("hcl-dependent")
)
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
20 changes: 10 additions & 10 deletions schema/block_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ type BlockSchema struct {
Labels []*LabelSchema
Type BlockType

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

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

newBs := &BlockSchema{
Type: bs.Type,
SemanticTokenModifier: bs.SemanticTokenModifier,
IsDeprecated: bs.IsDeprecated,
MinItems: bs.MinItems,
MaxItems: bs.MaxItems,
Description: bs.Description,
Body: bs.Body.Copy(),
Address: bs.Address.Copy(),
Type: bs.Type,
SemanticTokenModifiers: bs.SemanticTokenModifiers.Copy(),
IsDeprecated: bs.IsDeprecated,
MinItems: bs.MinItems,
MaxItems: bs.MaxItems,
Description: bs.Description,
Body: bs.Body.Copy(),
Address: bs.Address.Copy(),
}

if bs.Labels != nil {
Expand Down
Loading