Skip to content

Commit

Permalink
decoder: Implement hover for Object
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Feb 21, 2023
1 parent 664fb29 commit ad92c09
Show file tree
Hide file tree
Showing 5 changed files with 491 additions and 18 deletions.
5 changes: 0 additions & 5 deletions decoder/expr_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ type Object struct {
pathCtx *PathContext
}

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

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

import (
"context"
"fmt"

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

func (obj Object) HoverAtPos(ctx context.Context, pos hcl.Pos) *lang.HoverData {
eType, ok := obj.expr.(*hclsyntax.ObjectConsExpr)
if !ok {
return nil
}

for _, item := range eType.Items {
attrName, _, ok := getRawObjectAttributeName(item.KeyExpr)
if !ok {
continue
}

aSchema, ok := obj.cons.Attributes[attrName]
if !ok {
// unknown attribute
return nil
}

if item.KeyExpr.Range().ContainsPos(pos) {
itemRng := hcl.RangeBetween(item.KeyExpr.Range(), item.ValueExpr.Range())
content := hoverContentForAttribute(attrName, aSchema)

return &lang.HoverData{
Content: content,
Range: itemRng,
}
}

if item.ValueExpr.Range().ContainsPos(pos) {
expr := newExpression(obj.pathCtx, item.ValueExpr, aSchema.Constraint)
return expr.HoverAtPos(ctx, pos)
}
}

content := ""
hoverData := obj.cons.EmptyHoverData(1)
if hoverData != nil {
content = hoverData.Content.Value
}
content += fmt.Sprintf("_%s_", obj.cons.FriendlyName())
if obj.cons.Description.Value != "" {
content += "\n\n" + obj.cons.Description.Value
}

return &lang.HoverData{
Content: lang.Markdown(content),
Range: eType.Range(),
}
}

func hoverContentForAttribute(name string, aSchema *schema.AttributeSchema) lang.MarkupContent {
value := fmt.Sprintf("**%s** _%s_", name, detailForAttribute(aSchema))
if aSchema.Description.Value != "" {
value += fmt.Sprintf("\n\n%s", aSchema.Description.Value)
}
return lang.MarkupContent{
Kind: lang.MarkdownKind,
Value: value,
}
}
Loading

0 comments on commit ad92c09

Please sign in to comment.