Skip to content

Commit

Permalink
decoder: Implement semantic tokens for TypeDeclaration
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Jan 24, 2023
1 parent 7d50293 commit a27ed6c
Show file tree
Hide file tree
Showing 2 changed files with 493 additions and 1 deletion.
114 changes: 113 additions & 1 deletion decoder/expr_type_declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,119 @@ func (td TypeDeclaration) HoverAtPos(ctx context.Context, pos hcl.Pos) *lang.Hov
}

func (td TypeDeclaration) SemanticTokens(ctx context.Context) []lang.SemanticToken {
// TODO
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],
}
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,
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,
}
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,
}
tokens = append(tokens, cons.SemanticTokens(ctx)...)
}

return tokens
}
return nil
}

Expand Down
Loading

0 comments on commit a27ed6c

Please sign in to comment.