aboutsummaryrefslogtreecommitdiff
path: root/crates/libeditor/src/extend_selection.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/libeditor/src/extend_selection.rs')
-rw-r--r--crates/libeditor/src/extend_selection.rs34
1 files changed, 32 insertions, 2 deletions
diff --git a/crates/libeditor/src/extend_selection.rs b/crates/libeditor/src/extend_selection.rs
index d1724b528..154f89671 100644
--- a/crates/libeditor/src/extend_selection.rs
+++ b/crates/libeditor/src/extend_selection.rs
@@ -1,7 +1,7 @@
1use libsyntax2::{ 1use libsyntax2::{
2 File, TextRange, SyntaxNodeRef, 2 File, TextRange, SyntaxNodeRef,
3 SyntaxKind::WHITESPACE, 3 SyntaxKind::*,
4 algo::{find_leaf_at_offset, find_covering_node, ancestors}, 4 algo::{find_leaf_at_offset, find_covering_node, ancestors, Direction, siblings},
5}; 5};
6 6
7pub fn extend_selection(file: &File, range: TextRange) -> Option<TextRange> { 7pub fn extend_selection(file: &File, range: TextRange) -> Option<TextRange> {
@@ -28,9 +28,39 @@ pub(crate) fn extend(root: SyntaxNodeRef, range: TextRange) -> Option<TextRange>
28 return Some(ws.range()); 28 return Some(ws.range());
29 }; 29 };
30 let node = find_covering_node(root, range); 30 let node = find_covering_node(root, range);
31 if node.kind() == COMMENT && range == node.range() {
32 if let Some(range) = extend_comments(node) {
33 return Some(range);
34 }
35 }
31 36
32 match ancestors(node).skip_while(|n| n.range() == range).next() { 37 match ancestors(node).skip_while(|n| n.range() == range).next() {
33 None => None, 38 None => None,
34 Some(parent) => Some(parent.range()), 39 Some(parent) => Some(parent.range()),
35 } 40 }
36} 41}
42
43fn extend_comments(node: SyntaxNodeRef) -> Option<TextRange> {
44 let left = adj_comments(node, Direction::Backward);
45 let right = adj_comments(node, Direction::Forward);
46 if left != right {
47 Some(TextRange::from_to(
48 left.range().start(),
49 right.range().end(),
50 ))
51 } else {
52 None
53 }
54}
55
56fn adj_comments(node: SyntaxNodeRef, dir: Direction) -> SyntaxNodeRef {
57 let mut res = node;
58 for node in siblings(node, dir) {
59 match node.kind() {
60 COMMENT => res = node,
61 WHITESPACE if !node.leaf_text().unwrap().as_str().contains("\n\n") => (),
62 _ => break
63 }
64 }
65 res
66}