Skip to content

Commit

Permalink
decoder: Implement hover for LiteralType
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Mar 15, 2023
1 parent 7d6e2ba commit 680c316
Show file tree
Hide file tree
Showing 3 changed files with 552 additions and 5 deletions.
5 changes: 0 additions & 5 deletions decoder/expr_literal_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ type LiteralType struct {
pathCtx *PathContext
}

func (lt LiteralType) HoverAtPos(ctx context.Context, pos hcl.Pos) *lang.HoverData {
// TODO
return nil
}

func (lt LiteralType) SemanticTokens(ctx context.Context) []lang.SemanticToken {
// TODO
return nil
Expand Down
111 changes: 111 additions & 0 deletions decoder/expr_literal_type_hover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package decoder

import (
"context"

"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty/cty"
)

func (lt LiteralType) HoverAtPos(ctx context.Context, pos hcl.Pos) *lang.HoverData {
typ := lt.cons.Type

if typ == cty.Bool {
return lt.hoverBoolAtPos(ctx, pos)
}

if typ.IsListType() {
expr, ok := lt.expr.(*hclsyntax.TupleConsExpr)
if !ok {
return nil
}

cons := schema.List{
Elem: schema.LiteralType{
Type: typ.ElementType(),
},
}

return newExpression(lt.pathCtx, expr, cons).HoverAtPos(ctx, pos)
}

if typ.IsSetType() {
expr, ok := lt.expr.(*hclsyntax.TupleConsExpr)
if !ok {
return nil
}

cons := schema.Set{
Elem: schema.LiteralType{
Type: typ.ElementType(),
},
}

return newExpression(lt.pathCtx, expr, cons).HoverAtPos(ctx, pos)
}

if typ.IsTupleType() {
expr, ok := lt.expr.(*hclsyntax.TupleConsExpr)
if !ok {
return nil
}

elemTypes := typ.TupleElementTypes()
cons := schema.Tuple{
Elems: make([]schema.Constraint, len(elemTypes)),
}
for i, elemType := range elemTypes {
cons.Elems[i] = schema.LiteralType{
Type: elemType,
}
}

return newExpression(lt.pathCtx, expr, cons).HoverAtPos(ctx, pos)
}

if typ.IsMapType() {
expr, ok := lt.expr.(*hclsyntax.ObjectConsExpr)
if !ok {
return nil
}

cons := schema.Map{
Elem: schema.LiteralType{
Type: typ.ElementType(),
},
}
return newExpression(lt.pathCtx, expr, cons).HoverAtPos(ctx, pos)
}

if typ.IsObjectType() {
expr, ok := lt.expr.(*hclsyntax.ObjectConsExpr)
if !ok {
return nil
}

cons := schema.Object{
Attributes: ctyObjectToObjectAttributes(typ),
}
return newExpression(lt.pathCtx, expr, cons).HoverAtPos(ctx, pos)
}

return nil
}

func (lt LiteralType) hoverBoolAtPos(ctx context.Context, pos hcl.Pos) *lang.HoverData {
expr, ok := lt.expr.(*hclsyntax.LiteralValueExpr)
if !ok {
return nil
}
if expr.Val.Type() != cty.Bool {
return nil
}

return &lang.HoverData{
Content: lang.Markdown(`_bool_`),
Range: expr.Range(),
}
}
Loading

0 comments on commit 680c316

Please sign in to comment.