diff options
author | Aleksey Kladov <[email protected]> | 2018-08-22 09:56:36 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-22 09:56:36 +0100 |
commit | 641659d5a8dcca0b8a1c36ff6d1c517a91296116 (patch) | |
tree | 8ed23d27c1839d525cd69be56bbba2fb6cf52598 | |
parent | 9909875bfe89d2b901c35c0667bed018338b44e1 (diff) |
Smarter extend selection
-rw-r--r-- | crates/libeditor/src/extend_selection.rs | 20 | ||||
-rw-r--r-- | crates/libeditor/tests/test.rs | 35 | ||||
-rw-r--r-- | crates/libsyntax2/Cargo.toml | 2 | ||||
-rw-r--r-- | crates/libsyntax2/src/ast/generated.rs | 18 |
4 files changed, 53 insertions, 22 deletions
diff --git a/crates/libeditor/src/extend_selection.rs b/crates/libeditor/src/extend_selection.rs index 171e40692..32873f491 100644 --- a/crates/libeditor/src/extend_selection.rs +++ b/crates/libeditor/src/extend_selection.rs | |||
@@ -17,18 +17,14 @@ pub(crate) fn extend(root: SyntaxNodeRef, range: TextRange) -> Option<TextRange> | |||
17 | return Some(leaf.range()); | 17 | return Some(leaf.range()); |
18 | } | 18 | } |
19 | let ws = leaves.next()?; | 19 | let ws = leaves.next()?; |
20 | // let ws_suffix = file.text().slice( | 20 | let ws_text = ws.leaf_text().unwrap(); |
21 | // TextRange::from_to(offset, ws.range().end()) | 21 | let range = TextRange::from_to(offset, ws.range().end()) - ws.range().start(); |
22 | // ); | 22 | let ws_suffix = &ws_text.as_str()[range]; |
23 | // if ws.text().contains("\n") && !ws_suffix.contains("\n") { | 23 | if ws_text.contains("\n") && !ws_suffix.contains("\n") { |
24 | // if let Some(line_end) = file.text() | 24 | if let Some(node) = ws.next_sibling() { |
25 | // .slice(TextSuffix::from(ws.range().end())) | 25 | return Some(node.range()); |
26 | // .find("\n") | 26 | } |
27 | // { | 27 | } |
28 | // let range = TextRange::from_len(ws.range().end(), line_end); | ||
29 | // return Some(find_covering_node(file.root(), range).range()); | ||
30 | // } | ||
31 | // } | ||
32 | return Some(ws.range()); | 28 | return Some(ws.range()); |
33 | }; | 29 | }; |
34 | let node = find_covering_node(root, range); | 30 | let node = find_covering_node(root, range); |
diff --git a/crates/libeditor/tests/test.rs b/crates/libeditor/tests/test.rs index a2e26003a..f8365949e 100644 --- a/crates/libeditor/tests/test.rs +++ b/crates/libeditor/tests/test.rs | |||
@@ -12,15 +12,32 @@ use libeditor::{ | |||
12 | 12 | ||
13 | #[test] | 13 | #[test] |
14 | fn test_extend_selection() { | 14 | fn test_extend_selection() { |
15 | let file = file(r#"fn foo() { | 15 | fn do_check(before: &str, afters: &[&str]) { |
16 | 1 + 1 | 16 | let (cursor, before) = extract_cursor(before); |
17 | } | 17 | let file = file(&before); |
18 | "#); | 18 | let mut range = TextRange::offset_len(cursor, 0.into()); |
19 | let range = TextRange::offset_len(18.into(), 0.into()); | 19 | for &after in afters { |
20 | let range = extend_selection(&file, range).unwrap(); | 20 | range = extend_selection(&file, range) |
21 | assert_eq!(range, TextRange::from_to(17.into(), 18.into())); | 21 | .unwrap(); |
22 | let range = extend_selection(&file, range).unwrap(); | 22 | let actual = &before[range]; |
23 | assert_eq!(range, TextRange::from_to(15.into(), 20.into())); | 23 | assert_eq!(after, actual); |
24 | } | ||
25 | } | ||
26 | |||
27 | do_check( | ||
28 | r#"fn foo() { <|>1 + 1 }"#, | ||
29 | &["1", "1 + 1", "{ 1 + 1 }"], | ||
30 | ); | ||
31 | |||
32 | do_check( | ||
33 | r#" | ||
34 | impl S { | ||
35 | <|> fn foo() { | ||
36 | |||
37 | } | ||
38 | }"#, | ||
39 | &["fn foo() {\n\n }"] | ||
40 | ); | ||
24 | } | 41 | } |
25 | 42 | ||
26 | #[test] | 43 | #[test] |
diff --git a/crates/libsyntax2/Cargo.toml b/crates/libsyntax2/Cargo.toml index b55cdf481..e8810046a 100644 --- a/crates/libsyntax2/Cargo.toml +++ b/crates/libsyntax2/Cargo.toml | |||
@@ -6,7 +6,7 @@ license = "MIT OR Apache-2.0" | |||
6 | 6 | ||
7 | [dependencies] | 7 | [dependencies] |
8 | unicode-xid = "0.1.0" | 8 | unicode-xid = "0.1.0" |
9 | text_unit = "0.1.2" | 9 | text_unit = "0.1.3" |
10 | itertools = "0.7.8" | 10 | itertools = "0.7.8" |
11 | drop_bomb = "0.1.4" | 11 | drop_bomb = "0.1.4" |
12 | parking_lot = "0.6.0" | 12 | parking_lot = "0.6.0" |
diff --git a/crates/libsyntax2/src/ast/generated.rs b/crates/libsyntax2/src/ast/generated.rs index 610b5198c..0651da26d 100644 --- a/crates/libsyntax2/src/ast/generated.rs +++ b/crates/libsyntax2/src/ast/generated.rs | |||
@@ -641,3 +641,21 @@ impl<'a> AstNode<'a> for TypeRef<'a> { | |||
641 | 641 | ||
642 | impl<'a> TypeRef<'a> {} | 642 | impl<'a> TypeRef<'a> {} |
643 | 643 | ||
644 | // Whitespace | ||
645 | #[derive(Debug, Clone, Copy)] | ||
646 | pub struct Whitespace<'a> { | ||
647 | syntax: SyntaxNodeRef<'a>, | ||
648 | } | ||
649 | |||
650 | impl<'a> AstNode<'a> for Whitespace<'a> { | ||
651 | fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> { | ||
652 | match syntax.kind() { | ||
653 | WHITESPACE => Some(Whitespace { syntax }), | ||
654 | _ => None, | ||
655 | } | ||
656 | } | ||
657 | fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } | ||
658 | } | ||
659 | |||
660 | impl<'a> Whitespace<'a> {} | ||
661 | |||