Skip to content

Commit

Permalink
Add human readable name to rootmodules command API (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeschright committed Dec 8, 2020
1 parent 2f69164 commit e7dcfc6
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
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
16 changes: 10 additions & 6 deletions internal/langserver/handlers/execute_command_rootmodules_test.go
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(), filepath.Join("env", "dev"), prod.URI(), filepath.Join("env", "prod"), staging.URI(), filepath.Join("env", "staging")))
}
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 @@ -433,6 +433,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

0 comments on commit e7dcfc6

Please sign in to comment.