golang.org/x/tools/gopls@v0.15.3/internal/protocol/generate/tables.go (about)

     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  //go:build go1.19
     6  // +build go1.19
     7  
     8  package main
     9  
    10  import "log"
    11  
    12  // prop combines the name of a property with the name of the structure it is in.
    13  type prop [2]string
    14  
    15  const (
    16  	nothing = iota
    17  	wantStar
    18  	wantOpt
    19  	wantOptStar
    20  )
    21  
    22  // goplsStar records the optionality of each field in the protocol.
    23  // The comments are vague hints as to why removing the line is not trivial.
    24  // A.B.C.D means that one of B or C would change to a pointer
    25  // so a test or initialization would be needed
    26  var goplsStar = map[prop]int{
    27  	{"AnnotatedTextEdit", "annotationId"}:  wantOptStar,
    28  	{"ClientCapabilities", "textDocument"}: wantOpt, // A.B.C.D at fake/editor.go:255
    29  	{"ClientCapabilities", "window"}:       wantOpt, // test failures
    30  	{"ClientCapabilities", "workspace"}:    wantOpt, // test failures
    31  	{"CodeAction", "kind"}:                 wantOpt, // A.B.C.D
    32  
    33  	{"CodeActionClientCapabilities", "codeActionLiteralSupport"}: wantOpt, // test failures
    34  
    35  	{"CompletionClientCapabilities", "completionItem"}: wantOpt, // A.B.C.D
    36  	{"CompletionClientCapabilities", "insertTextMode"}: wantOpt, // A.B.C.D
    37  	{"CompletionItem", "kind"}:                         wantOpt, // need temporary variables
    38  	{"CompletionParams", "context"}:                    wantOpt, // needs nil checks
    39  
    40  	{"Diagnostic", "severity"}:            wantOpt,     // nil checks or more careful thought
    41  	{"DidSaveTextDocumentParams", "text"}: wantOptStar, // capabilities_test.go:112 logic
    42  	{"DocumentHighlight", "kind"}:         wantOpt,     // need temporary variables
    43  	{"Hover", "range"}:                    wantOpt,     // complex expressions
    44  	{"InlayHint", "kind"}:                 wantOpt,     // temporary variables
    45  
    46  	{"TextDocumentClientCapabilities", "codeAction"}:          wantOpt,     // A.B.C.D
    47  	{"TextDocumentClientCapabilities", "completion"}:          wantOpt,     // A.B.C.D
    48  	{"TextDocumentClientCapabilities", "documentSymbol"}:      wantOpt,     // A.B.C.D
    49  	{"TextDocumentClientCapabilities", "publishDiagnostics"}:  wantOpt,     //A.B.C.D
    50  	{"TextDocumentClientCapabilities", "semanticTokens"}:      wantOpt,     // A.B.C.D
    51  	{"TextDocumentContentChangePartial", "range"}:             wantOptStar, // == nil test
    52  	{"TextDocumentSyncOptions", "change"}:                     wantOpt,     // &constant
    53  	{"WorkDoneProgressParams", "workDoneToken"}:               wantOpt,     // test failures
    54  	{"WorkspaceClientCapabilities", "didChangeConfiguration"}: wantOpt,     // A.B.C.D
    55  	{"WorkspaceClientCapabilities", "didChangeWatchedFiles"}:  wantOpt,     // A.B.C.D
    56  }
    57  
    58  // keep track of which entries in goplsStar are used
    59  var usedGoplsStar = make(map[prop]bool)
    60  
    61  // For gopls compatibility, use a different, typically more restrictive, type for some fields.
    62  var renameProp = map[prop]string{
    63  	{"CancelParams", "id"}:         "interface{}",
    64  	{"Command", "arguments"}:       "[]json.RawMessage",
    65  	{"CompletionItem", "textEdit"}: "TextEdit",
    66  	{"CodeAction", "data"}:         "json.RawMessage", // delay unmarshalling commands
    67  	{"Diagnostic", "code"}:         "interface{}",
    68  	{"Diagnostic", "data"}:         "json.RawMessage", // delay unmarshalling quickfixes
    69  
    70  	{"DocumentDiagnosticReportPartialResult", "relatedDocuments"}: "map[DocumentURI]interface{}",
    71  
    72  	{"ExecuteCommandParams", "arguments"}: "[]json.RawMessage",
    73  	{"FoldingRange", "kind"}:              "string",
    74  	{"Hover", "contents"}:                 "MarkupContent",
    75  	{"InlayHint", "label"}:                "[]InlayHintLabelPart",
    76  
    77  	{"RelatedFullDocumentDiagnosticReport", "relatedDocuments"}:      "map[DocumentURI]interface{}",
    78  	{"RelatedUnchangedDocumentDiagnosticReport", "relatedDocuments"}: "map[DocumentURI]interface{}",
    79  
    80  	// PJW: this one is tricky.
    81  	{"ServerCapabilities", "codeActionProvider"}: "interface{}",
    82  
    83  	{"ServerCapabilities", "inlayHintProvider"}: "interface{}",
    84  	// slightly tricky
    85  	{"ServerCapabilities", "renameProvider"}: "interface{}",
    86  	// slightly tricky
    87  	{"ServerCapabilities", "semanticTokensProvider"}: "interface{}",
    88  	// slightly tricky
    89  	{"ServerCapabilities", "textDocumentSync"}: "interface{}",
    90  	{"TextDocumentSyncOptions", "save"}:        "SaveOptions",
    91  	{"WorkspaceEdit", "documentChanges"}:       "[]DocumentChanges",
    92  }
    93  
    94  // which entries of renameProp were used
    95  var usedRenameProp = make(map[prop]bool)
    96  
    97  type adjust struct {
    98  	prefix, suffix string
    99  }
   100  
   101  // disambiguate specifies prefixes or suffixes to add to all values of
   102  // some enum types to avoid name conflicts
   103  var disambiguate = map[string]adjust{
   104  	"CodeActionTriggerKind":        {"CodeAction", ""},
   105  	"CompletionItemKind":           {"", "Completion"},
   106  	"CompletionItemTag":            {"Compl", ""},
   107  	"DiagnosticSeverity":           {"Severity", ""},
   108  	"DocumentDiagnosticReportKind": {"Diagnostic", ""},
   109  	"FileOperationPatternKind":     {"", "Pattern"},
   110  	"InlineCompletionTriggerKind":  {"Inline", ""},
   111  	"InsertTextFormat":             {"", "TextFormat"},
   112  	"SemanticTokenModifiers":       {"Mod", ""},
   113  	"SemanticTokenTypes":           {"", "Type"},
   114  	"SignatureHelpTriggerKind":     {"Sig", ""},
   115  	"SymbolTag":                    {"", "Symbol"},
   116  	"WatchKind":                    {"Watch", ""},
   117  }
   118  
   119  // which entries of disambiguate got used
   120  var usedDisambiguate = make(map[string]bool)
   121  
   122  // for gopls compatibility, replace generated type names with existing ones
   123  var goplsType = map[string]string{
   124  	"And_RegOpt_textDocument_colorPresentation": "WorkDoneProgressOptionsAndTextDocumentRegistrationOptions",
   125  	"ConfigurationParams":                       "ParamConfiguration",
   126  	"DocumentDiagnosticParams":                  "string",
   127  	"DocumentDiagnosticReport":                  "string",
   128  	"DocumentUri":                               "DocumentURI",
   129  	"InitializeParams":                          "ParamInitialize",
   130  	"LSPAny":                                    "interface{}",
   131  
   132  	"Lit_SemanticTokensOptions_range_Item1": "PRangeESemanticTokensOptions",
   133  
   134  	"Or_Declaration": "[]Location",
   135  	"Or_DidChangeConfigurationRegistrationOptions_section": "OrPSection_workspace_didChangeConfiguration",
   136  	"Or_InlayHintLabelPart_tooltip":                        "OrPTooltipPLabel",
   137  	"Or_InlayHint_tooltip":                                 "OrPTooltip_textDocument_inlayHint",
   138  	"Or_LSPAny":                                            "interface{}",
   139  
   140  	"Or_ParameterInformation_documentation":            "string",
   141  	"Or_ParameterInformation_label":                    "string",
   142  	"Or_PrepareRenameResult":                           "PrepareRenamePlaceholder",
   143  	"Or_ProgressToken":                                 "interface{}",
   144  	"Or_Result_textDocument_completion":                "CompletionList",
   145  	"Or_Result_textDocument_declaration":               "Or_textDocument_declaration",
   146  	"Or_Result_textDocument_definition":                "[]Location",
   147  	"Or_Result_textDocument_documentSymbol":            "[]interface{}",
   148  	"Or_Result_textDocument_implementation":            "[]Location",
   149  	"Or_Result_textDocument_semanticTokens_full_delta": "interface{}",
   150  	"Or_Result_textDocument_typeDefinition":            "[]Location",
   151  	"Or_Result_workspace_symbol":                       "[]SymbolInformation",
   152  	"Or_TextDocumentContentChangeEvent":                "TextDocumentContentChangePartial",
   153  	"Or_RelativePattern_baseUri":                       "DocumentURI",
   154  
   155  	"Or_WorkspaceFoldersServerCapabilities_changeNotifications": "string",
   156  	"Or_WorkspaceSymbol_location":                               "OrPLocation_workspace_symbol",
   157  
   158  	"Tuple_ParameterInformation_label_Item1": "UIntCommaUInt",
   159  	"WorkspaceFoldersServerCapabilities":     "WorkspaceFolders5Gn",
   160  	"[]LSPAny":                               "[]interface{}",
   161  
   162  	"[]Or_Result_textDocument_codeAction_Item0_Elem": "[]CodeAction",
   163  	"[]PreviousResultId":                             "[]PreviousResultID",
   164  	"[]uinteger":                                     "[]uint32",
   165  	"boolean":                                        "bool",
   166  	"decimal":                                        "float64",
   167  	"integer":                                        "int32",
   168  	"map[DocumentUri][]TextEdit":                     "map[DocumentURI][]TextEdit",
   169  	"uinteger":                                       "uint32",
   170  }
   171  
   172  var usedGoplsType = make(map[string]bool)
   173  
   174  // methodNames is a map from the method to the name of the function that handles it
   175  var methodNames = map[string]string{
   176  	"$/cancelRequest":                        "CancelRequest",
   177  	"$/logTrace":                             "LogTrace",
   178  	"$/progress":                             "Progress",
   179  	"$/setTrace":                             "SetTrace",
   180  	"callHierarchy/incomingCalls":            "IncomingCalls",
   181  	"callHierarchy/outgoingCalls":            "OutgoingCalls",
   182  	"client/registerCapability":              "RegisterCapability",
   183  	"client/unregisterCapability":            "UnregisterCapability",
   184  	"codeAction/resolve":                     "ResolveCodeAction",
   185  	"codeLens/resolve":                       "ResolveCodeLens",
   186  	"completionItem/resolve":                 "ResolveCompletionItem",
   187  	"documentLink/resolve":                   "ResolveDocumentLink",
   188  	"exit":                                   "Exit",
   189  	"initialize":                             "Initialize",
   190  	"initialized":                            "Initialized",
   191  	"inlayHint/resolve":                      "Resolve",
   192  	"notebookDocument/didChange":             "DidChangeNotebookDocument",
   193  	"notebookDocument/didClose":              "DidCloseNotebookDocument",
   194  	"notebookDocument/didOpen":               "DidOpenNotebookDocument",
   195  	"notebookDocument/didSave":               "DidSaveNotebookDocument",
   196  	"shutdown":                               "Shutdown",
   197  	"telemetry/event":                        "Event",
   198  	"textDocument/codeAction":                "CodeAction",
   199  	"textDocument/codeLens":                  "CodeLens",
   200  	"textDocument/colorPresentation":         "ColorPresentation",
   201  	"textDocument/completion":                "Completion",
   202  	"textDocument/declaration":               "Declaration",
   203  	"textDocument/definition":                "Definition",
   204  	"textDocument/diagnostic":                "Diagnostic",
   205  	"textDocument/didChange":                 "DidChange",
   206  	"textDocument/didClose":                  "DidClose",
   207  	"textDocument/didOpen":                   "DidOpen",
   208  	"textDocument/didSave":                   "DidSave",
   209  	"textDocument/documentColor":             "DocumentColor",
   210  	"textDocument/documentHighlight":         "DocumentHighlight",
   211  	"textDocument/documentLink":              "DocumentLink",
   212  	"textDocument/documentSymbol":            "DocumentSymbol",
   213  	"textDocument/foldingRange":              "FoldingRange",
   214  	"textDocument/formatting":                "Formatting",
   215  	"textDocument/hover":                     "Hover",
   216  	"textDocument/implementation":            "Implementation",
   217  	"textDocument/inlayHint":                 "InlayHint",
   218  	"textDocument/inlineCompletion":          "InlineCompletion",
   219  	"textDocument/inlineValue":               "InlineValue",
   220  	"textDocument/linkedEditingRange":        "LinkedEditingRange",
   221  	"textDocument/moniker":                   "Moniker",
   222  	"textDocument/onTypeFormatting":          "OnTypeFormatting",
   223  	"textDocument/prepareCallHierarchy":      "PrepareCallHierarchy",
   224  	"textDocument/prepareRename":             "PrepareRename",
   225  	"textDocument/prepareTypeHierarchy":      "PrepareTypeHierarchy",
   226  	"textDocument/publishDiagnostics":        "PublishDiagnostics",
   227  	"textDocument/rangeFormatting":           "RangeFormatting",
   228  	"textDocument/rangesFormatting":          "RangesFormatting",
   229  	"textDocument/references":                "References",
   230  	"textDocument/rename":                    "Rename",
   231  	"textDocument/selectionRange":            "SelectionRange",
   232  	"textDocument/semanticTokens/full":       "SemanticTokensFull",
   233  	"textDocument/semanticTokens/full/delta": "SemanticTokensFullDelta",
   234  	"textDocument/semanticTokens/range":      "SemanticTokensRange",
   235  	"textDocument/signatureHelp":             "SignatureHelp",
   236  	"textDocument/typeDefinition":            "TypeDefinition",
   237  	"textDocument/willSave":                  "WillSave",
   238  	"textDocument/willSaveWaitUntil":         "WillSaveWaitUntil",
   239  	"typeHierarchy/subtypes":                 "Subtypes",
   240  	"typeHierarchy/supertypes":               "Supertypes",
   241  	"window/logMessage":                      "LogMessage",
   242  	"window/showDocument":                    "ShowDocument",
   243  	"window/showMessage":                     "ShowMessage",
   244  	"window/showMessageRequest":              "ShowMessageRequest",
   245  	"window/workDoneProgress/cancel":         "WorkDoneProgressCancel",
   246  	"window/workDoneProgress/create":         "WorkDoneProgressCreate",
   247  	"workspace/applyEdit":                    "ApplyEdit",
   248  	"workspace/codeLens/refresh":             "CodeLensRefresh",
   249  	"workspace/configuration":                "Configuration",
   250  	"workspace/diagnostic":                   "DiagnosticWorkspace",
   251  	"workspace/diagnostic/refresh":           "DiagnosticRefresh",
   252  	"workspace/didChangeConfiguration":       "DidChangeConfiguration",
   253  	"workspace/didChangeWatchedFiles":        "DidChangeWatchedFiles",
   254  	"workspace/didChangeWorkspaceFolders":    "DidChangeWorkspaceFolders",
   255  	"workspace/didCreateFiles":               "DidCreateFiles",
   256  	"workspace/didDeleteFiles":               "DidDeleteFiles",
   257  	"workspace/didRenameFiles":               "DidRenameFiles",
   258  	"workspace/executeCommand":               "ExecuteCommand",
   259  	"workspace/foldingRange/refresh":         "FoldingRangeRefresh",
   260  	"workspace/inlayHint/refresh":            "InlayHintRefresh",
   261  	"workspace/inlineValue/refresh":          "InlineValueRefresh",
   262  	"workspace/semanticTokens/refresh":       "SemanticTokensRefresh",
   263  	"workspace/symbol":                       "Symbol",
   264  	"workspace/willCreateFiles":              "WillCreateFiles",
   265  	"workspace/willDeleteFiles":              "WillDeleteFiles",
   266  	"workspace/willRenameFiles":              "WillRenameFiles",
   267  	"workspace/workspaceFolders":             "WorkspaceFolders",
   268  	"workspaceSymbol/resolve":                "ResolveWorkspaceSymbol",
   269  }
   270  
   271  func methodName(method string) string {
   272  	ans := methodNames[method]
   273  	if ans == "" {
   274  		log.Fatalf("unknown method %q", method)
   275  	}
   276  	return ans
   277  }