-
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.
- Loading branch information
1 parent
9f2f36b
commit 654d10d
Showing
4 changed files
with
469 additions
and
16 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,89 @@ | ||
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 := hoverDataForObjectAttributes(obj.cons.Attributes) | ||
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, | ||
} | ||
} | ||
|
||
func hoverDataForObjectAttributes(attributes schema.ObjectAttributes) string { | ||
if len(attributes) == 0 { | ||
return "" | ||
} | ||
|
||
data := "```\n{\n" | ||
for name, attr := range attributes { | ||
exprData := attr.Constraint.FriendlyName() | ||
// TODO: expand and render any "expandable" constraints | ||
// - Object, Map, List, Set, Tuple | ||
// Maybe introduce schema.Constraint.EmptyHoverData(nestingLevel int) *lang.HoverData | ||
|
||
if attr.IsOptional { | ||
exprData = fmt.Sprintf("optional, %s", exprData) | ||
} | ||
data += fmt.Sprintf("%s%s = %s\n", " ", name, exprData) | ||
} | ||
data += "}\n```\n" | ||
return data | ||
} |
Oops, something went wrong.