aboutsummaryrefslogtreecommitdiff
path: root/libeditor/src/extend_selection.rs
blob: 16d4bc0844b941227a5da443481337f7a8f58ec9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use libsyntax2::{
    TextRange, SyntaxNodeRef,
    SyntaxKind::WHITESPACE,
    algo::{find_leaf_at_offset, find_covering_node, ancestors},
};


pub(crate) fn extend_selection(root: SyntaxNodeRef, range: TextRange) -> Option<TextRange> {
    if range.is_empty() {
        let offset = range.start();
        let mut leaves = find_leaf_at_offset(root, offset);
        if let Some(leaf) = leaves.clone().find(|node| node.kind() != WHITESPACE) {
            return Some(leaf.range());
        }
        let ws = leaves.next()?;
//        let ws_suffix = file.text().slice(
//            TextRange::from_to(offset, ws.range().end())
//        );
//        if ws.text().contains("\n") && !ws_suffix.contains("\n") {
//            if let Some(line_end) = file.text()
//                .slice(TextSuffix::from(ws.range().end()))
//                .find("\n")
//            {
//                let range = TextRange::from_len(ws.range().end(), line_end);
//                return Some(find_covering_node(file.root(), range).range());
//            }
//        }
        return Some(ws.range());
    };
    let node = find_covering_node(root, range);

    match ancestors(node).skip_while(|n| n.range() == range).next() {
        None => None,
        Some(parent) => Some(parent.range()),
    }
}