-
Notifications
You must be signed in to change notification settings - Fork 26
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 TypeDeclaration
- Loading branch information
1 parent
cadf2cb
commit c1f53ff
Showing
3 changed files
with
522 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,129 @@ | ||
package decoder | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/hcl-lang/lang" | ||
"github.com/hashicorp/hcl/v2/hclsyntax" | ||
) | ||
|
||
func (td TypeDeclaration) SemanticTokens(ctx context.Context) []lang.SemanticToken { | ||
switch eType := td.expr.(type) { | ||
case *hclsyntax.ScopeTraversalExpr: | ||
if len(eType.Traversal) != 1 { | ||
return []lang.SemanticToken{} | ||
} | ||
|
||
if isPrimitiveTypeDeclaration(eType.Traversal.RootName()) { | ||
return []lang.SemanticToken{ | ||
{ | ||
Type: lang.TokenTypePrimitive, | ||
Modifiers: []lang.SemanticTokenModifier{}, | ||
Range: eType.Range(), | ||
}, | ||
} | ||
} | ||
case *hclsyntax.FunctionCallExpr: | ||
if isSingleArgTypeName(eType.Name) { | ||
tokens := make([]lang.SemanticToken, 0) | ||
|
||
tokens = append(tokens, lang.SemanticToken{ | ||
Type: lang.TokenTypeCapsule, | ||
Modifiers: []lang.SemanticTokenModifier{}, | ||
Range: eType.NameRange, | ||
}) | ||
|
||
if len(eType.Args) == 0 { | ||
return tokens | ||
} | ||
|
||
if len(eType.Args) == 1 { | ||
cons := TypeDeclaration{ | ||
expr: eType.Args[0], | ||
pathCtx: td.pathCtx, | ||
} | ||
tokens = append(tokens, cons.SemanticTokens(ctx)...) | ||
|
||
return tokens | ||
} | ||
|
||
return []lang.SemanticToken{} | ||
} | ||
|
||
if eType.Name == "object" { | ||
tokens := make([]lang.SemanticToken, 0) | ||
tokens = append(tokens, lang.SemanticToken{ | ||
Type: lang.TokenTypeCapsule, | ||
Modifiers: []lang.SemanticTokenModifier{}, | ||
Range: eType.NameRange, | ||
}) | ||
|
||
if len(eType.Args) == 0 { | ||
return tokens | ||
} | ||
|
||
if len(eType.Args) == 1 { | ||
objExpr, isObject := eType.Args[0].(*hclsyntax.ObjectConsExpr) | ||
if !isObject { | ||
return []lang.SemanticToken{} | ||
} | ||
|
||
cons := TypeDeclaration{ | ||
expr: objExpr, | ||
pathCtx: td.pathCtx, | ||
insideObject: true, | ||
} | ||
|
||
tokens = append(tokens, cons.SemanticTokens(ctx)...) | ||
|
||
return tokens | ||
} | ||
|
||
return []lang.SemanticToken{} | ||
} | ||
|
||
// multi argument type (tuple) | ||
if eType.Name == "tuple" { | ||
tokens := make([]lang.SemanticToken, 0) | ||
tokens = append(tokens, lang.SemanticToken{ | ||
Type: lang.TokenTypeCapsule, | ||
Modifiers: []lang.SemanticTokenModifier{}, | ||
Range: eType.NameRange, | ||
}) | ||
|
||
for _, expr := range eType.Args { | ||
cons := TypeDeclaration{ | ||
expr: expr, | ||
pathCtx: td.pathCtx, | ||
} | ||
tokens = append(tokens, cons.SemanticTokens(ctx)...) | ||
} | ||
|
||
return tokens | ||
} | ||
case *hclsyntax.ObjectConsExpr: | ||
if !td.insideObject { | ||
// no tokens in bare object notation w/out object() | ||
return []lang.SemanticToken{} | ||
} | ||
|
||
tokens := make([]lang.SemanticToken, 0) | ||
|
||
for _, item := range eType.Items { | ||
tokens = append(tokens, lang.SemanticToken{ | ||
Type: lang.TokenAttrName, | ||
Modifiers: []lang.SemanticTokenModifier{}, | ||
Range: item.KeyExpr.Range(), | ||
}) | ||
|
||
cons := TypeDeclaration{ | ||
expr: item.ValueExpr, | ||
pathCtx: td.pathCtx, | ||
} | ||
tokens = append(tokens, cons.SemanticTokens(ctx)...) | ||
} | ||
|
||
return tokens | ||
} | ||
return nil | ||
} |
Oops, something went wrong.