Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BodySchema: Introduce (dedicated) HoverURL #43

Merged
merged 1 commit into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions decoder/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,8 @@ func (d *Decoder) hoverContentForLabel(i int, block *hclsyntax.Block, bSchema *s
content += "\n\n" + labelSchema.Description.Value
}

if bs.DocsLink != nil {
link := bs.DocsLink
u, err := d.docsURL(link.URL, "documentHover")
if bs.HoverURL != "" {
u, err := d.docsURL(bs.HoverURL, "documentHover")
if err == nil {
content += fmt.Sprintf("\n\n[`%s` on %s](%s)",
value, u.Hostname(), u.String())
Expand Down
138 changes: 138 additions & 0 deletions decoder/hover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,141 @@ func TestDecoder_HoverAtPos_basic(t *testing.T) {
})
}
}

func TestDecoder_HoverAtPos_URL(t *testing.T) {
resourceLabelSchema := []*schema.LabelSchema{
{Name: "type", IsDepKey: true},
{Name: "name"},
}
blockSchema := &schema.BlockSchema{
Labels: resourceLabelSchema,
Description: lang.Markdown("My special block"),
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"any_attr": {Expr: schema.LiteralTypeOnly(cty.Number)},
},
},
DependentBody: map[schema.SchemaKey]*schema.BodySchema{
schema.NewSchemaKey(schema.DependencyKeys{
Labels: []schema.LabelDependent{
{Index: 0, Value: "sushi"},
},
}): {
Detail: "rice, fish etc.",
HoverURL: "https://en.wikipedia.org/wiki/Sushi",
Description: lang.Markdown("Sushi, the Rolls-Rice of Japanese cuisine"),
},
schema.NewSchemaKey(schema.DependencyKeys{
Labels: []schema.LabelDependent{
{Index: 0, Value: "ramen"},
},
}): {
Detail: "noodles, broth etc.",
DocsLink: &schema.DocsLink{
URL: "https://en.wikipedia.org/wiki/Ramen",
Tooltip: "Ramen docs",
},
Description: lang.Markdown("Ramen, a Japanese noodle soup"),
},
},
}
bodySchema := &schema.BodySchema{
Blocks: map[string]*schema.BlockSchema{
"myblock": blockSchema,
},
}

testCases := []struct {
name string
cfg string
pos hcl.Pos
expectedData *lang.HoverData
}{
{
"",
`myblock "sushi" "salmon" {
any_attr = 42
}
`,
hcl.Pos{
Line: 1,
Column: 12,
Byte: 11,
},
&lang.HoverData{
Content: lang.MarkupContent{
Value: "`sushi`" + ` rice, fish etc.

Sushi, the Rolls-Rice of Japanese cuisine

[` + "`sushi`" + ` on en.wikipedia.org](https://en.wikipedia.org/wiki/Sushi)`,
Kind: lang.MarkdownKind,
},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{
Line: 1,
Column: 9,
Byte: 8,
},
End: hcl.Pos{
Line: 1,
Column: 16,
Byte: 15,
},
},
},
},
{
"",
`myblock "ramen" "tonkotsu" {
any_attr = 42
}
`,
hcl.Pos{
Line: 1,
Column: 12,
Byte: 13,
},
&lang.HoverData{
Content: lang.MarkupContent{
Value: "`ramen` noodles, broth etc.\n\nRamen, a Japanese noodle soup",
Kind: lang.MarkdownKind,
},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{
Line: 1,
Column: 9,
Byte: 8,
},
End: hcl.Pos{
Line: 1,
Column: 16,
Byte: 15,
},
},
},
},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("%d-%s", i, tc.name), func(t *testing.T) {
d := NewDecoder()
d.SetSchema(bodySchema)

f, _ := hclsyntax.ParseConfig([]byte(tc.cfg), "test.tf", hcl.InitialPos)
err := d.LoadFile("test.tf", f)
if err != nil {
t.Fatal(err)
}

data, err := d.HoverAtPos("test.tf", tc.pos)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(tc.expectedData, data, ctydebug.CmpOptions); diff != "" {
t.Fatalf("hover data mismatch: %s", diff)
}
})
}
}
8 changes: 8 additions & 0 deletions schema/body_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@ type BodySchema struct {
Detail string
Description lang.MarkupContent

// DocsLink represents a link to docs that will be exposed
// as part of LinksInFile()
DocsLink *DocsLink

// HoverURL represents a URL that will be appended to the end
// of hover data in HoverAtPos(). This can differ from DocsLink,
// but often will match.
HoverURL string

// TODO: Functions
}

Expand Down Expand Up @@ -78,6 +85,7 @@ func (bs *BodySchema) Copy() *BodySchema {
Detail: bs.Detail,
Description: bs.Description,
AnyAttribute: bs.AnyAttribute.Copy(),
HoverURL: bs.HoverURL,
DocsLink: bs.DocsLink.Copy(),
}

Expand Down