| 1 | // Copyright 2025 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // This file defines types for an experimental feature (golang/go#76331, |
| 6 | // microsoft/language-server-protocol#1164). As an experimental feature, |
| 7 | // these types are not yet included in tsprotocol.go. |
| 8 | |
| 9 | package protocol |
| 10 | |
| 11 | import "encoding/json" |
| 12 | |
| 13 | // InteractiveResolveOptions represents the server capabilities for interactive |
| 14 | // resolve. |
| 15 | type InteractiveResolveOptions struct { |
| 16 | // The kinds of interactive resolutions that the server supports. |
| 17 | // |
| 18 | // For example, "command" indicates that the server supports resolving |
| 19 | // `ExecuteCommandParams` interactively through "command/resolve". |
| 20 | Kinds []string `json:"kinds"` |
| 21 | } |
| 22 | |
| 23 | // InteractiveResolveClientCapabilities represents the client capabilities for |
| 24 | // interactive resolve. |
| 25 | type InteractiveResolveClientCapabilities struct { |
| 26 | // The input types the client supports for interactive dialogs. |
| 27 | // The presence of this field implies support for interactive refactoring. |
| 28 | InputTypes []string `json:"inputTypes"` |
| 29 | } |
| 30 | |
| 31 | // FormFieldTypeString defines a text input. |
| 32 | // |
| 33 | // It is defined as a struct to allow for future extensibility, such as |
| 34 | // adding regex validation or file URI constraints. |
| 35 | type FormFieldTypeString struct { |
| 36 | // Kind must be "string". |
| 37 | Kind string `json:"kind"` |
| 38 | } |
| 39 | |
| 40 | // FileExistence whether the file denoted by a DocumentURI exists. |
| 41 | // |
| 42 | // It is a bit set allowing combinations of existence states. For |
| 43 | // example, New|Existing allows either state. |
| 44 | type FileExistence uint32 |
| 45 | |
| 46 | const ( |
| 47 | // New indicates that file has not yet been created. |
| 48 | FileExistenceNew FileExistence = 1 << 0 |
| 49 | // Existing indicates that the file exists already. |
| 50 | FileExistenceExisting FileExistence = 1 << 1 |
| 51 | ) |
| 52 | |
| 53 | // FileType represents the expected filesystem resource type. |
| 54 | // |
| 55 | // It is a bit set allowing combinations of file types. For example, Regular|Directory |
| 56 | // allows either types. |
| 57 | type FileType uint32 |
| 58 | |
| 59 | const ( |
| 60 | // Regular indicates the resource could be a regular file. |
| 61 | FileTypeRegular FileType = 1 << 0 |
| 62 | // Directory indicates the resource could be a directory. |
| 63 | FileTypeDirectory FileType = 1 << 1 |
| 64 | ) |
| 65 | |
| 66 | // FormFieldTypeFile defines an input for a file or directory URI. |
| 67 | // |
| 68 | // The client determines the best mechanism to collect this information from |
| 69 | // the user (e.g., a graphical file picker, a text input with autocomplete, etc). |
| 70 | // |
| 71 | // The value returned by the client must be a valid "DocumentUri" as defined |
| 72 | // in the LSP specification: |
| 73 | // https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#documentUri |
| 74 | type FormFieldTypeFile struct { |
| 75 | // Kind must be "file". |
| 76 | Kind string `json:"kind"` |
| 77 | |
| 78 | // Existence constraint. |
| 79 | Existence FileExistence `json:"existence"` |
| 80 | |
| 81 | // Type specifies the set of allowed file types (regular file, directory, etc). |
| 82 | // |
| 83 | // Only applicable against existing file. |
| 84 | Type FileType `json:"type"` |
| 85 | |
| 86 | // Filters specifies the allowed file extensions without the leading dot. A file |
| 87 | // is valid if it matches any of the extensions (OR logic). e.g. ["png", "jpg"]. |
| 88 | // |
| 89 | // If omitted or empty, no extension filter is applied. |
| 90 | Filters []string `json:"filters"` |
| 91 | } |
| 92 | |
| 93 | // FormFieldTypeBool defines a boolean input. |
| 94 | type FormFieldTypeBool struct { |
| 95 | // Kind must be "bool". |
| 96 | Kind string `json:"kind"` |
| 97 | } |
| 98 | |
| 99 | // FormFieldTypeNumber defines a numeric input. |
| 100 | // |
| 101 | // It is defined as a struct to allow for future extensibility, such as |
| 102 | // adding range constraints (min/max) or precision requirements. |
| 103 | type FormFieldTypeNumber struct { |
| 104 | // Kind must be "number". |
| 105 | Kind string `json:"kind"` |
| 106 | } |
| 107 | |
| 108 | // FormEnumEntry represents a single option in an enumeration. |
| 109 | type FormEnumEntry struct { |
| 110 | // Value is the unique string identifier for this option. |
| 111 | // |
| 112 | // This is the value that will be sent back to the server in |
| 113 | // 'FormAnswers' if the user selects this option. |
| 114 | Value string `json:"value"` |
| 115 | |
| 116 | // Description is the human-readable label presented to the user. |
| 117 | Description string `json:"description"` |
| 118 | } |
| 119 | |
| 120 | // FormFieldTypeEnum defines a selection from a set of values. |
| 121 | // |
| 122 | // Use this type when: |
| 123 | // - The number of options is small (e.g., < 20). |
| 124 | // - All options are known at the time the form is created. |
| 125 | type FormFieldTypeEnum struct { |
| 126 | // Kind must be "enum". |
| 127 | Kind string `json:"kind"` |
| 128 | |
| 129 | // Entries is the list of allowable options. |
| 130 | Entries []FormEnumEntry `json:"entries"` |
| 131 | } |
| 132 | |
| 133 | // FormFieldTypeLazyEnum defines a selection from a large or dynamic enum entry set. |
| 134 | // |
| 135 | // Use this type when: |
| 136 | // 1. The dataset is too large to send efficiently in a single payload |
| 137 | // (e.g., thousands of workspace symbols, file uri or cloud resources). |
| 138 | // 2. The available options depend on the user's input (e.g., semantic search). |
| 139 | // 3. Generating the list is expensive and should only be done if requested. |
| 140 | // |
| 141 | // The client is expected to render a search interface (e.g., a combo box with |
| 142 | // a text input) and query the server via 'interactive/listEnum' as the user types. |
| 143 | type FormFieldTypeLazyEnum struct { |
| 144 | // Kind must be "lazyEnum". |
| 145 | Kind string `json:"kind"` |
| 146 | |
| 147 | // TODO(hxjiang): consider make debounce configurable since fetching |
| 148 | // cloud resources could be expensive and slow. |
| 149 | |
| 150 | // Source identifies the data source on the server. |
| 151 | // |
| 152 | // Examples: "workspace/symbol", "database/schema", "git/tags". |
| 153 | Source string `json:"source"` |
| 154 | |
| 155 | // Config contains the static settings for the source. |
| 156 | // The client treats this as opaque data and echoes it back in the |
| 157 | // 'interactive/listEnum' request. |
| 158 | Config json.RawMessage `json:"config,omitempty"` |
| 159 | } |
| 160 | |
| 161 | // FormFieldTypeList defines a homogeneous list of items. |
| 162 | type FormFieldTypeList struct { |
| 163 | // Kind must be "list". |
| 164 | Kind string `json:"kind"` |
| 165 | |
| 166 | // ElementType specifies the type of the items in the list. |
| 167 | // It must be one of the FormFieldType* structs (e.g., FormFieldTypeString). |
| 168 | ElementType any `json:"elementType"` |
| 169 | } |
| 170 | |
| 171 | // FormField describes a single question in a form and its validation state. |
| 172 | type FormField struct { |
| 173 | // ID is a unique identifier for this field. This key is used as the property |
| 174 | // name in FormAnswers to map the user's input back to this specific field. |
| 175 | ID string `json:"id"` |
| 176 | |
| 177 | // Description is the text content of the question (the prompt) presented |
| 178 | // to the user. |
| 179 | Description string `json:"description"` |
| 180 | |
| 181 | // Type specifies the data type and validation constraints for the answer. |
| 182 | // |
| 183 | // It must be one of the FormFieldType* structs. The Kind field within the |
| 184 | // struct determines the expected data type. |
| 185 | // |
| 186 | // The language client is expected to render an input appropriate for this |
| 187 | // type. If the client does not support the specified type, it should |
| 188 | // fall back to a string input. |
| 189 | Type any `json:"type"` |
| 190 | |
| 191 | // Required specifies whether an answer is required for this field. |
| 192 | Required bool `json:"required"` |
| 193 | |
| 194 | // Default specifies an optional initial value for the answer. |
| 195 | // |
| 196 | // If Type is FormFieldTypeEnum, this value must be present in the enum's |
| 197 | // Values slice. |
| 198 | Default any `json:"default,omitempty"` |
| 199 | |
| 200 | // Error provides a validation message from the language server. |
| 201 | // If empty, the current answer is considered valid. |
| 202 | Error string `json:"error,omitempty"` |
| 203 | } |
| 204 | |
| 205 | // FormAnswer describes a single answer to a FormField, identified by its unique |
| 206 | // ID. |
| 207 | type FormAnswer struct { |
| 208 | // The ID of the FormField being answered. |
| 209 | ID string `json:"id"` |
| 210 | |
| 211 | // The user's answer value. |
| 212 | Value any `json:"value"` |
| 213 | } |
| 214 | |
| 215 | // InteractiveParams facilitates a multi-step, interactive dialogue between the |
| 216 | // client and server during a Language Server Protocol (LSP) request. |
| 217 | // |
| 218 | // It implements a non-standard protocol extension microsoft/language-server-protocol#1164 |
| 219 | // . By embedding this type into standard request parameters (such as |
| 220 | // [ExecuteCommandParams] or [RenameParams]) and pairing them with dedicated |
| 221 | // resolution methods (like [Server.ResolveCommand] or other ResolveXXX handlers), |
| 222 | // standard operations can be transformed into interactive workflows. |
| 223 | // |
| 224 | // Standard LSP resolution methods (like "codeAction/resolve") cannot be used |
| 225 | // for these interactive forms because editors often trigger them eagerly to |
| 226 | // render previews, which would prematurely present UI forms to the user. |
| 227 | // The dedicated ResolveXXX pattern ensures the interactive dialogue strictly |
| 228 | // begins only *after* the user has explicitly indicated intent (for example, |
| 229 | // by clicking a specific Code Action). |
| 230 | // |
| 231 | // The following sequence illustrates the typical handshake, using a code action |
| 232 | // that resolves to a command as an example: |
| 233 | // |
| 234 | // 1. The client requests code actions for the current text selection. |
| 235 | // 2. The server responds with a code action containing a standard LSP Command |
| 236 | // (title, command, and arguments). |
| 237 | // 3. The client calls [Server.ResolveCommand] with the initial command details |
| 238 | // wrapped in an [ExecuteCommandParams] to determine if the execution requires |
| 239 | // interactive input. |
| 240 | // 4. The server responds with an [ExecuteCommandParams]. If user input is |
| 241 | // required, the server populates the FormFields array with the required schema. |
| 242 | // 5. The client observes the non-empty FormFields and presents a corresponding |
| 243 | // user interface. |
| 244 | // 6. The user submits their input, and the client issues another |
| 245 | // [Server.ResolveCommand] request, this time populating the FormAnswers array. |
| 246 | // 7. The server validates the answers. If invalid, it returns a form with error |
| 247 | // messages attached to specific FormFields. Steps 5-7 repeat until the server |
| 248 | // omits FormFields entirely, indicating the answers are valid and complete. |
| 249 | // 8. The client calls [Server.ExecuteCommand] with the finalized FormAnswers to |
| 250 | // execute the action. |
| 251 | // |
| 252 | // The server populates FormFields to define the input schema. If FormFields is |
| 253 | // omitted or empty, the interactive phase is considered complete and the provided |
| 254 | // FormAnswers have been fully validated. |
| 255 | // |
| 256 | // The server may optionally populate FormAnswers alongside FormFields to preserve |
| 257 | // previous user input or provide default values for the client to render. |
| 258 | type InteractiveParams struct { |
| 259 | // FormFields defines the questions and validation errors in previous |
| 260 | // answers to the same questions. |
| 261 | // |
| 262 | // This is a server-to-client field. The language server defines these, and |
| 263 | // the client uses them to render the form. |
| 264 | // |
| 265 | // The interactive phase is considered complete when the server returns a |
| 266 | // response where this slice is omitted. |
| 267 | // |
| 268 | // Note: This is a non-standard protocol extension. See microsoft/language-server-protocol#1164. |
| 269 | FormFields []FormField `json:"formFields,omitempty"` |
| 270 | |
| 271 | // FormAnswers contains the answers for the form questions. |
| 272 | // |
| 273 | // When sent by the language server, this field is optional and contains the |
| 274 | // current or default answers to the questions to support editing previous values. |
| 275 | // |
| 276 | // When sent by the language client, this field contains the user's answers. |
| 277 | // Answers are linked to their respective questions using the field's unique |
| 278 | // `id` rather than their array index. The list must not contain duplicate IDs, |
| 279 | // and each answer's ID must correspond to a field ID defined in `formFields`. |
| 280 | // |
| 281 | // The client must include answers for all required fields (where `required` |
| 282 | // is true). Answers for optional fields (where `required` is false) |
| 283 | // may be omitted if no answer was provided, or included if an answer is available. |
| 284 | // |
| 285 | // Note: This is a non-standard protocol extension. See microsoft/language-server-protocol#1164. |
| 286 | FormAnswers []FormAnswer `json:"formAnswers,omitempty"` |
| 287 | } |
| 288 | |
| 289 | // InteractiveListEnumParams defines the parameters for the |
| 290 | // 'interactive/listEnum' request. |
| 291 | type InteractiveListEnumParams struct { |
| 292 | // Source identifies the data source on the server. |
| 293 | // |
| 294 | // The client treats this as opaque data and echoes it back in the |
| 295 | // 'interactive/listEnum' request. |
| 296 | // |
| 297 | // Examples: "workspace/symbol", "database/schema", "git/tags". |
| 298 | Source string `json:"source"` |
| 299 | |
| 300 | // Config contains the static settings for the specified source. |
| 301 | // |
| 302 | // The client treats this as opaque data and echoes it back in the |
| 303 | // 'interactive/listEnum' request. |
| 304 | Config json.RawMessage `json:"config,omitempty"` |
| 305 | |
| 306 | // A query string to filter enum entries by. |
| 307 | // |
| 308 | // The exact interpretation of this string (e.g., fuzzy matching, exact |
| 309 | // match, prefix search, or regular expression) is entirely up to the |
| 310 | // server and may vary depending on the source. This follows the similar |
| 311 | // semantics as the standard 'workspace/symbol' request. Clients may |
| 312 | // send an empty string here to request a default set of enum entries. |
| 313 | Query string `json:"query"` |
| 314 | } |