diff options
28 files changed, 637 insertions, 717 deletions
diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index 5ac002d82..d983cd910 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs | |||
@@ -309,7 +309,8 @@ impl Analysis { | |||
309 | 309 | ||
310 | /// Returns an edit which should be applied when opening a new line, fixing | 310 | /// Returns an edit which should be applied when opening a new line, fixing |
311 | /// up minor stuff like continuing the comment. | 311 | /// up minor stuff like continuing the comment. |
312 | pub fn on_enter(&self, position: FilePosition) -> Cancelable<Option<SourceChange>> { | 312 | /// The edit will be a snippet (with `$0`). |
313 | pub fn on_enter(&self, position: FilePosition) -> Cancelable<Option<TextEdit>> { | ||
313 | self.with_db(|db| typing::on_enter(&db, position)) | 314 | self.with_db(|db| typing::on_enter(&db, position)) |
314 | } | 315 | } |
315 | 316 | ||
diff --git a/crates/ra_ide/src/typing/on_enter.rs b/crates/ra_ide/src/typing/on_enter.rs index e7d64b4f6..a40d8af9c 100644 --- a/crates/ra_ide/src/typing/on_enter.rs +++ b/crates/ra_ide/src/typing/on_enter.rs | |||
@@ -11,9 +11,7 @@ use ra_syntax::{ | |||
11 | }; | 11 | }; |
12 | use ra_text_edit::TextEdit; | 12 | use ra_text_edit::TextEdit; |
13 | 13 | ||
14 | use crate::{SourceChange, SourceFileEdit}; | 14 | pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<TextEdit> { |
15 | |||
16 | pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<SourceChange> { | ||
17 | let parse = db.parse(position.file_id); | 15 | let parse = db.parse(position.file_id); |
18 | let file = parse.tree(); | 16 | let file = parse.tree(); |
19 | let comment = file | 17 | let comment = file |
@@ -41,9 +39,7 @@ pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<Sour | |||
41 | let inserted = format!("\n{}{} $0", indent, prefix); | 39 | let inserted = format!("\n{}{} $0", indent, prefix); |
42 | let edit = TextEdit::insert(position.offset, inserted); | 40 | let edit = TextEdit::insert(position.offset, inserted); |
43 | 41 | ||
44 | let mut res = SourceChange::from(SourceFileEdit { edit, file_id: position.file_id }); | 42 | Some(edit) |
45 | res.is_snippet = true; | ||
46 | Some(res) | ||
47 | } | 43 | } |
48 | 44 | ||
49 | fn followed_by_comment(comment: &ast::Comment) -> bool { | 45 | fn followed_by_comment(comment: &ast::Comment) -> bool { |
@@ -90,9 +86,8 @@ mod tests { | |||
90 | let (analysis, file_id) = single_file(&before); | 86 | let (analysis, file_id) = single_file(&before); |
91 | let result = analysis.on_enter(FilePosition { offset, file_id }).unwrap()?; | 87 | let result = analysis.on_enter(FilePosition { offset, file_id }).unwrap()?; |
92 | 88 | ||
93 | assert_eq!(result.source_file_edits.len(), 1); | ||
94 | let mut actual = before.to_string(); | 89 | let mut actual = before.to_string(); |
95 | result.source_file_edits[0].edit.apply(&mut actual); | 90 | result.apply(&mut actual); |
96 | Some(actual) | 91 | Some(actual) |
97 | } | 92 | } |
98 | 93 | ||
diff --git a/crates/rust-analyzer/src/caps.rs b/crates/rust-analyzer/src/caps.rs index 780fc9317..345693524 100644 --- a/crates/rust-analyzer/src/caps.rs +++ b/crates/rust-analyzer/src/caps.rs | |||
@@ -85,6 +85,8 @@ pub fn server_capabilities(client_caps: &ClientCapabilities) -> ServerCapabiliti | |||
85 | experimental: Some(json!({ | 85 | experimental: Some(json!({ |
86 | "joinLines": true, | 86 | "joinLines": true, |
87 | "ssr": true, | 87 | "ssr": true, |
88 | "onEnter": true, | ||
89 | "parentModule": true, | ||
88 | })), | 90 | })), |
89 | } | 91 | } |
90 | } | 92 | } |
diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs index 52e4fcbec..acb1dacb6 100644 --- a/crates/rust-analyzer/src/lsp_ext.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs | |||
@@ -3,7 +3,7 @@ | |||
3 | use std::{collections::HashMap, path::PathBuf}; | 3 | use std::{collections::HashMap, path::PathBuf}; |
4 | 4 | ||
5 | use lsp_types::request::Request; | 5 | use lsp_types::request::Request; |
6 | use lsp_types::{Location, Position, Range, TextDocumentIdentifier}; | 6 | use lsp_types::{Position, Range, TextDocumentIdentifier}; |
7 | use rustc_hash::FxHashMap; | 7 | use rustc_hash::FxHashMap; |
8 | use serde::{Deserialize, Serialize}; | 8 | use serde::{Deserialize, Serialize}; |
9 | 9 | ||
@@ -50,7 +50,7 @@ impl Request for ExpandMacro { | |||
50 | #[serde(rename_all = "camelCase")] | 50 | #[serde(rename_all = "camelCase")] |
51 | pub struct ExpandMacroParams { | 51 | pub struct ExpandMacroParams { |
52 | pub text_document: TextDocumentIdentifier, | 52 | pub text_document: TextDocumentIdentifier, |
53 | pub position: Option<Position>, | 53 | pub position: Position, |
54 | } | 54 | } |
55 | 55 | ||
56 | #[derive(Deserialize, Serialize, Debug)] | 56 | #[derive(Deserialize, Serialize, Debug)] |
@@ -79,8 +79,8 @@ pub enum ParentModule {} | |||
79 | 79 | ||
80 | impl Request for ParentModule { | 80 | impl Request for ParentModule { |
81 | type Params = lsp_types::TextDocumentPositionParams; | 81 | type Params = lsp_types::TextDocumentPositionParams; |
82 | type Result = Vec<Location>; | 82 | type Result = Option<lsp_types::GotoDefinitionResponse>; |
83 | const METHOD: &'static str = "rust-analyzer/parentModule"; | 83 | const METHOD: &'static str = "experimental/parentModule"; |
84 | } | 84 | } |
85 | 85 | ||
86 | pub enum JoinLines {} | 86 | pub enum JoinLines {} |
@@ -102,8 +102,8 @@ pub enum OnEnter {} | |||
102 | 102 | ||
103 | impl Request for OnEnter { | 103 | impl Request for OnEnter { |
104 | type Params = lsp_types::TextDocumentPositionParams; | 104 | type Params = lsp_types::TextDocumentPositionParams; |
105 | type Result = Option<SnippetWorkspaceEdit>; | 105 | type Result = Option<Vec<SnippetTextEdit>>; |
106 | const METHOD: &'static str = "rust-analyzer/onEnter"; | 106 | const METHOD: &'static str = "experimental/onEnter"; |
107 | } | 107 | } |
108 | 108 | ||
109 | pub enum Runnables {} | 109 | pub enum Runnables {} |
diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs index d73107968..1f910ff82 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs | |||
@@ -72,15 +72,10 @@ pub fn handle_expand_macro( | |||
72 | let _p = profile("handle_expand_macro"); | 72 | let _p = profile("handle_expand_macro"); |
73 | let file_id = from_proto::file_id(&world, ¶ms.text_document.uri)?; | 73 | let file_id = from_proto::file_id(&world, ¶ms.text_document.uri)?; |
74 | let line_index = world.analysis().file_line_index(file_id)?; | 74 | let line_index = world.analysis().file_line_index(file_id)?; |
75 | let offset = params.position.map(|p| from_proto::offset(&line_index, p)); | 75 | let offset = from_proto::offset(&line_index, params.position); |
76 | 76 | ||
77 | match offset { | 77 | let res = world.analysis().expand_macro(FilePosition { file_id, offset })?; |
78 | None => Ok(None), | 78 | Ok(res.map(|it| lsp_ext::ExpandedMacro { name: it.name, expansion: it.expansion })) |
79 | Some(offset) => { | ||
80 | let res = world.analysis().expand_macro(FilePosition { file_id, offset })?; | ||
81 | Ok(res.map(|it| lsp_ext::ExpandedMacro { name: it.name, expansion: it.expansion })) | ||
82 | } | ||
83 | } | ||
84 | } | 79 | } |
85 | 80 | ||
86 | pub fn handle_selection_range( | 81 | pub fn handle_selection_range( |
@@ -174,13 +169,17 @@ pub fn handle_join_lines( | |||
174 | pub fn handle_on_enter( | 169 | pub fn handle_on_enter( |
175 | world: WorldSnapshot, | 170 | world: WorldSnapshot, |
176 | params: lsp_types::TextDocumentPositionParams, | 171 | params: lsp_types::TextDocumentPositionParams, |
177 | ) -> Result<Option<lsp_ext::SnippetWorkspaceEdit>> { | 172 | ) -> Result<Option<Vec<lsp_ext::SnippetTextEdit>>> { |
178 | let _p = profile("handle_on_enter"); | 173 | let _p = profile("handle_on_enter"); |
179 | let position = from_proto::file_position(&world, params)?; | 174 | let position = from_proto::file_position(&world, params)?; |
180 | match world.analysis().on_enter(position)? { | 175 | let edit = match world.analysis().on_enter(position)? { |
181 | None => Ok(None), | 176 | None => return Ok(None), |
182 | Some(source_change) => to_proto::snippet_workspace_edit(&world, source_change).map(Some), | 177 | Some(it) => it, |
183 | } | 178 | }; |
179 | let line_index = world.analysis().file_line_index(position.file_id)?; | ||
180 | let line_endings = world.file_line_endings(position.file_id); | ||
181 | let edit = to_proto::snippet_text_edit_vec(&line_index, line_endings, true, edit); | ||
182 | Ok(Some(edit)) | ||
184 | } | 183 | } |
185 | 184 | ||
186 | // Don't forget to add new trigger characters to `ServerCapabilities` in `caps.rs`. | 185 | // Don't forget to add new trigger characters to `ServerCapabilities` in `caps.rs`. |
@@ -345,11 +344,8 @@ pub fn handle_goto_definition( | |||
345 | None => return Ok(None), | 344 | None => return Ok(None), |
346 | Some(it) => it, | 345 | Some(it) => it, |
347 | }; | 346 | }; |
348 | let res = to_proto::goto_definition_response( | 347 | let src = FileRange { file_id: position.file_id, range: nav_info.range }; |
349 | &world, | 348 | let res = to_proto::goto_definition_response(&world, Some(src), nav_info.info)?; |
350 | FileRange { file_id: position.file_id, range: nav_info.range }, | ||
351 | nav_info.info, | ||
352 | )?; | ||
353 | Ok(Some(res)) | 349 | Ok(Some(res)) |
354 | } | 350 | } |
355 | 351 | ||
@@ -363,11 +359,8 @@ pub fn handle_goto_implementation( | |||
363 | None => return Ok(None), | 359 | None => return Ok(None), |
364 | Some(it) => it, | 360 | Some(it) => it, |
365 | }; | 361 | }; |
366 | let res = to_proto::goto_definition_response( | 362 | let src = FileRange { file_id: position.file_id, range: nav_info.range }; |
367 | &world, | 363 | let res = to_proto::goto_definition_response(&world, Some(src), nav_info.info)?; |
368 | FileRange { file_id: position.file_id, range: nav_info.range }, | ||
369 | nav_info.info, | ||
370 | )?; | ||
371 | Ok(Some(res)) | 364 | Ok(Some(res)) |
372 | } | 365 | } |
373 | 366 | ||
@@ -381,26 +374,20 @@ pub fn handle_goto_type_definition( | |||
381 | None => return Ok(None), | 374 | None => return Ok(None), |
382 | Some(it) => it, | 375 | Some(it) => it, |
383 | }; | 376 | }; |
384 | let res = to_proto::goto_definition_response( | 377 | let src = FileRange { file_id: position.file_id, range: nav_info.range }; |
385 | &world, | 378 | let res = to_proto::goto_definition_response(&world, Some(src), nav_info.info)?; |
386 | FileRange { file_id: position.file_id, range: nav_info.range }, | ||
387 | nav_info.info, | ||
388 | )?; | ||
389 | Ok(Some(res)) | 379 | Ok(Some(res)) |
390 | } | 380 | } |
391 | 381 | ||
392 | pub fn handle_parent_module( | 382 | pub fn handle_parent_module( |
393 | world: WorldSnapshot, | 383 | world: WorldSnapshot, |
394 | params: lsp_types::TextDocumentPositionParams, | 384 | params: lsp_types::TextDocumentPositionParams, |
395 | ) -> Result<Vec<Location>> { | 385 | ) -> Result<Option<lsp_types::GotoDefinitionResponse>> { |
396 | let _p = profile("handle_parent_module"); | 386 | let _p = profile("handle_parent_module"); |
397 | let position = from_proto::file_position(&world, params)?; | 387 | let position = from_proto::file_position(&world, params)?; |
398 | world | 388 | let navs = world.analysis().parent_module(position)?; |
399 | .analysis() | 389 | let res = to_proto::goto_definition_response(&world, None, navs)?; |
400 | .parent_module(position)? | 390 | Ok(Some(res)) |
401 | .into_iter() | ||
402 | .map(|it| to_proto::location(&world, it.file_range())) | ||
403 | .collect::<Result<Vec<_>>>() | ||
404 | } | 391 | } |
405 | 392 | ||
406 | pub fn handle_runnables( | 393 | pub fn handle_runnables( |
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index ebaad4d19..8e8e7033d 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs | |||
@@ -135,6 +135,18 @@ pub(crate) fn text_edit_vec( | |||
135 | text_edit.into_iter().map(|indel| self::text_edit(line_index, line_endings, indel)).collect() | 135 | text_edit.into_iter().map(|indel| self::text_edit(line_index, line_endings, indel)).collect() |
136 | } | 136 | } |
137 | 137 | ||
138 | pub(crate) fn snippet_text_edit_vec( | ||
139 | line_index: &LineIndex, | ||
140 | line_endings: LineEndings, | ||
141 | is_snippet: bool, | ||
142 | text_edit: TextEdit, | ||
143 | ) -> Vec<lsp_ext::SnippetTextEdit> { | ||
144 | text_edit | ||
145 | .into_iter() | ||
146 | .map(|indel| self::snippet_text_edit(line_index, line_endings, is_snippet, indel)) | ||
147 | .collect() | ||
148 | } | ||
149 | |||
138 | pub(crate) fn completion_item( | 150 | pub(crate) fn completion_item( |
139 | line_index: &LineIndex, | 151 | line_index: &LineIndex, |
140 | line_endings: LineEndings, | 152 | line_endings: LineEndings, |
@@ -392,13 +404,20 @@ pub(crate) fn location(world: &WorldSnapshot, frange: FileRange) -> Result<lsp_t | |||
392 | 404 | ||
393 | pub(crate) fn location_link( | 405 | pub(crate) fn location_link( |
394 | world: &WorldSnapshot, | 406 | world: &WorldSnapshot, |
395 | src: FileRange, | 407 | src: Option<FileRange>, |
396 | target: NavigationTarget, | 408 | target: NavigationTarget, |
397 | ) -> Result<lsp_types::LocationLink> { | 409 | ) -> Result<lsp_types::LocationLink> { |
398 | let src_location = location(world, src)?; | 410 | let origin_selection_range = match src { |
411 | Some(src) => { | ||
412 | let line_index = world.analysis().file_line_index(src.file_id)?; | ||
413 | let range = range(&line_index, src.range); | ||
414 | Some(range) | ||
415 | } | ||
416 | None => None, | ||
417 | }; | ||
399 | let (target_uri, target_range, target_selection_range) = location_info(world, target)?; | 418 | let (target_uri, target_range, target_selection_range) = location_info(world, target)?; |
400 | let res = lsp_types::LocationLink { | 419 | let res = lsp_types::LocationLink { |
401 | origin_selection_range: Some(src_location.range), | 420 | origin_selection_range, |
402 | target_uri, | 421 | target_uri, |
403 | target_range, | 422 | target_range, |
404 | target_selection_range, | 423 | target_selection_range, |
@@ -421,7 +440,7 @@ fn location_info( | |||
421 | 440 | ||
422 | pub(crate) fn goto_definition_response( | 441 | pub(crate) fn goto_definition_response( |
423 | world: &WorldSnapshot, | 442 | world: &WorldSnapshot, |
424 | src: FileRange, | 443 | src: Option<FileRange>, |
425 | targets: Vec<NavigationTarget>, | 444 | targets: Vec<NavigationTarget>, |
426 | ) -> Result<lsp_types::GotoDefinitionResponse> { | 445 | ) -> Result<lsp_types::GotoDefinitionResponse> { |
427 | if world.config.client_caps.location_link { | 446 | if world.config.client_caps.location_link { |
diff --git a/crates/rust-analyzer/tests/heavy_tests/main.rs b/crates/rust-analyzer/tests/heavy_tests/main.rs index 738a9a8e3..b1bfc968a 100644 --- a/crates/rust-analyzer/tests/heavy_tests/main.rs +++ b/crates/rust-analyzer/tests/heavy_tests/main.rs | |||
@@ -473,23 +473,14 @@ fn main() {{}} | |||
473 | text_document: server.doc_id("src/m0.rs"), | 473 | text_document: server.doc_id("src/m0.rs"), |
474 | position: Position { line: 0, character: 5 }, | 474 | position: Position { line: 0, character: 5 }, |
475 | }, | 475 | }, |
476 | json!({ | 476 | json!([{ |
477 | "documentChanges": [ | 477 | "insertTextFormat": 2, |
478 | { | 478 | "newText": "\n/// $0", |
479 | "edits": [ | 479 | "range": { |
480 | { | 480 | "end": { "character": 5, "line": 0 }, |
481 | "insertTextFormat": 2, | 481 | "start": { "character": 5, "line": 0 } |
482 | "newText": "\n/// $0", | ||
483 | "range": { | ||
484 | "end": { "character": 5, "line": 0 }, | ||
485 | "start": { "character": 5, "line": 0 } | ||
486 | } | ||
487 | } | ||
488 | ], | ||
489 | "textDocument": { "uri": "file:///[..]src/m0.rs", "version": null } | ||
490 | } | 482 | } |
491 | ] | 483 | }]), |
492 | }), | ||
493 | ); | 484 | ); |
494 | let elapsed = start.elapsed(); | 485 | let elapsed = start.elapsed(); |
495 | assert!(elapsed.as_millis() < 2000, "typing enter took {:?}", elapsed); | 486 | assert!(elapsed.as_millis() < 2000, "typing enter took {:?}", elapsed); |
@@ -519,23 +510,14 @@ version = \"0.0.0\" | |||
519 | text_document: server.doc_id("src/main.rs"), | 510 | text_document: server.doc_id("src/main.rs"), |
520 | position: Position { line: 0, character: 8 }, | 511 | position: Position { line: 0, character: 8 }, |
521 | }, | 512 | }, |
522 | json!({ | 513 | json!([{ |
523 | "documentChanges": [ | 514 | "insertTextFormat": 2, |
524 | { | 515 | "newText": "\r\n/// $0", |
525 | "edits": [ | 516 | "range": { |
526 | { | 517 | "end": { "line": 0, "character": 8 }, |
527 | "insertTextFormat": 2, | 518 | "start": { "line": 0, "character": 8 } |
528 | "newText": "\r\n/// $0", | ||
529 | "range": { | ||
530 | "end": { "line": 0, "character": 8 }, | ||
531 | "start": { "line": 0, "character": 8 } | ||
532 | } | ||
533 | } | ||
534 | ], | ||
535 | "textDocument": { "uri": "file:///[..]src/main.rs", "version": null } | ||
536 | } | 519 | } |
537 | ] | 520 | }]), |
538 | }), | ||
539 | ); | 521 | ); |
540 | } | 522 | } |
541 | 523 | ||
diff --git a/docs/dev/lsp-extensions.md b/docs/dev/lsp-extensions.md index 55035cfae..209f470eb 100644 --- a/docs/dev/lsp-extensions.md +++ b/docs/dev/lsp-extensions.md | |||
@@ -87,6 +87,40 @@ Invoking code action at this position will yield two code actions for importing | |||
87 | * Is a fixed two-level structure enough? | 87 | * Is a fixed two-level structure enough? |
88 | * Should we devise a general way to encode custom interaction protocols for GUI refactorings? | 88 | * Should we devise a general way to encode custom interaction protocols for GUI refactorings? |
89 | 89 | ||
90 | ## Parent Module | ||
91 | |||
92 | **Issue:** https://github.com/microsoft/language-server-protocol/issues/1002 | ||
93 | |||
94 | **Server Capability:** `{ "parentModule": boolean }` | ||
95 | |||
96 | This request is send from client to server to handle "Goto Parent Module" editor action. | ||
97 | |||
98 | **Method:** `experimental/parentModule` | ||
99 | |||
100 | **Request:** `TextDocumentPositionParams` | ||
101 | |||
102 | **Response:** `Location | Location[] | LocationLink[] | null` | ||
103 | |||
104 | |||
105 | ### Example | ||
106 | |||
107 | ```rust | ||
108 | // src/main.rs | ||
109 | mod foo; | ||
110 | // src/foo.rs | ||
111 | |||
112 | /* cursor here*/ | ||
113 | ``` | ||
114 | |||
115 | `experimental/parentModule` returns a single `Link` to the `mod foo;` declaration. | ||
116 | |||
117 | ### Unresolved Question | ||
118 | |||
119 | * An alternative would be to use a more general "gotoSuper" request, which would work for super methods, super classes and super modules. | ||
120 | This is the approach IntelliJ Rust is takeing. | ||
121 | However, experience shows that super module (which generally has a feeling of navigation between files) should be separate. | ||
122 | If you want super module, but the cursor happens to be inside an overriden function, the behavior with single "gotoSuper" request is surprising. | ||
123 | |||
90 | ## Join Lines | 124 | ## Join Lines |
91 | 125 | ||
92 | **Issue:** https://github.com/microsoft/language-server-protocol/issues/992 | 126 | **Issue:** https://github.com/microsoft/language-server-protocol/issues/992 |
@@ -108,11 +142,7 @@ interface JoinLinesParams { | |||
108 | } | 142 | } |
109 | ``` | 143 | ``` |
110 | 144 | ||
111 | **Response:** | 145 | **Response:** `TextEdit[]` |
112 | |||
113 | ```typescript | ||
114 | TextEdit[] | ||
115 | ``` | ||
116 | 146 | ||
117 | ### Example | 147 | ### Example |
118 | 148 | ||
@@ -138,6 +168,59 @@ fn main() { | |||
138 | Currently this is left to editor's discretion, but it might be useful to specify on the server via snippets. | 168 | Currently this is left to editor's discretion, but it might be useful to specify on the server via snippets. |
139 | However, it then becomes unclear how it works with multi cursor. | 169 | However, it then becomes unclear how it works with multi cursor. |
140 | 170 | ||
171 | ## On Enter | ||
172 | |||
173 | **Issue:** https://github.com/microsoft/language-server-protocol/issues/1001 | ||
174 | |||
175 | **Server Capability:** `{ "onEnter": boolean }` | ||
176 | |||
177 | This request is send from client to server to handle <kbd>Enter</kbd> keypress. | ||
178 | |||
179 | **Method:** `experimental/onEnter` | ||
180 | |||
181 | **Request:**: `TextDocumentPositionParams` | ||
182 | |||
183 | **Response:** | ||
184 | |||
185 | ```typescript | ||
186 | SnippetTextEdit[] | ||
187 | ``` | ||
188 | |||
189 | ### Example | ||
190 | |||
191 | ```rust | ||
192 | fn main() { | ||
193 | // Some /*cursor here*/ docs | ||
194 | let x = 92; | ||
195 | } | ||
196 | ``` | ||
197 | |||
198 | `experimental/onEnter` returns the following snippet | ||
199 | |||
200 | ```rust | ||
201 | fn main() { | ||
202 | // Some | ||
203 | // $0 docs | ||
204 | let x = 92; | ||
205 | } | ||
206 | ``` | ||
207 | |||
208 | The primary goal of `onEnter` is to handle automatic indentation when opening a new line. | ||
209 | This is not yet implemented. | ||
210 | The secondary goal is to handle fixing up syntax, like continuing doc strings and comments, and escaping `\n` in string literals. | ||
211 | |||
212 | As proper cursor positioning is raison-d'etat for `onEnter`, it uses `SnippetTextEdit`. | ||
213 | |||
214 | ### Unresolved Question | ||
215 | |||
216 | * How to deal with synchronicity of the request? | ||
217 | One option is to require the client to block until the server returns the response. | ||
218 | Another option is to do a OT-style merging of edits from client and server. | ||
219 | A third option is to do a record-replay: client applies heuristic on enter immediatelly, then applies all user's keypresses. | ||
220 | When the server is ready with the response, the client rollbacks all the changes and applies the recorded actions on top of the correct response. | ||
221 | * How to deal with multiple carets? | ||
222 | * Should we extend this to arbitrary typed events and not just `onEnter`? | ||
223 | |||
141 | ## Structural Search Replace (SSR) | 224 | ## Structural Search Replace (SSR) |
142 | 225 | ||
143 | **Server Capability:** `{ "ssr": boolean }` | 226 | **Server Capability:** `{ "ssr": boolean }` |
@@ -265,7 +348,7 @@ Primarily for debugging, but very useful for all people working on rust-analyzer | |||
265 | ```typescript | 348 | ```typescript |
266 | interface ExpandMacroParams { | 349 | interface ExpandMacroParams { |
267 | textDocument: TextDocumentIdentifier, | 350 | textDocument: TextDocumentIdentifier, |
268 | position?: Position, | 351 | position: Position, |
269 | } | 352 | } |
270 | ``` | 353 | ``` |
271 | 354 | ||
diff --git a/editors/code/package.json b/editors/code/package.json index 3127e6a30..acf3ca4b5 100644 --- a/editors/code/package.json +++ b/editors/code/package.json | |||
@@ -587,6 +587,11 @@ | |||
587 | "description": "Style for attributes" | 587 | "description": "Style for attributes" |
588 | }, | 588 | }, |
589 | { | 589 | { |
590 | "id": "boolean", | ||
591 | "description": "Style for boolean literals", | ||
592 | "superType": "keyword" | ||
593 | }, | ||
594 | { | ||
590 | "id": "builtinType", | 595 | "id": "builtinType", |
591 | "description": "Style for builtin types", | 596 | "description": "Style for builtin types", |
592 | "superType": "type" | 597 | "superType": "type" |
diff --git a/editors/code/src/commands/syntax_tree.ts b/editors/code/src/ast_inspector.ts index a5446c327..4fdd167bd 100644 --- a/editors/code/src/commands/syntax_tree.ts +++ b/editors/code/src/ast_inspector.ts | |||
@@ -1,93 +1,15 @@ | |||
1 | import * as vscode from 'vscode'; | 1 | import * as vscode from 'vscode'; |
2 | import * as ra from '../rust-analyzer-api'; | ||
3 | |||
4 | import { Ctx, Cmd, Disposable } from '../ctx'; | ||
5 | import { isRustDocument, RustEditor, isRustEditor, sleep } from '../util'; | ||
6 | |||
7 | const AST_FILE_SCHEME = "rust-analyzer"; | ||
8 | |||
9 | // Opens the virtual file that will show the syntax tree | ||
10 | // | ||
11 | // The contents of the file come from the `TextDocumentContentProvider` | ||
12 | export function syntaxTree(ctx: Ctx): Cmd { | ||
13 | const tdcp = new TextDocumentContentProvider(ctx); | ||
14 | |||
15 | void new AstInspector(ctx); | ||
16 | |||
17 | ctx.pushCleanup(vscode.workspace.registerTextDocumentContentProvider(AST_FILE_SCHEME, tdcp)); | ||
18 | ctx.pushCleanup(vscode.languages.setLanguageConfiguration("ra_syntax_tree", { | ||
19 | brackets: [["[", ")"]], | ||
20 | })); | ||
21 | |||
22 | return async () => { | ||
23 | const editor = vscode.window.activeTextEditor; | ||
24 | const rangeEnabled = !!editor && !editor.selection.isEmpty; | ||
25 | |||
26 | const uri = rangeEnabled | ||
27 | ? vscode.Uri.parse(`${tdcp.uri.toString()}?range=true`) | ||
28 | : tdcp.uri; | ||
29 | |||
30 | const document = await vscode.workspace.openTextDocument(uri); | ||
31 | |||
32 | tdcp.eventEmitter.fire(uri); | ||
33 | |||
34 | void await vscode.window.showTextDocument(document, { | ||
35 | viewColumn: vscode.ViewColumn.Two, | ||
36 | preserveFocus: true | ||
37 | }); | ||
38 | }; | ||
39 | } | ||
40 | |||
41 | class TextDocumentContentProvider implements vscode.TextDocumentContentProvider { | ||
42 | readonly uri = vscode.Uri.parse('rust-analyzer://syntaxtree/tree.rast'); | ||
43 | readonly eventEmitter = new vscode.EventEmitter<vscode.Uri>(); | ||
44 | |||
45 | |||
46 | constructor(private readonly ctx: Ctx) { | ||
47 | vscode.workspace.onDidChangeTextDocument(this.onDidChangeTextDocument, this, ctx.subscriptions); | ||
48 | vscode.window.onDidChangeActiveTextEditor(this.onDidChangeActiveTextEditor, this, ctx.subscriptions); | ||
49 | } | ||
50 | |||
51 | private onDidChangeTextDocument(event: vscode.TextDocumentChangeEvent) { | ||
52 | if (isRustDocument(event.document)) { | ||
53 | // We need to order this after language server updates, but there's no API for that. | ||
54 | // Hence, good old sleep(). | ||
55 | void sleep(10).then(() => this.eventEmitter.fire(this.uri)); | ||
56 | } | ||
57 | } | ||
58 | private onDidChangeActiveTextEditor(editor: vscode.TextEditor | undefined) { | ||
59 | if (editor && isRustEditor(editor)) { | ||
60 | this.eventEmitter.fire(this.uri); | ||
61 | } | ||
62 | } | ||
63 | |||
64 | provideTextDocumentContent(uri: vscode.Uri, ct: vscode.CancellationToken): vscode.ProviderResult<string> { | ||
65 | const rustEditor = this.ctx.activeRustEditor; | ||
66 | if (!rustEditor) return ''; | ||
67 | |||
68 | // When the range based query is enabled we take the range of the selection | ||
69 | const range = uri.query === 'range=true' && !rustEditor.selection.isEmpty | ||
70 | ? this.ctx.client.code2ProtocolConverter.asRange(rustEditor.selection) | ||
71 | : null; | ||
72 | |||
73 | const params = { textDocument: { uri: rustEditor.document.uri.toString() }, range, }; | ||
74 | return this.ctx.client.sendRequest(ra.syntaxTree, params, ct); | ||
75 | } | ||
76 | |||
77 | get onDidChange(): vscode.Event<vscode.Uri> { | ||
78 | return this.eventEmitter.event; | ||
79 | } | ||
80 | } | ||
81 | 2 | ||
3 | import { Ctx, Disposable } from './ctx'; | ||
4 | import { RustEditor, isRustEditor } from './util'; | ||
82 | 5 | ||
83 | // FIXME: consider implementing this via the Tree View API? | 6 | // FIXME: consider implementing this via the Tree View API? |
84 | // https://code.visualstudio.com/api/extension-guides/tree-view | 7 | // https://code.visualstudio.com/api/extension-guides/tree-view |
85 | class AstInspector implements vscode.HoverProvider, vscode.DefinitionProvider, Disposable { | 8 | export class AstInspector implements vscode.HoverProvider, vscode.DefinitionProvider, Disposable { |
86 | private readonly astDecorationType = vscode.window.createTextEditorDecorationType({ | 9 | private readonly astDecorationType = vscode.window.createTextEditorDecorationType({ |
87 | borderColor: new vscode.ThemeColor('rust_analyzer.syntaxTreeBorder'), | 10 | borderColor: new vscode.ThemeColor('rust_analyzer.syntaxTreeBorder'), |
88 | borderStyle: "solid", | 11 | borderStyle: "solid", |
89 | borderWidth: "2px", | 12 | borderWidth: "2px", |
90 | |||
91 | }); | 13 | }); |
92 | private rustEditor: undefined | RustEditor; | 14 | private rustEditor: undefined | RustEditor; |
93 | 15 | ||
@@ -113,7 +35,7 @@ class AstInspector implements vscode.HoverProvider, vscode.DefinitionProvider, D | |||
113 | }); | 35 | }); |
114 | 36 | ||
115 | constructor(ctx: Ctx) { | 37 | constructor(ctx: Ctx) { |
116 | ctx.pushCleanup(vscode.languages.registerHoverProvider({ scheme: AST_FILE_SCHEME }, this)); | 38 | ctx.pushCleanup(vscode.languages.registerHoverProvider({ scheme: 'rust-analyzer' }, this)); |
117 | ctx.pushCleanup(vscode.languages.registerDefinitionProvider({ language: "rust" }, this)); | 39 | ctx.pushCleanup(vscode.languages.registerDefinitionProvider({ language: "rust" }, this)); |
118 | vscode.workspace.onDidCloseTextDocument(this.onDidCloseTextDocument, this, ctx.subscriptions); | 40 | vscode.workspace.onDidCloseTextDocument(this.onDidCloseTextDocument, this, ctx.subscriptions); |
119 | vscode.workspace.onDidChangeTextDocument(this.onDidChangeTextDocument, this, ctx.subscriptions); | 41 | vscode.workspace.onDidChangeTextDocument(this.onDidChangeTextDocument, this, ctx.subscriptions); |
@@ -146,7 +68,7 @@ class AstInspector implements vscode.HoverProvider, vscode.DefinitionProvider, D | |||
146 | } | 68 | } |
147 | 69 | ||
148 | private findAstTextEditor(): undefined | vscode.TextEditor { | 70 | private findAstTextEditor(): undefined | vscode.TextEditor { |
149 | return vscode.window.visibleTextEditors.find(it => it.document.uri.scheme === AST_FILE_SCHEME); | 71 | return vscode.window.visibleTextEditors.find(it => it.document.uri.scheme === 'rust-analyzer'); |
150 | } | 72 | } |
151 | 73 | ||
152 | private setRustEditor(newRustEditor: undefined | RustEditor) { | 74 | private setRustEditor(newRustEditor: undefined | RustEditor) { |
diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts new file mode 100644 index 000000000..86302db37 --- /dev/null +++ b/editors/code/src/commands.ts | |||
@@ -0,0 +1,370 @@ | |||
1 | import * as vscode from 'vscode'; | ||
2 | import * as lc from 'vscode-languageclient'; | ||
3 | import * as ra from './lsp_ext'; | ||
4 | |||
5 | import { Ctx, Cmd } from './ctx'; | ||
6 | import { applySnippetWorkspaceEdit, applySnippetTextEdits } from './snippets'; | ||
7 | import { spawnSync } from 'child_process'; | ||
8 | import { RunnableQuickPick, selectRunnable, createTask } from './run'; | ||
9 | import { AstInspector } from './ast_inspector'; | ||
10 | import { isRustDocument, sleep, isRustEditor } from './util'; | ||
11 | |||
12 | export * from './ast_inspector'; | ||
13 | export * from './run'; | ||
14 | |||
15 | export function analyzerStatus(ctx: Ctx): Cmd { | ||
16 | const tdcp = new class implements vscode.TextDocumentContentProvider { | ||
17 | readonly uri = vscode.Uri.parse('rust-analyzer-status://status'); | ||
18 | readonly eventEmitter = new vscode.EventEmitter<vscode.Uri>(); | ||
19 | |||
20 | provideTextDocumentContent(_uri: vscode.Uri): vscode.ProviderResult<string> { | ||
21 | if (!vscode.window.activeTextEditor) return ''; | ||
22 | |||
23 | return ctx.client.sendRequest(ra.analyzerStatus, null); | ||
24 | } | ||
25 | |||
26 | get onDidChange(): vscode.Event<vscode.Uri> { | ||
27 | return this.eventEmitter.event; | ||
28 | } | ||
29 | }(); | ||
30 | |||
31 | let poller: NodeJS.Timer | undefined = undefined; | ||
32 | |||
33 | ctx.pushCleanup( | ||
34 | vscode.workspace.registerTextDocumentContentProvider( | ||
35 | 'rust-analyzer-status', | ||
36 | tdcp, | ||
37 | ), | ||
38 | ); | ||
39 | |||
40 | ctx.pushCleanup({ | ||
41 | dispose() { | ||
42 | if (poller !== undefined) { | ||
43 | clearInterval(poller); | ||
44 | } | ||
45 | }, | ||
46 | }); | ||
47 | |||
48 | return async () => { | ||
49 | if (poller === undefined) { | ||
50 | poller = setInterval(() => tdcp.eventEmitter.fire(tdcp.uri), 1000); | ||
51 | } | ||
52 | const document = await vscode.workspace.openTextDocument(tdcp.uri); | ||
53 | return vscode.window.showTextDocument(document, vscode.ViewColumn.Two, true); | ||
54 | }; | ||
55 | } | ||
56 | |||
57 | export function matchingBrace(ctx: Ctx): Cmd { | ||
58 | return async () => { | ||
59 | const editor = ctx.activeRustEditor; | ||
60 | const client = ctx.client; | ||
61 | if (!editor || !client) return; | ||
62 | |||
63 | const response = await client.sendRequest(ra.matchingBrace, { | ||
64 | textDocument: { uri: editor.document.uri.toString() }, | ||
65 | positions: editor.selections.map(s => | ||
66 | client.code2ProtocolConverter.asPosition(s.active), | ||
67 | ), | ||
68 | }); | ||
69 | editor.selections = editor.selections.map((sel, idx) => { | ||
70 | const active = client.protocol2CodeConverter.asPosition( | ||
71 | response[idx], | ||
72 | ); | ||
73 | const anchor = sel.isEmpty ? active : sel.anchor; | ||
74 | return new vscode.Selection(anchor, active); | ||
75 | }); | ||
76 | editor.revealRange(editor.selection); | ||
77 | }; | ||
78 | } | ||
79 | |||
80 | export function joinLines(ctx: Ctx): Cmd { | ||
81 | return async () => { | ||
82 | const editor = ctx.activeRustEditor; | ||
83 | const client = ctx.client; | ||
84 | if (!editor || !client) return; | ||
85 | |||
86 | const items: lc.TextEdit[] = await client.sendRequest(ra.joinLines, { | ||
87 | ranges: editor.selections.map((it) => client.code2ProtocolConverter.asRange(it)), | ||
88 | textDocument: { uri: editor.document.uri.toString() }, | ||
89 | }); | ||
90 | editor.edit((builder) => { | ||
91 | client.protocol2CodeConverter.asTextEdits(items).forEach((edit) => { | ||
92 | builder.replace(edit.range, edit.newText); | ||
93 | }); | ||
94 | }); | ||
95 | }; | ||
96 | } | ||
97 | |||
98 | export function onEnter(ctx: Ctx): Cmd { | ||
99 | async function handleKeypress() { | ||
100 | const editor = ctx.activeRustEditor; | ||
101 | const client = ctx.client; | ||
102 | |||
103 | if (!editor || !client) return false; | ||
104 | |||
105 | const lcEdits = await client.sendRequest(ra.onEnter, { | ||
106 | textDocument: { uri: editor.document.uri.toString() }, | ||
107 | position: client.code2ProtocolConverter.asPosition( | ||
108 | editor.selection.active, | ||
109 | ), | ||
110 | }).catch(_error => { | ||
111 | // client.logFailedRequest(OnEnterRequest.type, error); | ||
112 | return null; | ||
113 | }); | ||
114 | if (!lcEdits) return false; | ||
115 | |||
116 | const edits = client.protocol2CodeConverter.asTextEdits(lcEdits); | ||
117 | await applySnippetTextEdits(editor, edits); | ||
118 | return true; | ||
119 | } | ||
120 | |||
121 | return async () => { | ||
122 | if (await handleKeypress()) return; | ||
123 | |||
124 | await vscode.commands.executeCommand('default:type', { text: '\n' }); | ||
125 | }; | ||
126 | } | ||
127 | |||
128 | export function parentModule(ctx: Ctx): Cmd { | ||
129 | return async () => { | ||
130 | const editor = ctx.activeRustEditor; | ||
131 | const client = ctx.client; | ||
132 | if (!editor || !client) return; | ||
133 | |||
134 | const response = await client.sendRequest(ra.parentModule, { | ||
135 | textDocument: { uri: editor.document.uri.toString() }, | ||
136 | position: client.code2ProtocolConverter.asPosition( | ||
137 | editor.selection.active, | ||
138 | ), | ||
139 | }); | ||
140 | const loc = response[0]; | ||
141 | if (!loc) return; | ||
142 | |||
143 | const uri = client.protocol2CodeConverter.asUri(loc.targetUri); | ||
144 | const range = client.protocol2CodeConverter.asRange(loc.targetRange); | ||
145 | |||
146 | const doc = await vscode.workspace.openTextDocument(uri); | ||
147 | const e = await vscode.window.showTextDocument(doc); | ||
148 | e.selection = new vscode.Selection(range.start, range.start); | ||
149 | e.revealRange(range, vscode.TextEditorRevealType.InCenter); | ||
150 | }; | ||
151 | } | ||
152 | |||
153 | export function ssr(ctx: Ctx): Cmd { | ||
154 | return async () => { | ||
155 | const client = ctx.client; | ||
156 | if (!client) return; | ||
157 | |||
158 | const options: vscode.InputBoxOptions = { | ||
159 | value: "() ==>> ()", | ||
160 | prompt: "Enter request, for example 'Foo($a:expr) ==> Foo::new($a)' ", | ||
161 | validateInput: async (x: string) => { | ||
162 | try { | ||
163 | await client.sendRequest(ra.ssr, { query: x, parseOnly: true }); | ||
164 | } catch (e) { | ||
165 | return e.toString(); | ||
166 | } | ||
167 | return null; | ||
168 | } | ||
169 | }; | ||
170 | const request = await vscode.window.showInputBox(options); | ||
171 | if (!request) return; | ||
172 | |||
173 | const edit = await client.sendRequest(ra.ssr, { query: request, parseOnly: false }); | ||
174 | |||
175 | await vscode.workspace.applyEdit(client.protocol2CodeConverter.asWorkspaceEdit(edit)); | ||
176 | }; | ||
177 | } | ||
178 | |||
179 | export function serverVersion(ctx: Ctx): Cmd { | ||
180 | return async () => { | ||
181 | const { stdout } = spawnSync(ctx.serverPath, ["--version"], { encoding: "utf8" }); | ||
182 | const commitHash = stdout.slice(`rust-analyzer `.length).trim(); | ||
183 | const { releaseTag } = ctx.config.package; | ||
184 | |||
185 | void vscode.window.showInformationMessage( | ||
186 | `rust-analyzer version: ${releaseTag ?? "unreleased"} (${commitHash})` | ||
187 | ); | ||
188 | }; | ||
189 | } | ||
190 | |||
191 | export function toggleInlayHints(ctx: Ctx): Cmd { | ||
192 | return async () => { | ||
193 | await vscode | ||
194 | .workspace | ||
195 | .getConfiguration(`${ctx.config.rootSection}.inlayHints`) | ||
196 | .update('enable', !ctx.config.inlayHints.enable, vscode.ConfigurationTarget.Workspace); | ||
197 | }; | ||
198 | } | ||
199 | |||
200 | export function run(ctx: Ctx): Cmd { | ||
201 | let prevRunnable: RunnableQuickPick | undefined; | ||
202 | |||
203 | return async () => { | ||
204 | const item = await selectRunnable(ctx, prevRunnable); | ||
205 | if (!item) return; | ||
206 | |||
207 | item.detail = 'rerun'; | ||
208 | prevRunnable = item; | ||
209 | const task = createTask(item.runnable); | ||
210 | return await vscode.tasks.executeTask(task); | ||
211 | }; | ||
212 | } | ||
213 | |||
214 | // Opens the virtual file that will show the syntax tree | ||
215 | // | ||
216 | // The contents of the file come from the `TextDocumentContentProvider` | ||
217 | export function syntaxTree(ctx: Ctx): Cmd { | ||
218 | const tdcp = new class implements vscode.TextDocumentContentProvider { | ||
219 | readonly uri = vscode.Uri.parse('rust-analyzer://syntaxtree/tree.rast'); | ||
220 | readonly eventEmitter = new vscode.EventEmitter<vscode.Uri>(); | ||
221 | constructor() { | ||
222 | vscode.workspace.onDidChangeTextDocument(this.onDidChangeTextDocument, this, ctx.subscriptions); | ||
223 | vscode.window.onDidChangeActiveTextEditor(this.onDidChangeActiveTextEditor, this, ctx.subscriptions); | ||
224 | } | ||
225 | |||
226 | private onDidChangeTextDocument(event: vscode.TextDocumentChangeEvent) { | ||
227 | if (isRustDocument(event.document)) { | ||
228 | // We need to order this after language server updates, but there's no API for that. | ||
229 | // Hence, good old sleep(). | ||
230 | void sleep(10).then(() => this.eventEmitter.fire(this.uri)); | ||
231 | } | ||
232 | } | ||
233 | private onDidChangeActiveTextEditor(editor: vscode.TextEditor | undefined) { | ||
234 | if (editor && isRustEditor(editor)) { | ||
235 | this.eventEmitter.fire(this.uri); | ||
236 | } | ||
237 | } | ||
238 | |||
239 | provideTextDocumentContent(uri: vscode.Uri, ct: vscode.CancellationToken): vscode.ProviderResult<string> { | ||
240 | const rustEditor = ctx.activeRustEditor; | ||
241 | if (!rustEditor) return ''; | ||
242 | |||
243 | // When the range based query is enabled we take the range of the selection | ||
244 | const range = uri.query === 'range=true' && !rustEditor.selection.isEmpty | ||
245 | ? ctx.client.code2ProtocolConverter.asRange(rustEditor.selection) | ||
246 | : null; | ||
247 | |||
248 | const params = { textDocument: { uri: rustEditor.document.uri.toString() }, range, }; | ||
249 | return ctx.client.sendRequest(ra.syntaxTree, params, ct); | ||
250 | } | ||
251 | |||
252 | get onDidChange(): vscode.Event<vscode.Uri> { | ||
253 | return this.eventEmitter.event; | ||
254 | } | ||
255 | }; | ||
256 | |||
257 | void new AstInspector(ctx); | ||
258 | |||
259 | ctx.pushCleanup(vscode.workspace.registerTextDocumentContentProvider('rust-analyzer', tdcp)); | ||
260 | ctx.pushCleanup(vscode.languages.setLanguageConfiguration("ra_syntax_tree", { | ||
261 | brackets: [["[", ")"]], | ||
262 | })); | ||
263 | |||
264 | return async () => { | ||
265 | const editor = vscode.window.activeTextEditor; | ||
266 | const rangeEnabled = !!editor && !editor.selection.isEmpty; | ||
267 | |||
268 | const uri = rangeEnabled | ||
269 | ? vscode.Uri.parse(`${tdcp.uri.toString()}?range=true`) | ||
270 | : tdcp.uri; | ||
271 | |||
272 | const document = await vscode.workspace.openTextDocument(uri); | ||
273 | |||
274 | tdcp.eventEmitter.fire(uri); | ||
275 | |||
276 | void await vscode.window.showTextDocument(document, { | ||
277 | viewColumn: vscode.ViewColumn.Two, | ||
278 | preserveFocus: true | ||
279 | }); | ||
280 | }; | ||
281 | } | ||
282 | |||
283 | |||
284 | // Opens the virtual file that will show the syntax tree | ||
285 | // | ||
286 | // The contents of the file come from the `TextDocumentContentProvider` | ||
287 | export function expandMacro(ctx: Ctx): Cmd { | ||
288 | function codeFormat(expanded: ra.ExpandedMacro): string { | ||
289 | let result = `// Recursive expansion of ${expanded.name}! macro\n`; | ||
290 | result += '// ' + '='.repeat(result.length - 3); | ||
291 | result += '\n\n'; | ||
292 | result += expanded.expansion; | ||
293 | |||
294 | return result; | ||
295 | } | ||
296 | |||
297 | const tdcp = new class implements vscode.TextDocumentContentProvider { | ||
298 | uri = vscode.Uri.parse('rust-analyzer://expandMacro/[EXPANSION].rs'); | ||
299 | eventEmitter = new vscode.EventEmitter<vscode.Uri>(); | ||
300 | async provideTextDocumentContent(_uri: vscode.Uri): Promise<string> { | ||
301 | const editor = vscode.window.activeTextEditor; | ||
302 | const client = ctx.client; | ||
303 | if (!editor || !client) return ''; | ||
304 | |||
305 | const position = editor.selection.active; | ||
306 | |||
307 | const expanded = await client.sendRequest(ra.expandMacro, { | ||
308 | textDocument: { uri: editor.document.uri.toString() }, | ||
309 | position, | ||
310 | }); | ||
311 | |||
312 | if (expanded == null) return 'Not available'; | ||
313 | |||
314 | return codeFormat(expanded); | ||
315 | } | ||
316 | |||
317 | get onDidChange(): vscode.Event<vscode.Uri> { | ||
318 | return this.eventEmitter.event; | ||
319 | } | ||
320 | }(); | ||
321 | |||
322 | ctx.pushCleanup( | ||
323 | vscode.workspace.registerTextDocumentContentProvider( | ||
324 | 'rust-analyzer', | ||
325 | tdcp, | ||
326 | ), | ||
327 | ); | ||
328 | |||
329 | return async () => { | ||
330 | const document = await vscode.workspace.openTextDocument(tdcp.uri); | ||
331 | tdcp.eventEmitter.fire(tdcp.uri); | ||
332 | return vscode.window.showTextDocument( | ||
333 | document, | ||
334 | vscode.ViewColumn.Two, | ||
335 | true, | ||
336 | ); | ||
337 | }; | ||
338 | } | ||
339 | |||
340 | export function collectGarbage(ctx: Ctx): Cmd { | ||
341 | return async () => ctx.client.sendRequest(ra.collectGarbage, null); | ||
342 | } | ||
343 | |||
344 | export function showReferences(ctx: Ctx): Cmd { | ||
345 | return (uri: string, position: lc.Position, locations: lc.Location[]) => { | ||
346 | const client = ctx.client; | ||
347 | if (client) { | ||
348 | vscode.commands.executeCommand( | ||
349 | 'editor.action.showReferences', | ||
350 | vscode.Uri.parse(uri), | ||
351 | client.protocol2CodeConverter.asPosition(position), | ||
352 | locations.map(client.protocol2CodeConverter.asLocation), | ||
353 | ); | ||
354 | } | ||
355 | }; | ||
356 | } | ||
357 | |||
358 | export function applyActionGroup(_ctx: Ctx): Cmd { | ||
359 | return async (actions: { label: string; edit: vscode.WorkspaceEdit }[]) => { | ||
360 | const selectedAction = await vscode.window.showQuickPick(actions); | ||
361 | if (!selectedAction) return; | ||
362 | await applySnippetWorkspaceEdit(selectedAction.edit); | ||
363 | }; | ||
364 | } | ||
365 | |||
366 | export function applySnippetWorkspaceEditCommand(_ctx: Ctx): Cmd { | ||
367 | return async (edit: vscode.WorkspaceEdit) => { | ||
368 | await applySnippetWorkspaceEdit(edit); | ||
369 | }; | ||
370 | } | ||
diff --git a/editors/code/src/commands/analyzer_status.ts b/editors/code/src/commands/analyzer_status.ts deleted file mode 100644 index 09daa3402..000000000 --- a/editors/code/src/commands/analyzer_status.ts +++ /dev/null | |||
@@ -1,51 +0,0 @@ | |||
1 | import * as vscode from 'vscode'; | ||
2 | |||
3 | import * as ra from '../rust-analyzer-api'; | ||
4 | import { Ctx, Cmd } from '../ctx'; | ||
5 | |||
6 | // Shows status of rust-analyzer (for debugging) | ||
7 | export function analyzerStatus(ctx: Ctx): Cmd { | ||
8 | let poller: NodeJS.Timer | undefined = undefined; | ||
9 | const tdcp = new TextDocumentContentProvider(ctx); | ||
10 | |||
11 | ctx.pushCleanup( | ||
12 | vscode.workspace.registerTextDocumentContentProvider( | ||
13 | 'rust-analyzer-status', | ||
14 | tdcp, | ||
15 | ), | ||
16 | ); | ||
17 | |||
18 | ctx.pushCleanup({ | ||
19 | dispose() { | ||
20 | if (poller !== undefined) { | ||
21 | clearInterval(poller); | ||
22 | } | ||
23 | }, | ||
24 | }); | ||
25 | |||
26 | return async () => { | ||
27 | if (poller === undefined) { | ||
28 | poller = setInterval(() => tdcp.eventEmitter.fire(tdcp.uri), 1000); | ||
29 | } | ||
30 | const document = await vscode.workspace.openTextDocument(tdcp.uri); | ||
31 | return vscode.window.showTextDocument(document, vscode.ViewColumn.Two, true); | ||
32 | }; | ||
33 | } | ||
34 | |||
35 | class TextDocumentContentProvider implements vscode.TextDocumentContentProvider { | ||
36 | readonly uri = vscode.Uri.parse('rust-analyzer-status://status'); | ||
37 | readonly eventEmitter = new vscode.EventEmitter<vscode.Uri>(); | ||
38 | |||
39 | constructor(private readonly ctx: Ctx) { | ||
40 | } | ||
41 | |||
42 | provideTextDocumentContent(_uri: vscode.Uri): vscode.ProviderResult<string> { | ||
43 | if (!vscode.window.activeTextEditor) return ''; | ||
44 | |||
45 | return this.ctx.client.sendRequest(ra.analyzerStatus, null); | ||
46 | } | ||
47 | |||
48 | get onDidChange(): vscode.Event<vscode.Uri> { | ||
49 | return this.eventEmitter.event; | ||
50 | } | ||
51 | } | ||
diff --git a/editors/code/src/commands/expand_macro.ts b/editors/code/src/commands/expand_macro.ts deleted file mode 100644 index 23f2ef1d5..000000000 --- a/editors/code/src/commands/expand_macro.ts +++ /dev/null | |||
@@ -1,66 +0,0 @@ | |||
1 | import * as vscode from 'vscode'; | ||
2 | import * as ra from '../rust-analyzer-api'; | ||
3 | |||
4 | import { Ctx, Cmd } from '../ctx'; | ||
5 | |||
6 | // Opens the virtual file that will show the syntax tree | ||
7 | // | ||
8 | // The contents of the file come from the `TextDocumentContentProvider` | ||
9 | export function expandMacro(ctx: Ctx): Cmd { | ||
10 | const tdcp = new TextDocumentContentProvider(ctx); | ||
11 | ctx.pushCleanup( | ||
12 | vscode.workspace.registerTextDocumentContentProvider( | ||
13 | 'rust-analyzer', | ||
14 | tdcp, | ||
15 | ), | ||
16 | ); | ||
17 | |||
18 | return async () => { | ||
19 | const document = await vscode.workspace.openTextDocument(tdcp.uri); | ||
20 | tdcp.eventEmitter.fire(tdcp.uri); | ||
21 | return vscode.window.showTextDocument( | ||
22 | document, | ||
23 | vscode.ViewColumn.Two, | ||
24 | true, | ||
25 | ); | ||
26 | }; | ||
27 | } | ||
28 | |||
29 | function codeFormat(expanded: ra.ExpandedMacro): string { | ||
30 | let result = `// Recursive expansion of ${expanded.name}! macro\n`; | ||
31 | result += '// ' + '='.repeat(result.length - 3); | ||
32 | result += '\n\n'; | ||
33 | result += expanded.expansion; | ||
34 | |||
35 | return result; | ||
36 | } | ||
37 | |||
38 | class TextDocumentContentProvider | ||
39 | implements vscode.TextDocumentContentProvider { | ||
40 | uri = vscode.Uri.parse('rust-analyzer://expandMacro/[EXPANSION].rs'); | ||
41 | eventEmitter = new vscode.EventEmitter<vscode.Uri>(); | ||
42 | |||
43 | constructor(private readonly ctx: Ctx) { | ||
44 | } | ||
45 | |||
46 | async provideTextDocumentContent(_uri: vscode.Uri): Promise<string> { | ||
47 | const editor = vscode.window.activeTextEditor; | ||
48 | const client = this.ctx.client; | ||
49 | if (!editor || !client) return ''; | ||
50 | |||
51 | const position = editor.selection.active; | ||
52 | |||
53 | const expanded = await client.sendRequest(ra.expandMacro, { | ||
54 | textDocument: { uri: editor.document.uri.toString() }, | ||
55 | position, | ||
56 | }); | ||
57 | |||
58 | if (expanded == null) return 'Not available'; | ||
59 | |||
60 | return codeFormat(expanded); | ||
61 | } | ||
62 | |||
63 | get onDidChange(): vscode.Event<vscode.Uri> { | ||
64 | return this.eventEmitter.event; | ||
65 | } | ||
66 | } | ||
diff --git a/editors/code/src/commands/join_lines.ts b/editors/code/src/commands/join_lines.ts deleted file mode 100644 index 0bf1ee6e6..000000000 --- a/editors/code/src/commands/join_lines.ts +++ /dev/null | |||
@@ -1,22 +0,0 @@ | |||
1 | import * as ra from '../rust-analyzer-api'; | ||
2 | import * as lc from 'vscode-languageclient'; | ||
3 | |||
4 | import { Ctx, Cmd } from '../ctx'; | ||
5 | |||
6 | export function joinLines(ctx: Ctx): Cmd { | ||
7 | return async () => { | ||
8 | const editor = ctx.activeRustEditor; | ||
9 | const client = ctx.client; | ||
10 | if (!editor || !client) return; | ||
11 | |||
12 | const items: lc.TextEdit[] = await client.sendRequest(ra.joinLines, { | ||
13 | ranges: editor.selections.map((it) => client.code2ProtocolConverter.asRange(it)), | ||
14 | textDocument: { uri: editor.document.uri.toString() }, | ||
15 | }); | ||
16 | editor.edit((builder) => { | ||
17 | client.protocol2CodeConverter.asTextEdits(items).forEach((edit) => { | ||
18 | builder.replace(edit.range, edit.newText); | ||
19 | }); | ||
20 | }); | ||
21 | }; | ||
22 | } | ||
diff --git a/editors/code/src/commands/matching_brace.ts b/editors/code/src/commands/matching_brace.ts deleted file mode 100644 index 9c418b887..000000000 --- a/editors/code/src/commands/matching_brace.ts +++ /dev/null | |||
@@ -1,27 +0,0 @@ | |||
1 | import * as vscode from 'vscode'; | ||
2 | import * as ra from '../rust-analyzer-api'; | ||
3 | |||
4 | import { Ctx, Cmd } from '../ctx'; | ||
5 | |||
6 | export function matchingBrace(ctx: Ctx): Cmd { | ||
7 | return async () => { | ||
8 | const editor = ctx.activeRustEditor; | ||
9 | const client = ctx.client; | ||
10 | if (!editor || !client) return; | ||
11 | |||
12 | const response = await client.sendRequest(ra.matchingBrace, { | ||
13 | textDocument: { uri: editor.document.uri.toString() }, | ||
14 | positions: editor.selections.map(s => | ||
15 | client.code2ProtocolConverter.asPosition(s.active), | ||
16 | ), | ||
17 | }); | ||
18 | editor.selections = editor.selections.map((sel, idx) => { | ||
19 | const active = client.protocol2CodeConverter.asPosition( | ||
20 | response[idx], | ||
21 | ); | ||
22 | const anchor = sel.isEmpty ? active : sel.anchor; | ||
23 | return new vscode.Selection(anchor, active); | ||
24 | }); | ||
25 | editor.revealRange(editor.selection); | ||
26 | }; | ||
27 | } | ||
diff --git a/editors/code/src/commands/on_enter.ts b/editors/code/src/commands/on_enter.ts deleted file mode 100644 index a7871c31e..000000000 --- a/editors/code/src/commands/on_enter.ts +++ /dev/null | |||
@@ -1,35 +0,0 @@ | |||
1 | import * as vscode from 'vscode'; | ||
2 | import * as ra from '../rust-analyzer-api'; | ||
3 | |||
4 | import { Cmd, Ctx } from '../ctx'; | ||
5 | import { applySnippetWorkspaceEdit } from '.'; | ||
6 | |||
7 | async function handleKeypress(ctx: Ctx) { | ||
8 | const editor = ctx.activeRustEditor; | ||
9 | const client = ctx.client; | ||
10 | |||
11 | if (!editor || !client) return false; | ||
12 | |||
13 | const change = await client.sendRequest(ra.onEnter, { | ||
14 | textDocument: { uri: editor.document.uri.toString() }, | ||
15 | position: client.code2ProtocolConverter.asPosition( | ||
16 | editor.selection.active, | ||
17 | ), | ||
18 | }).catch(_error => { | ||
19 | // client.logFailedRequest(OnEnterRequest.type, error); | ||
20 | return null; | ||
21 | }); | ||
22 | if (!change) return false; | ||
23 | |||
24 | const workspaceEdit = client.protocol2CodeConverter.asWorkspaceEdit(change); | ||
25 | await applySnippetWorkspaceEdit(workspaceEdit); | ||
26 | return true; | ||
27 | } | ||
28 | |||
29 | export function onEnter(ctx: Ctx): Cmd { | ||
30 | return async () => { | ||
31 | if (await handleKeypress(ctx)) return; | ||
32 | |||
33 | await vscode.commands.executeCommand('default:type', { text: '\n' }); | ||
34 | }; | ||
35 | } | ||
diff --git a/editors/code/src/commands/parent_module.ts b/editors/code/src/commands/parent_module.ts deleted file mode 100644 index 8f78ddd71..000000000 --- a/editors/code/src/commands/parent_module.ts +++ /dev/null | |||
@@ -1,29 +0,0 @@ | |||
1 | import * as vscode from 'vscode'; | ||
2 | import * as ra from '../rust-analyzer-api'; | ||
3 | |||
4 | import { Ctx, Cmd } from '../ctx'; | ||
5 | |||
6 | export function parentModule(ctx: Ctx): Cmd { | ||
7 | return async () => { | ||
8 | const editor = ctx.activeRustEditor; | ||
9 | const client = ctx.client; | ||
10 | if (!editor || !client) return; | ||
11 | |||
12 | const response = await client.sendRequest(ra.parentModule, { | ||
13 | textDocument: { uri: editor.document.uri.toString() }, | ||
14 | position: client.code2ProtocolConverter.asPosition( | ||
15 | editor.selection.active, | ||
16 | ), | ||
17 | }); | ||
18 | const loc = response[0]; | ||
19 | if (loc == null) return; | ||
20 | |||
21 | const uri = client.protocol2CodeConverter.asUri(loc.uri); | ||
22 | const range = client.protocol2CodeConverter.asRange(loc.range); | ||
23 | |||
24 | const doc = await vscode.workspace.openTextDocument(uri); | ||
25 | const e = await vscode.window.showTextDocument(doc); | ||
26 | e.selection = new vscode.Selection(range.start, range.start); | ||
27 | e.revealRange(range, vscode.TextEditorRevealType.InCenter); | ||
28 | }; | ||
29 | } | ||
diff --git a/editors/code/src/commands/server_version.ts b/editors/code/src/commands/server_version.ts deleted file mode 100644 index d64ac726e..000000000 --- a/editors/code/src/commands/server_version.ts +++ /dev/null | |||
@@ -1,15 +0,0 @@ | |||
1 | import * as vscode from "vscode"; | ||
2 | import { spawnSync } from "child_process"; | ||
3 | import { Ctx, Cmd } from '../ctx'; | ||
4 | |||
5 | export function serverVersion(ctx: Ctx): Cmd { | ||
6 | return async () => { | ||
7 | const { stdout } = spawnSync(ctx.serverPath, ["--version"], { encoding: "utf8" }); | ||
8 | const commitHash = stdout.slice(`rust-analyzer `.length).trim(); | ||
9 | const { releaseTag } = ctx.config.package; | ||
10 | |||
11 | void vscode.window.showInformationMessage( | ||
12 | `rust-analyzer version: ${releaseTag ?? "unreleased"} (${commitHash})` | ||
13 | ); | ||
14 | }; | ||
15 | } | ||
diff --git a/editors/code/src/commands/ssr.ts b/editors/code/src/commands/ssr.ts deleted file mode 100644 index 5d40a64d2..000000000 --- a/editors/code/src/commands/ssr.ts +++ /dev/null | |||
@@ -1,30 +0,0 @@ | |||
1 | import * as vscode from 'vscode'; | ||
2 | import * as ra from "../rust-analyzer-api"; | ||
3 | |||
4 | import { Ctx, Cmd } from '../ctx'; | ||
5 | |||
6 | export function ssr(ctx: Ctx): Cmd { | ||
7 | return async () => { | ||
8 | const client = ctx.client; | ||
9 | if (!client) return; | ||
10 | |||
11 | const options: vscode.InputBoxOptions = { | ||
12 | value: "() ==>> ()", | ||
13 | prompt: "Enter request, for example 'Foo($a:expr) ==> Foo::new($a)' ", | ||
14 | validateInput: async (x: string) => { | ||
15 | try { | ||
16 | await client.sendRequest(ra.ssr, { query: x, parseOnly: true }); | ||
17 | } catch (e) { | ||
18 | return e.toString(); | ||
19 | } | ||
20 | return null; | ||
21 | } | ||
22 | }; | ||
23 | const request = await vscode.window.showInputBox(options); | ||
24 | if (!request) return; | ||
25 | |||
26 | const edit = await client.sendRequest(ra.ssr, { query: request, parseOnly: false }); | ||
27 | |||
28 | await vscode.workspace.applyEdit(client.protocol2CodeConverter.asWorkspaceEdit(edit)); | ||
29 | }; | ||
30 | } | ||
diff --git a/editors/code/src/commands/toggle_inlay_hints.ts b/editors/code/src/commands/toggle_inlay_hints.ts deleted file mode 100644 index 7606af8d0..000000000 --- a/editors/code/src/commands/toggle_inlay_hints.ts +++ /dev/null | |||
@@ -1,11 +0,0 @@ | |||
1 | import * as vscode from 'vscode'; | ||
2 | import { Ctx, Cmd } from '../ctx'; | ||
3 | |||
4 | export function toggleInlayHints(ctx: Ctx): Cmd { | ||
5 | return async () => { | ||
6 | await vscode | ||
7 | .workspace | ||
8 | .getConfiguration(`${ctx.config.rootSection}.inlayHints`) | ||
9 | .update('enable', !ctx.config.inlayHints.enable, vscode.ConfigurationTarget.Workspace); | ||
10 | }; | ||
11 | } | ||
diff --git a/editors/code/src/debug.ts b/editors/code/src/debug.ts index d3fe588e8..027504ecd 100644 --- a/editors/code/src/debug.ts +++ b/editors/code/src/debug.ts | |||
@@ -1,7 +1,7 @@ | |||
1 | import * as os from "os"; | 1 | import * as os from "os"; |
2 | import * as vscode from 'vscode'; | 2 | import * as vscode from 'vscode'; |
3 | import * as path from 'path'; | 3 | import * as path from 'path'; |
4 | import * as ra from './rust-analyzer-api'; | 4 | import * as ra from './lsp_ext'; |
5 | 5 | ||
6 | import { Cargo } from './cargo'; | 6 | import { Cargo } from './cargo'; |
7 | import { Ctx } from "./ctx"; | 7 | import { Ctx } from "./ctx"; |
diff --git a/editors/code/src/inlay_hints.ts b/editors/code/src/inlay_hints.ts index a2b07d003..9e6d6045f 100644 --- a/editors/code/src/inlay_hints.ts +++ b/editors/code/src/inlay_hints.ts | |||
@@ -1,6 +1,6 @@ | |||
1 | import * as lc from "vscode-languageclient"; | 1 | import * as lc from "vscode-languageclient"; |
2 | import * as vscode from 'vscode'; | 2 | import * as vscode from 'vscode'; |
3 | import * as ra from './rust-analyzer-api'; | 3 | import * as ra from './lsp_ext'; |
4 | 4 | ||
5 | import { Ctx, Disposable } from './ctx'; | 5 | import { Ctx, Disposable } from './ctx'; |
6 | import { sendRequestWithRetry, isRustDocument, RustDocument, RustEditor, sleep } from './util'; | 6 | import { sendRequestWithRetry, isRustDocument, RustDocument, RustEditor, sleep } from './util'; |
diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts new file mode 100644 index 000000000..4da12eb30 --- /dev/null +++ b/editors/code/src/lsp_ext.ts | |||
@@ -0,0 +1,84 @@ | |||
1 | /** | ||
2 | * This file mirrors `crates/rust-analyzer/src/req.rs` declarations. | ||
3 | */ | ||
4 | |||
5 | import * as lc from "vscode-languageclient"; | ||
6 | |||
7 | export const analyzerStatus = new lc.RequestType<null, string, void>("rust-analyzer/analyzerStatus"); | ||
8 | |||
9 | export const collectGarbage = new lc.RequestType<null, null, void>("rust-analyzer/collectGarbage"); | ||
10 | |||
11 | export interface SyntaxTreeParams { | ||
12 | textDocument: lc.TextDocumentIdentifier; | ||
13 | range: lc.Range | null; | ||
14 | } | ||
15 | export const syntaxTree = new lc.RequestType<SyntaxTreeParams, string, void>("rust-analyzer/syntaxTree"); | ||
16 | |||
17 | |||
18 | export interface ExpandMacroParams { | ||
19 | textDocument: lc.TextDocumentIdentifier; | ||
20 | position: lc.Position; | ||
21 | } | ||
22 | export interface ExpandedMacro { | ||
23 | name: string; | ||
24 | expansion: string; | ||
25 | } | ||
26 | export const expandMacro = new lc.RequestType<ExpandMacroParams, ExpandedMacro | null, void>("rust-analyzer/expandMacro"); | ||
27 | |||
28 | export interface MatchingBraceParams { | ||
29 | textDocument: lc.TextDocumentIdentifier; | ||
30 | positions: lc.Position[]; | ||
31 | } | ||
32 | export const matchingBrace = new lc.RequestType<MatchingBraceParams, lc.Position[], void>("experimental/matchingBrace"); | ||
33 | |||
34 | export const parentModule = new lc.RequestType<lc.TextDocumentPositionParams, lc.LocationLink[], void>("experimental/parentModule"); | ||
35 | |||
36 | export interface JoinLinesParams { | ||
37 | textDocument: lc.TextDocumentIdentifier; | ||
38 | ranges: lc.Range[]; | ||
39 | } | ||
40 | export const joinLines = new lc.RequestType<JoinLinesParams, lc.TextEdit[], void>("experimental/joinLines"); | ||
41 | |||
42 | export const onEnter = new lc.RequestType<lc.TextDocumentPositionParams, lc.TextEdit[], void>("experimental/onEnter"); | ||
43 | |||
44 | export interface RunnablesParams { | ||
45 | textDocument: lc.TextDocumentIdentifier; | ||
46 | position: lc.Position | null; | ||
47 | } | ||
48 | export interface Runnable { | ||
49 | range: lc.Range; | ||
50 | label: string; | ||
51 | bin: string; | ||
52 | args: string[]; | ||
53 | extraArgs: string[]; | ||
54 | env: { [key: string]: string }; | ||
55 | cwd: string | null; | ||
56 | } | ||
57 | export const runnables = new lc.RequestType<RunnablesParams, Runnable[], void>("rust-analyzer/runnables"); | ||
58 | |||
59 | export type InlayHint = InlayHint.TypeHint | InlayHint.ParamHint | InlayHint.ChainingHint; | ||
60 | |||
61 | export namespace InlayHint { | ||
62 | export const enum Kind { | ||
63 | TypeHint = "TypeHint", | ||
64 | ParamHint = "ParameterHint", | ||
65 | ChainingHint = "ChainingHint", | ||
66 | } | ||
67 | interface Common { | ||
68 | range: lc.Range; | ||
69 | label: string; | ||
70 | } | ||
71 | export type TypeHint = Common & { kind: Kind.TypeHint }; | ||
72 | export type ParamHint = Common & { kind: Kind.ParamHint }; | ||
73 | export type ChainingHint = Common & { kind: Kind.ChainingHint }; | ||
74 | } | ||
75 | export interface InlayHintsParams { | ||
76 | textDocument: lc.TextDocumentIdentifier; | ||
77 | } | ||
78 | export const inlayHints = new lc.RequestType<InlayHintsParams, InlayHint[], void>("rust-analyzer/inlayHints"); | ||
79 | |||
80 | export interface SsrParams { | ||
81 | query: string; | ||
82 | parseOnly: boolean; | ||
83 | } | ||
84 | export const ssr = new lc.RequestType<SsrParams, lc.WorkspaceEdit, void>('experimental/ssr'); | ||
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 0e5a20641..31ac81ee8 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts | |||
@@ -92,7 +92,6 @@ export async function activate(context: vscode.ExtensionContext) { | |||
92 | ctx.registerCommand('runSingle', commands.runSingle); | 92 | ctx.registerCommand('runSingle', commands.runSingle); |
93 | ctx.registerCommand('debugSingle', commands.debugSingle); | 93 | ctx.registerCommand('debugSingle', commands.debugSingle); |
94 | ctx.registerCommand('showReferences', commands.showReferences); | 94 | ctx.registerCommand('showReferences', commands.showReferences); |
95 | ctx.registerCommand('applySourceChange', commands.applySourceChange); | ||
96 | ctx.registerCommand('applySnippetWorkspaceEdit', commands.applySnippetWorkspaceEditCommand); | 95 | ctx.registerCommand('applySnippetWorkspaceEdit', commands.applySnippetWorkspaceEditCommand); |
97 | ctx.registerCommand('applyActionGroup', commands.applyActionGroup); | 96 | ctx.registerCommand('applyActionGroup', commands.applyActionGroup); |
98 | 97 | ||
diff --git a/editors/code/src/commands/runnables.ts b/editors/code/src/run.ts index 0bd30fb07..2a7a429cf 100644 --- a/editors/code/src/commands/runnables.ts +++ b/editors/code/src/run.ts | |||
@@ -1,13 +1,13 @@ | |||
1 | import * as vscode from 'vscode'; | 1 | import * as vscode from 'vscode'; |
2 | import * as lc from 'vscode-languageclient'; | 2 | import * as lc from 'vscode-languageclient'; |
3 | import * as ra from '../rust-analyzer-api'; | 3 | import * as ra from './lsp_ext'; |
4 | 4 | ||
5 | import { Ctx, Cmd } from '../ctx'; | 5 | import { Ctx, Cmd } from './ctx'; |
6 | import { startDebugSession, getDebugConfiguration } from '../debug'; | 6 | import { startDebugSession, getDebugConfiguration } from './debug'; |
7 | 7 | ||
8 | const quickPickButtons = [{ iconPath: new vscode.ThemeIcon("save"), tooltip: "Save as a launch.json configurtation." }]; | 8 | const quickPickButtons = [{ iconPath: new vscode.ThemeIcon("save"), tooltip: "Save as a launch.json configurtation." }]; |
9 | 9 | ||
10 | async function selectRunnable(ctx: Ctx, prevRunnable?: RunnableQuickPick, debuggeeOnly = false, showButtons: boolean = true): Promise<RunnableQuickPick | undefined> { | 10 | export async function selectRunnable(ctx: Ctx, prevRunnable?: RunnableQuickPick, debuggeeOnly = false, showButtons: boolean = true): Promise<RunnableQuickPick | undefined> { |
11 | const editor = ctx.activeRustEditor; | 11 | const editor = ctx.activeRustEditor; |
12 | const client = ctx.client; | 12 | const client = ctx.client; |
13 | if (!editor || !client) return; | 13 | if (!editor || !client) return; |
@@ -83,20 +83,6 @@ async function selectRunnable(ctx: Ctx, prevRunnable?: RunnableQuickPick, debugg | |||
83 | }); | 83 | }); |
84 | } | 84 | } |
85 | 85 | ||
86 | export function run(ctx: Ctx): Cmd { | ||
87 | let prevRunnable: RunnableQuickPick | undefined; | ||
88 | |||
89 | return async () => { | ||
90 | const item = await selectRunnable(ctx, prevRunnable); | ||
91 | if (!item) return; | ||
92 | |||
93 | item.detail = 'rerun'; | ||
94 | prevRunnable = item; | ||
95 | const task = createTask(item.runnable); | ||
96 | return await vscode.tasks.executeTask(task); | ||
97 | }; | ||
98 | } | ||
99 | |||
100 | export function runSingle(ctx: Ctx): Cmd { | 86 | export function runSingle(ctx: Ctx): Cmd { |
101 | return async (runnable: ra.Runnable) => { | 87 | return async (runnable: ra.Runnable) => { |
102 | const editor = ctx.activeRustEditor; | 88 | const editor = ctx.activeRustEditor; |
@@ -165,7 +151,7 @@ export function newDebugConfig(ctx: Ctx): Cmd { | |||
165 | }; | 151 | }; |
166 | } | 152 | } |
167 | 153 | ||
168 | class RunnableQuickPick implements vscode.QuickPickItem { | 154 | export class RunnableQuickPick implements vscode.QuickPickItem { |
169 | public label: string; | 155 | public label: string; |
170 | public description?: string | undefined; | 156 | public description?: string | undefined; |
171 | public detail?: string | undefined; | 157 | public detail?: string | undefined; |
@@ -184,7 +170,7 @@ interface CargoTaskDefinition extends vscode.TaskDefinition { | |||
184 | env?: { [key: string]: string }; | 170 | env?: { [key: string]: string }; |
185 | } | 171 | } |
186 | 172 | ||
187 | function createTask(spec: ra.Runnable): vscode.Task { | 173 | export function createTask(spec: ra.Runnable): vscode.Task { |
188 | const TASK_SOURCE = 'Rust'; | 174 | const TASK_SOURCE = 'Rust'; |
189 | const definition: CargoTaskDefinition = { | 175 | const definition: CargoTaskDefinition = { |
190 | type: 'cargo', | 176 | type: 'cargo', |
diff --git a/editors/code/src/rust-analyzer-api.ts b/editors/code/src/rust-analyzer-api.ts deleted file mode 100644 index 900c5cd5b..000000000 --- a/editors/code/src/rust-analyzer-api.ts +++ /dev/null | |||
@@ -1,124 +0,0 @@ | |||
1 | /** | ||
2 | * This file mirrors `crates/rust-analyzer/src/req.rs` declarations. | ||
3 | */ | ||
4 | |||
5 | import * as lc from "vscode-languageclient"; | ||
6 | |||
7 | type Option<T> = null | T; | ||
8 | type Vec<T> = T[]; | ||
9 | type FxHashMap<K extends PropertyKey, V> = Record<K, V>; | ||
10 | |||
11 | function request<TParams, TResult>(method: string) { | ||
12 | return new lc.RequestType<TParams, TResult, unknown>(`rust-analyzer/${method}`); | ||
13 | } | ||
14 | function notification<TParam>(method: string) { | ||
15 | return new lc.NotificationType<TParam>(method); | ||
16 | } | ||
17 | |||
18 | |||
19 | export const analyzerStatus = request<null, string>("analyzerStatus"); | ||
20 | |||
21 | |||
22 | export const collectGarbage = request<null, null>("collectGarbage"); | ||
23 | |||
24 | |||
25 | export interface SyntaxTreeParams { | ||
26 | textDocument: lc.TextDocumentIdentifier; | ||
27 | range: Option<lc.Range>; | ||
28 | } | ||
29 | export const syntaxTree = request<SyntaxTreeParams, string>("syntaxTree"); | ||
30 | |||
31 | |||
32 | export interface ExpandMacroParams { | ||
33 | textDocument: lc.TextDocumentIdentifier; | ||
34 | position: Option<lc.Position>; | ||
35 | } | ||
36 | export interface ExpandedMacro { | ||
37 | name: string; | ||
38 | expansion: string; | ||
39 | } | ||
40 | export const expandMacro = request<ExpandMacroParams, Option<ExpandedMacro>>("expandMacro"); | ||
41 | |||
42 | |||
43 | export interface MatchingBraceParams { | ||
44 | textDocument: lc.TextDocumentIdentifier; | ||
45 | positions: lc.Position[]; | ||
46 | } | ||
47 | export const matchingBrace = new lc.RequestType<MatchingBraceParams, lc.Position[], unknown>('experimental/matchingBrace'); | ||
48 | |||
49 | export interface PublishDecorationsParams { | ||
50 | uri: string; | ||
51 | decorations: Vec<Decoration>; | ||
52 | } | ||
53 | export interface Decoration { | ||
54 | range: lc.Range; | ||
55 | tag: string; | ||
56 | bindingHash: Option<string>; | ||
57 | } | ||
58 | export const decorationsRequest = request<lc.TextDocumentIdentifier, Vec<Decoration>>("decorationsRequest"); | ||
59 | |||
60 | |||
61 | export const parentModule = request<lc.TextDocumentPositionParams, Vec<lc.Location>>("parentModule"); | ||
62 | |||
63 | |||
64 | export interface JoinLinesParams { | ||
65 | textDocument: lc.TextDocumentIdentifier; | ||
66 | ranges: lc.Range[]; | ||
67 | } | ||
68 | export const joinLines = new lc.RequestType<JoinLinesParams, lc.TextEdit[], unknown>('experimental/joinLines'); | ||
69 | |||
70 | |||
71 | export const onEnter = request<lc.TextDocumentPositionParams, Option<lc.WorkspaceEdit>>("onEnter"); | ||
72 | |||
73 | export interface RunnablesParams { | ||
74 | textDocument: lc.TextDocumentIdentifier; | ||
75 | position: Option<lc.Position>; | ||
76 | } | ||
77 | export interface Runnable { | ||
78 | range: lc.Range; | ||
79 | label: string; | ||
80 | bin: string; | ||
81 | args: Vec<string>; | ||
82 | extraArgs: Vec<string>; | ||
83 | env: FxHashMap<string, string>; | ||
84 | cwd: Option<string>; | ||
85 | } | ||
86 | export const runnables = request<RunnablesParams, Vec<Runnable>>("runnables"); | ||
87 | |||
88 | export type InlayHint = InlayHint.TypeHint | InlayHint.ParamHint | InlayHint.ChainingHint; | ||
89 | |||
90 | export namespace InlayHint { | ||
91 | export const enum Kind { | ||
92 | TypeHint = "TypeHint", | ||
93 | ParamHint = "ParameterHint", | ||
94 | ChainingHint = "ChainingHint", | ||
95 | } | ||
96 | interface Common { | ||
97 | range: lc.Range; | ||
98 | label: string; | ||
99 | } | ||
100 | export type TypeHint = Common & { kind: Kind.TypeHint }; | ||
101 | export type ParamHint = Common & { kind: Kind.ParamHint }; | ||
102 | export type ChainingHint = Common & { kind: Kind.ChainingHint }; | ||
103 | } | ||
104 | export interface InlayHintsParams { | ||
105 | textDocument: lc.TextDocumentIdentifier; | ||
106 | } | ||
107 | export const inlayHints = request<InlayHintsParams, Vec<InlayHint>>("inlayHints"); | ||
108 | |||
109 | |||
110 | export interface SsrParams { | ||
111 | query: string; | ||
112 | parseOnly: boolean; | ||
113 | } | ||
114 | export const ssr = new lc.RequestType<SsrParams, lc.WorkspaceEdit, unknown>('experimental/ssr'); | ||
115 | |||
116 | |||
117 | export const publishDecorations = notification<PublishDecorationsParams>("publishDecorations"); | ||
118 | |||
119 | |||
120 | export interface SourceChange { | ||
121 | label: string; | ||
122 | workspaceEdit: lc.WorkspaceEdit; | ||
123 | cursorPosition: Option<lc.TextDocumentPositionParams>; | ||
124 | } | ||
diff --git a/editors/code/src/commands/index.ts b/editors/code/src/snippets.ts index c2a232d5f..bcb3f2cc7 100644 --- a/editors/code/src/commands/index.ts +++ b/editors/code/src/snippets.ts | |||
@@ -1,60 +1,6 @@ | |||
1 | import * as vscode from 'vscode'; | 1 | import * as vscode from 'vscode'; |
2 | import * as lc from 'vscode-languageclient'; | ||
3 | import * as ra from '../rust-analyzer-api'; | ||
4 | 2 | ||
5 | import { Ctx, Cmd } from '../ctx'; | 3 | import { assert } from './util'; |
6 | import * as sourceChange from '../source_change'; | ||
7 | import { assert } from '../util'; | ||
8 | |||
9 | export * from './analyzer_status'; | ||
10 | export * from './matching_brace'; | ||
11 | export * from './join_lines'; | ||
12 | export * from './on_enter'; | ||
13 | export * from './parent_module'; | ||
14 | export * from './syntax_tree'; | ||
15 | export * from './expand_macro'; | ||
16 | export * from './runnables'; | ||
17 | export * from './ssr'; | ||
18 | export * from './server_version'; | ||
19 | export * from './toggle_inlay_hints'; | ||
20 | |||
21 | export function collectGarbage(ctx: Ctx): Cmd { | ||
22 | return async () => ctx.client.sendRequest(ra.collectGarbage, null); | ||
23 | } | ||
24 | |||
25 | export function showReferences(ctx: Ctx): Cmd { | ||
26 | return (uri: string, position: lc.Position, locations: lc.Location[]) => { | ||
27 | const client = ctx.client; | ||
28 | if (client) { | ||
29 | vscode.commands.executeCommand( | ||
30 | 'editor.action.showReferences', | ||
31 | vscode.Uri.parse(uri), | ||
32 | client.protocol2CodeConverter.asPosition(position), | ||
33 | locations.map(client.protocol2CodeConverter.asLocation), | ||
34 | ); | ||
35 | } | ||
36 | }; | ||
37 | } | ||
38 | |||
39 | export function applySourceChange(ctx: Ctx): Cmd { | ||
40 | return async (change: ra.SourceChange) => { | ||
41 | await sourceChange.applySourceChange(ctx, change); | ||
42 | }; | ||
43 | } | ||
44 | |||
45 | export function applyActionGroup(_ctx: Ctx): Cmd { | ||
46 | return async (actions: { label: string; edit: vscode.WorkspaceEdit }[]) => { | ||
47 | const selectedAction = await vscode.window.showQuickPick(actions); | ||
48 | if (!selectedAction) return; | ||
49 | await applySnippetWorkspaceEdit(selectedAction.edit); | ||
50 | }; | ||
51 | } | ||
52 | |||
53 | export function applySnippetWorkspaceEditCommand(_ctx: Ctx): Cmd { | ||
54 | return async (edit: vscode.WorkspaceEdit) => { | ||
55 | await applySnippetWorkspaceEdit(edit); | ||
56 | }; | ||
57 | } | ||
58 | 4 | ||
59 | export async function applySnippetWorkspaceEdit(edit: vscode.WorkspaceEdit) { | 5 | export async function applySnippetWorkspaceEdit(edit: vscode.WorkspaceEdit) { |
60 | assert(edit.entries().length === 1, `bad ws edit: ${JSON.stringify(edit)}`); | 6 | assert(edit.entries().length === 1, `bad ws edit: ${JSON.stringify(edit)}`); |
@@ -62,7 +8,10 @@ export async function applySnippetWorkspaceEdit(edit: vscode.WorkspaceEdit) { | |||
62 | 8 | ||
63 | const editor = vscode.window.visibleTextEditors.find((it) => it.document.uri.toString() === uri.toString()); | 9 | const editor = vscode.window.visibleTextEditors.find((it) => it.document.uri.toString() === uri.toString()); |
64 | if (!editor) return; | 10 | if (!editor) return; |
11 | await applySnippetTextEdits(editor, edits); | ||
12 | } | ||
65 | 13 | ||
14 | export async function applySnippetTextEdits(editor: vscode.TextEditor, edits: vscode.TextEdit[]) { | ||
66 | let selection: vscode.Selection | undefined = undefined; | 15 | let selection: vscode.Selection | undefined = undefined; |
67 | let lineDelta = 0; | 16 | let lineDelta = 0; |
68 | await editor.edit((builder) => { | 17 | await editor.edit((builder) => { |
diff --git a/editors/code/src/source_change.ts b/editors/code/src/source_change.ts deleted file mode 100644 index af8f1df51..000000000 --- a/editors/code/src/source_change.ts +++ /dev/null | |||
@@ -1,54 +0,0 @@ | |||
1 | import * as vscode from 'vscode'; | ||
2 | import * as lc from 'vscode-languageclient'; | ||
3 | import * as ra from './rust-analyzer-api'; | ||
4 | |||
5 | import { Ctx } from './ctx'; | ||
6 | |||
7 | export async function applySourceChange(ctx: Ctx, change: ra.SourceChange) { | ||
8 | const client = ctx.client; | ||
9 | if (!client) return; | ||
10 | |||
11 | const wsEdit = client.protocol2CodeConverter.asWorkspaceEdit( | ||
12 | change.workspaceEdit, | ||
13 | ); | ||
14 | let created; | ||
15 | let moved; | ||
16 | if (change.workspaceEdit.documentChanges) { | ||
17 | for (const docChange of change.workspaceEdit.documentChanges) { | ||
18 | if (lc.CreateFile.is(docChange)) { | ||
19 | created = docChange.uri; | ||
20 | } else if (lc.RenameFile.is(docChange)) { | ||
21 | moved = docChange.newUri; | ||
22 | } | ||
23 | } | ||
24 | } | ||
25 | const toOpen = created || moved; | ||
26 | const toReveal = change.cursorPosition; | ||
27 | await vscode.workspace.applyEdit(wsEdit); | ||
28 | if (toOpen) { | ||
29 | const toOpenUri = vscode.Uri.parse(toOpen); | ||
30 | const doc = await vscode.workspace.openTextDocument(toOpenUri); | ||
31 | await vscode.window.showTextDocument(doc); | ||
32 | } else if (toReveal) { | ||
33 | const uri = client.protocol2CodeConverter.asUri( | ||
34 | toReveal.textDocument.uri, | ||
35 | ); | ||
36 | const position = client.protocol2CodeConverter.asPosition( | ||
37 | toReveal.position, | ||
38 | ); | ||
39 | const editor = vscode.window.activeTextEditor; | ||
40 | if (!editor || !editor.selection.isEmpty) { | ||
41 | return; | ||
42 | } | ||
43 | |||
44 | if (editor.document.uri !== uri) { | ||
45 | const doc = await vscode.workspace.openTextDocument(uri); | ||
46 | await vscode.window.showTextDocument(doc); | ||
47 | } | ||
48 | editor.selection = new vscode.Selection(position, position); | ||
49 | editor.revealRange( | ||
50 | new vscode.Range(position, position), | ||
51 | vscode.TextEditorRevealType.Default, | ||
52 | ); | ||
53 | } | ||
54 | } | ||