aboutsummaryrefslogtreecommitdiff
path: root/crates/libeditor/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/libeditor/src')
-rw-r--r--crates/libeditor/src/code_actions.rs29
-rw-r--r--crates/libeditor/src/edit.rs15
-rw-r--r--crates/libeditor/src/lib.rs9
3 files changed, 40 insertions, 13 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
13pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> Edit + 'a> { 13pub struct ActionResult {
14 pub edit: Edit,
15 pub cursor_position: CursorPosition,
16}
17
18pub enum CursorPosition {
19 Same,
20 Offset(TextUnit),
21}
22
23pub 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
28pub fn add_derive<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> Edit + 'a> { 41pub 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
diff --git a/crates/libeditor/src/edit.rs b/crates/libeditor/src/edit.rs
index 15a2a904f..3edd0809d 100644
--- a/crates/libeditor/src/edit.rs
+++ b/crates/libeditor/src/edit.rs
@@ -67,6 +67,21 @@ impl Edit {
67 assert_eq!(buf.len(), total_len); 67 assert_eq!(buf.len(), total_len);
68 buf 68 buf
69 } 69 }
70
71 pub fn apply_to_offset(&self, offset: TextUnit) -> Option<TextUnit> {
72 let mut res = offset;
73 for atom in self.atoms.iter() {
74 if atom.delete.start() >= offset {
75 break;
76 }
77 if offset < atom.delete.end() {
78 return None
79 }
80 res += TextUnit::of_str(&atom.insert);
81 res -= atom.delete.len();
82 }
83 Some(res)
84 }
70} 85}
71 86
72impl AtomEdit { 87impl AtomEdit {
diff --git a/crates/libeditor/src/lib.rs b/crates/libeditor/src/lib.rs
index 2c46ca45f..9e44f5d92 100644
--- a/crates/libeditor/src/lib.rs
+++ b/crates/libeditor/src/lib.rs
@@ -21,7 +21,7 @@ pub use self::{
21 extend_selection::extend_selection, 21 extend_selection::extend_selection,
22 symbols::{StructureNode, file_structure, FileSymbol, file_symbols}, 22 symbols::{StructureNode, file_structure, FileSymbol, file_symbols},
23 edit::{EditBuilder, Edit, AtomEdit}, 23 edit::{EditBuilder, Edit, AtomEdit},
24 code_actions::{flip_comma, add_derive}, 24 code_actions::{flip_comma, add_derive, ActionResult, CursorPosition},
25}; 25};
26 26
27#[derive(Debug)] 27#[derive(Debug)]
@@ -37,13 +37,6 @@ pub struct Diagnostic {
37} 37}
38 38
39#[derive(Debug)] 39#[derive(Debug)]
40pub struct Symbol {
41 // pub parent: ???,
42 pub name: String,
43 pub range: TextRange,
44}
45
46#[derive(Debug)]
47pub struct Runnable { 40pub struct Runnable {
48 pub range: TextRange, 41 pub range: TextRange,
49 pub kind: RunnableKind, 42 pub kind: RunnableKind,