diff options
author | Aleksey Kladov <[email protected]> | 2018-08-10 20:33:29 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-10 20:33:29 +0100 |
commit | 7c67612b8a894187fa3b64725531a5459f9211bf (patch) | |
tree | 9e2a536efa0c880d921fd8d4d74423afc9451fd4 /crates/libeditor/src/extend_selection.rs | |
parent | 26262aaf05983c5b7f41cc438e287523268fe1eb (diff) |
organizize
Diffstat (limited to 'crates/libeditor/src/extend_selection.rs')
-rw-r--r-- | crates/libeditor/src/extend_selection.rs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/crates/libeditor/src/extend_selection.rs b/crates/libeditor/src/extend_selection.rs new file mode 100644 index 000000000..16d4bc084 --- /dev/null +++ b/crates/libeditor/src/extend_selection.rs | |||
@@ -0,0 +1,36 @@ | |||
1 | use libsyntax2::{ | ||
2 | TextRange, SyntaxNodeRef, | ||
3 | SyntaxKind::WHITESPACE, | ||
4 | algo::{find_leaf_at_offset, find_covering_node, ancestors}, | ||
5 | }; | ||
6 | |||
7 | |||
8 | pub(crate) fn extend_selection(root: SyntaxNodeRef, range: TextRange) -> Option<TextRange> { | ||
9 | if range.is_empty() { | ||
10 | let offset = range.start(); | ||
11 | let mut leaves = find_leaf_at_offset(root, offset); | ||
12 | if let Some(leaf) = leaves.clone().find(|node| node.kind() != WHITESPACE) { | ||
13 | return Some(leaf.range()); | ||
14 | } | ||
15 | let ws = leaves.next()?; | ||
16 | // let ws_suffix = file.text().slice( | ||
17 | // TextRange::from_to(offset, ws.range().end()) | ||
18 | // ); | ||
19 | // if ws.text().contains("\n") && !ws_suffix.contains("\n") { | ||
20 | // if let Some(line_end) = file.text() | ||
21 | // .slice(TextSuffix::from(ws.range().end())) | ||
22 | // .find("\n") | ||
23 | // { | ||
24 | // let range = TextRange::from_len(ws.range().end(), line_end); | ||
25 | // return Some(find_covering_node(file.root(), range).range()); | ||
26 | // } | ||
27 | // } | ||
28 | return Some(ws.range()); | ||
29 | }; | ||
30 | let node = find_covering_node(root, range); | ||
31 | |||
32 | match ancestors(node).skip_while(|n| n.range() == range).next() { | ||
33 | None => None, | ||
34 | Some(parent) => Some(parent.range()), | ||
35 | } | ||
36 | } | ||