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

Introduce go-to-variable from module input name #700

Merged
merged 12 commits into from
Nov 23, 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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ jobs:
run: make fmt
-
name: Run tests
run: go test -cover -covermode=atomic -race ./...
run: go test -cover -covermode=atomic -timeout=5m -race ./...
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-20211123142056-191cd51dec5b
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-20211118125251-a89436f69539
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
11 changes: 5 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ 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-20211118124824-da3a292c5d7a/go.mod h1:0W3+VP07azoS+fCX5hWk1KxwHnqf1s9J7oBg2cFXm1c=
github.com/hashicorp/hcl-lang v0.0.0-20211123142056-191cd51dec5b h1:UircH3n8tUnq881dS1EV/m+ZcIKjZq9jzl0M65PAdWU=
github.com/hashicorp/hcl-lang v0.0.0-20211123142056-191cd51dec5b/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 +207,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-20211118125251-a89436f69539 h1:cKcbX33DsyhYo6niqnNuJxU/rj+U3ix/g+T6HevO3C8=
github.com/hashicorp/terraform-schema v0.0.0-20211118125251-a89436f69539/go.mod h1:8AxXLNebdxejfusRC5/sYkL7bx4ZpY1ZUjLOXMix3TM=
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 +383,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
24 changes: 21 additions & 3 deletions internal/codelens/reference_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"sort"

"github.com/hashicorp/hcl-lang/decoder"
"github.com/hashicorp/hcl-lang/lang"
Expand All @@ -17,12 +18,17 @@ func ReferenceCount(showReferencesCmdId string) lang.CodeLensFunc {
return func(ctx context.Context, path lang.Path, file string) ([]lang.CodeLens, error) {
lenses := make([]lang.CodeLens, 0)

pathCtx, err := decoder.PathCtx(ctx)
localCtx, err := decoder.PathCtx(ctx)
if err != nil {
return nil, err
}

refTargets := pathCtx.ReferenceTargets.OutermostInFile(file)
pathReader, err := decoder.PathReaderFromContext(ctx)
if err != nil {
return nil, err
}

refTargets := localCtx.ReferenceTargets.OutermostInFile(file)
if err != nil {
return nil, err
}
Expand All @@ -48,7 +54,14 @@ func ReferenceCount(showReferencesCmdId string) lang.CodeLensFunc {
defRange = refTarget.DefRangePtr
}

originCount += len(pathCtx.ReferenceOrigins.Targeting(refTarget))
paths := pathReader.Paths(ctx)
for _, p := range paths {
pathCtx, err := pathReader.PathContext(p)
if err != nil {
continue
}
originCount += len(pathCtx.ReferenceOrigins.Match(p, refTarget, path))
}
}

if originCount == 0 {
Expand All @@ -74,6 +87,11 @@ func ReferenceCount(showReferencesCmdId string) lang.CodeLensFunc {
},
})
}

sort.SliceStable(lenses, func(i, j int) bool {
return lenses[i].Range.Start.Byte < lenses[j].Range.Start.Byte
})

return lenses, nil
}
}
Expand Down
34 changes: 30 additions & 4 deletions internal/decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"context"

"github.com/hashicorp/hcl-lang/decoder"
"github.com/hashicorp/hcl-lang/reference"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/terraform-ls/internal/codelens"
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
"github.com/hashicorp/terraform-ls/internal/state"
"github.com/hashicorp/terraform-ls/internal/terraform/ast"
tfschema "github.com/hashicorp/terraform-schema/schema"
)

