1 //! rust-analyzer extensions to the LSP.
3 use std::{collections::HashMap, path::PathBuf};
5 use lsp_types::request::Request;
7 notification::Notification, CodeActionKind, Position, Range, TextDocumentIdentifier,
9 use serde::{Deserialize, Serialize};
11 pub enum AnalyzerStatus {}
13 impl Request for AnalyzerStatus {
14 type Params = AnalyzerStatusParams;
16 const METHOD: &'static str = "rust-analyzer/analyzerStatus";
19 #[derive(Deserialize, Serialize, Debug)]
20 #[serde(rename_all = "camelCase")]
21 pub struct AnalyzerStatusParams {
22 pub text_document: Option<TextDocumentIdentifier>,
25 pub enum MemoryUsage {}
27 impl Request for MemoryUsage {
30 const METHOD: &'static str = "rust-analyzer/memoryUsage";
33 pub enum ReloadWorkspace {}
35 impl Request for ReloadWorkspace {
38 const METHOD: &'static str = "rust-analyzer/reloadWorkspace";
41 pub enum SyntaxTree {}
43 impl Request for SyntaxTree {
44 type Params = SyntaxTreeParams;
46 const METHOD: &'static str = "rust-analyzer/syntaxTree";
49 #[derive(Deserialize, Serialize, Debug)]
50 #[serde(rename_all = "camelCase")]
51 pub struct SyntaxTreeParams {
52 pub text_document: TextDocumentIdentifier,
53 pub range: Option<Range>,
58 impl Request for ViewHir {
59 type Params = lsp_types::TextDocumentPositionParams;
61 const METHOD: &'static str = "rust-analyzer/viewHir";
64 pub enum ExpandMacro {}
66 impl Request for ExpandMacro {
67 type Params = ExpandMacroParams;
68 type Result = Option<ExpandedMacro>;
69 const METHOD: &'static str = "rust-analyzer/expandMacro";
72 #[derive(Deserialize, Serialize, Debug)]
73 #[serde(rename_all = "camelCase")]
74 pub struct ExpandMacroParams {
75 pub text_document: TextDocumentIdentifier,
76 pub position: Position,
79 #[derive(Deserialize, Serialize, Debug)]
80 #[serde(rename_all = "camelCase")]
81 pub struct ExpandedMacro {
83 pub expansion: String,
86 pub enum MatchingBrace {}
88 impl Request for MatchingBrace {
89 type Params = MatchingBraceParams;
90 type Result = Vec<Position>;
91 const METHOD: &'static str = "experimental/matchingBrace";
94 #[derive(Deserialize, Serialize, Debug)]
95 #[serde(rename_all = "camelCase")]
96 pub struct MatchingBraceParams {
97 pub text_document: TextDocumentIdentifier,
98 pub positions: Vec<Position>,
101 pub enum ParentModule {}
103 impl Request for ParentModule {
104 type Params = lsp_types::TextDocumentPositionParams;
105 type Result = Option<lsp_types::GotoDefinitionResponse>;
106 const METHOD: &'static str = "experimental/parentModule";
109 pub enum JoinLines {}
111 impl Request for JoinLines {
112 type Params = JoinLinesParams;
113 type Result = Vec<lsp_types::TextEdit>;
114 const METHOD: &'static str = "experimental/joinLines";
117 #[derive(Deserialize, Serialize, Debug)]
118 #[serde(rename_all = "camelCase")]
119 pub struct JoinLinesParams {
120 pub text_document: TextDocumentIdentifier,
121 pub ranges: Vec<Range>,
126 impl Request for OnEnter {
127 type Params = lsp_types::TextDocumentPositionParams;
128 type Result = Option<Vec<SnippetTextEdit>>;
129 const METHOD: &'static str = "experimental/onEnter";
132 pub enum Runnables {}
134 impl Request for Runnables {
135 type Params = RunnablesParams;
136 type Result = Vec<Runnable>;
137 const METHOD: &'static str = "experimental/runnables";
140 #[derive(Serialize, Deserialize, Debug)]
141 #[serde(rename_all = "camelCase")]
142 pub struct RunnablesParams {
143 pub text_document: TextDocumentIdentifier,
144 pub position: Option<Position>,
147 #[derive(Deserialize, Serialize, Debug)]
148 #[serde(rename_all = "camelCase")]
149 pub struct Runnable {
151 #[serde(skip_serializing_if = "Option::is_none")]
152 pub location: Option<lsp_types::LocationLink>,
153 pub kind: RunnableKind,
154 pub args: CargoRunnable,
157 #[derive(Serialize, Deserialize, Debug)]
158 #[serde(rename_all = "lowercase")]
159 pub enum RunnableKind {
163 #[derive(Deserialize, Serialize, Debug)]
164 #[serde(rename_all = "camelCase")]
165 pub struct CargoRunnable {
166 // command to be executed instead of cargo
167 pub override_cargo: Option<String>,
168 #[serde(skip_serializing_if = "Option::is_none")]
169 pub workspace_root: Option<PathBuf>,
170 // command, --package and --lib stuff
171 pub cargo_args: Vec<String>,
172 // user-specified additional cargo args, like `--release`.
173 pub cargo_extra_args: Vec<String>,
175 pub executable_args: Vec<String>,
176 #[serde(skip_serializing_if = "Option::is_none")]
177 pub expect_test: Option<bool>,
180 pub enum RelatedTests {}
182 impl Request for RelatedTests {
183 type Params = lsp_types::TextDocumentPositionParams;
184 type Result = Vec<TestInfo>;
185 const METHOD: &'static str = "rust-analyzer/relatedTests";
188 #[derive(Debug, Deserialize, Serialize)]
189 pub struct TestInfo {
190 pub runnable: Runnable,
193 pub enum InlayHints {}
195 impl Request for InlayHints {
196 type Params = InlayHintsParams;
197 type Result = Vec<InlayHint>;
198 const METHOD: &'static str = "rust-analyzer/inlayHints";
201 #[derive(Serialize, Deserialize, Debug)]
202 #[serde(rename_all = "camelCase")]
203 pub struct InlayHintsParams {
204 pub text_document: TextDocumentIdentifier,
207 #[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
214 #[derive(Debug, Deserialize, Serialize)]
215 pub struct InlayHint {
223 impl Request for Ssr {
224 type Params = SsrParams;
225 type Result = lsp_types::WorkspaceEdit;
226 const METHOD: &'static str = "experimental/ssr";
229 #[derive(Debug, Deserialize, Serialize)]
230 #[serde(rename_all = "camelCase")]
231 pub struct SsrParams {
233 pub parse_only: bool,
235 /// File position where SSR was invoked. Paths in `query` will be resolved relative to this
238 pub position: lsp_types::TextDocumentPositionParams,
240 /// Current selections. Search/replace will be restricted to these if non-empty.
241 pub selections: Vec<lsp_types::Range>,
244 pub enum ServerStatusNotification {}
246 impl Notification for ServerStatusNotification {
247 type Params = ServerStatusParams;
248 const METHOD: &'static str = "experimental/serverStatus";
251 #[derive(Deserialize, Serialize, PartialEq, Eq, Clone)]
252 pub struct ServerStatusParams {
255 pub message: Option<String>,
258 #[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
259 #[serde(rename_all = "camelCase")]
266 pub enum CodeActionRequest {}
268 impl Request for CodeActionRequest {
269 type Params = lsp_types::CodeActionParams;
270 type Result = Option<Vec<CodeAction>>;
271 const METHOD: &'static str = "textDocument/codeAction";
274 pub enum CodeActionResolveRequest {}
275 impl Request for CodeActionResolveRequest {
276 type Params = CodeAction;
277 type Result = CodeAction;
278 const METHOD: &'static str = "codeAction/resolve";
281 #[derive(Debug, PartialEq, Clone, Default, Deserialize, Serialize)]
282 #[serde(rename_all = "camelCase")]
283 pub struct CodeAction {
285 #[serde(skip_serializing_if = "Option::is_none")]
286 pub group: Option<String>,
287 #[serde(skip_serializing_if = "Option::is_none")]
288 pub kind: Option<CodeActionKind>,
289 // We don't handle commands on the client-side
290 // #[serde(skip_serializing_if = "Option::is_none")]
291 // pub command: Option<lsp_types::Command>,
292 #[serde(skip_serializing_if = "Option::is_none")]
293 pub edit: Option<SnippetWorkspaceEdit>,
294 #[serde(skip_serializing_if = "Option::is_none")]
295 pub is_preferred: Option<bool>,
297 #[serde(skip_serializing_if = "Option::is_none")]
298 pub data: Option<CodeActionData>,
301 #[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
302 #[serde(rename_all = "camelCase")]
303 pub struct CodeActionData {
304 pub code_action_params: lsp_types::CodeActionParams,
308 #[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)]
309 #[serde(rename_all = "camelCase")]
310 pub struct SnippetWorkspaceEdit {
311 #[serde(skip_serializing_if = "Option::is_none")]
312 pub changes: Option<HashMap<lsp_types::Url, Vec<lsp_types::TextEdit>>>,
313 #[serde(skip_serializing_if = "Option::is_none")]
314 pub document_changes: Option<Vec<SnippetDocumentChangeOperation>>,
315 #[serde(skip_serializing_if = "Option::is_none")]
316 pub change_annotations:
317 Option<HashMap<lsp_types::ChangeAnnotationIdentifier, lsp_types::ChangeAnnotation>>,
320 #[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
321 #[serde(untagged, rename_all = "lowercase")]
322 pub enum SnippetDocumentChangeOperation {
323 Op(lsp_types::ResourceOp),
324 Edit(SnippetTextDocumentEdit),
327 #[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
328 #[serde(rename_all = "camelCase")]
329 pub struct SnippetTextDocumentEdit {
330 pub text_document: lsp_types::OptionalVersionedTextDocumentIdentifier,
331 pub edits: Vec<SnippetTextEdit>,
334 #[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)]
335 #[serde(rename_all = "camelCase")]
336 pub struct SnippetTextEdit {
338 pub new_text: String,
339 #[serde(skip_serializing_if = "Option::is_none")]
340 pub insert_text_format: Option<lsp_types::InsertTextFormat>,
341 /// The annotation id if this is an annotated
342 #[serde(skip_serializing_if = "Option::is_none")]
343 pub annotation_id: Option<lsp_types::ChangeAnnotationIdentifier>,
346 pub enum HoverRequest {}
348 impl Request for HoverRequest {
349 type Params = lsp_types::HoverParams;
350 type Result = Option<Hover>;
351 const METHOD: &'static str = "textDocument/hover";
354 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
357 pub hover: lsp_types::Hover,
358 #[serde(skip_serializing_if = "Vec::is_empty")]
359 pub actions: Vec<CommandLinkGroup>,
362 #[derive(Debug, PartialEq, Clone, Default, Deserialize, Serialize)]
363 pub struct CommandLinkGroup {
364 #[serde(skip_serializing_if = "Option::is_none")]
365 pub title: Option<String>,
366 pub commands: Vec<CommandLink>,
369 // LSP v3.15 Command does not have a `tooltip` field, vscode supports one.
370 #[derive(Debug, PartialEq, Clone, Default, Deserialize, Serialize)]
371 pub struct CommandLink {
373 pub command: lsp_types::Command,
374 #[serde(skip_serializing_if = "Option::is_none")]
375 pub tooltip: Option<String>,
378 pub enum ExternalDocs {}
380 impl Request for ExternalDocs {
381 type Params = lsp_types::TextDocumentPositionParams;
382 type Result = Option<lsp_types::Url>;
383 const METHOD: &'static str = "experimental/externalDocs";
386 pub enum OpenCargoToml {}
388 impl Request for OpenCargoToml {
389 type Params = OpenCargoTomlParams;
390 type Result = Option<lsp_types::GotoDefinitionResponse>;
391 const METHOD: &'static str = "experimental/openCargoToml";
394 #[derive(Serialize, Deserialize, Debug)]
395 #[serde(rename_all = "camelCase")]
396 pub struct OpenCargoTomlParams {
397 pub text_document: TextDocumentIdentifier,
400 /// Information about CodeLens, that is to be resolved.
401 #[derive(Debug, Serialize, Deserialize)]
402 #[serde(rename_all = "camelCase")]
403 pub(crate) enum CodeLensResolveData {
404 Impls(lsp_types::request::GotoImplementationParams),
405 References(lsp_types::TextDocumentPositionParams),
408 pub fn supports_utf8(caps: &lsp_types::ClientCapabilities) -> bool {
409 caps.offset_encoding.as_deref().unwrap_or_default().iter().any(|it| it == "utf-8")
414 impl Request for MoveItem {
415 type Params = MoveItemParams;
416 type Result = Vec<SnippetTextEdit>;
417 const METHOD: &'static str = "experimental/moveItem";
420 #[derive(Serialize, Deserialize, Debug)]
421 #[serde(rename_all = "camelCase")]
422 pub struct MoveItemParams {
423 pub direction: MoveItemDirection,
424 pub text_document: TextDocumentIdentifier,
428 #[derive(Serialize, Deserialize, Debug)]
429 pub enum MoveItemDirection {