]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/lsp_ext.rs
Merge #4927
[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::{Position, Range, TextDocumentIdentifier};
7 use serde::{Deserialize, Serialize};
8
9 pub enum AnalyzerStatus {}
10
11 impl Request for AnalyzerStatus {
12     type Params = ();
13     type Result = String;
14     const METHOD: &'static str = "rust-analyzer/analyzerStatus";
15 }
16
17 pub enum CollectGarbage {}
18
19 impl Request for CollectGarbage {
20     type Params = ();
21     type Result = ();
22     const METHOD: &'static str = "rust-analyzer/collectGarbage";
23 }
24
25 pub enum SyntaxTree {}
26
27 impl Request for SyntaxTree {
28     type Params = SyntaxTreeParams;
29     type Result = String;
30     const METHOD: &'static str = "rust-analyzer/syntaxTree";
31 }
32
33 #[derive(Deserialize, Serialize, Debug)]
34 #[serde(rename_all = "camelCase")]
35 pub struct SyntaxTreeParams {
36     pub text_document: TextDocumentIdentifier,
37     pub range: Option<Range>,
38 }
39
40 pub enum ExpandMacro {}
41
42 impl Request for ExpandMacro {
43     type Params = ExpandMacroParams;
44     type Result = Option<ExpandedMacro>;
45     const METHOD: &'static str = "rust-analyzer/expandMacro";
46 }
47
48 #[derive(Deserialize, Serialize, Debug)]
49 #[serde(rename_all = "camelCase")]
50 pub struct ExpandMacroParams {
51     pub text_document: TextDocumentIdentifier,
52     pub position: Position,
53 }
54
55 #[derive(Deserialize, Serialize, Debug)]
56 #[serde(rename_all = "camelCase")]
57 pub struct ExpandedMacro {
58     pub name: String,
59     pub expansion: String,
60 }
61
62 pub enum MatchingBrace {}
63
64 impl Request for MatchingBrace {
65     type Params = MatchingBraceParams;
66     type Result = Vec<Position>;
67     const METHOD: &'static str = "experimental/matchingBrace";
68 }
69
70 #[derive(Deserialize, Serialize, Debug)]
71 #[serde(rename_all = "camelCase")]
72 pub struct MatchingBraceParams {
73     pub text_document: TextDocumentIdentifier,
74     pub positions: Vec<Position>,
75 }
76
77 pub enum ParentModule {}
78
79 impl Request for ParentModule {
80     type Params = lsp_types::TextDocumentPositionParams;
81     type Result = Option<lsp_types::GotoDefinitionResponse>;
82     const METHOD: &'static str = "experimental/parentModule";
83 }
84
85 pub enum JoinLines {}
86
87 impl Request for JoinLines {
88     type Params = JoinLinesParams;
89     type Result = Vec<lsp_types::TextEdit>;
90     const METHOD: &'static str = "experimental/joinLines";
91 }
92
93 #[derive(Deserialize, Serialize, Debug)]
94 #[serde(rename_all = "camelCase")]
95 pub struct JoinLinesParams {
96     pub text_document: TextDocumentIdentifier,
97     pub ranges: Vec<Range>,
98 }
99
100 pub enum ResolveCodeActionRequest {}
101
102 impl Request for ResolveCodeActionRequest {
103     type Params = ResolveCodeActionParams;
104     type Result = Option<SnippetWorkspaceEdit>;
105     const METHOD: &'static str = "experimental/resolveCodeAction";
106 }
107
108 /// Params for the ResolveCodeActionRequest
109 #[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
110 #[serde(rename_all = "camelCase")]
111 pub struct ResolveCodeActionParams {
112     pub code_action_params: lsp_types::CodeActionParams,
113     pub id: String,
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     #[serde(skip_serializing_if = "Option::is_none")]
159     pub workspace_root: Option<PathBuf>,
160     // command, --package and --lib stuff
161     pub cargo_args: Vec<String>,
162     // stuff after --
163     pub executable_args: Vec<String>,
164 }
165
166 pub enum InlayHints {}
167
168 impl Request for InlayHints {
169     type Params = InlayHintsParams;
170     type Result = Vec<InlayHint>;
171     const METHOD: &'static str = "rust-analyzer/inlayHints";
172 }
173
174 #[derive(Serialize, Deserialize, Debug)]
175 #[serde(rename_all = "camelCase")]
176 pub struct InlayHintsParams {
177     pub text_document: TextDocumentIdentifier,
178 }
179
180 #[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
181 pub enum InlayKind {
182     TypeHint,
183     ParameterHint,
184     ChainingHint,
185 }
186
187 #[derive(Debug, Deserialize, Serialize)]
188 pub struct InlayHint {
189     pub range: Range,
190     pub kind: InlayKind,
191     pub label: String,
192 }
193
194 pub enum Ssr {}
195
196 impl Request for Ssr {
197     type Params = SsrParams;
198     type Result = lsp_types::WorkspaceEdit;
199     const METHOD: &'static str = "experimental/ssr";
200 }
201
202 #[derive(Debug, Deserialize, Serialize)]
203 #[serde(rename_all = "camelCase")]
204 pub struct SsrParams {
205     pub query: String,
206     pub parse_only: bool,
207 }
208
209 pub enum CodeActionRequest {}
210
211 impl Request for CodeActionRequest {
212     type Params = lsp_types::CodeActionParams;
213     type Result = Option<Vec<CodeAction>>;
214     const METHOD: &'static str = "textDocument/codeAction";
215 }
216
217 #[derive(Debug, PartialEq, Clone, Default, Deserialize, Serialize)]
218 pub struct CodeAction {
219     pub title: String,
220     #[serde(skip_serializing_if = "Option::is_none")]
221     pub id: Option<String>,
222     #[serde(skip_serializing_if = "Option::is_none")]
223     pub group: Option<String>,
224     #[serde(skip_serializing_if = "Option::is_none")]
225     pub kind: Option<String>,
226     #[serde(skip_serializing_if = "Option::is_none")]
227     pub command: Option<lsp_types::Command>,
228     #[serde(skip_serializing_if = "Option::is_none")]
229     pub edit: Option<SnippetWorkspaceEdit>,
230 }
231
232 #[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)]
233 #[serde(rename_all = "camelCase")]
234 pub struct SnippetWorkspaceEdit {
235     #[serde(skip_serializing_if = "Option::is_none")]
236     pub changes: Option<HashMap<lsp_types::Url, Vec<lsp_types::TextEdit>>>,
237     #[serde(skip_serializing_if = "Option::is_none")]
238     pub document_changes: Option<Vec<SnippetDocumentChangeOperation>>,
239 }
240
241 #[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
242 #[serde(untagged, rename_all = "lowercase")]
243 pub enum SnippetDocumentChangeOperation {
244     Op(lsp_types::ResourceOp),
245     Edit(SnippetTextDocumentEdit),
246 }
247
248 #[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
249 #[serde(rename_all = "camelCase")]
250 pub struct SnippetTextDocumentEdit {
251     pub text_document: lsp_types::VersionedTextDocumentIdentifier,
252     pub edits: Vec<SnippetTextEdit>,
253 }
254
255 #[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)]
256 #[serde(rename_all = "camelCase")]
257 pub struct SnippetTextEdit {
258     pub range: Range,
259     pub new_text: String,
260     #[serde(skip_serializing_if = "Option::is_none")]
261     pub insert_text_format: Option<lsp_types::InsertTextFormat>,
262 }
263
264 pub enum HoverRequest {}
265
266 impl Request for HoverRequest {
267     type Params = lsp_types::HoverParams;
268     type Result = Option<Hover>;
269     const METHOD: &'static str = "textDocument/hover";
270 }
271
272 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
273 pub struct Hover {
274     #[serde(flatten)]
275     pub hover: lsp_types::Hover,
276     #[serde(skip_serializing_if = "Vec::is_empty")]
277     pub actions: Vec<CommandLinkGroup>,
278 }
279
280 #[derive(Debug, PartialEq, Clone, Default, Deserialize, Serialize)]
281 pub struct CommandLinkGroup {
282     #[serde(skip_serializing_if = "Option::is_none")]
283     pub title: Option<String>,
284     pub commands: Vec<CommandLink>,
285 }
286
287 // LSP v3.15 Command does not have a `tooltip` field, vscode supports one.
288 #[derive(Debug, PartialEq, Clone, Default, Deserialize, Serialize)]
289 pub struct CommandLink {
290     #[serde(flatten)]
291     pub command: lsp_types::Command,
292     #[serde(skip_serializing_if = "Option::is_none")]
293     pub tooltip: Option<String>,
294 }