aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api_light/src/formatting.rs
blob: 1f34b85d693e7cdc96f4bebedd8d552a9f5761fc (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use ra_syntax::{
    AstNode,
    SyntaxNode, SyntaxKind::*,
    ast::{self, AstToken},
    algo::generate,
};

/// If the node is on the beginning of the line, calculate indent.
pub(crate) fn leading_indent(node: &SyntaxNode) -> Option<&str> {
    for leaf in prev_leaves(node) {
        if let Some(ws) = ast::Whitespace::cast(leaf) {
            let ws_text = ws.text();
            if let Some(pos) = ws_text.rfind('\n') {
                return Some(&ws_text[pos + 1..]);
            }
        }
        if leaf.leaf_text().unwrap().contains('\n') {
            break;
        }
    }
    None
}

fn prev_leaves(node: &SyntaxNode) -> impl Iterator<Item = &SyntaxNode> {
    generate(prev_leaf(node), |&node| prev_leaf(node))
}

fn prev_leaf(node: &SyntaxNode) -> Option<&SyntaxNode> {
    generate(node.ancestors().find_map(SyntaxNode::prev_sibling), |it| {
        it.last_child()
    })
    .last()
}

pub(crate) fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> {
    let expr = block.expr()?;
    if expr.syntax().text().contains('\n') {
        return None;
    }
    let non_trivial_children = block.syntax().children().filter(|it| match it.kind() {
        WHITESPACE | L_CURLY | R_CURLY => false,
        _ => it != &expr.syntax(),
    });
    if non_trivial_children.count() > 0 {
        return None;
    }
    Some(expr)
}

pub(crate) fn compute_ws(left: &SyntaxNode, right: &SyntaxNode) -> &'static str {
    match left.kind() {
        L_PAREN | L_BRACK => return "",
        L_CURLY => {
            if let USE_TREE = right.kind() {
                return "";
            }
        }
        _ => (),
    }
    match right.kind() {
        R_PAREN | R_BRACK => return "",
        R_CURLY => {
            if let USE_TREE = left.kind() {
                return "";
            }
        }
        DOT => return "",
        _ => (),
    }
    " "
}