From ecd32218bd7f33fd31c1abbfb74283690bedd093 Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Wed, 27 Nov 2019 14:14:59 -0500 Subject: [PATCH] internal/lsp: make sure CodeAction.Command is a pointer This is causing the "command '' not found" errors that we've been seeing, specifically reported in microsoft/vscode-go#2920. Also, fixed an issue with import organization in single-line files that was caught as a result of this. Change-Id: I2dfedb5d1b8dda976f356b0d6fcd146e53f1a650 Reviewed-on: https://go-review.googlesource.com/c/tools/+/209219 Run-TryBot: Rebecca Stambler Reviewed-by: Heschi Kreinick TryBot-Result: Gobot Gobot --- internal/lsp/cmd/capabilities_test.go | 18 +++++++++++++++++- internal/lsp/code_action.go | 2 +- internal/lsp/protocol/tsprotocol.go | 2 +- internal/lsp/source/format.go | 4 +++- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/internal/lsp/cmd/capabilities_test.go b/internal/lsp/cmd/capabilities_test.go index 68683d281c..9985637723 100644 --- a/internal/lsp/cmd/capabilities_test.go +++ b/internal/lsp/cmd/capabilities_test.go @@ -79,12 +79,28 @@ func TestCapabilities(t *testing.T) { ContentChanges: []protocol.TextDocumentContentChangeEvent{ { Range: nil, - Text: `package main; func main() {}; func main2() {};`, + Text: `package main; func main() { fmt.Println("") }`, }, }, }); err != nil { t.Fatal(err) } + + // Send a code action request to validate expected types. + actions, err := c.Server.CodeAction(ctx, &protocol.CodeActionParams{ + TextDocument: protocol.TextDocumentIdentifier{ + URI: uri, + }, + }) + if err != nil { + t.Fatal(err) + } + for _, action := range actions { + // Validate that an empty command is sent along with import organization responses. + if action.Kind == protocol.SourceOrganizeImports && action.Command != nil { + t.Errorf("unexpected command for import organization") + } + } } func validateCapabilities(result *protocol.InitializeResult) error { diff --git a/internal/lsp/code_action.go b/internal/lsp/code_action.go index 8b007f9b5a..1422b2aa2e 100644 --- a/internal/lsp/code_action.go +++ b/internal/lsp/code_action.go @@ -62,7 +62,7 @@ func (s *Server) codeAction(ctx context.Context, params *protocol.CodeActionPara codeActions = append(codeActions, protocol.CodeAction{ Title: "Tidy", Kind: protocol.SourceOrganizeImports, - Command: protocol.Command{ + Command: &protocol.Command{ Title: "Tidy", Command: "tidy", Arguments: []interface{}{ diff --git a/internal/lsp/protocol/tsprotocol.go b/internal/lsp/protocol/tsprotocol.go index 562ee3e3f8..8f06d02e99 100644 --- a/internal/lsp/protocol/tsprotocol.go +++ b/internal/lsp/protocol/tsprotocol.go @@ -126,7 +126,7 @@ type CodeAction struct { * provides a edit and a command, first the edit is * executed and then the command. */ - Command Command `json:"command,omitempty"` + Command *Command `json:"command,omitempty"` } /** diff --git a/internal/lsp/source/format.go b/internal/lsp/source/format.go index b0851d7cbe..8b47756d3c 100644 --- a/internal/lsp/source/format.go +++ b/internal/lsp/source/format.go @@ -315,7 +315,9 @@ func trimToFirstNonImport(fset *token.FileSet, f *ast.File, src []byte, err erro } end := f.End() if firstDecl != nil { - end = tok.LineStart(fset.Position(firstDecl.Pos()).Line - 1) + if firstDeclLine := fset.Position(firstDecl.Pos()).Line; firstDeclLine > 1 { + end = tok.LineStart(firstDeclLine - 1) + } } // Any errors in the file must be after the part of the file that we care about. switch err := err.(type) {