aboutsummaryrefslogtreecommitdiff
path: root/crates/libeditor/src/extend_selection.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-31 12:52:29 +0100
committerAleksey Kladov <[email protected]>2018-08-31 12:52:29 +0100
commit05a9d42f542c8eb876d06791579d948b2f571e04 (patch)
tree33b5962641f57d65b8545b477885fcfadbbe801c /crates/libeditor/src/extend_selection.rs
parent8fc7f438c4347e027deda5cda4bcd5e560610bb7 (diff)
tweak extend selection
Diffstat (limited to 'crates/libeditor/src/extend_selection.rs')
-rw-r--r--crates/libeditor/src/extend_selection.rs21
1 files changed, 16 insertions, 5 deletions
diff --git a/crates/libeditor/src/extend_selection.rs b/crates/libeditor/src/extend_selection.rs
index 30cff6558..3adb1e45e 100644
--- a/crates/libeditor/src/extend_selection.rs
+++ b/crates/libeditor/src/extend_selection.rs
@@ -1,5 +1,5 @@
1use libsyntax2::{ 1use libsyntax2::{
2 File, TextRange, SyntaxNodeRef, 2 File, TextRange, SyntaxNodeRef, TextUnit,
3 SyntaxKind::*, 3 SyntaxKind::*,
4 algo::{find_leaf_at_offset, find_covering_node, ancestors, Direction, siblings}, 4 algo::{find_leaf_at_offset, find_covering_node, ancestors, Direction, siblings},
5}; 5};
@@ -18,11 +18,22 @@ pub(crate) fn extend(root: SyntaxNodeRef, range: TextRange) -> Option<TextRange>
18 } 18 }
19 let ws = leaves.next()?; 19 let ws = leaves.next()?;
20 let ws_text = ws.leaf_text().unwrap(); 20 let ws_text = ws.leaf_text().unwrap();
21 let range = TextRange::from_to(offset, ws.range().end()) - ws.range().start(); 21 let suffix = TextRange::from_to(offset, ws.range().end()) - ws.range().start();
22 let ws_suffix = &ws_text.as_str()[range]; 22 let prefix = TextRange::from_to(ws.range().start(), offset) - ws.range().start();
23 let ws_suffix = &ws_text.as_str()[suffix];
24 let ws_prefix = &ws_text.as_str()[prefix];
23 if ws_text.contains("\n") && !ws_suffix.contains("\n") { 25 if ws_text.contains("\n") && !ws_suffix.contains("\n") {
24 if let Some(node) = ws.next_sibling() { 26 if let Some(node) = ws.next_sibling() {
25 return Some(node.range()); 27 let start = match ws_prefix.rfind('\n') {
28 Some(idx) => ws.range().start() + TextUnit::from((idx + 1) as u32),
29 None => node.range().start()
30 };
31 let end = if root.text().char_at(node.range().end()) == Some('\n') {
32 node.range().end() + TextUnit::of_char('\n')
33 } else {
34 node.range().end()
35 };
36 return Some(TextRange::from_to(start, end));
26 } 37 }
27 } 38 }
28 return Some(ws.range()); 39 return Some(ws.range());
@@ -99,7 +110,7 @@ impl S {
99 110
100 } 111 }
101}"#, 112}"#,
102 &["fn foo() {\n\n }"] 113 &[" fn foo() {\n\n }\n"]
103 ); 114 );
104 } 115 }
105 116