Skip to content

Commit

Permalink
Implement basic root module response
Browse files Browse the repository at this point in the history
  • Loading branch information
appilon committed Oct 14, 2020
1 parent 04feec1 commit 39c955f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
35 changes: 23 additions & 12 deletions langserver/handlers/execute_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,23 @@ import (

type executeCommandHandler func(context.Context, lsp.ExecuteCommandParams) (interface{}, error)

type rootmoduleCommandResponse struct {
Rootmodule string `json:"rootmodule"`
Initialized bool `json:"initialized"`
}

var executeCommandHandlers = map[string]executeCommandHandler{
"rootmodule": func(ctx context.Context, params lsp.ExecuteCommandParams) (interface{}, error) {
walker, err := lsctx.RootModuleWalker(ctx)
if err != nil {
return nil, err
}

if err := Waiter(func() (bool, error) {
return !walker.IsWalking(), nil
}).Waitf("root module walker is still walking"); err != nil {
return nil, err
}

uri := lsp.DocumentURI(params.Arguments[0].(string))
fh := ilsp.FileHandlerFromDocumentURI(uri)
Expand All @@ -22,21 +37,17 @@ var executeCommandHandlers = map[string]executeCommandHandler{
return nil, err
}

walker, err := lsctx.RootModuleWalker(ctx)
if err != nil {
return nil, err
candidates := cf.RootModuleCandidatesByPath(fh.Dir())
if len(candidates) > 1 {
return nil, fmt.Errorf("%q appears to have multiple candidate root modules", uri)
}

if err := Waiter(func() (bool, error) {
return !walker.IsWalking(), nil
}).Waitf("walker is still walking"); err != nil {
return nil, err
var resp rootmoduleCommandResponse
if len(candidates) == 1 {
resp.Initialized = true
resp.Rootmodule = candidates[0].Path()
}

rootDir, _ := lsctx.RootDirectory(ctx)
readableDir := humanReadablePath(rootDir, f.Dir())
candidates := cf.RootModuleCandidatesByPath(f.Dir())
return nil, nil
return resp, nil
},
}

Expand Down
2 changes: 1 addition & 1 deletion langserver/handlers/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const intializeResponse = `{
"documentSymbolProvider":true,
"documentFormattingProvider":true,
"executeCommandProvider": {
"commands": ["workspaces"]
"commands": ["rootmodule"]
}
}
}
Expand Down
1 change: 0 additions & 1 deletion langserver/handlers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ func (svc *service) Assigner() (jrpc2.Assigner, error) {
return nil, err
}

ctx = lsctx.WithDocumentStorage(ctx, fs)
ctx = lsctx.WithRootDirectory(ctx, &rootDir)
ctx = lsctx.WithRootModuleCandidateFinder(ctx, svc.modMgr)
ctx = lsctx.WithRootModuleWalker(ctx, svc.walker)
Expand Down

0 comments on commit 39c955f

Please sign in to comment.