diff --git a/internal/langserver/handlers/complete/complete.go b/internal/langserver/handlers/complete/complete.go index 22191888..56466d58 100644 --- a/internal/langserver/handlers/complete/complete.go +++ b/internal/langserver/handlers/complete/complete.go @@ -66,7 +66,9 @@ func CandidatesAtPos(data []byte, filename string, pos hcl.Pos, logger *log.Logg candidateList = append(candidateList, snippets.CodeSampleCandidates(block, editRange)...) } } - } else { + } + // the cursor is not in a block + if block == nil { editRange := lsp.Range{ Start: ilsp.HCLPosToLSP(pos), End: ilsp.HCLPosToLSP(pos), diff --git a/internal/langserver/handlers/complete_test.go b/internal/langserver/handlers/complete_test.go index 145b68b6..9f72661a 100644 --- a/internal/langserver/handlers/complete_test.go +++ b/internal/langserver/handlers/complete_test.go @@ -653,6 +653,53 @@ func TestCompletion_template(t *testing.T) { } } +func TestCompletion_templateInsideOtherResource(t *testing.T) { + tmpDir := TempDir(t) + InitPluginCache(t, tmpDir.Dir()) + + ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{})) + stop := ls.Start(t) + defer stop() + + config, err := os.ReadFile(fmt.Sprintf("./testdata/%s/main.tf", t.Name())) + if err != nil { + t.Fatal(err) + } + + 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: buildReqParamsTextDocument(string(config), tmpDir.URI()), + }) + + rawResponse := ls.Call(t, &langserver.CallRequest{ + Method: "textDocument/completion", + ReqParams: buildReqParamsCompletion(2, 3, tmpDir.URI()), + }) + + var result map[string]interface{} + err = json.Unmarshal(rawResponse.Result, &result) + if err != nil { + t.Fatalf("failed to unmarshal response: %v", err) + } + + items := result["items"].([]interface{}) + if len(items) != 0 { + t.Fatalf("expected 0 items, got %d", len(items)) + } +} + func buildReqParamsCompletion(line int, character int, uri string) string { param := make(map[string]interface{}) textDocument := make(map[string]interface{}) diff --git a/internal/langserver/handlers/testdata/TestCompletion_templateInsideOtherResource/main.tf b/internal/langserver/handlers/testdata/TestCompletion_templateInsideOtherResource/main.tf new file mode 100644 index 00000000..c31e52ef --- /dev/null +++ b/internal/langserver/handlers/testdata/TestCompletion_templateInsideOtherResource/main.tf @@ -0,0 +1,3 @@ +module "avm_res_network_virtualnetwork" { + +} \ No newline at end of file