Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add human readable name to rootmodules command API #332

Merged
merged 4 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions internal/langserver/handlers/command/rootmodules.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ type rootmodulesCommandResponse struct {
}

type rootModuleInfo struct {
URI string `json:"uri"`
URI string `json:"uri"`
Name string `json:"name"`
}

func RootModulesHandler(ctx context.Context, args cmd.CommandArgs) (interface{}, error) {
Expand All @@ -44,11 +45,13 @@ func RootModulesHandler(ctx context.Context, args cmd.CommandArgs) (interface{},
}
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()),
URI: uri.FromPath(candidate.Path()),
Name: candidate.HumanReadablePath(rootDir),
}
}
sort.SliceStable(rootModules, func(i, j int) bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,12 @@ func TestLangServer_workspaceExecuteCommand_rootmodules_basic(t *testing.T) {
"doneLoading": true,
"rootModules": [
{
"uri": %q
"uri": %q,
"name": %q
}
]
}
}`, tmpDir.URI()))
}`, tmpDir.URI(), t.Name()))
}

func TestLangServer_workspaceExecuteCommand_rootmodules_multiple(t *testing.T) {
Expand Down Expand Up @@ -169,15 +170,18 @@ func TestLangServer_workspaceExecuteCommand_rootmodules_multiple(t *testing.T) {
"doneLoading": true,
"rootModules": [
{
"uri": %q
"uri": %q,
"name": %q
},
{
"uri": %q
"uri": %q,
"name": %q
},
{
"uri": %q
"uri": %q,
"name": %q
}
]
}
}`, dev.URI(), prod.URI(), staging.URI()))
}`, dev.URI(), "env/dev", prod.URI(), "env/prod", staging.URI(), "env/staging"))
aeschright marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions internal/langserver/handlers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ func (svc *service) Assigner() (jrpc2.Assigner, error) {
ctx = lsctx.WithRootModuleFinder(ctx, svc.modMgr)
ctx = lsctx.WithRootModuleWalker(ctx, svc.walker)
ctx = lsctx.WithWatcher(ctx, ww)
ctx = lsctx.WithRootDirectory(ctx, &rootDir)

return handle(ctx, req, lh.WorkspaceExecuteCommand)
},
Expand Down
21 changes: 21 additions & 0 deletions internal/terraform/rootmodule/root_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,27 @@ func (rm *rootModule) MatchesPath(path string) bool {
return filepath.Clean(rm.path) == filepath.Clean(path)
}

// HumanReadablePath helps display shorter, but still relevant paths
func (rm *rootModule) HumanReadablePath(rootDir string) string {
if rootDir == "" {
return rm.path
}

// absolute paths can be too long for UI/messages,
// so we just display relative to root dir
relDir, err := filepath.Rel(rootDir, rm.path)
if err != nil {
return rm.path
}

if relDir == "." {
// Name of the root dir is more helpful than "."
return filepath.Base(rootDir)
}

return relDir
}

func (rm *rootModule) UpdateModuleManifest(lockFile File) error {
rm.moduleMu.Lock()
defer rm.moduleMu.Unlock()
Expand Down
1 change: 1 addition & 0 deletions internal/terraform/rootmodule/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type RootModule interface {
IsTerraformAvailable() bool
ExecuteTerraformInit(ctx context.Context) error
Modules() []ModuleRecord
HumanReadablePath(string) string
}

type RootModuleFactory func(context.Context, string) (*rootModule, error)
Expand Down