diff options
author | Aleksey Kladov <[email protected]> | 2018-08-15 21:24:20 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-15 21:24:20 +0100 |
commit | aa0d344581dcfd7f18c595688a4b2709b0f2421e (patch) | |
tree | 9846f587dcad204e6744ca5ff37faccf4251104c /crates/libeditor/src/code_actions.rs | |
parent | a7d31b55a4292f55851bc75265643b2ae2e675df (diff) |
Edits with cursors
Diffstat (limited to 'crates/libeditor/src/code_actions.rs')
-rw-r--r-- | crates/libeditor/src/code_actions.rs | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/crates/libeditor/src/code_actions.rs b/crates/libeditor/src/code_actions.rs index 6e2c73f29..88c348436 100644 --- a/crates/libeditor/src/code_actions.rs +++ b/crates/libeditor/src/code_actions.rs | |||
@@ -10,7 +10,17 @@ use libsyntax2::{ | |||
10 | }, | 10 | }, |
11 | }; | 11 | }; |
12 | 12 | ||
13 | pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> Edit + 'a> { | 13 | pub struct ActionResult { |
14 | pub edit: Edit, | ||
15 | pub cursor_position: CursorPosition, | ||
16 | } | ||
17 | |||
18 | pub enum CursorPosition { | ||
19 | Same, | ||
20 | Offset(TextUnit), | ||
21 | } | ||
22 | |||
23 | pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> { | ||
14 | let syntax = file.syntax(); | 24 | let syntax = file.syntax(); |
15 | let syntax = syntax.as_ref(); | 25 | let syntax = syntax.as_ref(); |
16 | 26 | ||
@@ -21,18 +31,27 @@ pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() | |||
21 | let mut edit = EditBuilder::new(); | 31 | let mut edit = EditBuilder::new(); |
22 | edit.replace(left.range(), right.text()); | 32 | edit.replace(left.range(), right.text()); |
23 | edit.replace(right.range(), left.text()); | 33 | edit.replace(right.range(), left.text()); |
24 | edit.finish() | 34 | ActionResult { |
35 | edit: edit.finish(), | ||
36 | cursor_position: CursorPosition::Same, | ||
37 | } | ||
25 | }) | 38 | }) |
26 | } | 39 | } |
27 | 40 | ||
28 | pub fn add_derive<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> Edit + 'a> { | 41 | pub fn add_derive<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> { |
29 | let syntax = file.syntax(); | 42 | let syntax = file.syntax(); |
30 | let syntax = syntax.as_ref(); | 43 | let syntax = syntax.as_ref(); |
31 | let nominal = find_node::<ast::NominalDef<_>>(syntax, offset)?; | 44 | let nominal = find_node::<ast::NominalDef<_>>(syntax, offset)?; |
32 | Some(move || { | 45 | Some(move || { |
33 | let mut edit = EditBuilder::new(); | 46 | let mut edit = EditBuilder::new(); |
34 | edit.insert(nominal.syntax().range().start(), "#[derive()]\n".to_string()); | 47 | let node_start = nominal.syntax().range().start(); |
35 | edit.finish() | 48 | edit.insert(node_start, "#[derive()]\n".to_string()); |
49 | ActionResult { | ||
50 | edit: edit.finish(), | ||
51 | cursor_position: CursorPosition::Offset( | ||
52 | node_start + TextUnit::of_str("#[derive(") | ||
53 | ), | ||
54 | } | ||
36 | }) | 55 | }) |
37 | } | 56 | } |
38 | 57 | ||