Skip to content

Commit 64beb95

Browse files
committed
gopls/internal/lsp/command: add maybePromptForTelemetry
VS Code collapses notifications when there are multiple notification messages or progress messages. In order to increase the visibility, VS Code can pick the quiet time (when there was no message exchanges for a while and show prompt). This internal command makes the invocation easy. I thought about setting the telemetryPrompt option when VS Code wants gopls to prompt, but that is a configuration change and may cause other progress notifications to get in the way. Change-Id: Id9c18118a27931e41ef96e1648c0e8b6fe27f7ae Reviewed-on: https://go-review.googlesource.com/c/tools/+/530457 Reviewed-by: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Hyang-Ah Hana Kim <[email protected]>
1 parent 1c59c38 commit 64beb95

File tree

8 files changed

+117
-33
lines changed

8 files changed

+117
-33
lines changed

gopls/doc/commands.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,12 @@ Result:
238238
}
239239
```
240240

241+
### **checks for the right conditions, and then prompts**
242+
Identifier: `gopls.maybe_prompt_for_telemetry`
243+
244+
the user to ask if they want to enable Go telemetry uploading. If the user
245+
responds 'Yes', the telemetry mode is set to "on".
246+
241247
### **fetch memory statistics**
242248
Identifier: `gopls.mem_stats`
243249

gopls/internal/lsp/command.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ type commandHandler struct {
6464
params *protocol.ExecuteCommandParams
6565
}
6666

67-
// AddTelemetryCounters implements command.Interface.
67+
func (h *commandHandler) MaybePromptForTelemetry(ctx context.Context) error {
68+
go h.s.maybePromptForTelemetry(ctx, true)
69+
return nil
70+
}
71+
6872
func (*commandHandler) AddTelemetryCounters(_ context.Context, args command.AddTelemetryCountersArgs) error {
6973
if len(args.Names) != len(args.Values) {
7074
return fmt.Errorf("Names and Values must have the same length")

gopls/internal/lsp/command/command_gen.go

Lines changed: 45 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gopls/internal/lsp/command/interface.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ type Interface interface {
195195
// Gopls will prepend "fwd/" to all the counters updated using this command
196196
// to avoid conflicts with other counters gopls collects.
197197
AddTelemetryCounters(context.Context, AddTelemetryCountersArgs) error
198+
199+
// MaybePromptForTelemetry: checks for the right conditions, and then prompts
200+
// the user to ask if they want to enable Go telemetry uploading. If the user
201+
// responds 'Yes', the telemetry mode is set to "on".
202+
MaybePromptForTelemetry(context.Context) error
198203
}
199204

200205
type RunTestsArgs struct {

gopls/internal/lsp/general.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func (s *Server) initialized(ctx context.Context, params *protocol.InitializedPa
241241

242242
// Ask (maybe) about enabling telemetry. Do this asynchronously, as it's OK
243243
// for users to ignore or dismiss the question.
244-
go s.maybePromptForTelemetry(ctx)
244+
go s.maybePromptForTelemetry(ctx, options.TelemetryPrompt)
245245

246246
return nil
247247
}

gopls/internal/lsp/prompt.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,15 @@ func (s *Server) setTelemetryMode(mode string) error {
8181
//
8282
// The actual conditions for prompting are defensive, erring on the side of not
8383
// prompting.
84-
func (s *Server) maybePromptForTelemetry(ctx context.Context) {
84+
// If enabled is false, this will not prompt the user in any condition,
85+
// but will send work progress reports to help testing.
86+
func (s *Server) maybePromptForTelemetry(ctx context.Context, enabled bool) {
8587
if s.Options().VerboseWorkDoneProgress {
8688
work := s.progress.Start(ctx, TelemetryPromptWorkTitle, "Checking if gopls should prompt about telemetry...", nil, nil)
8789
defer work.End(ctx, "Done.")
8890
}
8991

90-
if !s.Options().TelemetryPrompt {
92+
if !enabled { // check this after the work progress message for testing.
9193
return // prompt is disabled
9294
}
9395

gopls/internal/lsp/source/api_json.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gopls/internal/regtest/misc/prompt_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"testing"
1313

1414
"golang.org/x/tools/gopls/internal/lsp"
15+
"golang.org/x/tools/gopls/internal/lsp/command"
1516
"golang.org/x/tools/gopls/internal/lsp/protocol"
1617
. "golang.org/x/tools/gopls/internal/lsp/regtest"
1718
)
@@ -177,3 +178,48 @@ func main() {
177178
})
178179
}
179180
}
181+
182+
// Test that gopls prompts for telemetry only when it is supposed to.
183+
func TestTelemetryPrompt_Conditions2(t *testing.T) {
184+
const src = `
185+
-- go.mod --
186+
module mod.com
187+
188+
go 1.12
189+
-- main.go --
190+
package main
191+
192+
func main() {
193+
}
194+
`
195+
modeFile := filepath.Join(t.TempDir(), "mode")
196+
WithOptions(
197+
Modes(Default), // no need to run this in all modes
198+
EnvVars{
199+
lsp.GoplsConfigDirEnvvar: t.TempDir(),
200+
lsp.FakeTelemetryModefileEnvvar: modeFile,
201+
},
202+
Settings{
203+
// off because we are testing
204+
// if we can trigger the prompt with command.
205+
"telemetryPrompt": false,
206+
},
207+
).Run(t, src, func(t *testing.T, env *Env) {
208+
cmd, err := command.NewMaybePromptForTelemetryCommand("prompt")
209+
if err != nil {
210+
t.Fatal(err)
211+
}
212+
var result error
213+
env.ExecuteCommand(&protocol.ExecuteCommandParams{
214+
Command: cmd.Command,
215+
}, &result)
216+
if result != nil {
217+
t.Fatal(err)
218+
}
219+
expectation := ShownMessageRequest(".*Would you like to enable Go telemetry?")
220+
env.OnceMet(
221+
CompletedWork(lsp.TelemetryPromptWorkTitle, 2, true),
222+
expectation,
223+
)
224+
})
225+
}

0 commit comments

Comments
 (0)