-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathrootmodules.go
65 lines (55 loc) · 1.78 KB
/
rootmodules.go
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package command
import (
"context"
"fmt"
"sort"
"github.com/creachadair/jrpc2/code"
lsctx "github.com/hashicorp/terraform-ls/internal/context"
"github.com/hashicorp/terraform-ls/internal/langserver/cmd"
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
"github.com/hashicorp/terraform-ls/internal/uri"
)
const rootmodulesCommandResponseVersion = 0
type rootmodulesCommandResponse struct {
ResponseVersion int `json:"responseVersion"`
DoneLoading bool `json:"doneLoading"`
RootModules []rootModuleInfo `json:"rootModules"`
}
type rootModuleInfo struct {
URI string `json:"uri"`
Name string `json:"name"`
}
func RootModulesHandler(ctx context.Context, args cmd.CommandArgs) (interface{}, error) {
walker, err := lsctx.RootModuleWalker(ctx)
if err != nil {
return nil, err
}
fileUri, ok := args.GetString("uri")
if !ok || fileUri == "" {
return nil, fmt.Errorf("%w: expected uri argument to be set", code.InvalidParams.Err())
}
fh := ilsp.FileHandlerFromDocumentURI(lsp.DocumentURI(fileUri))
cf, err := lsctx.RootModuleFinder(ctx)
if err != nil {
return nil, err
}
doneLoading := !walker.IsWalking()
candidates := cf.RootModuleCandidatesByPath(fh.Dir())
rootDir, _ := lsctx.RootDirectory(ctx)
rootModules := make([]rootModuleInfo, len(candidates))
for i, candidate := range candidates {
rootModules[i] = rootModuleInfo{
URI: uri.FromPath(candidate.Path()),
Name: candidate.HumanReadablePath(rootDir),
}
}
sort.SliceStable(rootModules, func(i, j int) bool {
return rootModules[i].URI < rootModules[j].URI
})
return rootmodulesCommandResponse{
ResponseVersion: rootmodulesCommandResponseVersion,
DoneLoading: doneLoading,
RootModules: rootModules,
}, nil
}