-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
decoder: Implement hover for LiteralType
- Loading branch information
1 parent
7d6e2ba
commit 680c316
Showing
3 changed files
with
552 additions
and
5 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,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(), | ||
} | ||
} |
Oops, something went wrong.