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.rs15
-rw-r--r--crates/libeditor/src/completion.rs3
-rw-r--r--crates/libeditor/src/lib.rs4
-rw-r--r--crates/libeditor/src/test_utils.rs4
-rw-r--r--crates/libeditor/src/typing.rs48
5 files changed, 54 insertions, 20 deletions
diff --git a/crates/libeditor/src/code_actions.rs b/crates/libeditor/src/code_actions.rs
index 08a85f6e2..dadbd63ab 100644
--- a/crates/libeditor/src/code_actions.rs
+++ b/crates/libeditor/src/code_actions.rs
@@ -13,13 +13,14 @@ use libsyntax2::{
13 13
14use {EditBuilder, Edit, find_node_at_offset}; 14use {EditBuilder, Edit, find_node_at_offset};
15 15
16// TODO: rename to FileEdit
16#[derive(Debug)] 17#[derive(Debug)]
17pub struct ActionResult { 18pub struct LocalEdit {
18 pub edit: Edit, 19 pub edit: Edit,
19 pub cursor_position: Option<TextUnit>, 20 pub cursor_position: Option<TextUnit>,
20} 21}
21 22
22pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> { 23pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> LocalEdit + 'a> {
23 let syntax = file.syntax(); 24 let syntax = file.syntax();
24 25
25 let comma = find_leaf_at_offset(syntax, offset).find(|leaf| leaf.kind() == COMMA)?; 26 let comma = find_leaf_at_offset(syntax, offset).find(|leaf| leaf.kind() == COMMA)?;
@@ -29,14 +30,14 @@ pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce()
29 let mut edit = EditBuilder::new(); 30 let mut edit = EditBuilder::new();
30 edit.replace(left.range(), right.text().to_string()); 31 edit.replace(left.range(), right.text().to_string());
31 edit.replace(right.range(), left.text().to_string()); 32 edit.replace(right.range(), left.text().to_string());
32 ActionResult { 33 LocalEdit {
33 edit: edit.finish(), 34 edit: edit.finish(),
34 cursor_position: None, 35 cursor_position: None,
35 } 36 }
36 }) 37 })
37} 38}
38 39
39pub fn add_derive<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> { 40pub fn add_derive<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> LocalEdit + 'a> {
40 let nominal = find_node_at_offset::<ast::NominalDef>(file.syntax(), offset)?; 41 let nominal = find_node_at_offset::<ast::NominalDef>(file.syntax(), offset)?;
41 Some(move || { 42 Some(move || {
42 let derive_attr = nominal 43 let derive_attr = nominal
@@ -56,14 +57,14 @@ pub fn add_derive<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce()
56 tt.syntax().range().end() - TextUnit::of_char(')') 57 tt.syntax().range().end() - TextUnit::of_char(')')
57 } 58 }
58 }; 59 };
59 ActionResult { 60 LocalEdit {
60 edit: edit.finish(), 61 edit: edit.finish(),
61 cursor_position: Some(offset), 62 cursor_position: Some(offset),
62 } 63 }
63 }) 64 })
64} 65}
65 66
66pub fn add_impl<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> { 67pub fn add_impl<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> LocalEdit + 'a> {
67 let nominal = find_node_at_offset::<ast::NominalDef>(file.syntax(), offset)?; 68 let nominal = find_node_at_offset::<ast::NominalDef>(file.syntax(), offset)?;
68 let name = nominal.name()?; 69 let name = nominal.name()?;
69 70
@@ -90,7 +91,7 @@ pub fn add_impl<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() ->
90 let offset = start_offset + TextUnit::of_str(&buf); 91 let offset = start_offset + TextUnit::of_str(&buf);
91 buf.push_str("\n}"); 92 buf.push_str("\n}");
92 edit.insert(start_offset, buf); 93 edit.insert(start_offset, buf);
93 ActionResult { 94 LocalEdit {
94 edit: edit.finish(), 95 edit: edit.finish(),
95 cursor_position: Some(offset), 96 cursor_position: Some(offset),
96 } 97 }
diff --git a/crates/libeditor/src/completion.rs b/crates/libeditor/src/completion.rs
index fe4c9b217..d95c40773 100644
--- a/crates/libeditor/src/completion.rs
+++ b/crates/libeditor/src/completion.rs
@@ -12,8 +12,7 @@ use {
12}; 12};
13 13
14#[derive(Debug)] 14#[derive(Debug)]
15pub struct 15pub struct CompletionItem {
16 CompletionItem {
17 pub name: String, 16 pub name: String,
18 pub snippet: Option<String> 17 pub snippet: Option<String>
19} 18}
diff --git a/crates/libeditor/src/lib.rs b/crates/libeditor/src/lib.rs
index 4895f6fa9..4700ef328 100644
--- a/crates/libeditor/src/lib.rs
+++ b/crates/libeditor/src/lib.rs
@@ -30,11 +30,11 @@ pub use self::{
30 symbols::{StructureNode, file_structure, FileSymbol, file_symbols}, 30 symbols::{StructureNode, file_structure, FileSymbol, file_symbols},
31 edit::{EditBuilder, Edit}, 31 edit::{EditBuilder, Edit},
32 code_actions::{ 32 code_actions::{
33 ActionResult, 33 LocalEdit,
34 flip_comma, add_derive, add_impl, 34 flip_comma, add_derive, add_impl,
35 }, 35 },
36 typing::{join_lines, on_eq_typed}, 36 typing::{join_lines, on_eq_typed},
37 completion::scope_completion, 37 completion::{scope_completion, CompletionItem},
38}; 38};
39 39
40#[derive(Debug)] 40#[derive(Debug)]
diff --git a/crates/libeditor/src/test_utils.rs b/crates/libeditor/src/test_utils.rs
index 475f560fa..037319cd0 100644
--- a/crates/libeditor/src/test_utils.rs
+++ b/crates/libeditor/src/test_utils.rs
@@ -1,8 +1,8 @@
1use libsyntax2::{File, TextUnit}; 1use libsyntax2::{File, TextUnit};
2pub use _test_utils::*; 2pub use _test_utils::*;
3use ActionResult; 3use LocalEdit;
4 4
5pub fn check_action<F: Fn(&File, TextUnit) -> Option<ActionResult>> ( 5pub fn check_action<F: Fn(&File, TextUnit) -> Option<LocalEdit>> (
6 before: &str, 6 before: &str,
7 after: &str, 7 after: &str,
8 f: F, 8 f: F,
diff --git a/crates/libeditor/src/typing.rs b/crates/libeditor/src/typing.rs
index 5008b8d49..f888f3240 100644
--- a/crates/libeditor/src/typing.rs
+++ b/crates/libeditor/src/typing.rs
@@ -11,14 +11,14 @@ use libsyntax2::{
11 SyntaxKind::*, 11 SyntaxKind::*,
12}; 12};
13 13
14use {ActionResult, EditBuilder, find_node_at_offset}; 14use {LocalEdit, EditBuilder, find_node_at_offset};
15 15
16pub fn join_lines(file: &File, range: TextRange) -> ActionResult { 16pub fn join_lines(file: &File, range: TextRange) -> LocalEdit {
17 let range = if range.is_empty() { 17 let range = if range.is_empty() {
18 let syntax = file.syntax(); 18 let syntax = file.syntax();
19 let text = syntax.text().slice(range.start()..); 19 let text = syntax.text().slice(range.start()..);
20 let pos = match text.find('\n') { 20 let pos = match text.find('\n') {
21 None => return ActionResult { 21 None => return LocalEdit {
22 edit: EditBuilder::new().finish(), 22 edit: EditBuilder::new().finish(),
23 cursor_position: None 23 cursor_position: None
24 }, 24 },
@@ -50,13 +50,13 @@ pub fn join_lines(file: &File, range: TextRange) -> ActionResult {
50 } 50 }
51 eprintln!("{:?}", edit); 51 eprintln!("{:?}", edit);
52 52
53 ActionResult { 53 LocalEdit {
54 edit: edit.finish(), 54 edit: edit.finish(),
55 cursor_position: None, 55 cursor_position: None,
56 } 56 }
57} 57}
58 58
59pub fn on_eq_typed(file: &File, offset: TextUnit) -> Option<ActionResult> { 59pub fn on_eq_typed(file: &File, offset: TextUnit) -> Option<LocalEdit> {
60 let let_stmt: ast::LetStmt = find_node_at_offset(file.syntax(), offset)?; 60 let let_stmt: ast::LetStmt = find_node_at_offset(file.syntax(), offset)?;
61 if let_stmt.has_semi() { 61 if let_stmt.has_semi() {
62 return None; 62 return None;
@@ -75,7 +75,7 @@ pub fn on_eq_typed(file: &File, offset: TextUnit) -> Option<ActionResult> {
75 let offset = let_stmt.syntax().range().end(); 75 let offset = let_stmt.syntax().range().end();
76 let mut edit = EditBuilder::new(); 76 let mut edit = EditBuilder::new();
77 edit.insert(offset, ";".to_string()); 77 edit.insert(offset, ";".to_string());
78 Some(ActionResult { 78 Some(LocalEdit {
79 edit: edit.finish(), 79 edit: edit.finish(),
80 cursor_position: None, 80 cursor_position: None,
81 }) 81 })
@@ -277,7 +277,41 @@ fn foo() {
277}", r" 277}", r"
278fn foo() { 278fn foo() {
279 join(type_params.type_params().filter_map(|it| it.name()).map(|it| it.text())) 279 join(type_params.type_params().filter_map(|it| it.name()).map(|it| it.text()))
280}") 280}");
281
282 do_check(r"
283pub fn handle_find_matching_brace(
284 world: ServerWorld,
285 params: req::FindMatchingBraceParams,
286) -> Result<Vec<Position>> {
287 let file_id = params.text_document.try_conv_with(&world)?;
288 let file = world.analysis().file_syntax(file_id);
289 let line_index = world.analysis().file_line_index(file_id);
290 let res = params.offsets
291 .into_iter()
292 .map_conv_with(&line_index)
293 .map(|offset| <|>{
294 world.analysis().matching_brace(&file, offset).unwrap_or(offset)
295 }<|>)
296 .map_conv_with(&line_index)
297 .collect();
298 Ok(res)
299}", r"
300pub fn handle_find_matching_brace(
301 world: ServerWorld,
302 params: req::FindMatchingBraceParams,
303) -> Result<Vec<Position>> {
304 let file_id = params.text_document.try_conv_with(&world)?;
305 let file = world.analysis().file_syntax(file_id);
306 let line_index = world.analysis().file_line_index(file_id);
307 let res = params.offsets
308 .into_iter()
309 .map_conv_with(&line_index)
310 .map(|offset| world.analysis().matching_brace(&file, offset).unwrap_or(offset))
311 .map_conv_with(&line_index)
312 .collect();
313 Ok(res)
314}");
281 } 315 }
282 316
283 #[test] 317 #[test]