From 26d8bc2d93ace3fac908388e53dfd4d982f7faa4 Mon Sep 17 00:00:00 2001 From: njucz Date: Tue, 11 Aug 2020 16:38:31 +0800 Subject: [PATCH] prevent block the main goroutine when waiting user's action --- langserver/handlers/did_open.go | 61 ++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/langserver/handlers/did_open.go b/langserver/handlers/did_open.go index dc528ca12..8e4ce8ea6 100644 --- a/langserver/handlers/did_open.go +++ b/langserver/handlers/did_open.go @@ -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())