From f99398d9d5e47e28f3749c7903df67b9030ac6e0 Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Sun, 6 Jan 2019 00:58:03 +0100 Subject: indent on typing dot. fixes #439 --- crates/ra_editor/src/lib.rs | 2 +- crates/ra_editor/src/typing.rs | 54 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 4 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index ac283e2e0..a3c85ed5d 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs @@ -16,7 +16,7 @@ pub use self::{ line_index::{LineCol, LineIndex}, line_index_utils::translate_offset_with_edit, structure::{file_structure, StructureNode}, - typing::{join_lines, on_enter, on_eq_typed}, + typing::{join_lines, on_enter, on_dot_typed, on_eq_typed}, diagnostics::diagnostics }; use ra_text_edit::TextEditBuilder; diff --git a/crates/ra_editor/src/typing.rs b/crates/ra_editor/src/typing.rs index 1b568e96c..aaea858ea 100644 --- a/crates/ra_editor/src/typing.rs +++ b/crates/ra_editor/src/typing.rs @@ -1,5 +1,6 @@ use std::mem; +use itertools::Itertools; use ra_syntax::{ algo::{find_covering_node, find_leaf_at_offset, LeafAtOffset}, ast, @@ -9,9 +10,8 @@ use ra_syntax::{ SyntaxNodeRef, TextRange, TextUnit, }; use ra_text_edit::text_utils::contains_offset_nonstrict; -use itertools::Itertools; -use crate::{find_node_at_offset, TextEditBuilder, LocalEdit}; +use crate::{find_node_at_offset, LocalEdit, TextEditBuilder}; pub fn join_lines(file: &SourceFileNode, range: TextRange) -> LocalEdit { let range = if range.is_empty() { @@ -136,6 +136,27 @@ pub fn on_eq_typed(file: &SourceFileNode, offset: TextUnit) -> Option }) } +pub fn on_dot_typed(file: &SourceFileNode, offset: TextUnit) -> Option { + let before_dot_offset = offset - TextUnit::of_char('.'); + + let whitespace = find_leaf_at_offset(file.syntax(), before_dot_offset) + .left_biased() + .and_then(ast::Whitespace::cast)?; + + // whitespace found just left of the dot + // TODO: indent is always 4 spaces now. A better heuristic could look on the previous line(s) + let indent = " ".to_string(); + + let cursor_position = offset + TextUnit::of_str(&indent);; + let mut edit = TextEditBuilder::default(); + edit.insert(before_dot_offset, indent); + Some(LocalEdit { + label: "indent dot".to_string(), + edit: edit.finish(), + cursor_position: Some(cursor_position), + }) +} + fn remove_newline( edit: &mut TextEditBuilder, node: SyntaxNodeRef, @@ -283,7 +304,9 @@ fn compute_ws(left: SyntaxNodeRef, right: SyntaxNodeRef) -> &'static str { #[cfg(test)] mod tests { use super::*; - use crate::test_utils::{add_cursor, check_action, extract_offset, extract_range, assert_eq_text}; + use crate::test_utils::{ + add_cursor, assert_eq_text, check_action, extract_offset, extract_range, + }; fn check_join_lines(before: &str, after: &str) { check_action(before, after, |file, offset| { @@ -614,6 +637,31 @@ fn foo() { // "); } + #[test] + fn test_on_dot_typed() { + fn do_check(before: &str, after: &str) { + let (offset, before) = extract_offset(before); + let file = SourceFileNode::parse(&before); + let result = on_dot_typed(&file, offset).unwrap(); + let actual = result.edit.apply(&before); + assert_eq_text!(after, &actual); + } + do_check( + r" + pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable> { + self.child_impl(db, name) + .<|> + } +", + r" + pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable> { + self.child_impl(db, name) + . + } +", + ); + } + #[test] fn test_on_enter() { fn apply_on_enter(before: &str) -> Option { -- cgit v1.2.3 From cfaaf33069079ebd2627098aee09bd8ac76f9bd8 Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Sun, 6 Jan 2019 07:56:02 +0100 Subject: rename unused variable --- crates/ra_editor/src/typing.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/typing.rs b/crates/ra_editor/src/typing.rs index aaea858ea..9742a10cf 100644 --- a/crates/ra_editor/src/typing.rs +++ b/crates/ra_editor/src/typing.rs @@ -139,7 +139,7 @@ pub fn on_eq_typed(file: &SourceFileNode, offset: TextUnit) -> Option pub fn on_dot_typed(file: &SourceFileNode, offset: TextUnit) -> Option { let before_dot_offset = offset - TextUnit::of_char('.'); - let whitespace = find_leaf_at_offset(file.syntax(), before_dot_offset) + let _whitespace = find_leaf_at_offset(file.syntax(), before_dot_offset) .left_biased() .and_then(ast::Whitespace::cast)?; -- cgit v1.2.3 From bb8624dff65dca10997e3eec62df975e6e347558 Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Sun, 6 Jan 2019 08:08:23 +0100 Subject: format code --- crates/ra_editor/src/typing.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/typing.rs b/crates/ra_editor/src/typing.rs index 9742a10cf..ceef8b879 100644 --- a/crates/ra_editor/src/typing.rs +++ b/crates/ra_editor/src/typing.rs @@ -306,7 +306,7 @@ mod tests { use super::*; use crate::test_utils::{ add_cursor, assert_eq_text, check_action, extract_offset, extract_range, - }; +}; fn check_join_lines(before: &str, after: &str) { check_action(before, after, |file, offset| { -- cgit v1.2.3 From 4f3bc42349534518940f5a9a4db64287d897c03f Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Sun, 6 Jan 2019 12:24:33 +0100 Subject: add more tests --- crates/ra_editor/src/typing.rs | 55 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/typing.rs b/crates/ra_editor/src/typing.rs index ceef8b879..528c7b200 100644 --- a/crates/ra_editor/src/typing.rs +++ b/crates/ra_editor/src/typing.rs @@ -306,7 +306,7 @@ mod tests { use super::*; use crate::test_utils::{ add_cursor, assert_eq_text, check_action, extract_offset, extract_range, -}; + }; fn check_join_lines(before: &str, after: &str) { check_action(before, after, |file, offset| { @@ -646,6 +646,7 @@ fn foo() { let actual = result.edit.apply(&before); assert_eq_text!(after, &actual); } + // indent if continuing chain call do_check( r" pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable> { @@ -658,6 +659,58 @@ fn foo() { self.child_impl(db, name) . } +", + ); + + // do not indent if already indented + do_check( + r" + pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable> { + self.child_impl(db, name) + .<|> + } +", + r" + pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable> { + self.child_impl(db, name) + . + } +", + ); + + // indent if the previous line is already indented + do_check( + r" + pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable> { + self.child_impl(db, name) + .first() + .<|> + } +", + r" + pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable> { + self.child_impl(db, name) + .first() + . + } +", + ); + + // don't indent if indent matches previous line + do_check( + r" + pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable> { + self.child_impl(db, name) + .first() + .<|> + } +", + r" + pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable> { + self.child_impl(db, name) + .first() + . + } ", ); } -- cgit v1.2.3 From 0be055072d511f4634855377ab26a3c227e2ab31 Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Sun, 6 Jan 2019 21:59:14 +0100 Subject: fix tests --- crates/ra_editor/src/typing.rs | 82 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 72 insertions(+), 10 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/typing.rs b/crates/ra_editor/src/typing.rs index 528c7b200..c9bc55ccc 100644 --- a/crates/ra_editor/src/typing.rs +++ b/crates/ra_editor/src/typing.rs @@ -5,7 +5,7 @@ use ra_syntax::{ algo::{find_covering_node, find_leaf_at_offset, LeafAtOffset}, ast, text_utils::intersect, - AstNode, SourceFileNode, SyntaxKind, + AstNode, Direction, SourceFileNode, SyntaxKind, SyntaxKind::*, SyntaxNodeRef, TextRange, TextUnit, }; @@ -139,15 +139,41 @@ pub fn on_eq_typed(file: &SourceFileNode, offset: TextUnit) -> Option pub fn on_dot_typed(file: &SourceFileNode, offset: TextUnit) -> Option { let before_dot_offset = offset - TextUnit::of_char('.'); - let _whitespace = find_leaf_at_offset(file.syntax(), before_dot_offset) - .left_biased() - .and_then(ast::Whitespace::cast)?; + let _whitespace = find_leaf_at_offset(file.syntax(), before_dot_offset).left_biased()?; + + // find whitespace just left of the dot + ast::Whitespace::cast(_whitespace)?; + + // make sure there is a method call + let _method_call = _whitespace + .siblings(Direction::Prev) + // first is whitespace + .skip(1) + .next()?; + + ast::MethodCallExprNode::cast(_method_call)?; + + // find how much the _method call is indented + let method_chain_indent = _method_call + .ancestors() + .skip(1) + .next()? + .siblings(Direction::Prev) + .skip(1) + .next()? + .leaf_text() + .map(|x| last_line_indent_in_whitespace(x))?; - // whitespace found just left of the dot + let current_indent = TextUnit::of_str(last_line_indent_in_whitespace(_whitespace.leaf_text()?)); // TODO: indent is always 4 spaces now. A better heuristic could look on the previous line(s) - let indent = " ".to_string(); - let cursor_position = offset + TextUnit::of_str(&indent);; + let target_indent = TextUnit::of_str(method_chain_indent) + TextUnit::from_usize(4); + + let diff = target_indent - current_indent; + + let indent = "".repeat(diff.to_usize()); + + let cursor_position = offset + diff; let mut edit = TextEditBuilder::default(); edit.insert(before_dot_offset, indent); Some(LocalEdit { @@ -157,6 +183,11 @@ pub fn on_dot_typed(file: &SourceFileNode, offset: TextUnit) -> Option &str { + ws.split('\n').last().unwrap_or("") +} + fn remove_newline( edit: &mut TextEditBuilder, node: SyntaxNodeRef, @@ -642,9 +673,10 @@ fn foo() { fn do_check(before: &str, after: &str) { let (offset, before) = extract_offset(before); let file = SourceFileNode::parse(&before); - let result = on_dot_typed(&file, offset).unwrap(); - let actual = result.edit.apply(&before); - assert_eq_text!(after, &actual); + if let Some(result) = on_eq_typed(&file, offset) { + let actual = result.edit.apply(&before); + assert_eq_text!(after, &actual); + }; } // indent if continuing chain call do_check( @@ -711,6 +743,36 @@ fn foo() { .first() . } +", + ); + + // don't indent if there is no method call on previous line + do_check( + r" + pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable> { + .<|> + } +", + r" + pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable> { + . + } +", + ); + + // indent to match previous expr + do_check( + r" + pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable> { + self.child_impl(db, name) +.<|> + } +", + r" + pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable> { + self.child_impl(db, name) + . + } ", ); } -- cgit v1.2.3 From bbc044990a567fe23b3eb7b884e60f1b7be83c43 Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Sun, 6 Jan 2019 22:06:22 +0100 Subject: formatting --- crates/ra_editor/src/typing.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/typing.rs b/crates/ra_editor/src/typing.rs index c9bc55ccc..4ee4f394f 100644 --- a/crates/ra_editor/src/typing.rs +++ b/crates/ra_editor/src/typing.rs @@ -337,7 +337,7 @@ mod tests { use super::*; use crate::test_utils::{ add_cursor, assert_eq_text, check_action, extract_offset, extract_range, - }; +}; fn check_join_lines(before: &str, after: &str) { check_action(before, after, |file, offset| { -- cgit v1.2.3 From 979dcf36e42f7dac5cf7adfdd48257973e95ca6f Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Mon, 7 Jan 2019 06:16:04 +0100 Subject: fix nits --- crates/ra_editor/src/typing.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/typing.rs b/crates/ra_editor/src/typing.rs index 4ee4f394f..c0b22a9ec 100644 --- a/crates/ra_editor/src/typing.rs +++ b/crates/ra_editor/src/typing.rs @@ -139,32 +139,30 @@ pub fn on_eq_typed(file: &SourceFileNode, offset: TextUnit) -> Option pub fn on_dot_typed(file: &SourceFileNode, offset: TextUnit) -> Option { let before_dot_offset = offset - TextUnit::of_char('.'); - let _whitespace = find_leaf_at_offset(file.syntax(), before_dot_offset).left_biased()?; + let whitespace = find_leaf_at_offset(file.syntax(), before_dot_offset).left_biased()?; // find whitespace just left of the dot - ast::Whitespace::cast(_whitespace)?; + ast::Whitespace::cast(whitespace)?; // make sure there is a method call - let _method_call = _whitespace + let method_call = whitespace .siblings(Direction::Prev) // first is whitespace .skip(1) .next()?; - ast::MethodCallExprNode::cast(_method_call)?; + ast::MethodCallExprNode::cast(method_call)?; // find how much the _method call is indented - let method_chain_indent = _method_call - .ancestors() - .skip(1) - .next()? + let method_chain_indent = method_call + .parent()? .siblings(Direction::Prev) .skip(1) .next()? .leaf_text() .map(|x| last_line_indent_in_whitespace(x))?; - let current_indent = TextUnit::of_str(last_line_indent_in_whitespace(_whitespace.leaf_text()?)); + let current_indent = TextUnit::of_str(last_line_indent_in_whitespace(whitespace.leaf_text()?)); // TODO: indent is always 4 spaces now. A better heuristic could look on the previous line(s) let target_indent = TextUnit::of_str(method_chain_indent) + TextUnit::from_usize(4); @@ -337,7 +335,7 @@ mod tests { use super::*; use crate::test_utils::{ add_cursor, assert_eq_text, check_action, extract_offset, extract_range, -}; + }; fn check_join_lines(before: &str, after: &str) { check_action(before, after, |file, offset| { -- cgit v1.2.3 From f3c708ab7babc4e94250cbfbaae0fdd3919284ce Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Mon, 7 Jan 2019 06:24:07 +0100 Subject: my formatting tool locally messes things up --- crates/ra_editor/src/typing.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/typing.rs b/crates/ra_editor/src/typing.rs index c0b22a9ec..12500854c 100644 --- a/crates/ra_editor/src/typing.rs +++ b/crates/ra_editor/src/typing.rs @@ -335,7 +335,7 @@ mod tests { use super::*; use crate::test_utils::{ add_cursor, assert_eq_text, check_action, extract_offset, extract_range, - }; +}; fn check_join_lines(before: &str, after: &str) { check_action(before, after, |file, offset| { -- cgit v1.2.3