forked from hashicorp/terraform-ls
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add workspace-wide symbol navigation (hashicorp#427)
- Loading branch information
1 parent
74bd6b9
commit db5b7aa
Showing
7 changed files
with
230 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
|
||
lsctx "github.com/hashicorp/terraform-ls/internal/context" | ||
"github.com/hashicorp/terraform-ls/internal/decoder" | ||
ilsp "github.com/hashicorp/terraform-ls/internal/lsp" | ||
lsp "github.com/hashicorp/terraform-ls/internal/protocol" | ||
) | ||
|
||
func (h *logHandler) WorkspaceSymbol(ctx context.Context, params lsp.WorkspaceSymbolParams) ([]lsp.SymbolInformation, error) { | ||
var symbols []lsp.SymbolInformation | ||
|
||
mm, err := lsctx.ModuleFinder(ctx) | ||
if err != nil { | ||
return symbols, err | ||
} | ||
|
||
cc, err := lsctx.ClientCapabilities(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
modules := mm.ListModules() | ||
for _, mod := range modules { | ||
d, err := decoder.DecoderForModule(ctx, mod) | ||
if err != nil { | ||
return symbols, err | ||
} | ||
|
||
modSymbols, err := d.Symbols(params.Query) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
symbols = append(symbols, ilsp.SymbolInformation(mod.Path(), modSymbols, | ||
cc.Workspace.WorkspaceClientCapabilities.Symbol)...) | ||
} | ||
|
||
return symbols, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package handlers | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-ls/internal/langserver" | ||
"github.com/hashicorp/terraform-ls/internal/terraform/exec" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
func TestLangServer_workspace_symbol_basic(t *testing.T) { | ||
tmpDir := TempDir(t) | ||
InitPluginCache(t, tmpDir.Dir()) | ||
|
||
ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{ | ||
TerraformCalls: &exec.TerraformMockCalls{ | ||
PerWorkDir: map[string][]*mock.Call{ | ||
tmpDir.Dir(): validTfMockCalls(), | ||
}, | ||
}, | ||
})) | ||
stop := ls.Start(t) | ||
defer stop() | ||
|
||
ls.Call(t, &langserver.CallRequest{ | ||
Method: "initialize", | ||
ReqParams: fmt.Sprintf(`{ | ||
"capabilities": { | ||
"workspace": { | ||
"symbol": { | ||
"symbolKind": { | ||
"valueSet": [ | ||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, | ||
11, 12, 13, 14, 15, 16, 17, 18, | ||
19, 20, 21, 22, 23, 24, 25, 26 | ||
] | ||
}, | ||
"tagSupport": { | ||
"valueSet": [ 1 ] | ||
} | ||
} | ||
} | ||
}, | ||
"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": "provider \"github\" {}", | ||
"uri": "%s/first.tf" | ||
} | ||
}`, tmpDir.URI())}) | ||
ls.Call(t, &langserver.CallRequest{ | ||
Method: "textDocument/didOpen", | ||
ReqParams: fmt.Sprintf(`{ | ||
"textDocument": { | ||
"version": 0, | ||
"languageId": "terraform", | ||
"text": "provider \"google\" {}", | ||
"uri": "%s/second.tf" | ||
} | ||
}`, tmpDir.URI())}) | ||
ls.Call(t, &langserver.CallRequest{ | ||
Method: "textDocument/didOpen", | ||
ReqParams: fmt.Sprintf(`{ | ||
"textDocument": { | ||
"version": 0, | ||
"languageId": "terraform", | ||
"text": "myblock \"custom\" {}", | ||
"uri": "%s/blah/third.tf" | ||
} | ||
}`, tmpDir.URI())}) | ||
|
||
ls.CallAndExpectResponse(t, &langserver.CallRequest{ | ||
Method: "workspace/symbol", | ||
ReqParams: `{ | ||
"query": "" | ||
}`}, fmt.Sprintf(`{ | ||
"jsonrpc": "2.0", | ||
"id": 5, | ||
"result": [ | ||
{ | ||
"name": "provider \"github\"", | ||
"kind": 5, | ||
"location": { | ||
"uri": "%s/first.tf", | ||
"range": { | ||
"start": {"line": 0, "character": 0}, | ||
"end": {"line": 0, "character": 20} | ||
} | ||
} | ||
}, | ||
{ | ||
"name": "provider \"google\"", | ||
"kind": 5, | ||
"location": { | ||
"uri": "%s/second.tf", | ||
"range": { | ||
"start": {"line": 0, "character": 0}, | ||
"end": {"line": 0, "character": 20} | ||
} | ||
} | ||
}, | ||
{ | ||
"name": "myblock \"custom\"", | ||
"kind": 5, | ||
"location": { | ||
"uri": "%s/blah/third.tf", | ||
"range": { | ||
"start": {"line": 0, "character": 0}, | ||
"end": {"line": 0, "character": 19} | ||
} | ||
} | ||
} | ||
] | ||
}`, tmpDir.URI(), tmpDir.URI(), tmpDir.URI())) | ||
|
||
ls.CallAndExpectResponse(t, &langserver.CallRequest{ | ||
Method: "workspace/symbol", | ||
ReqParams: `{ | ||
"query": "myb" | ||
}`}, fmt.Sprintf(`{ | ||
"jsonrpc": "2.0", | ||
"id": 6, | ||
"result": [ | ||
{ | ||
"name": "myblock \"custom\"", | ||
"kind": 5, | ||
"location": { | ||
"uri": "%s/blah/third.tf", | ||
"range": { | ||
"start": {"line": 0, "character": 0}, | ||
"end": {"line": 0, "character": 19} | ||
} | ||
} | ||
} | ||
] | ||
}`, tmpDir.URI())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters