From 283182c2361ce86bea67adf4cc0f6e72c4e6600f Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Thu, 3 Jun 2021 12:16:49 +0100 Subject: [PATCH] Provide relevant HoverURL in hover over block types (#45) --- decoder/hover.go | 13 +++++++++++-- decoder/hover_test.go | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/decoder/hover.go b/decoder/hover.go index a8c70732..89c8fcd9 100644 --- a/decoder/hover.go +++ b/decoder/hover.go @@ -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 } @@ -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, diff --git a/decoder/hover_test.go b/decoder/hover_test.go index 67ee3ace..9afb9406 100644 --- a/decoder/hover_test.go +++ b/decoder/hover_test.go @@ -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)}, }, @@ -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) {