From dc815fd4f5a5908f5385b585567a20b08f8259e7 Mon Sep 17 00:00:00 2001 From: Daniel Banck Date: Tue, 12 Apr 2022 16:53:29 +0200 Subject: [PATCH] add links for dependecy key attributes --- decoder/links.go | 24 ++++++++++++++++++++++++ decoder/links_test.go | 6 +++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/decoder/links.go b/decoder/links.go index ea8217a7..2bbadd3c 100644 --- a/decoder/links.go +++ b/decoder/links.go @@ -28,6 +28,30 @@ func (d *PathDecoder) linksInBody(body hcl.Body, bodySchema *schema.BodySchema) content := decodeBody(body, bodySchema) links := make([]lang.Link, 0) + // Attributes acting as dependency keys can have links associated + for _, attr := range content.Attributes { + aSchema, ok := bodySchema.Attributes[attr.Name] + + if !ok { + if bodySchema.AnyAttribute == nil { + // skip unknown attribute + continue + } + aSchema = bodySchema.AnyAttribute + } + + if aSchema.IsDepKey && bodySchema.DocsLink != nil { + u, err := d.docsURL(bodySchema.DocsLink.URL, "documentLink") + if err == nil { + links = append(links, lang.Link{ + URI: u.String(), + Tooltip: bodySchema.DocsLink.Tooltip, + Range: attr.Expr.Range(), + }) + } + } + } + for _, block := range content.Blocks { blockSchema, ok := bodySchema.Blocks[block.Type] if !ok { diff --git a/decoder/links_test.go b/decoder/links_test.go index fe8f253c..88007ff5 100644 --- a/decoder/links_test.go +++ b/decoder/links_test.go @@ -118,8 +118,8 @@ func TestLinksInFile_json(t *testing.T) { }) _, err := d.LinksInFile("test.tf.json") - unknownFormatErr := &UnknownFileFormatError{} - if !errors.As(err, &unknownFormatErr) { - t.Fatal("expected UnknownFileFormatError for JSON body") + noSchemaErr := &NoSchemaError{} + if !errors.As(err, &noSchemaErr) { + t.Fatal("expected NoSchemaError for JSON body") } }