summaryrefslogtreecommitdiff
path: root/src/changes.rs
blob: 986e9c42df6cf46b3d0a360ec1e50f475ad7e0b8 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use std::path::PathBuf;

use rowan::NodeOrToken;
use crate::pipeline::Change;

pub fn apply_changes(content: &str, mut changes: Vec<Change>, debug: bool) -> String {
    let mut last_pos = 0;
    let mut ncontent = String::new();

    changes.sort_by_key(|change| change.node.text_range().start());

    for change in changes {
        use crate::queries::Operator::*;
        match change.kind {
            Remove => {
                let (before, after) = remove_node(&change.node);
                if last_pos > before { continue }
                ncontent += &content[last_pos..before];
                if debug {
                    println!("removing: {}", &content[before..after])
                }
                last_pos = after;
            }
            Keep => {
                let (before, after) = surrounding_noise(&change.node);
                let (before, after) = eat_whitespace(&before, &after);
                // let (before, after) = (change.node.text_range().start().into(), change.node.text_range().end().into());
                let (before, after) = (before.text_range().start().into(), after.text_range().end().into());
                if debug {
                    println!("keeping: {}", &content[before..after])
                };
                ncontent += &content[before..after];
                // last_pos = after;
            }
        }
    }

    ncontent += &content[last_pos..];

    ncontent
}



// TODO: does rnix really not have an alias for this?
type NixNodeOrToken = NodeOrToken<rowan::SyntaxNode<rnix::NixLanguage>, rowan::SyntaxToken<rnix::NixLanguage>>;

/// eat all tokens of the given kind (backwards or forwards)
fn eat_kind(node: &NixNodeOrToken, kind: rnix::SyntaxKind, backwards: bool) ->  NixNodeOrToken {
    let mut span = node.clone();

    loop {
        span = match if backwards { span.prev_sibling_or_token() } else { span.next_sibling_or_token() } {
            Some(NodeOrToken::Token(t))
                if t.kind() == kind
                => NodeOrToken::Token(t),
            _ => break
        };
    }
    span
}


/// eat whitespace outwards both ways
fn eat_whitespace(begin: &NixNodeOrToken, end: &NixNodeOrToken)
                  -> (NixNodeOrToken, NixNodeOrToken) {
    let begin = eat_kind(begin, rnix::SyntaxKind::TOKEN_WHITESPACE, true);
    let end = eat_kind(end, rnix::SyntaxKind::TOKEN_WHITESPACE, false);
    (begin, end)
}

/// for a node, get all the "syntactic noise" which surrounds it
/// (i.e. brackets, whitespace, etc.) which should be removed if
/// the node itself is removed, except the final bit of whitespace
/// potentially separating it from neighbouring nodes, which should
/// be handled separately to keep formatting nice
fn surrounding_noise(node: &rnix::SyntaxNode) -> (NixNodeOrToken, NixNodeOrToken) {

    fn eat_matching_parens(begin: &NixNodeOrToken, end: &NixNodeOrToken)
        -> Option<(NixNodeOrToken, NixNodeOrToken)> {
        match (begin.prev_sibling_or_token(), end.next_sibling_or_token()) {
            (Some(begin), Some(end))
                if begin.kind() == rnix::SyntaxKind::TOKEN_L_PAREN
                && end.kind() == rnix::SyntaxKind::TOKEN_R_PAREN
                => Some((begin, end)),
            _ => None
        }
    }

    let (mut begin, mut end): (NixNodeOrToken, NixNodeOrToken) =
        (NodeOrToken::Node(node.clone()), NodeOrToken::Node(node.clone()));

    loop {
        let (a, b) = eat_whitespace(&begin, &end);
        // println!("loop: {begin}, {end}");
        // println!("loop: {a}, {b}");
        // println!("{:?}", (begin.prev_sibling_or_token(), end.next_sibling_or_token()));
        if let Some(res) = eat_matching_parens(&a, &b) {
            // println!("ate parens!");
            (begin, end) = res;
        } else {
          break
        }
    }

    (begin, end)
}


