Debian Codegraph @

gopls/doc/design/integrating-interactive-refactoring.md

raw symbols SCIP
1 ---
2 title: "Integrating Interactive Refactoring"
3 ---
4
5 This document describes how a language client should interact with `gopls` to support interactive refactorings. This is an experimental feature based on the proposal in [golang/go#76331](https://github.com/golang/go/issues/76331).
6
7 <!-- TODO(hxjiang): add link to x/proposal once available -->
8
9 ## Capabilities
10
11 > The prototype implementation in `gopls` differs slightly from the formal LSP proposal. Specifically, it uses the `experimental` section of client or server capabilities rather than introducing new top-level structures.
12
13 Client Capabilities
14
15 To enable interactive refactoring, the language client must advertise its support for specific input types by adding an `interactiveResolve` object with an `inputTypes` field to the `experimental` section of its client capabilities.
16
17 The value of `inputTypes` should be a `[]string` containing the types of input UI the client can render.
18
19 Example:
20 ```json
21 {
22 // ... existing client capabilities ...
23 "experimental": {
24 "interactiveResolve": {
25 "inputTypes": [
26 "string",
27 "enum",
28 "bool"
29 ]
30 }
31 }
32 }
33 ```
34
35 Common input types used by `gopls` include:
36 - `"string"`: A simple text input.
37 - `"file"`: A file or directory URI picker.
38 - `"bool"`: A boolean checkbox or toggle.
39 - `"number"`: A numeric input.
40 - `"enum"`: A selection from a static set of options.
41 - `"lazyEnum"`: A selection from a dynamic set of options queried on demand.
42 - `"list"`: A homogenous list of items.
43
44 Server Capabilities
45
46 To enable interactive refactoring, the server must advertise its support for resolving commands by adding an `interactiveResolveProvider` object to the `experimental` section of its server capabilities.
47
48 ```json
49 {
50 // ... existing server capabilities ...
51 "experimental": {
52 "interactiveResolveProvider": {
53 "kinds": [
54 "command"
55 ]
56 }
57 }
58 }
59 ```
60
61 The `kinds` field is a `[]string` indicating the supported resolution targets. If `"command"` is present in this list, the client may safely invoke the `command/resolve` method to interactively resolve `ExecuteCommandParams`. Otherwise, the client should not attempt to call this method.
62
63 Additional kinds may be supported in the future by adding them to this array.
64
65 ## Request
66
67 The `command/resolve` request is sent from the client to the server to interactively resolve `ExecuteCommandParams` for a command before execution. This is useful for operations where the server requires additional input from the user that cannot be determined statically.
68
69 When a client receives a `CodeAction` containing a command that supports interactive resolution, it should **not** execute the command immediately via `workspace/executeCommand`. Instead, the client must first send a `command/resolve` request to the server, passing the `ExecuteCommandParams` received in the code action.
70
71 The server responds with `ExecuteCommandParams` that may include a `formFields` property. If `formFields` is present and non-empty, it indicates that the command requires user inputs. The client must not proceed with command execution, but must instead present the questions from `formFields` to the user to collect answers. The `formFields` array contains `FormField` objects, each describing a unique ID, prompt, expected type, whether it is required, and optional default value.
72
73 Once the user provides answers, the client sends another `command/resolve` request to the server, populating the `formAnswers` property in the `ExecuteCommandParams` and omitting the `formFields` property. Answers are linked to their respective questions using the field's unique `id`. The list must not contain duplicate IDs, and each answer's ID must correspond to a field ID defined in `formFields`. The client must include answers for all required fields (where `required` is true). Answers for optional fields may be omitted or included as available.
74
75 Upon receiving `formAnswers`, the server validates the input. If the input is invalid, the server returns `ExecuteCommandParams` with `formFields` again, populating the `error` property on the fields that failed validation (identified by matching `id`). The client can then choose to re-render the UI to display these errors and allow the user to correct their input for a retry, or it may abort the operation entirely.
76
77 This process repeats until the server returns a response where `formFields` is omitted or empty. This signals that the parameters are fully resolved and valid. At this point, the client may proceed to execute the command by calling `workspace/executeCommand` with the finalized `ExecuteCommandParams` containing the valid `formAnswers`.
78
79 Method: `command/resolve`
80
81 Params: `ExecuteCommandParams` defined as follows:
82
83 ```typescript
84 export interface ExecuteCommandParams extends InteractiveParams {
85 // ... original fields (command, arguments) ...
86 }
87
88 export interface InteractiveParams {
89 // FormFields defines the questions and validation errors in previous
90 // answers to the same questions.
91 //
92 // This is a server-to-client field. The language server defines these, and
93 // the client uses them to render the form.
94 //
95 // The interactive phase is considered complete when the server returns a
96 // response where this slice is omitted.
97 formFields?: FormField[];
98
99 // FormAnswers contains the answers for the form questions.
100 //
101 // When sent by the language server, this field is optional and contains the
102 // current or default answers to the questions to support editing previous values.
103 //
104 // When sent by the language client, this field contains the user's answers.
105 // Answers are linked to their respective questions using the field's unique
106 // `id` rather than their array index. The list must not contain duplicate IDs,
107 // and each answer's ID must correspond to a field ID defined in `formFields`.
108 //
109 // The client must include answers for all required fields (where `required`
110 // is true). Answers for optional fields (where `required` is false)
111 // may be omitted if no answer was provided, or included if an answer is available.
112 formAnswers?: FormAnswer[];
113 }
114
115 // FormField describes a single question in a form and its validation state.
116 export interface FormField {
117 // ID is a unique identifier for this field. This key is used as the property
118 // name in FormAnswers to map the user's input back to this specific field.
119 id: string;
120
121 // Description is the text content of the question (the prompt) presented to the user.
122 description: string;
123
124 // Type specifies the data type and validation constraints for the answer.
125 type: FormFieldType;
126
127 // Required specifies whether an answer is required for this field.
128 required: boolean;
129
130 // Default specifies an optional initial value for the answer.
131 // If Type is FormFieldTypeEnum, this value must be present in the enum's values array.
132 default?: any;
133
134 // Error provides a validation message from the language server.
135 // If empty or undefined, the current answer is considered valid.
136 error?: string;
137 }
138
139 // FormAnswer describes a single answer to a FormField, identified by its unique ID.
140 export interface FormAnswer {
141 // The ID of the FormField being answered.
142 id: string;
143
144 // The user's answer value.
145 value: any;
146 }
147
148 // FormFieldTypeString defines a text input.
149 export interface FormFieldTypeString {
150 kind: 'string';
151 }
152
153
154 // FileExistence whether the file denoted by a DocumentURI exists.
155 //
156 // It is a bit set allowing combinations of existence states. For
157 // example, New|Existing allows either state.
158 export enum FileExistence {
159 // New indicates that file has not yet been created.
160 New = 1 << 0,
161 // Existing indicates that the file exists already.
162 Existing = 1 << 1
163 }
164
165 // FileType represents the expected filesystem resource type.
166 //
167 // It is a bit set allowing combinations of file types. For example, Regular|Directory
168 // allows either types.
169 export enum FileType {
170 // Regular indicates the resource could be a regular file.
171 Regular = 1 << 0,
172 // Directory indicates the resource could be a directory.
173 Directory = 1 << 1
174 }
175
176 // FormFieldTypeFile defines an input for a file or directory URI.
177 //
178 // The client determines the best mechanism to collect this information from
179 // the user (e.g., a graphical file picker, a text input with autocomplete, etc).
180 //
181 // The value returned by the client must be a valid "DocumentUri" as defined
182 // in the LSP specification:
183 // https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#documentUri
184 export interface FormFieldTypeFile {
185 kind: 'file';
186
187 // Existence constraint.
188 existence: FileExistence;
189
190 // Type specifies the set of allowed file types (regular file, directory, etc).
191 //
192 // Only applicable against existing file.
193 type: FileType;
194
195 // Filters specifies the allowed file extensions without the leading dot. A file
196 // is valid if it matches any of the extensions (OR logic). e.g. ["png", "jpg"].
197 //
198 // If omitted or empty, no extension filter is applied.
199 filters?: string[];
200 }
201
202
203 // FormFieldTypeBool defines a boolean input.
204 export interface FormFieldTypeBool {
205 kind: 'bool';
206 }
207
208 // FormFieldTypeNumber defines a numeric input.
209 export interface FormFieldTypeNumber {
210 kind: 'number';
211 }
212
213 // FormEnumEntry represents a single option in an enumeration.
214 export interface FormEnumEntry {
215 // Value is the unique string identifier for this option.
216 //
217 // This is the value that will be sent back to the server in
218 // 'FormAnswers' if the user selects this option.
219 value: string;
220
221 // Description is the human-readable label presented to the user.
222 description: string;
223 }
224
225 // FormFieldTypeEnum defines a selection from a set of values.
226 //
227 // Use this type when:
228 // - The number of options is small (e.g., < 20).
229 // - All options are known at the time the form is created.
230 export interface FormFieldTypeEnum {
231 kind: 'enum';
232
233 // Name is an optional identifier for the enum type.
234 name?: string;
235
236 // Entries is the list of allowable options.
237 entries: FormEnumEntry[];
238 }
239
240 // FormFieldTypeLazyEnum defines a selection from a large or dynamic enum entry set.
241 //
242 // Use this type when:
243 // 1. The dataset is too large to send efficiently in a single payload
244 // (e.g., thousands of workspace symbols, file uri or cloud resources).
245 // 2. The available options depend on the user's input (e.g., semantic search).
246 // 3. Generating the list is expensive and should only be done if requested.
247 //
248 // The client is expected to render a search interface (e.g., a combo box with
249 // a text input) and query the server via 'interactive/listEnum' as the user types.
250 export interface FormFieldTypeLazyEnum {
251 kind: 'lazyEnum';
252
253 // Source identifies the data source on the server.
254 //
255 // Examples: "workspace/symbol", "database/schema", "git/tags".
256 source: string;
257
258 // Config contains the static settings for the source.
259 // The client treats this as opaque data and echoes it back in the
260 // 'interactive/listEnum' request.
261 config?: any;
262 }
263
264 // FormFieldTypeList defines a homogenous list of items.
265 export interface FormFieldTypeList {
266 kind: 'list';
267
268 // ElementType specifies the type of the items in the list.
269 // Recursive reference to the union type.
270 elementType: FormFieldType;
271 }
272 ```
273
274 Response:
275 - Result: `ExecuteCommandParams` (the server returns the same params object, potentially modified with new `formFields` or with `formFields` omitted to signal completion).
276 - error: code and message set in case an exception happens during the code action request.
277
278 ## Input Types
279
280 The following input types are used in `gopls` forms. The client is responsible for collecting valid values for these types:
281
282 - **`"string"`**: A simple text value.
283 - **`"file"`**: A valid Document URI string.
284 - **`"bool"`**: A boolean value.
285 - **`"number"`**: A numeric value.
286 - **`"enum"`**: A selection from a static set of options provided by the server in the `entries` array.
287 - **`"lazyEnum"`**: A selection from a dynamic set of options queried on demand (see below).
288 - **`"list"`**: A homogenous list of items.
289
290 The `"lazyEnum"` type is used when the set of options is large, dynamic, or expensive to compute (e.g., workspace symbols).
291
292 Instead of providing options upfront, the server specifies a `source` and an optional `config` object. As the user provides a search query, the client must call the custom method `interactive/listEnum` to fetch matching options from the server.
293
294 Request:
295 - Method: `interactive/listEnum`
296 - Params: `InteractiveListEnumParams` defined as follows:
297
298 ```typescript
299 export interface InteractiveListEnumParams {
300 // Identifies the data source on the server (e.g., "workspace/symbol").
301 source: string;
302
303 // Opaque configuration data provided by the server in the form field,
304 // which the client must echo back.
305 config?: any;
306
307 // The search query entered by the user. An empty string requests a default set.
308 query: string;
309 }
310 ```
311
312 Response:
313 - Result: `FormFieldTypeEnum` containing the matching `entries` for the query.
314 - error: code and message set in case an exception happens during interactive enum fetching.
315
316 ## Interaction Example
317
318 Here is a concrete example of the interaction flow for `gopls.modify_tags` (adding struct tags):
319
320 1. The client requests code actions. The server returns a Code Action containing a `Command`.
321 ```json
322 {
323 "command": {
324 "title": "Add struct tags",
325 "command": "gopls.modify_tags",
326 "arguments": [{ "Modification": "add" }]
327 }
328 }
329 ```
330
331 2. Resolution request: Before executing the command, the client calls `command/resolve` passing the `ExecuteCommandParams`.
332 ```json
333 {
334 "command": "gopls.modify_tags",
335 "arguments": [{ "Modification": "add" }]
336 }
337 ```
338
339 3. Resolution response: The server responds with `ExecuteCommandParams` containing a `formFields` array.
340 ```json
341 {
342 "command": "gopls.modify_tags",
343 "arguments": [{ "Modification": "add" }],
344 "formFields": [
345 {
346 "id": "tags",
347 "description": "comma-separated list of tags to add",
348 "type": { "kind": "string" },
349 "required": true,
350 "default": "json"
351 },
352 {
353 "id": "transform",
354 "description": "transform rule for added tags",
355 "type": { "kind": "enum", "entries": [...] },
356 "required": true,
357 "default": "camelcase"
358 }
359 ]
360 }
361 ```
362
363 4. User fills in form: The client must render UI to collect answers for these fields.
364
365 5. Resolution request: The client collects user input and calls `command/resolve` again, populating `formAnswers`.
366 ```json
367 {
368 "command": "gopls.modify_tags",
369 "arguments": [{ "Modification": "add" }],
370 "formAnswers": [
371 { "id": "tags", "value": "json,foo" },
372 { "id": "transform", "value": "camelcase" }
373 ]
374 }
375 ```
376
377 6. Resolution Response: The server validates the input. There are two possible outcomes:
378
379 * **Case A: Validation Failure**
380 If the input is invalid (e.g., the user entered `"json,fo o"` with a space), the server returns `formFields` again with one error per invalid answer. The error is attached to the `FormField` object that has the corresponding `id`. The client may decide to drop the entire command resolve and command execution or try to return to step 4 to recollect user input.
381 ```json
382 {
383 "command": "gopls.modify_tags",
384 "arguments": [{ "Modification": "add" }],
385 "formFields": [
386 {
387 "id": "tags",
388 "description": "comma-separated list of tags to add",
389 "type": { "kind": "string" },
390 "required": true,
391 "default": "json",
392 "error": "cannot contain spaces, quotes, colons, or control characters"
393 },
394 {
395 "id": "transform",
396 "description": "transform rule for added tags",
397 "type": { "kind": "enum", "entries": [...] },
398 "required": true,
399 "default": "camelcase"
400 }
401 ],
402 "formAnswers": [
403 { "id": "tags", "value": "json,fo o" },
404 { "id": "transform", "value": "camelcase" }
405 ]
406 }
407 ```
408
409 * **Case B: Success & Execution**
410 If the input is valid, the server returns a response with `formFields` omitted.
411 ```json
412 {
413 "command": "gopls.modify_tags",
414 "arguments": [{ "Modification": "add" }],
415 "formAnswers": [
416 { "id": "tags", "value": "json,foo" },
417 { "id": "transform", "value": "camelcase" }
418 ]
419 }
420 ```
421 At this point, the client proceeds to execute the command via `workspace/executeCommand`, passing the finalized params (including `formAnswers`).
422 ```json
423 {
424 "command": "gopls.modify_tags",
425 "arguments": [{ "Modification": "add" }],
426 "formAnswers": [
427 { "id": "tags", "value": "json,foo" },
428 { "id": "transform", "value": "camelcase" }
429 ]
430 }
431 ```