Skip to content

Commit

Permalink
Introduce go-to-variable from module input name
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Nov 8, 2021
1 parent 8aceec7 commit 9dd1615
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 32 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion internal/codelens/reference_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions internal/langserver/handlers/go_to_ref_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
8 changes: 4 additions & 4 deletions internal/langserver/handlers/go_to_ref_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ output "foo" {
}`, tmpDir.URI())}, fmt.Sprintf(`{
"jsonrpc": "2.0",
"id": 3,
"result": {
"result": [{
"uri":"%s/main.tf",
"range": {
"start": {
Expand All @@ -99,7 +99,7 @@ output "foo" {
"character": 1
}
}
}
}]
}`, tmpDir.URI()))
}

Expand Down Expand Up @@ -180,7 +180,7 @@ output "foo" {
}`, tmpDir.URI())}, fmt.Sprintf(`{
"jsonrpc": "2.0",
"id": 3,
"result": {
"result": [{
"uri":"%s/main.tf",
"range": {
"start": {
Expand All @@ -192,6 +192,6 @@ output "foo" {
"character": 1
}
}
}
}]
}`, tmpDir.URI()))
}
40 changes: 29 additions & 11 deletions internal/lsp/location_links.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
9 changes: 9 additions & 0 deletions internal/terraform/module/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion internal/terraform/module/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,20 @@ 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)

if w != nil {
w.AddModule(mc.Path)
}
}

// reparse references to module variables etc.
modMgr.EnqueueModuleOp(modPath, op.OpTypeDecodeReferenceTargets, nil)
modMgr.EnqueueModuleOp(modPath, op.OpTypeDecodeReferenceOrigins, nil)
}
}

Expand Down

0 comments on commit 9dd1615

Please sign in to comment.