Expand All @@ -26,11 +28,24 @@ func modulePathContext(mod *state.Module, schemaReader state.SchemaReader, modRe

pathCtx := &decoder.PathContext{
Schema: schema,
ReferenceOrigins: mod.RefOrigins,
ReferenceTargets: mod.RefTargets,
ReferenceOrigins: make(reference.Origins, 0),
ReferenceTargets: make(reference.Targets, 0),
Files: make(map[string]*hcl.File, 0),
}

for _, origin := range mod.RefOrigins {
if ast.IsModuleFilename(origin.OriginRange().Filename) {
pathCtx.ReferenceOrigins = append(pathCtx.ReferenceOrigins, origin)
}
}
for _, target := range mod.RefTargets {
if target.RangePtr != nil && ast.IsModuleFilename(target.RangePtr.Filename) {
pathCtx.ReferenceTargets = append(pathCtx.ReferenceTargets, target)
} else if target.RangePtr == nil {
pathCtx.ReferenceTargets = append(pathCtx.ReferenceTargets, target)
}
}

for name, f := range mod.ParsedModuleFiles {
pathCtx.Files[name.String()] = f
}
Expand All @@ -46,11 +61,22 @@ func varsPathContext(mod *state.Module) (*decoder.PathContext, error) {

pathCtx := &decoder.PathContext{
Schema: schema,
ReferenceOrigins: mod.RefOrigins,
ReferenceTargets: mod.RefTargets,
ReferenceOrigins: make(reference.Origins, 0),
ReferenceTargets: make(reference.Targets, 0),
Files: make(map[string]*hcl.File, 0),
}

for _, origin := range mod.RefOrigins {
if ast.IsVarsFilename(origin.OriginRange().Filename) {
pathCtx.ReferenceOrigins = append(pathCtx.ReferenceOrigins, origin)
}
}
for _, target := range mod.RefTargets {
if target.RangePtr != nil && ast.IsVarsFilename(target.RangePtr.Filename) {
pathCtx.ReferenceTargets = append(pathCtx.ReferenceTargets, target)
}
}

for name, f := range mod.ParsedVarsFiles {
pathCtx.Files[name.String()] = f
}
Expand Down
9 changes: 5 additions & 4 deletions internal/langserver/handlers/code_lens.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 @@ -22,12 +23,12 @@ func (svc *service) TextDocumentCodeLens(ctx context.Context, params lsp.CodeLen
return list, err
}

d, err := svc.decoderForDocument(ctx, doc)
if err != nil {
return nil, err
path := lang.Path{
Path: doc.Dir(),
LanguageID: doc.LanguageID(),
}

lenses, err := d.CodeLensesForFile(ctx, doc.Filename())
lenses, err := svc.decoder.CodeLensesForFile(ctx, path, doc.Filename())
if err != nil {
return nil, err
}
Expand Down
159 changes: 159 additions & 0 deletions internal/langserver/handlers/code_lens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package handlers
import (
"encoding/json"
"fmt"
"path/filepath"
"testing"
"time"

"github.com/hashicorp/go-version"
tfjson "github.com/hashicorp/terraform-json"
"github.com/hashicorp/terraform-ls/internal/langserver"
"github.com/hashicorp/terraform-ls/internal/langserver/session"
"github.com/hashicorp/terraform-ls/internal/lsp"
"github.com/hashicorp/terraform-ls/internal/terraform/exec"
"github.com/stretchr/testify/mock"
)
Expand Down Expand Up @@ -195,3 +198,159 @@ output "test" {
]
}`)
}

func TestCodeLens_referenceCount_crossModule(t *testing.T) {
rootModPath, err := filepath.Abs(filepath.Join("testdata", "single-submodule"))
if err != nil {
t.Fatal(err)
}

submodPath := filepath.Join(rootModPath, "application")

rootModUri := lsp.FileHandlerFromDirPath(rootModPath)
submodUri := lsp.FileHandlerFromDirPath(submodPath)

var testSchema tfjson.ProviderSchemas
err = json.Unmarshal([]byte(testModuleSchemaOutput), &testSchema)
if err != nil {
t.Fatal(err)
}

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
TerraformCalls: &exec.TerraformMockCalls{
PerWorkDir: map[string][]*mock.Call{
submodPath: validTfMockCalls(),
rootModPath: validTfMockCalls(),
},
}}))
stop := ls.Start(t)
defer stop()

ls.Call(t, &langserver.CallRequest{
Method: "initialize",
ReqParams: fmt.Sprintf(`{
"capabilities": {
"experimental": {
"showReferencesCommandId": "test.id"
}
},
"rootUri": %q,
"processId": 12345
}`, rootModUri.URI())})
ls.Notify(t, &langserver.CallRequest{
Method: "initialized",
ReqParams: "{}",
})
ls.Call(t, &langserver.CallRequest{
Method: "textDocument/didOpen",
ReqParams: fmt.Sprintf(`{
"textDocument": {
"version": 0,
"languageId": "terraform",
"text": %q,
"uri": "%s/main.tf"
}
}`, `variable "environment_name" {
type = string
}
variable "app_prefix" {
type = string
}
variable "instances" {
type = number
}
`, submodUri.URI())})
// TODO remove once we support synchronous dependent tasks
// See https://github.com/hashicorp/terraform-ls/issues/719
time.Sleep(2 * time.Second)
ls.CallAndExpectResponse(t, &langserver.CallRequest{
Method: "textDocument/codeLens",
ReqParams: fmt.Sprintf(`{
"textDocument": {
"uri": "%s/main.tf"
}
}`, submodUri.URI()),
}, `{
"jsonrpc": "2.0",
"id": 3,
"result": [
{
"range": {
"start": {
"line": 0,
"character": 0
},
"end": {
"line": 2,
"character": 1
}
},
"command": {
"title": "1 reference",
"command": "test.id",
"arguments": [
{
"line": 0,
"character": 13
},
{
"includeDeclaration": false
}
]
}
},
{
"range": {
"start": {
"line": 4,
"character": 0
},
"end": {
"line": 6,
"character": 1
}
},
"command": {
"title": "1 reference",
"command": "test.id",
"arguments": [
{
"line": 4,
"character": 10
},
{
"includeDeclaration": false
}
]
}
},
{
"range": {
"start": {
"line": 8,
"character": 0
},
"end": {
"line": 10,
"character": 1
}
},
"command": {
"title": "1 reference",
"command": "test.id",
"arguments": [
{
"line": 8,
"character": 10
},
{
"includeDeclaration": false
}
]
}
}
]
}`)
}
6 changes: 2 additions & 4 deletions internal/langserver/handlers/complete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,11 +717,9 @@ output "test" {
}
}`, mainCfg, tmpDir.URI())})

// module manifest-dependent tasks are scheduled & executed
// asynchronously and we currently have no way of waiting
// for them to complete.
// TODO remove once we support synchronous dependent tasks
time.Sleep(1500 * time.Millisecond)
// See https://github.com/hashicorp/terraform-ls/issues/719
time.Sleep(2 * time.Second)

ls.CallAndExpectResponse(t, &langserver.CallRequest{
Method: "textDocument/completion",
Expand Down
Loading