Skip to content

Commit a5d9e45

Browse files
committed
internal/lsp/source: be consistent about command identifiers
The primary identifier for gopls commands was changed from Command.Name to Command.ID(), but this change was not made in the commands passed back to the client via ExecuteCommandOptions. Fix this, and ensure that our regtests actually check commands against the list of supported commands that has been sent. For golang/go#41985 Change-Id: If566584f157e8a86d26eac6353dbfd772b298cfc Reviewed-on: https://go-review.googlesource.com/c/tools/+/262597 Run-TryBot: Robert Findley <[email protected]> Trust: Robert Findley <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent b29caf7 commit a5d9e45

File tree

5 files changed

+24
-6
lines changed

5 files changed

+24
-6
lines changed

gopls/internal/regtest/codelens_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func main() {
121121
if found.Command.Command == "" {
122122
t.Fatalf("did not find lens %q, got %v", want, lenses)
123123
}
124-
if _, err := env.Editor.Server.ExecuteCommand(env.Ctx, &protocol.ExecuteCommandParams{
124+
if _, err := env.Editor.ExecuteCommand(env.Ctx, &protocol.ExecuteCommandParams{
125125
Command: found.Command.Command,
126126
Arguments: found.Command.Arguments,
127127
}); err != nil {

gopls/internal/regtest/wrappers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func (e *Env) ExecuteCodeLensCommand(path string, cmd *source.Command) {
283283
if !found {
284284
e.T.Fatalf("found no command with the ID %s", cmd.ID())
285285
}
286-
if _, err := e.Editor.Server.ExecuteCommand(e.Ctx, &protocol.ExecuteCommandParams{
286+
if _, err := e.Editor.ExecuteCommand(e.Ctx, &protocol.ExecuteCommandParams{
287287
Command: lens.Command.Command,
288288
Arguments: lens.Command.Arguments,
289289
}); err != nil {

internal/lsp/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (s *Server) executeCommand(ctx context.Context, params *protocol.ExecuteCom
3434
}
3535
var match bool
3636
for _, name := range s.session.Options().SupportedCommands {
37-
if command.Name == name {
37+
if command.ID() == name {
3838
match = true
3939
break
4040
}

internal/lsp/fake/editor.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ func (e *Editor) codeAction(ctx context.Context, path string, rng *protocol.Rang
713713
// Execute any commands. The specification says that commands are
714714
// executed after edits are applied.
715715
if action.Command != nil {
716-
if _, err := e.Server.ExecuteCommand(ctx, &protocol.ExecuteCommandParams{
716+
if _, err := e.ExecuteCommand(ctx, &protocol.ExecuteCommandParams{
717717
Command: action.Command.Command,
718718
Arguments: action.Command.Arguments,
719719
}); err != nil {
@@ -724,6 +724,24 @@ func (e *Editor) codeAction(ctx context.Context, path string, rng *protocol.Rang
724724
return nil
725725
}
726726

727+
func (e *Editor) ExecuteCommand(ctx context.Context, params *protocol.ExecuteCommandParams) (interface{}, error) {
728+
if e.Server == nil {
729+
return nil, nil
730+
}
731+
var match bool
732+
// Ensure that this command was actually listed as a supported command.
733+
for _, command := range e.serverCapabilities.ExecuteCommandProvider.Commands {
734+
if command == params.Command {
735+
match = true
736+
break
737+
}
738+
}
739+
if !match {
740+
return nil, fmt.Errorf("unsupported command %q", params.Command)
741+
}
742+
return e.Server.ExecuteCommand(ctx, params)
743+
}
744+
727745
func convertEdits(protocolEdits []protocol.TextEdit) []Edit {
728746
var edits []Edit
729747
for _, lspEdit := range protocolEdits {
@@ -785,7 +803,7 @@ func (e *Editor) RunGenerate(ctx context.Context, dir string) error {
785803
Command: source.CommandGenerate.ID(),
786804
Arguments: jsonArgs,
787805
}
788-
if _, err := e.Server.ExecuteCommand(ctx, params); err != nil {
806+
if _, err := e.ExecuteCommand(ctx, params); err != nil {
789807
return fmt.Errorf("running generate: %v", err)
790808
}
791809
// Unfortunately we can't simply poll the workdir for file changes here,

internal/lsp/source/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func DefaultOptions() *Options {
6868
optionsOnce.Do(func() {
6969
var commands []string
7070
for _, c := range Commands {
71-
commands = append(commands, c.Name)
71+
commands = append(commands, c.ID())
7272
}
7373
defaultOptions = &Options{
7474
ClientOptions: ClientOptions{

0 commit comments

Comments
 (0)