languageserver_types/
request.rs

1use super::*;
2
3pub trait Request {
4    type Params;
5    type Result;
6    const METHOD: &'static str;
7}
8
9#[macro_export]
10macro_rules! lsp_request {
11    ("initialize") => {
12        $crate::request::Initialize
13    };
14    ("shutdown") => {
15        $crate::request::Shutdown
16    };
17
18    ("window/showMessageRequest") => {
19        $crate::request::ShowMessageRequest
20    };
21
22    ("client/registerCapability") => {
23        $crate::request::RegisterCapability
24    };
25    ("client/unregisterCapability") => {
26        $crate::request::UnregisterCapability
27    };
28
29    ("workspace/symbol") => {
30        $crate::request::WorkspaceSymbol
31    };
32    ("workspace/executeCommand") => {
33        $crate::request::ExecuteCommand
34    };
35
36    ("textDocument/completion") => {
37        $crate::request::Completion
38    };
39    ("completionItem/resolve") => {
40        $crate::request::ResolveCompletionItem
41    };
42    ("textDocument/hover") => {
43        $crate::request::HoverRequest
44    };
45    ("textDocument/signatureHelp") => {
46        $crate::request::SignatureHelpRequest
47    };
48    ("textDocument/declaration") => {
49        $crate::request::GotoDeclaration
50    };
51    ("textDocument/definition") => {
52        $crate::request::GotoDefinition
53    };
54    ("textDocument/references") => {
55        $crate::request::References
56    };
57    ("textDocument/documentHighlight") => {
58        $crate::request::DocumentHighlightRequest
59    };
60    ("textDocument/documentSymbol") => {
61        $crate::request::DocumentSymbolRequest
62    };
63    ("textDocument/codeAction") => {
64        $crate::request::CodeActionRequest
65    };
66    ("textDocument/codeLens") => {
67        $crate::request::CodeLensRequest
68    };
69    ("codeLens/resolve") => {
70        $crate::request::CodeLensResolve
71    };
72    ("textDocument/documentLink") => {
73        $crate::request::DocumentLinkRequest
74    };
75    ("documentLink/resolve") => {
76        $crate::request::DocumentLinkResolve
77    };
78    ("workspace/applyEdit") => {
79        $crate::request::ApplyWorkspaceEdit
80    };
81    ("textDocument/rangeFormatting") => {
82        $crate::request::RangeFormatting
83    };
84    ("textDocument/onTypeFormatting") => {
85        $crate::request::OnTypeFormatting
86    };
87    ("textDocument/formatting") => {
88        $crate::request::Formatting
89    };
90    ("textDocument/rename") => {
91        $crate::request::Rename
92    };
93    ("textDocument/documentColor") => {
94        $crate::request::DocumentColor
95    };
96    ("textDocument/colorPresentation") => {
97        $crate::request::ColorPresentationRequest
98    };
99    ("textDocument/foldingRange") => {
100        $crate::request::FoldingRangeRequest
101    };
102    ("textDocument/prepareRename") => {
103        $crate::request::PrepareRenameRequest
104    };
105    ("textDocument/implementation") => {
106        $crate::request::GotoImplementation
107    };
108    ("textDocument/typeDefinition") => {
109        $crate::request::GotoTypeDefinition
110    };
111    ("workspace/workspaceFolders") => {
112        $crate::request::WorkspaceFoldersRequest
113    };
114}
115
116/**
117
118 The initialize request is sent as the first request from the client to the server.
119 If the server receives request or notification before the `initialize` request it should act as follows:
120
121 * for a request the respond should be errored with `code: -32001`. The message can be picked by the server.
122 * notifications should be dropped.
123
124*/
125#[derive(Debug)]
126pub enum Initialize {}
127
128impl Request for Initialize {
129    type Params = InitializeParams;
130    type Result = InitializeResult;
131    const METHOD: &'static str = "initialize";
132}
133
134/**
135 * The shutdown request is sent from the client to the server. It asks the server to shut down,
136 * but to not exit (otherwise the response might not be delivered correctly to the client).
137 * There is a separate exit notification that asks the server to exit.
138 */
139#[derive(Debug)]
140pub enum Shutdown {}
141
142impl Request for Shutdown {
143    type Params = ();
144    type Result = ();
145    const METHOD: &'static str = "shutdown";
146}
147
148/**
149 * The show message request is sent from a server to a client to ask the client to display a particular message
150 * in the user interface. In addition to the show message notification the request allows to pass actions and to
151 * wait for an answer from the client.
152 */
153#[derive(Debug)]
154pub enum ShowMessageRequest {}
155
156impl Request for ShowMessageRequest {
157    type Params = ShowMessageRequestParams;
158    type Result = Option<MessageActionItem>;
159    const METHOD: &'static str = "window/showMessageRequest";
160}
161
162/**
163 * The client/registerCapability request is sent from the server to the client to register for a new capability on the client side. Not all clients need to support dynamic capability registration. A client opts in via the ClientCapabilities.GenericCapability property.
164 */
165#[derive(Debug)]
166pub enum RegisterCapability {}
167
168impl Request for RegisterCapability {
169    type Params = RegistrationParams;
170    type Result = ();
171    const METHOD: &'static str = "client/registerCapability";
172}
173
174/// The client/unregisterCapability request is sent from the server to the client to unregister a
175/// previously register capability.
176#[derive(Debug)]
177pub enum UnregisterCapability {}
178
179impl Request for UnregisterCapability {
180    type Params = UnregistrationParams;
181    type Result = ();
182    const METHOD: &'static str = "client/unregisterCapability";
183}
184
185/**
186 The Completion request is sent from the client to the server to compute completion items at a given cursor position.
187 Completion items are presented in the IntelliSense user interface. If computing full completion items is expensive,
188 servers can additionally provide a handler for the completion item resolve request ('completionItem/resolve').
189 This request is sent when a completion item is selected in the user interface. A typical use case is for example:
190 the 'textDocument/completion' request doesn’t fill in the documentation property for returned completion items
191 since it is expensive to compute. When the item is selected in the user interface then a ‘completionItem/resolve’
192 request is sent with the selected completion item as a param. The returned completion item should have the
193 documentation property filled in. The request can delay the computation of the detail and documentation properties.
194 However, properties that are needed for the initial sorting and filtering, like sortText, filterText, insertText,
195 and textEdit must be provided in the textDocument/completion request and must not be changed during resolve.
196
197*/
198#[derive(Debug)]
199pub enum Completion {}
200
201impl Request for Completion {
202    type Params = CompletionParams;
203    type Result = Option<CompletionResponse>;
204    const METHOD: &'static str = "textDocument/completion";
205}
206
207/// The request is sent from the client to the server to resolve additional information for a given completion item.
208#[derive(Debug)]
209pub enum ResolveCompletionItem {}
210
211impl Request for ResolveCompletionItem {
212    type Params = CompletionItem;
213    type Result = CompletionItem;
214    const METHOD: &'static str = "completionItem/resolve";
215}
216
217/// The hover request is sent from the client to the server to request hover information at a given text
218/// document position.
219#[derive(Debug)]
220pub enum HoverRequest {}
221
222impl Request for HoverRequest {
223    type Params = TextDocumentPositionParams;
224    type Result = Option<Hover>;
225    const METHOD: &'static str = "textDocument/hover";
226}
227
228/// The signature help request is sent from the client to the server to request signature information at
229/// a given cursor position.
230#[derive(Debug)]
231pub enum SignatureHelpRequest {}
232
233impl Request for SignatureHelpRequest {
234    type Params = TextDocumentPositionParams;
235    type Result = Option<SignatureHelp>;
236    const METHOD: &'static str = "textDocument/signatureHelp";
237}
238
239#[derive(Debug)]
240pub enum GotoDeclaration {}
241
242/// The goto declaration request is sent from the client to the server to resolve the declaration location of
243/// a symbol at a given text document position.
244impl Request for GotoDeclaration {
245    type Params = TextDocumentPositionParams;
246    type Result = Option<GotoDefinitionResponse>;
247    const METHOD: &'static str = "textDocument/declaration";
248}
249
250/// The goto definition request is sent from the client to the server to resolve the definition location of
251/// a symbol at a given text document position.
252#[derive(Debug)]
253pub enum GotoDefinition {}
254
255impl Request for GotoDefinition {
256    type Params = TextDocumentPositionParams;
257    type Result = Option<GotoDefinitionResponse>;
258    const METHOD: &'static str = "textDocument/definition";
259}
260
261 /// GotoDefinition response can be single location, or multiple Locations or a link.
262#[derive(Debug, PartialEq, Serialize, Deserialize)]
263#[serde(untagged)]
264pub enum GotoDefinitionResponse {
265    Scalar(Location),
266    Array(Vec<Location>),
267    Link(Vec<LocationLink>)
268}
269
270/// The references request is sent from the client to the server to resolve project-wide references for the
271/// symbol denoted by the given text document position.
272#[derive(Debug)]
273pub enum References {}
274
275impl Request for References {
276    type Params = ReferenceParams;
277    type Result = Option<Vec<Location>>;
278    const METHOD: &'static str = "textDocument/references";
279}
280
281/// The goto type definition request is sent from the client to the
282/// server to resolve the type definition location of a symbol at a
283/// given text document position.
284#[derive(Debug)]
285pub enum GotoTypeDefinition {}
286
287pub type GotoTypeDefinitionResponse = GotoDefinitionResponse;
288
289impl Request for GotoTypeDefinition {
290    type Params = TextDocumentPositionParams;
291    type Result = Option<GotoTypeDefinitionResponse>;
292    const METHOD: &'static str = "textDocument/typeDefinition";
293}
294
295/// The goto implementation request is sent from the client to the
296/// server to resolve the implementation location of a symbol at a
297/// given text document position.
298#[derive(Debug)]
299pub enum GotoImplementation {}
300
301pub type GotoImplementationResponse = GotoDefinitionResponse;
302
303impl Request for GotoImplementation {
304    type Params = TextDocumentPositionParams;
305    type Result = Option<GotoImplementationResponse>;
306    const METHOD: &'static str = "textDocument/implementation";
307}
308
309/**
310 The document highlight request is sent from the client to the server to resolve a document highlights
311 for a given text document position.
312 For programming languages this usually highlights all references to the symbol scoped to this file.
313 However we kept 'textDocument/documentHighlight' and 'textDocument/references' separate requests since
314 the first one is allowed to be more fuzzy.
315 Symbol matches usually have a DocumentHighlightKind of Read or Write whereas fuzzy or textual matches
316 use Text as the kind.
317*/
318#[derive(Debug)]
319pub enum DocumentHighlightRequest {}
320
321impl Request for DocumentHighlightRequest {
322    type Params = TextDocumentPositionParams;
323    type Result = Option<Vec<DocumentHighlight>>;
324    const METHOD: &'static str = "textDocument/documentHighlight";
325}
326
327/**
328 * The document symbol request is sent from the client to the server to list all symbols found in a given
329 * text document.
330 */
331#[derive(Debug)]
332pub enum DocumentSymbolRequest {}
333
334impl Request for DocumentSymbolRequest {
335    type Params = DocumentSymbolParams;
336    type Result = Option<DocumentSymbolResponse>;
337    const METHOD: &'static str = "textDocument/documentSymbol";
338}
339
340/**
341 * The workspace symbol request is sent from the client to the server to list project-wide symbols
342 * matching the query string.
343 */
344#[derive(Debug)]
345pub enum WorkspaceSymbol {}
346
347impl Request for WorkspaceSymbol {
348    type Params = WorkspaceSymbolParams;
349    type Result = Option<Vec<SymbolInformation>>;
350    const METHOD: &'static str = "workspace/symbol";
351}
352
353/// The workspace/executeCommand request is sent from the client to the server to trigger command execution on the server. In most cases the server creates a WorkspaceEdit structure and applies the changes to the workspace using the request workspace/applyEdit which is sent from the server to the client.
354#[derive(Debug)]
355pub enum ExecuteCommand {}
356
357impl Request for ExecuteCommand {
358    type Params = ExecuteCommandParams;
359    type Result = Option<Value>;
360    const METHOD: &'static str = "workspace/executeCommand";
361}
362
363/// The workspace/applyEdit request is sent from the server to the client to modify resource on the
364/// client side.
365#[derive(Debug)]
366pub enum ApplyWorkspaceEdit {}
367
368impl Request for ApplyWorkspaceEdit {
369    type Params = ApplyWorkspaceEditParams;
370    type Result = ApplyWorkspaceEditResponse;
371    const METHOD: &'static str = "workspace/applyEdit";
372}
373
374/**
375 * The code action request is sent from the client to the server to compute commands for a given text document
376 * and range. The request is triggered when the user moves the cursor into a problem marker in the editor or
377 * presses the lightbulb associated with a marker.
378 */
379#[derive(Debug)]
380pub enum CodeActionRequest {}
381
382impl Request for CodeActionRequest {
383    type Params = CodeActionParams;
384    type Result = Option<CodeActionResponse>;
385    const METHOD: &'static str = "textDocument/codeAction";
386}
387
388/**
389 * The code lens request is sent from the client to the server to compute code lenses for a given text document.
390 */
391#[derive(Debug)]
392pub enum CodeLensRequest {}
393
394impl Request for CodeLensRequest {
395    type Params = CodeLensParams;
396    type Result = Option<Vec<CodeLens>>;
397    const METHOD: &'static str = "textDocument/codeLens";
398}
399
400/**
401 * The code lens resolve request is sent from the client to the server to resolve the command for a
402 * given code lens item.
403 */
404#[derive(Debug)]
405pub enum CodeLensResolve {}
406
407impl Request for CodeLensResolve {
408    type Params = CodeLens;
409    type Result = CodeLens;
410    const METHOD: &'static str = "codeLens/resolve";
411}
412
413/// The document links request is sent from the client to the server to request the location of links in a document.
414#[derive(Debug)]
415pub enum DocumentLinkRequest {}
416
417impl Request for DocumentLinkRequest {
418    type Params = DocumentLinkParams;
419    type Result = Option<Vec<DocumentLink>>;
420    const METHOD: &'static str = "textDocument/documentLink";
421}
422
423/**
424 The document link resolve request is sent from the client to the server to resolve the target of
425 a given document link.
426*/
427#[derive(Debug)]
428pub enum DocumentLinkResolve {}
429
430impl Request for DocumentLinkResolve {
431    type Params = DocumentLink;
432    type Result = DocumentLink;
433    const METHOD: &'static str = "documentLink/resolve";
434}
435
436/**
437 * The document formatting request is sent from the server to the client to format a whole document.
438 */
439#[derive(Debug)]
440pub enum Formatting {}
441
442impl Request for Formatting {
443    type Params = DocumentFormattingParams;
444    type Result = Option<Vec<TextEdit>>;
445    const METHOD: &'static str = "textDocument/formatting";
446}
447
448/// The document range formatting request is sent from the client to the server to format a given range in a document.
449#[derive(Debug)]
450pub enum RangeFormatting {}
451
452impl Request for RangeFormatting {
453    type Params = DocumentRangeFormattingParams;
454    type Result = Option<Vec<TextEdit>>;
455    const METHOD: &'static str = "textDocument/rangeFormatting";
456}
457
458/**
459 * The document on type formatting request is sent from the client to the server to format parts of
460 * the document during typing.
461 */
462#[derive(Debug)]
463pub enum OnTypeFormatting {}
464
465impl Request for OnTypeFormatting {
466    type Params = DocumentOnTypeFormattingParams;
467    type Result = Option<Vec<TextEdit>>;
468    const METHOD: &'static str = "textDocument/onTypeFormatting";
469}
470
471/**
472 * The rename request is sent from the client to the server to perform a workspace-wide rename of a symbol.
473 */
474#[derive(Debug)]
475pub enum Rename {}
476
477impl Request for Rename {
478    type Params = RenameParams;
479    type Result = Option<WorkspaceEdit>;
480    const METHOD: &'static str = "textDocument/rename";
481}
482
483/// The document color request is sent from the client to the server to list all color references found in a given text document.
484/// Along with the range, a color value in RGB is returned.
485#[derive(Debug)]
486pub enum DocumentColor {}
487
488impl Request for DocumentColor {
489    type Params = DocumentColorParams;
490    type Result = Vec<ColorInformation>;
491    const METHOD: &'static str = "textDocument/documentColor";
492}
493
494/// The color presentation request is sent from the client to the server to obtain a list of presentations for a color value
495/// at a given location.
496#[derive(Debug)]
497pub enum ColorPresentationRequest {}
498
499impl Request for ColorPresentationRequest {
500    type Params = ColorPresentationParams;
501    type Result = Vec<ColorPresentation>;
502    const METHOD: &'static str = "textDocument/colorPresentation";
503}
504
505/// The folding range request is sent from the client to the server to return all folding ranges found in a given text document.
506#[derive(Debug)]
507pub enum FoldingRangeRequest {}
508
509impl Request for FoldingRangeRequest {
510    type Params = FoldingRangeParams;
511    type Result = Option<Vec<FoldingRange>>;
512    const METHOD: &'static str = "textDocument/foldingRange";
513}
514
515/// The prepare rename request is sent from the client to the server to setup and test the validity of a rename operation
516/// at a given location.
517#[derive(Debug)]
518pub enum PrepareRenameRequest {}
519
520impl Request for PrepareRenameRequest {
521    type Params = TextDocumentPositionParams;
522    type Result = Option<PrepareRenameResponse>;
523    const METHOD: &'static str = "textDocument/prepareRename";
524}
525
526/// The workspace/workspaceFolders request is sent from the server to the client to fetch the current open list of
527/// workspace folders. Returns null in the response if only a single file is open in the tool.
528/// Returns an empty array if a workspace is open but no folders are configured.
529#[derive(Debug)]
530pub enum WorkspaceFoldersRequest {}
531
532impl Request for WorkspaceFoldersRequest {
533    type Params = ();
534    type Result = Option<Vec<WorkspaceFolder>>;
535    const METHOD: &'static str = "workspace/workspaceFolders";
536}
537
538#[cfg(test)]
539mod test {
540    use super::*;
541
542    fn fake_call<R>()
543    where
544        R: Request,
545        R::Params: serde::Serialize,
546        R::Result: serde::de::DeserializeOwned,
547    {
548    }
549
550    macro_rules! check_macro {
551        ($name:tt) => {
552            // check whethe the macro name matches the method
553            assert_eq!(<lsp_request!($name) as Request>::METHOD, $name);
554            // test whether type checking passes for each component
555            fake_call::<lsp_request!($name)>();
556        };
557    }
558
559    #[test]
560    fn check_macro_definitions() {
561        check_macro!("initialize");
562        check_macro!("shutdown");
563        check_macro!("window/showMessageRequest");
564        check_macro!("client/registerCapability");
565        check_macro!("client/unregisterCapability");
566        check_macro!("workspace/symbol");
567        check_macro!("workspace/executeCommand");
568        check_macro!("textDocument/completion");
569        check_macro!("completionItem/resolve");
570        check_macro!("textDocument/hover");
571        check_macro!("textDocument/signatureHelp");
572        check_macro!("textDocument/declaration");
573        check_macro!("textDocument/definition");
574        check_macro!("textDocument/references");
575        check_macro!("textDocument/documentHighlight");
576        check_macro!("textDocument/documentSymbol");
577        check_macro!("textDocument/codeAction");
578        check_macro!("textDocument/codeLens");
579        check_macro!("codeLens/resolve");
580        check_macro!("textDocument/documentLink");
581        check_macro!("documentLink/resolve");
582        check_macro!("workspace/applyEdit");
583        check_macro!("textDocument/rangeFormatting");
584        check_macro!("textDocument/onTypeFormatting");
585        check_macro!("textDocument/formatting");
586        check_macro!("textDocument/rename");
587        check_macro!("textDocument/documentColor");
588        check_macro!("textDocument/colorPresentation");
589        check_macro!("textDocument/foldingRange");
590        check_macro!("textDocument/prepareRename");
591        check_macro!("workspace/workspaceFolders");
592        check_macro!("textDocument/implementation");
593        check_macro!("textDocument/typeDefinition");
594    }
595}