Skip to content

Commit

Permalink
Provide relevant HoverURL in hover over block types (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored Jun 3, 2021
1 parent 81056fd commit 283182c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
13 changes: 11 additions & 2 deletions decoder/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (d *Decoder) hoverAtPos(body *hclsyntax.Body, bodySchema *schema.BodySchema

if block.TypeRange.ContainsPos(pos) {
return &lang.HoverData{
Content: hoverContentForBlock(block.Type, bSchema),
Content: d.hoverContentForBlock(block.Type, bSchema),
Range: block.TypeRange,
}, nil
}
Expand Down Expand Up @@ -198,11 +198,20 @@ func hoverContentForAttribute(name string, schema *schema.AttributeSchema) lang.
}
}

func hoverContentForBlock(bType string, schema *schema.BlockSchema) lang.MarkupContent {
func (d *Decoder) hoverContentForBlock(bType string, schema *schema.BlockSchema) lang.MarkupContent {
value := fmt.Sprintf("**%s** _%s_", bType, detailForBlock(schema))
if schema.Description.Value != "" {
value += fmt.Sprintf("\n\n%s", schema.Description.Value)
}

if schema.Body.HoverURL != "" {
u, err := d.docsURL(schema.Body.HoverURL, "documentHover")
if err == nil {
value += fmt.Sprintf("\n\n[`%s` on %s](%s)",
bType, u.Hostname(), u.String())
}
}

return lang.MarkupContent{
Kind: lang.MarkdownKind,
Value: value,
Expand Down
38 changes: 37 additions & 1 deletion decoder/hover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,9 @@ func TestDecoder_HoverAtPos_URL(t *testing.T) {
}
blockSchema := &schema.BlockSchema{
Labels: resourceLabelSchema,
Description: lang.Markdown("My special block"),
Description: lang.Markdown("My food block"),
Body: &schema.BodySchema{
HoverURL: "https://en.wikipedia.org/wiki/Food",
Attributes: map[string]*schema.AttributeSchema{
"any_attr": {Expr: schema.LiteralTypeOnly(cty.Number)},
},
Expand Down Expand Up @@ -526,6 +527,41 @@ Sushi, the Rolls-Rice of Japanese cuisine
},
},
},
{
"",
`myblock "ramen" "tonkotsu" {
any_attr = 42
}
`,
hcl.Pos{
Line: 1,
Column: 2,
Byte: 1,
},
&lang.HoverData{
Content: lang.MarkupContent{
Value: `**myblock** _Block_
My food block
[` + "`myblock`" + ` on en.wikipedia.org](https://en.wikipedia.org/wiki/Food)`,
Kind: lang.MarkdownKind,
},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{
Line: 1,
Column: 1,
Byte: 0,
},
End: hcl.Pos{
Line: 1,
Column: 8,
Byte: 7,
},
},
},
},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("%d-%s", i, tc.name), func(t *testing.T) {
Expand Down

0 comments on commit 283182c

Please sign in to comment.