From 9dd1615f3e7b96a8bd9e01ea64193fbd0933e0ea Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Sun, 7 Nov 2021 16:29:21 +0000 Subject: [PATCH] Introduce go-to-variable from module input name --- go.mod | 4 +- go.sum | 10 ++--- internal/codelens/reference_count.go | 2 +- .../langserver/handlers/go_to_ref_target.go | 14 +++---- .../handlers/go_to_ref_target_test.go | 8 ++-- internal/lsp/location_links.go | 40 ++++++++++++++----- internal/terraform/module/walker.go | 9 +++++ internal/terraform/module/watcher.go | 6 ++- 8 files changed, 61 insertions(+), 32 deletions(-) diff --git a/go.mod b/go.mod index ebcee5105..134e7d723 100644 --- a/go.mod +++ b/go.mod @@ -14,12 +14,12 @@ require ( github.com/hashicorp/go-multierror v1.1.1 github.com/hashicorp/go-uuid v1.0.2 github.com/hashicorp/go-version v1.3.0 - github.com/hashicorp/hcl-lang v0.0.0-20211029211837-70678d4f0419 + github.com/hashicorp/hcl-lang v0.0.0-20211107161646-952b623675aa github.com/hashicorp/hcl/v2 v2.10.1 github.com/hashicorp/terraform-exec v0.15.0 github.com/hashicorp/terraform-json v0.13.0 github.com/hashicorp/terraform-registry-address v0.0.0-20210816115301-cb2034eba045 - github.com/hashicorp/terraform-schema v0.0.0-20211021151419-21dfff199031 + github.com/hashicorp/terraform-schema v0.0.0-20211107162059-760d033e6981 github.com/kylelemons/godebug v1.1.0 // indirect github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5 github.com/mitchellh/cli v1.1.2 diff --git a/go.sum b/go.sum index fd889b754..f7787cd25 100644 --- a/go.sum +++ b/go.sum @@ -191,9 +191,8 @@ github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+l github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= -github.com/hashicorp/hcl-lang v0.0.0-20210803155453-7c098e4940bc/go.mod h1:xzXU6Fn+TWVaZUFxV8CyAsObi2oMgSEFAmLvCx2ArzM= -github.com/hashicorp/hcl-lang v0.0.0-20211029211837-70678d4f0419 h1:4coi4Rt0qgJMrkVxMa/NcwmvX6CnFS5gluVbZElhsWI= -github.com/hashicorp/hcl-lang v0.0.0-20211029211837-70678d4f0419/go.mod h1:NQq9vfyCPpRTPS4L5xeJGxp32qqp83UkDAO37NyBGF8= +github.com/hashicorp/hcl-lang v0.0.0-20211107161646-952b623675aa h1:qvVIR53Pp/qs9nOGHggtvJBn4r3B7Ppzj9PZjEp7Sak= +github.com/hashicorp/hcl-lang v0.0.0-20211107161646-952b623675aa/go.mod h1:0W3+VP07azoS+fCX5hWk1KxwHnqf1s9J7oBg2cFXm1c= github.com/hashicorp/hcl/v2 v2.10.1 h1:h4Xx4fsrRE26ohAk/1iGF/JBqRQbyUqu5Lvj60U54ys= github.com/hashicorp/hcl/v2 v2.10.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg= github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= @@ -207,8 +206,8 @@ github.com/hashicorp/terraform-json v0.13.0/go.mod h1:y5OdLBCT+rxbwnpxZs9kGL7R9E github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896/go.mod h1:bzBPnUIkI0RxauU8Dqo+2KrZZ28Cf48s8V6IHt3p4co= github.com/hashicorp/terraform-registry-address v0.0.0-20210816115301-cb2034eba045 h1:R/I8ofvXuPcTNoc//N4ruvaHGZcShI/VuU2iXo875Lo= github.com/hashicorp/terraform-registry-address v0.0.0-20210816115301-cb2034eba045/go.mod h1:anRyJbe12BZscpFgaeGu9gH12qfdBP094LYFtuAFzd4= -github.com/hashicorp/terraform-schema v0.0.0-20211021151419-21dfff199031 h1:HwQTGktZUBlRENcwb9MKm+cfqNcv0C5vagJnjKAqNKY= -github.com/hashicorp/terraform-schema v0.0.0-20211021151419-21dfff199031/go.mod h1:DlxWg9rEgltUs+FD5ElEgBoP985cjAeA9YHcYliAGVg= +github.com/hashicorp/terraform-schema v0.0.0-20211107162059-760d033e6981 h1:F8o3nrh47NIeCGy0PI+nFfwHyjFZzlPoiQThVj9L5oo= +github.com/hashicorp/terraform-schema v0.0.0-20211107162059-760d033e6981/go.mod h1:lwRQZEMORJTdDslW336tYMlwKDqmTLjB8vL4EbIHbiY= github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKLsbzeOsfXmKNpr3GiT18XAblV0BjCbzL8KQAMZGa0= github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734/go.mod h1:kNDNcF7sN4DocDLBkQYz73HGKwN1ANB1blq4lIYLYvg= github.com/huandu/xstrings v1.3.2 h1:L18LIDzqlW6xN2rEkpdV8+oL/IXWJ1APd+vsdYy4Wdw= @@ -383,7 +382,6 @@ github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1 github.com/zclconf/go-cty v1.1.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s= github.com/zclconf/go-cty v1.2.0/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8= github.com/zclconf/go-cty v1.8.0/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk= -github.com/zclconf/go-cty v1.9.0/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk= github.com/zclconf/go-cty v1.9.1/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk= github.com/zclconf/go-cty v1.10.0 h1:mp9ZXQeIcN8kAwuqorjH+Q+njbJKjLrvB2yIh4q7U+0= github.com/zclconf/go-cty v1.10.0/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk= diff --git a/internal/codelens/reference_count.go b/internal/codelens/reference_count.go index ede2f50fa..e4670347d 100644 --- a/internal/codelens/reference_count.go +++ b/internal/codelens/reference_count.go @@ -48,7 +48,7 @@ func ReferenceCount(showReferencesCmdId string) lang.CodeLensFunc { defRange = refTarget.DefRangePtr } - originCount += len(pathCtx.ReferenceOrigins.Targeting(refTarget)) + originCount += len(pathCtx.ReferenceOrigins.Match(refTarget, path)) } if originCount == 0 { diff --git a/internal/langserver/handlers/go_to_ref_target.go b/internal/langserver/handlers/go_to_ref_target.go index d6f91333a..30be5f4fc 100644 --- a/internal/langserver/handlers/go_to_ref_target.go +++ b/internal/langserver/handlers/go_to_ref_target.go @@ -3,6 +3,7 @@ package handlers import ( "context" + "github.com/hashicorp/hcl-lang/lang" lsctx "github.com/hashicorp/terraform-ls/internal/context" ilsp "github.com/hashicorp/terraform-ls/internal/lsp" lsp "github.com/hashicorp/terraform-ls/internal/protocol" @@ -24,20 +25,19 @@ func (svc *service) GoToReferenceTarget(ctx context.Context, params lsp.TextDocu return nil, err } - d, err := svc.decoderForDocument(ctx, doc) - if err != nil { - return nil, err - } - fPos, err := ilsp.FilePositionFromDocumentPosition(params, doc) if err != nil { return nil, err } - target, err := d.ReferenceTargetForOriginAtPos(doc.Filename(), fPos.Position()) + path := lang.Path{ + Path: doc.Dir(), + LanguageID: doc.LanguageID(), + } + targets, err := svc.decoder.ReferenceTargetsForOriginAtPos(path, doc.Filename(), fPos.Position()) if err != nil { return nil, err } - return ilsp.RefTargetToLocationLink(target, cc.TextDocument.Declaration.LinkSupport), nil + return ilsp.RefTargetsToLocationLinks(targets, cc.TextDocument.Declaration.LinkSupport), nil } diff --git a/internal/langserver/handlers/go_to_ref_target_test.go b/internal/langserver/handlers/go_to_ref_target_test.go index dfc18924e..0abad9dde 100644 --- a/internal/langserver/handlers/go_to_ref_target_test.go +++ b/internal/langserver/handlers/go_to_ref_target_test.go @@ -87,7 +87,7 @@ output "foo" { }`, tmpDir.URI())}, fmt.Sprintf(`{ "jsonrpc": "2.0", "id": 3, - "result": { + "result": [{ "uri":"%s/main.tf", "range": { "start": { @@ -99,7 +99,7 @@ output "foo" { "character": 1 } } - } + }] }`, tmpDir.URI())) } @@ -180,7 +180,7 @@ output "foo" { }`, tmpDir.URI())}, fmt.Sprintf(`{ "jsonrpc": "2.0", "id": 3, - "result": { + "result": [{ "uri":"%s/main.tf", "range": { "start": { @@ -192,6 +192,6 @@ output "foo" { "character": 1 } } - } + }] }`, tmpDir.URI())) } diff --git a/internal/lsp/location_links.go b/internal/lsp/location_links.go index 674ec9af6..977b9a41e 100644 --- a/internal/lsp/location_links.go +++ b/internal/lsp/location_links.go @@ -8,23 +8,41 @@ import ( "github.com/hashicorp/terraform-ls/internal/uri" ) -func RefTargetToLocationLink(target *decoder.ReferenceTarget, linkSupport bool) interface{} { - targetUri := uri.FromPath(filepath.Join(target.Path.Path, target.Range.Filename)) - +func RefTargetsToLocationLinks(targets decoder.ReferenceTargets, linkSupport bool) interface{} { if linkSupport { - locLink := lsp.LocationLink{ - OriginSelectionRange: HCLRangeToLSP(target.OriginRange), - TargetURI: lsp.DocumentURI(targetUri), - TargetRange: HCLRangeToLSP(target.Range), + links := make([]lsp.LocationLink, 0) + for _, target := range targets { + links = append(links, refTargetToLocationLink(target)) } + return links + } - if target.DefRangePtr != nil { - locLink.TargetSelectionRange = HCLRangeToLSP(*target.DefRangePtr) - } + locations := make([]lsp.Location, 0) + for _, target := range targets { + locations = append(locations, refTargetToLocation(target)) + } + return locations +} + +func refTargetToLocationLink(target *decoder.ReferenceTarget) lsp.LocationLink { + targetUri := uri.FromPath(filepath.Join(target.Path.Path, target.Range.Filename)) + + locLink := lsp.LocationLink{ + OriginSelectionRange: HCLRangeToLSP(target.OriginRange), + TargetURI: lsp.DocumentURI(targetUri), + TargetRange: HCLRangeToLSP(target.Range), + } - return locLink + if target.DefRangePtr != nil { + locLink.TargetSelectionRange = HCLRangeToLSP(*target.DefRangePtr) } + return locLink +} + +func refTargetToLocation(target *decoder.ReferenceTarget) lsp.Location { + targetUri := uri.FromPath(filepath.Join(target.Path.Path, target.Range.Filename)) + return lsp.Location{ URI: lsp.DocumentURI(targetUri), Range: HCLRangeToLSP(target.Range), diff --git a/internal/terraform/module/walker.go b/internal/terraform/module/walker.go index 38bbf4ff9..80a312e78 100644 --- a/internal/terraform/module/walker.go +++ b/internal/terraform/module/walker.go @@ -252,7 +252,16 @@ func (w *Walker) walk(ctx context.Context, rootPath string) error { if err != nil { return err } + err = w.modMgr.EnqueueModuleOp(dir, op.OpTypeDecodeReferenceTargets, nil) + if err != nil { + return err + } + err = w.modMgr.EnqueueModuleOp(dir, op.OpTypeDecodeReferenceOrigins, nil) + if err != nil { + return err + } } + if dataDir.PluginLockFilePath != "" { err = w.modMgr.EnqueueModuleOp(dir, op.OpTypeObtainSchema, nil) if err != nil { diff --git a/internal/terraform/module/watcher.go b/internal/terraform/module/watcher.go index 657e26f83..453fc358a 100644 --- a/internal/terraform/module/watcher.go +++ b/internal/terraform/module/watcher.go @@ -246,9 +246,9 @@ func decodeCalledModulesFunc(modMgr ModuleManager, w Watcher, modPath string) De modMgr.AddModule(mc.Path) modMgr.EnqueueModuleOpWait(mc.Path, op.OpTypeParseModuleConfiguration) + modMgr.EnqueueModuleOpWait(mc.Path, op.OpTypeLoadModuleMetadata) modMgr.EnqueueModuleOp(mc.Path, op.OpTypeParseVariables, nil) - modMgr.EnqueueModuleOp(mc.Path, op.OpTypeLoadModuleMetadata, nil) modMgr.EnqueueModuleOp(mc.Path, op.OpTypeDecodeReferenceTargets, nil) modMgr.EnqueueModuleOp(mc.Path, op.OpTypeDecodeReferenceOrigins, nil) @@ -256,6 +256,10 @@ func decodeCalledModulesFunc(modMgr ModuleManager, w Watcher, modPath string) De w.AddModule(mc.Path) } } + + // reparse references to module variables etc. + modMgr.EnqueueModuleOp(modPath, op.OpTypeDecodeReferenceTargets, nil) + modMgr.EnqueueModuleOp(modPath, op.OpTypeDecodeReferenceOrigins, nil) } }