diff --git a/go.mod b/go.mod index 4fad63167..7862bdbe9 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/hashicorp/go-uuid v1.0.2 github.com/hashicorp/go-version v1.4.0 github.com/hashicorp/hc-install v0.3.1 - github.com/hashicorp/hcl-lang v0.0.0-20220304112739-b2b314ea7c44 + github.com/hashicorp/hcl-lang v0.0.0-20220314150337-d770b425fb22 github.com/hashicorp/hcl/v2 v2.11.1 github.com/hashicorp/terraform-exec v0.16.0 github.com/hashicorp/terraform-json v0.13.0 diff --git a/go.sum b/go.sum index a87f777f5..f9a03015b 100644 --- a/go.sum +++ b/go.sum @@ -296,8 +296,8 @@ github.com/hashicorp/hc-install v0.3.1/go.mod h1:3LCdWcCDS1gaHC9mhHCGbkYfoY6vdsK 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-20211118124824-da3a292c5d7a/go.mod h1:0W3+VP07azoS+fCX5hWk1KxwHnqf1s9J7oBg2cFXm1c= -github.com/hashicorp/hcl-lang v0.0.0-20220304112739-b2b314ea7c44 h1:R+ZKsdqgXle9KO2pLE6AwXg6IStjfM8wgQY4u0qPtx0= -github.com/hashicorp/hcl-lang v0.0.0-20220304112739-b2b314ea7c44/go.mod h1:vyszbX6YNHCKIaVUhbh3LIZljxwYOtgWCIkhT5zKfjc= +github.com/hashicorp/hcl-lang v0.0.0-20220314150337-d770b425fb22 h1:u++Zu5hfJPSNuHh7cV1QfTItINnEGRMdOvT9KjaDQUQ= +github.com/hashicorp/hcl-lang v0.0.0-20220314150337-d770b425fb22/go.mod h1:vyszbX6YNHCKIaVUhbh3LIZljxwYOtgWCIkhT5zKfjc= github.com/hashicorp/hcl/v2 v2.10.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg= github.com/hashicorp/hcl/v2 v2.11.1 h1:yTyWcXcm9XB0TEkyU/JCRU6rYy4K+mgLtzn2wlrJbcc= github.com/hashicorp/hcl/v2 v2.11.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg= diff --git a/internal/langserver/handlers/complete_test.go b/internal/langserver/handlers/complete_test.go index ca8171186..803aea6d1 100644 --- a/internal/langserver/handlers/complete_test.go +++ b/internal/langserver/handlers/complete_test.go @@ -1231,6 +1231,172 @@ output "test" { }`) } +func TestVarReferenceCompletion_withValidData(t *testing.T) { + tmpDir := TempDir(t) + InitPluginCache(t, tmpDir.Path()) + + variableDecls := `variable "aaa" {} +variable "bbb" {} +variable "ccc" {} +` + f, err := os.Create(filepath.Join(tmpDir.Path(), "variables.tf")) + if err != nil { + t.Fatal(err) + } + _, err = f.WriteString(variableDecls) + if err != nil { + t.Fatal(err) + } + f.Close() + + 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{ + tmpDir.Path(): { + { + Method: "Version", + Repeatability: 1, + Arguments: []interface{}{ + mock.AnythingOfType(""), + }, + ReturnArguments: []interface{}{ + version.Must(version.NewVersion("0.12.0")), + nil, + nil, + }, + }, + { + Method: "GetExecPath", + Repeatability: 1, + ReturnArguments: []interface{}{ + "", + }, + }, + { + Method: "ProviderSchemas", + Repeatability: 1, + Arguments: []interface{}{ + mock.AnythingOfType(""), + }, + ReturnArguments: []interface{}{ + &testSchema, + nil, + }, + }, + }, + }, + }})) + stop := ls.Start(t) + defer stop() + + ls.Call(t, &langserver.CallRequest{ + Method: "initialize", + ReqParams: fmt.Sprintf(`{ + "capabilities": {}, + "rootUri": %q, + "processId": 12345 + }`, tmpDir.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": "output \"test\" {\n value = var.\n}\n", + "uri": "%s/outputs.tf" + } + }`, tmpDir.URI)}) + ls.CallAndExpectResponse(t, &langserver.CallRequest{ + Method: "textDocument/completion", + ReqParams: fmt.Sprintf(`{ + "textDocument": { + "uri": "%s/outputs.tf" + }, + "position": { + "character": 14, + "line": 1 + } + }`, tmpDir.URI)}, `{ + "jsonrpc": "2.0", + "id": 3, + "result": { + "isIncomplete": false, + "items": [ + { + "label": "var.aaa", + "labelDetails": {}, + "kind": 6, + "detail": "dynamic", + "insertTextFormat": 1, + "textEdit": { + "range": { + "start": { + "line": 1, + "character": 10 + }, + "end": { + "line": 1, + "character": 14 + } + }, + "newText": "var.aaa" + } + }, + { + "label": "var.bbb", + "labelDetails": {}, + "kind": 6, + "detail": "dynamic", + "insertTextFormat": 1, + "textEdit": { + "range": { + "start": { + "line": 1, + "character": 10 + }, + "end": { + "line": 1, + "character": 14 + } + }, + "newText": "var.bbb" + } + }, + { + "label": "var.ccc", + "labelDetails": {}, + "kind": 6, + "detail": "dynamic", + "insertTextFormat": 1, + "textEdit": { + "range": { + "start": { + "line": 1, + "character": 10 + }, + "end": { + "line": 1, + "character": 14 + } + }, + "newText": "var.ccc" + } + } + ] + } + }`) +} + func tfExecutor(t *testing.T, workdir, tfVersion string) exec.TerraformExecutor { ctx := context.Background() installDir := filepath.Join(t.TempDir(), "hcinstall")