Skip to content

Commit

Permalink
add a test to prevent showing templates inside other resources
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-henglu committed Aug 16, 2024
1 parent f460516 commit 217a8d5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
4 changes: 3 additions & 1 deletion internal/langserver/handlers/complete/complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
47 changes: 47 additions & 0 deletions internal/langserver/handlers/complete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module "avm_res_network_virtualnetwork" {

}

0 comments on commit 217a8d5

Please sign in to comment.