fn remove_node(node: &rnix::SyntaxNode) -> (usize, usize) {
    let (before, after) = surrounding_noise(node);

    let (mut prev, /*mut*/ next) =
        eat_whitespace(&before, &after);

    // println!("{prev:?}, {next:?}");
    if let Some(t) = prev.as_token().map(|t| t.prev_token()).flatten() {
        if t.kind() == rnix::SyntaxKind::TOKEN_WHITESPACE {
            prev = NodeOrToken::Token(t);
        }
    };
    // if let Some(t) = next.as_token().map(|t| t.next_token()).flatten() {
    //     if t.kind() == rnix::SyntaxKind::TOKEN_WHITESPACE {
    //         next = NodeOrToken::Token(t);
    //     }
    // };
    // println!("{prev:?}, {next:?}");


    if prev.kind() == rnix::SyntaxKind::TOKEN_WHITESPACE
        && next.kind() == rnix::SyntaxKind::TOKEN_WHITESPACE {
            // println!("inside if!");
            if prev.to_string().lines().count() < next.to_string().lines().count() {
                let start: usize = (node.text_range().start() - prev.text_range().len()).into();
                let end: usize = node.text_range().end().into();
                (start + previous_indentation(&node).unwrap_or(0)
                 , end + next_indentation(&node).unwrap_or(0))
            } else {
                let start: usize = node.text_range().start().into();
                let end: usize = (node.text_range().end() + next.text_range().len()).into();
                (start, end)
                // (start - previous_indentation(&node).unwrap_or(0),
                //  end - next_indentation(&node).unwrap_or(0))
            }
        }
    else {
        (prev.text_range().start().into(), next.text_range().end().into())
    }
}

fn indentation_of_string(string: &str) -> Option<usize> {
    if string.contains("\n") {
        None
    } else {
        Some(string.lines().last().unwrap().len())
    }
}

fn previous_indentation(node: &rnix::SyntaxNode) -> Option<usize> {
    let whitespace_token = node.prev_sibling_or_token()?;

    if whitespace_token.kind() == rnix::SyntaxKind::TOKEN_WHITESPACE {
        indentation_of_string(&whitespace_token.to_string())
    } else {
        None
    }
}

fn next_indentation(node: &rnix::SyntaxNode) -> Option<usize> {
    let whitespace_token = node.next_sibling_or_token()?;

    if whitespace_token.kind() == rnix::SyntaxKind::TOKEN_WHITESPACE {
        indentation_of_string(&whitespace_token.to_string())
        // Some(whitespace_token.to_string().lines().last().unwrap().len())
    } else {
        None
    }
}

/// give line & column of a position marker in the given string
fn line_at_textpos(content: &str, pos: rowan::TextSize) -> Option<(usize, usize)> {
    let pos: usize = pos.into();
    content
        .split("\n")
        .scan(0usize, |acc,line| {
            let start = *acc;
            let end = line.len() + 1 + start;
            *acc = end;
            Some((start, end))
        })
        .enumerate()
        .find(|(_, (_, end))| *end > pos)
        .map(|(line, (start, _))| (line + 1, pos + 1 - start))
}

pub fn format_pos(path: &PathBuf, content: &str, pos1: rowan::TextSize) -> String {
    let (line1, col1) = line_at_textpos(content, pos1).unwrap();
    format!("{path:?}:{line1}:{col1}")
}

pub fn format_range(path: &PathBuf, content: &str, range: rowan::TextRange) -> String {
    let (line1, col1) = line_at_textpos(content, range.start()).unwrap();
    let (line2, col2) = line_at_textpos(content, range.end()).unwrap();
    format!("{}:{line1}:{col1}-{line2}:{col2}", path.to_string_lossy())
}

pub fn format_range_json(path: &PathBuf, content: &str, range: rowan::TextRange) -> serde_json::Value {
    let (line1, col1) = line_at_textpos(content, range.start()).unwrap();
    let (line2, col2) = line_at_textpos(content, range.end()).unwrap();
    serde_json::json!({
        "file": path,
        "start": {
            "line": line1,
            "column": col1
        },
        "end": {
            "line": line2,
            "column": col2
        }
    })
}