Debian Codegraph @

gopls/internal/protocol/generate/tables.go

raw symbols SCIP
1 // Copyright 2022 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 package main
6
7 import "log"
8
9 // prop combines the name of a property (class.field) with the name of
10 // the structure it is in, using LSP field capitalization.
11 type prop [2]string
12
13 const (
14 nothing = iota
15 wantOpt // omitempty
16 wantOptStar // omitempty, indirect
17 )
18
19 // goplsStar records the optionality of each field in the protocol.
20 // The comments are vague hints as to why removing the line is not trivial.
21 // A.B.C.D means that one of B or C would change to a pointer
22 // so a test or initialization would be needed
23 var goplsStar = map[prop]int{
24 {"AnnotatedTextEdit", "annotationId"}: wantOptStar,
25 {"ClientCapabilities", "textDocument"}: wantOpt, // A.B.C.D at fake/editor.go:255
26 {"ClientCapabilities", "window"}: wantOpt, // test failures
27 {"ClientCapabilities", "workspace"}: wantOpt, // test failures
28 {"CodeAction", "kind"}: wantOpt, // A.B.C.D
29
30 {"CodeActionClientCapabilities", "codeActionLiteralSupport"}: wantOpt, // test failures
31
32 {"CompletionClientCapabilities", "completionItem"}: wantOpt, // A.B.C.D
33 {"CompletionClientCapabilities", "insertTextMode"}: wantOpt, // A.B.C.D
34 {"CompletionItem", "kind"}: wantOpt, // need temporary variables
35 {"CompletionParams", "context"}: wantOpt, // needs nil checks
36
37 {"Diagnostic", "severity"}: wantOpt, // needs nil checks or more careful thought
38 {"DidSaveTextDocumentParams", "text"}: wantOptStar, // capabilities_test.go:112 logic
39 {"DocumentHighlight", "kind"}: wantOpt, // need temporary variables
40
41 {"FoldingRange", "startLine"}: wantOptStar, // unset != zero (#71489)
42 {"FoldingRange", "startCharacter"}: wantOptStar, // unset != zero (#71489)
43 {"FoldingRange", "endLine"}: wantOptStar, // unset != zero (#71489)
44 {"FoldingRange", "endCharacter"}: wantOptStar, // unset != zero (#71489)
45
46 {"Hover", "range"}: wantOpt, // complex expressions
47 {"InlayHint", "kind"}: wantOpt, // temporary variables
48
49 {"PublishDiagnosticsParams", "version"}: wantOpt, // zero => missing (#73501)
50 {"SignatureHelp", "activeParameter"}: wantOptStar, // unset != zero
51 {"SignatureInformation", "activeParameter"}: wantOptStar, // unset != zero
52 {"TextDocumentClientCapabilities", "codeAction"}: wantOpt, // A.B.C.D
53 {"TextDocumentClientCapabilities", "completion"}: wantOpt, // A.B.C.D
54 {"TextDocumentClientCapabilities", "documentSymbol"}: wantOpt, // A.B.C.D
55 {"TextDocumentClientCapabilities", "publishDiagnostics"}: wantOpt, // A.B.C.D
56 {"TextDocumentClientCapabilities", "semanticTokens"}: wantOpt, // A.B.C.D
57 {"TextDocumentContentChangePartial", "range"}: wantOptStar, // == nil test
58 {"TextDocumentContentChangePartial", "rangeLength"}: wantOptStar, // unset != zero
59 {"TextDocumentSyncOptions", "change"}: wantOpt, // &constant
60 {"WorkDoneProgressBegin", "percentage"}: wantOptStar, // unset != zero
61 {"WorkDoneProgressParams", "workDoneToken"}: wantOpt, // test failures
62 {"WorkDoneProgressReport", "percentage"}: wantOptStar, // unset != zero
63 {"WorkspaceClientCapabilities", "didChangeConfiguration"}: wantOpt, // A.B.C.D
64 {"WorkspaceClientCapabilities", "didChangeWatchedFiles"}: wantOpt, // A.B.C.D
65 }
66
67 // keep track of which entries in goplsStar are used
68 var usedGoplsStar = make(map[prop]bool)
69
70 // For gopls compatibility, use a different, typically more restrictive, type for some fields.
71 var renameProp = map[prop]string{
72 {"CancelParams", "id"}: "any",
73 {"Command", "arguments"}: "[]json.RawMessage",
74 {"CodeAction", "data"}: "json.RawMessage", // delay unmarshalling commands
75 {"Diagnostic", "code"}: "any",
76 {"Diagnostic", "data"}: "json.RawMessage", // delay unmarshalling quickfixes
77
78 {"DocumentDiagnosticReportPartialResult", "relatedDocuments"}: "map[DocumentURI]any",
79
80 {"ExecuteCommandParams", "arguments"}: "[]json.RawMessage",
81 {"FileCreate", "uri"}: "DocumentURI", // see go.dev/issue/74652
82 {"FileDelete", "uri"}: "DocumentURI",
83 {"FileRename", "oldUri"}: "DocumentURI",
84 {"FileRename", "newUri"}: "DocumentURI",
85 {"FoldingRange", "kind"}: "string",
86 {"Hover", "contents"}: "MarkupContent",
87 {"InlayHint", "label"}: "[]InlayHintLabelPart",
88
89 {"RelatedFullDocumentDiagnosticReport", "relatedDocuments"}: "map[DocumentURI]any",
90 {"RelatedUnchangedDocumentDiagnosticReport", "relatedDocuments"}: "map[DocumentURI]any",
91
92 // PJW: this one is tricky.
93 {"ServerCapabilities", "codeActionProvider"}: "any",
94
95 {"ServerCapabilities", "inlayHintProvider"}: "any",
96 // slightly tricky
97 {"ServerCapabilities", "renameProvider"}: "any",
98 // slightly tricky
99 {"ServerCapabilities", "semanticTokensProvider"}: "any",
100 // slightly tricky
101 {"ServerCapabilities", "textDocumentSync"}: "any",
102 {"TextDocumentSyncOptions", "save"}: "SaveOptions",
103 {"WorkspaceEdit", "documentChanges"}: "[]DocumentChange",
104 }
105
106 // which entries of renameProp were used
107 var usedRenameProp = make(map[prop]bool)
108
109 type adjust struct {
110 prefix, suffix string
111 }
112
113 // disambiguate specifies prefixes or suffixes to add to all values of
114 // some enum types to avoid name conflicts
115 var disambiguate = map[string]adjust{
116 "CodeActionTriggerKind": {"CodeAction", ""},
117 "CompletionItemKind": {"", "Completion"},
118 "CompletionItemTag": {"Compl", ""},
119 "DiagnosticSeverity": {"Severity", ""},
120 "DocumentDiagnosticReportKind": {"Diagnostic", ""},
121 "FileOperationPatternKind": {"", "Pattern"},
122 "InlineCompletionTriggerKind": {"Inline", ""},
123 "InsertTextFormat": {"", "TextFormat"},
124 "LanguageKind": {"Lang", ""},
125 "SemanticTokenModifiers": {"Mod", ""},
126 "SemanticTokenTypes": {"", "Type"},
127 "SignatureHelpTriggerKind": {"Sig", ""},
128 "SymbolTag": {"", "Symbol"},
129 "WatchKind": {"Watch", ""},
130 }
131
132 // which entries of disambiguate got used
133 var usedDisambiguate = make(map[string]bool)
134
135 // for gopls compatibility, replace generated type names with existing ones
136 var goplsType = map[string]string{
137 "And_RegOpt_textDocument_colorPresentation": "WorkDoneProgressOptionsAndTextDocumentRegistrationOptions",
138 "ConfigurationParams": "ParamConfiguration",
139 "DocumentUri": "DocumentURI",
140 "InitializeParams": "ParamInitialize",
141 "LSPAny": "any",
142
143 "Lit_SemanticTokensOptions_range_Item1": "PRangeESemanticTokensOptions",
144
145 "Or_Declaration": "[]Location",
146 "Or_DidChangeConfigurationRegistrationOptions_section": "OrPSection_workspace_didChangeConfiguration",
147 "Or_InlayHintLabelPart_tooltip": "OrPTooltipPLabel",
148 "Or_InlayHint_tooltip": "OrPTooltip_textDocument_inlayHint",
149 "Or_LSPAny": "any",
150
151 "Or_ParameterInformation_documentation": "string",
152 "Or_ParameterInformation_label": "string",
153 "Or_PrepareRenameResult": "PrepareRenamePlaceholder",
154 "Or_ProgressToken": "any",
155 "Or_Result_textDocument_completion": "CompletionList",
156 "Or_Result_textDocument_declaration": "Or_textDocument_declaration",
157 "Or_Result_textDocument_definition": "[]Location",
158 "Or_Result_textDocument_documentSymbol": "[]any",
159 "Or_Result_textDocument_implementation": "[]Location",
160 "Or_Result_textDocument_semanticTokens_full_delta": "any",
161 "Or_Result_textDocument_typeDefinition": "[]Location",
162 "Or_Result_workspace_symbol": "[]SymbolInformation",
163 "Or_TextDocumentContentChangeEvent": "TextDocumentContentChangePartial",
164 "Or_RelativePattern_baseUri": "DocumentURI",
165
166 "Or_WorkspaceFoldersServerCapabilities_changeNotifications": "string",
167 "Or_WorkspaceSymbol_location": "OrPLocation_workspace_symbol",
168
169 "Tuple_ParameterInformation_label_Item1": "UIntCommaUInt",
170 "WorkspaceFoldersServerCapabilities": "WorkspaceFolders5Gn",
171 "[]LSPAny": "[]any",
172
173 "[]Or_Result_textDocument_codeAction_Item0_Elem": "[]CodeAction",
174 "[]PreviousResultId": "[]PreviousResultID",
175 "[]uinteger": "[]uint32",
176 "boolean": "bool",
177 "decimal": "float64",
178 "integer": "int32",
179 "map[DocumentUri][]TextEdit": "map[DocumentURI][]TextEdit",
180 "uinteger": "uint32",
181 }
182
183 var usedGoplsType = make(map[string]bool)
184
185 // methodNames is a map from the method to the name of the function that handles it
186 var methodNames = map[string]string{
187 "$/cancelRequest": "CancelRequest",
188 "$/logTrace": "LogTrace",
189 "$/progress": "Progress",
190 "$/setTrace": "SetTrace",
191 "callHierarchy/incomingCalls": "IncomingCalls",
192 "callHierarchy/outgoingCalls": "OutgoingCalls",
193 "client/registerCapability": "RegisterCapability",
194 "client/unregisterCapability": "UnregisterCapability",
195 "codeAction/resolve": "ResolveCodeAction",
196 "codeLens/resolve": "ResolveCodeLens",
197 "completionItem/resolve": "ResolveCompletionItem",
198 "command/resolve": "ResolveCommand",
199 "documentLink/resolve": "ResolveDocumentLink",
200 "exit": "Exit",
201 "initialize": "Initialize",
202 "initialized": "Initialized",
203 "inlayHint/resolve": "Resolve",
204 "interactive/listEnum": "InteractiveListEnum",
205 "notebookDocument/didChange": "DidChangeNotebookDocument",
206 "notebookDocument/didClose": "DidCloseNotebookDocument",
207 "notebookDocument/didOpen": "DidOpenNotebookDocument",
208 "notebookDocument/didSave": "DidSaveNotebookDocument",
209 "shutdown": "Shutdown",
210 "telemetry/event": "Event",
211 "textDocument/codeAction": "CodeAction",
212 "textDocument/codeLens": "CodeLens",
213 "textDocument/colorPresentation": "ColorPresentation",
214 "textDocument/completion": "Completion",
215 "textDocument/declaration": "Declaration",
216 "textDocument/definition": "Definition",
217 "textDocument/diagnostic": "Diagnostic",
218 "textDocument/didChange": "DidChange",
219 "textDocument/didClose": "DidClose",
220 "textDocument/didOpen": "DidOpen",
221 "textDocument/didSave": "DidSave",
222 "textDocument/documentColor": "DocumentColor",
223 "textDocument/documentHighlight": "DocumentHighlight",
224 "textDocument/documentLink": "DocumentLink",
225 "textDocument/documentSymbol": "DocumentSymbol",
226 "textDocument/foldingRange": "FoldingRange",
227 "textDocument/formatting": "Formatting",
228 "textDocument/hover": "Hover",
229 "textDocument/implementation": "Implementation",
230 "textDocument/inlayHint": "InlayHint",
231 "textDocument/inlineCompletion": "InlineCompletion",
232 "textDocument/inlineValue": "InlineValue",
233 "textDocument/linkedEditingRange": "LinkedEditingRange",
234 "textDocument/moniker": "Moniker",
235 "textDocument/onTypeFormatting": "OnTypeFormatting",
236 "textDocument/prepareCallHierarchy": "PrepareCallHierarchy",
237 "textDocument/prepareRename": "PrepareRename",
238 "textDocument/prepareTypeHierarchy": "PrepareTypeHierarchy",
239 "textDocument/publishDiagnostics": "PublishDiagnostics",
240 "textDocument/rangeFormatting": "RangeFormatting",
241 "textDocument/rangesFormatting": "RangesFormatting",
242 "textDocument/references": "References",
243 "textDocument/rename": "Rename",
244 "textDocument/selectionRange": "SelectionRange",
245 "textDocument/semanticTokens/full": "SemanticTokensFull",
246 "textDocument/semanticTokens/full/delta": "SemanticTokensFullDelta",
247 "textDocument/semanticTokens/range": "SemanticTokensRange",
248 "textDocument/signatureHelp": "SignatureHelp",
249 "textDocument/typeDefinition": "TypeDefinition",
250 "textDocument/willSave": "WillSave",
251 "textDocument/willSaveWaitUntil": "WillSaveWaitUntil",
252 "typeHierarchy/subtypes": "Subtypes",
253 "typeHierarchy/supertypes": "Supertypes",
254 "window/logMessage": "LogMessage",
255 "window/showDocument": "ShowDocument",
256 "window/showMessage": "ShowMessage",
257 "window/showMessageRequest": "ShowMessageRequest",
258 "window/workDoneProgress/cancel": "WorkDoneProgressCancel",
259 "window/workDoneProgress/create": "WorkDoneProgressCreate",
260 "workspace/applyEdit": "ApplyEdit",
261 "workspace/codeLens/refresh": "CodeLensRefresh",
262 "workspace/configuration": "Configuration",
263 "workspace/diagnostic": "DiagnosticWorkspace",
264 "workspace/diagnostic/refresh": "DiagnosticRefresh",
265 "workspace/didChangeConfiguration": "DidChangeConfiguration",
266 "workspace/didChangeWatchedFiles": "DidChangeWatchedFiles",
267 "workspace/didChangeWorkspaceFolders": "DidChangeWorkspaceFolders",
268 "workspace/didCreateFiles": "DidCreateFiles",
269 "workspace/didDeleteFiles": "DidDeleteFiles",
270 "workspace/didRenameFiles": "DidRenameFiles",
271 "workspace/executeCommand": "ExecuteCommand",
272 "workspace/foldingRange/refresh": "FoldingRangeRefresh",
273 "workspace/inlayHint/refresh": "InlayHintRefresh",
274 "workspace/inlineValue/refresh": "InlineValueRefresh",
275 "workspace/semanticTokens/refresh": "SemanticTokensRefresh",
276 "workspace/symbol": "Symbol",
277 "workspace/textDocumentContent": "TextDocumentContent",
278 "workspace/textDocumentContent/refresh": "TextDocumentContentRefresh",
279 "workspace/willCreateFiles": "WillCreateFiles",
280 "workspace/willDeleteFiles": "WillDeleteFiles",
281 "workspace/willRenameFiles": "WillRenameFiles",
282 "workspace/workspaceFolders": "WorkspaceFolders",
283 "workspaceSymbol/resolve": "ResolveWorkspaceSymbol",
284 }
285
286 func methodName(method string) string {
287 ans := methodNames[method]
288 if ans == "" {
289 log.Fatalf("unknown method %q", method)
290 }
291 return ans
292 }
293
294 // prependMethodDocComments specifies doc comments that will be prepend to
295 // an LSP method name defined in both Server and Client interface.
296 var prependMethodDocComments = map[string]string{
297 "ResolveCommand": `// ResolveCommand handles the interactive resolution of a command prior to
298 // its execution.
299 //
300 // It processes an [ExecuteCommandParams] to determine if the command requires
301 // interactive input, or to validate user-provided answers submitted via the
302 // embedded [InteractiveParams].
303 //
304 // If the command requires user input (e.g., the initial probe) or if the
305 // provided answers are invalid, it returns a modified [ExecuteCommandParams]
306 // populated with FormFields to prompt the user. If the input is valid and
307 // complete, or if the command requires no interaction at all, it returns an
308 // [ExecuteCommandParams] with an empty form, signaling the client to proceed
309 // with execution.
310 //
311 // See [InteractiveParams] for the complete multi-step client-server handshake
312 // and the architectural reasoning behind dedicated ResolveXXX methods.`,
313 "InteractiveListEnum": `// InteractiveListEnum is the request handler for fetching dynamic enum options.
314 //
315 // This method is called by the client when the user interacts with a
316 // FormFieldTypeLazyEnum field (e.g., typing in a combo box). The server
317 // uses the provided Action and Params to determine the context (e.g.,
318 // "search workspace symbols for interfaces") and returns a filtered list
319 // of matching entries.`,
320 }
321
322 // prependMethodDocComments specifies doc comments that will be prepend to
323 // an LSP type's properties existing doc comments.
324 var appendTypePropDocComments = map[string]map[string]string{
325 "TextDocumentPositionParams": {"position": ` //
326 // Deprecated: gopls should use [TextDocumentPositionParams.Range] instead.
327 `},
328 }
329
330 // appendTypeProp specifies block of code (typically properties with doc comment)
331 // that will be append to a struct.
332 var appendTypeProp = map[string]string{
333 "ExecuteCommandParams": `
334 // Support interactive command execution.
335 //
336 // Note: This is a non-standard protocol extension. See golang/go#76331.
337 InteractiveParams
338 `,
339 }