]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/lsp_ext.rs
Switch to upstream protocol for resolving code action
[rust.git] / crates / rust-analyzer / src / lsp_ext.rs
1 //! rust-analyzer extensions to the LSP.
2
3 use std::{collections::HashMap, path::PathBuf};
4
5 use lsp_types::request::Request;
6 use lsp_types::{
7     notification::Notification, CodeActionKind, Position, Range, TextDocumentIdentifier,
8 };
9 use serde::{Deserialize, Serialize};
10
11 pub enum AnalyzerStatus {}
12
13 impl Request for AnalyzerStatus {
14     type Params = AnalyzerStatusParams;
15     type Result = String;
16     const METHOD: &'static str = "rust-analyzer/analyzerStatus";
17 }
18
19 #[derive(Deserialize, Serialize, Debug)]
20 #[serde(rename_all = "camelCase")]
21 pub struct AnalyzerStatusParams {
22     pub text_document: Option<TextDocumentIdentifier>,
23 }
24
25 pub enum MemoryUsage {}
26
27 impl Request for MemoryUsage {
28     type Params = ();
29     type Result = String;
30     const METHOD: &'static str = "rust-analyzer/memoryUsage";
31 }
32
33 pub enum ReloadWorkspace {}
34
35 impl Request for ReloadWorkspace {
36     type Params = ();
37     type Result = ();
38     const METHOD: &'static str = "rust-analyzer/reloadWorkspace";
39 }
40
41 pub enum SyntaxTree {}
42
43 impl Request for SyntaxTree {
44     type Params = SyntaxTreeParams;
45     type Result = String;
46     const METHOD: &'static str = "rust-analyzer/syntaxTree";
47 }
48
49 #[derive(Deserialize, Serialize, Debug)]
50 #[serde(rename_all = "camelCase")]
51 pub struct SyntaxTreeParams {
52     pub text_document: TextDocumentIdentifier,
53     pub range: Option<Range>,
54 }
55
56 pub enum ExpandMacro {}
57
58 impl Request for ExpandMacro {
59     type Params = ExpandMacroParams;
60     type Result = Option<ExpandedMacro>;
61     const METHOD: &'static str = "rust-analyzer/expandMacro";
62 }
63
64 #[derive(Deserialize, Serialize, Debug)]
65 #[serde(rename_all = "camelCase")]
66 pub struct ExpandMacroParams {
67     pub text_document: TextDocumentIdentifier,
68     pub position: Position,
69 }
70
71 #[derive(Deserialize, Serialize, Debug)]
72 #[serde(rename_all = "camelCase")]
73 pub struct ExpandedMacro {
74     pub name: String,
75     pub expansion: String,
76 }
77
78 pub enum MatchingBrace {}
79
80 impl Request for MatchingBrace {
81     type Params = MatchingBraceParams;
82     type Result = Vec<Position>;
83     const METHOD: &'static str = "experimental/matchingBrace";
84 }
85
86 #[derive(Deserialize, Serialize, Debug)]
87 #[serde(rename_all = "camelCase")]
88 pub struct MatchingBraceParams {
89     pub text_document: TextDocumentIdentifier,
90     pub positions: Vec<Position>,
91 }
92
93 pub enum ParentModule {}
94
95 impl Request for ParentModule {
96     type Params = lsp_types::TextDocumentPositionParams;
97     type Result = Option<lsp_types::GotoDefinitionResponse>;
98     const METHOD: &'static str = "experimental/parentModule";
99 }
100
101 pub enum JoinLines {}
102
103 impl Request for JoinLines {
104     type Params = JoinLinesParams;
105     type Result = Vec<lsp_types::TextEdit>;
106     const METHOD: &'static str = "experimental/joinLines";
107 }
108
109 #[derive(Deserialize, Serialize, Debug)]
110 #[serde(rename_all = "camelCase")]
111 pub struct JoinLinesParams {
112     pub text_document: TextDocumentIdentifier,
113     pub ranges: Vec<Range>,
114 }
115
116 pub enum OnEnter {}
117
118 impl Request for OnEnter {
119     type Params = lsp_types::TextDocumentPositionParams;
120     type Result = Option<Vec<SnippetTextEdit>>;
121     const METHOD: &'static str = "experimental/onEnter";
122 }
123
124 pub enum Runnables {}
125
126 impl Request for Runnables {
127     type Params = RunnablesParams;
128     type Result = Vec<Runnable>;
129     const METHOD: &'static str = "experimental/runnables";
130 }
131
132 #[derive(Serialize, Deserialize, Debug)]
133 #[serde(rename_all = "camelCase")]
134 pub struct RunnablesParams {
135     pub text_document: TextDocumentIdentifier,
136     pub position: Option<Position>,
137 }
138
139 #[derive(Deserialize, Serialize, Debug)]
140 #[serde(rename_all = "camelCase")]
141 pub struct Runnable {
142     pub label: String,
143     #[serde(skip_serializing_if = "Option::is_none")]
144     pub location: Option<lsp_types::LocationLink>,
145     pub kind: RunnableKind,
146     pub args: CargoRunnable,
147 }
148
149 #[derive(Serialize, Deserialize, Debug)]
150 #[serde(rename_all = "lowercase")]
151 pub enum RunnableKind {
152     Cargo,
153 }
154
155 #[derive(Deserialize, Serialize, Debug)]
156 #[serde(rename_all = "camelCase")]
157 pub struct CargoRunnable {
158     // command to be executed instead of cargo
159     pub override_cargo: Option<String>,
160     #[serde(skip_serializing_if = "Option::is_none")]
161     pub workspace_root: Option<PathBuf>,
162     // command, --package and --lib stuff
163     pub cargo_args: Vec<String>,
164     // user-specified additional cargo args, like `--release`.
165     pub cargo_extra_args: Vec<String>,
166     // stuff after --
167     pub executable_args: Vec<String>,
168     #[serde(skip_serializing_if = "Option::is_none")]
169     pub expect_test: Option<bool>,
170 }
171
172 pub enum InlayHints {}
173
174 impl Request for InlayHints {
175     type Params = InlayHintsParams;
176     type Result = Vec<InlayHint>;
177     const METHOD: &'static str = "rust-analyzer/inlayHints";
178 }
179
180 #[derive(Serialize, Deserialize, Debug)]
181 #[serde(rename_all = "camelCase")]
182 pub struct InlayHintsParams {
183     pub text_document: TextDocumentIdentifier,
184 }
185
186 #[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
187 pub enum InlayKind {
188     TypeHint,
189     ParameterHint,
190     ChainingHint,
191 }
192
193 #[derive(Debug, Deserialize, Serialize)]
194 pub struct InlayHint {
195     pub range: Range,
196     pub kind: InlayKind,
197     pub label: String,
198 }
199
200 pub enum Ssr {}
201
202 impl Request for Ssr {
203     type Params = SsrParams;
204     type Result = lsp_types::WorkspaceEdit;
205     const METHOD: &'static str = "experimental/ssr";
206 }
207
208 #[derive(Debug, Deserialize, Serialize)]
209 #[serde(rename_all = "camelCase")]
210 pub struct SsrParams {
211     pub query: String,
212     pub parse_only: bool,
213
214     /// File position where SSR was invoked. Paths in `query` will be resolved relative to this
215     /// position.
216     #[serde(flatten)]
217     pub position: lsp_types::TextDocumentPositionParams,
218
219     /// Current selections. Search/replace will be restricted to these if non-empty.
220     pub selections: Vec<lsp_types::Range>,
221 }
222
223 pub enum StatusNotification {}
224
225 #[serde(rename_all = "camelCase")]
226 #[derive(Serialize, Deserialize)]
227 pub enum Status {
228     Loading,
229     Ready,
230     NeedsReload,
231     Invalid,
232 }
233
234 #[derive(Deserialize, Serialize)]
235 pub struct StatusParams {
236     pub status: Status,
237 }
238
239 impl Notification for StatusNotification {
240     type Params = StatusParams;
241     const METHOD: &'static str = "rust-analyzer/status";
242 }
243
244 pub enum CodeActionRequest {}
245
246 impl Request for CodeActionRequest {
247     type Params = lsp_types::CodeActionParams;
248     type Result = Option<Vec<CodeAction>>;
249     const METHOD: &'static str = "textDocument/codeAction";
250 }
251
252 pub enum CodeActionResolveRequest {}
253 impl Request for CodeActionResolveRequest {
254     type Params = CodeAction;
255     type Result = CodeAction;
256     const METHOD: &'static str = "codeAction/resolve";
257 }
258
259 #[derive(Debug, PartialEq, Clone, Default, Deserialize, Serialize)]
260 #[serde(rename_all = "camelCase")]
261 pub struct CodeAction {
262     pub title: String,
263     #[serde(skip_serializing_if = "Option::is_none")]
264     pub group: Option<String>,
265     #[serde(skip_serializing_if = "Option::is_none")]
266     pub kind: Option<CodeActionKind>,
267     // We don't handle commands on the client-side
268     // #[serde(skip_serializing_if = "Option::is_none")]
269     // pub command: Option<lsp_types::Command>,
270     #[serde(skip_serializing_if = "Option::is_none")]
271     pub edit: Option<SnippetWorkspaceEdit>,
272     #[serde(skip_serializing_if = "Option::is_none")]
273     pub is_preferred: Option<bool>,
274
275     #[serde(skip_serializing_if = "Option::is_none")]
276     pub data: Option<CodeActionData>,
277 }
278
279 #[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
280 #[serde(rename_all = "camelCase")]
281 pub struct CodeActionData {
282     pub code_action_params: lsp_types::CodeActionParams,
283     pub id: String,
284 }
285
286 #[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)]
287 #[serde(rename_all = "camelCase")]
288 pub struct SnippetWorkspaceEdit {
289     #[serde(skip_serializing_if = "Option::is_none")]
290     pub changes: Option<HashMap<lsp_types::Url, Vec<lsp_types::TextEdit>>>,
291     #[serde(skip_serializing_if = "Option::is_none")]
292     pub document_changes: Option<Vec<SnippetDocumentChangeOperation>>,
293 }
294
295 #[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
296 #[serde(untagged, rename_all = "lowercase")]
297 pub enum SnippetDocumentChangeOperation {
298     Op(lsp_types::ResourceOp),
299     Edit(SnippetTextDocumentEdit),
300 }
301
302 #[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
303 #[serde(rename_all = "camelCase")]
304 pub struct SnippetTextDocumentEdit {
305     pub text_document: lsp_types::VersionedTextDocumentIdentifier,
306     pub edits: Vec<SnippetTextEdit>,
307 }
308
309 #[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)]
310 #[serde(rename_all = "camelCase")]
311 pub struct SnippetTextEdit {
312     pub range: Range,
313     pub new_text: String,
314     #[serde(skip_serializing_if = "Option::is_none")]
315     pub insert_text_format: Option<lsp_types::InsertTextFormat>,
316 }
317
318 pub enum HoverRequest {}
319
320 impl Request for HoverRequest {
321     type Params = lsp_types::HoverParams;
322     type Result = Option<Hover>;
323     const METHOD: &'static str = "textDocument/hover";
324 }
325
326 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
327 pub struct Hover {
328     #[serde(flatten)]
329     pub hover: lsp_types::Hover,
330     #[serde(skip_serializing_if = "Vec::is_empty")]
331     pub actions: Vec<CommandLinkGroup>,
332 }
333
334 #[derive(Debug, PartialEq, Clone, Default, Deserialize, Serialize)]
335 pub struct CommandLinkGroup {
336     #[serde(skip_serializing_if = "Option::is_none")]
337     pub title: Option<String>,
338     pub commands: Vec<CommandLink>,
339 }
340
341 // LSP v3.15 Command does not have a `tooltip` field, vscode supports one.
342 #[derive(Debug, PartialEq, Clone, Default, Deserialize, Serialize)]
343 pub struct CommandLink {
344     #[serde(flatten)]
345     pub command: lsp_types::Command,
346     #[serde(skip_serializing_if = "Option::is_none")]
347     pub tooltip: Option<String>,
348 }
349
350 pub enum ExternalDocs {}
351
352 impl Request for ExternalDocs {
353     type Params = lsp_types::TextDocumentPositionParams;
354     type Result = Option<lsp_types::Url>;
355     const METHOD: &'static str = "experimental/externalDocs";
356 }