Skip to content

Commit

Permalink
prevent block the main goroutine when waiting user's action
Browse files Browse the repository at this point in the history
  • Loading branch information
njuCZ committed Aug 11, 2020
1 parent d9b4b44 commit 26d8bc2
Showing 1 changed file with 34 additions and 27 deletions.
61 changes: 34 additions & 27 deletions langserver/handlers/did_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,40 +44,47 @@ func (lh *logHandler) TextDocumentDidOpen(ctx context.Context, params lsp.DidOpe
lh.logger.Printf("walker has not finished walking yet, data may be inaccurate for %s", f.FullPath())
} else if len(candidates) == 0 {
// TODO: Only notify once per f.Dir() per session
msg := fmt.Sprintf("No root module found for %q."+
" Functionality may be limited."+
// Unfortunately we can't be any more specific wrt where
// because we don't gather "init-able folders" in any way
" You may need to run terraform init"+
" and reload your editor.", readableDir)
resp, _ := jrpc2.PushCall(ctx, "window/showMessageRequest", lsp.ShowMessageRequestParams{
Type: lsp.MTWarning,
Message: msg,
Actions: []lsp.MessageActionItem{
{
Title: "run `terraform init`",
go func() {
msg := fmt.Sprintf("No root module found for %q."+
" Functionality may be limited."+
// Unfortunately we can't be any more specific wrt where
// because we don't gather "init-able folders" in any way
" You may need to run terraform init"+
" and reload your editor.", readableDir)
resp, err := jrpc2.PushCall(ctx, "window/showMessageRequest", lsp.ShowMessageRequestParams{
Type: lsp.MTWarning,
Message: msg,
Actions: []lsp.MessageActionItem{
{
Title: "run `terraform init`",
},
},
},
})
var action lsp.MessageActionItem
if err := resp.UnmarshalResult(&action); err != nil {
return err
}
if action.Title == "run `terraform init`" {
rmm, err := lsctx.RootModuleManager(ctx)
})
if err != nil {
return err
lh.logger.Printf("%+v", err)
return
}
rm, err := rmm.NewPotentialRootModule(f.Dir())
if err != nil {
return err
var action lsp.MessageActionItem
if err := resp.UnmarshalResult(&action); err != nil {
lh.logger.Printf("unmarshal MessageActionItem: %+v", err)
return
}
go func() {
if action.Title == "run `terraform init`" {
rmm, err := lsctx.RootModuleManager(ctx)
if err != nil {
lh.logger.Printf("%+v", err)
return
}
rm, err := rmm.NewPotentialRootModule(f.Dir())
if err != nil {
lh.logger.Printf("%+v", err)
return
}
if err := rm.ExecuteTerraformInit(ctx); err != nil {
lh.logger.Printf("failed to execute `terraform init`: %+v", err)
}
}()
}
}
}()
}
if len(candidates) > 1 {
candidateDir := humanReadablePath(rootDir, candidates[0].Path())
Expand Down

0 comments on commit 26d8bc2

Please sign in to comment.