dprint 0.11.1

Binary for dprint code formatter—a pluggable and configurable code formatting platform.
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
use dissimilar::*;
use crossterm::style::Colorize;

use dprint_core::types::ErrBox;

// TODO: This file needs improvement as it is kind of buggy, but
// does the job for now.

/// Gets a string showing the difference between two strings.
/// Note: This returns a Result because this funciton has been unstable.
pub fn get_difference(text1: &str, text2: &str) -> Result<String, ErrBox> {
    debug_assert!(text1 != text2);

    // normalize newlines
    let text1 = text1.replace("\r\n", "\n");
    let text2 = text2.replace("\r\n", "\n");

    if text1 == text2 {
        return Ok(String::from(" | Text differed by line endings."));
    }

    let grouped_changes = get_grouped_changes(&text1, &text2);
    let mut text = String::new();

    for (i, grouped_change) in grouped_changes.into_iter().enumerate() {
        if i > 0 {
            text.push_str("\n...\n");
        }

        let max_line_num_width = grouped_change.end_line_number.to_string().chars().count();
        text.push_str(&format!("{:width$}| ", grouped_change.start_line_number, width = max_line_num_width));
        text.push_str(&annotate_whitespace(get_line_start_text(&text1, grouped_change.start_index)?));
        let mut last_index = grouped_change.start_index;

        for change in grouped_change.changes {
            for (i, line) in text1[last_index..change.start_index()].split('\n').enumerate() {
                if i > 0 {
                    text.push_str(&format!("\n{}| ", " ".repeat(max_line_num_width)));
                }

                text.push_str(&annotate_whitespace(line));
            }

            last_index = change.end_index();

            match change {
                Change::Addition(addition) => {
                    for (i, line) in addition.new_text.split('\n').enumerate() {
                        if i > 0 {
                            text.push_str("\n");
                            text.push_str(&get_addition_text(&format!("{}| ", " ".repeat(max_line_num_width))));
                        }

                        if !line.is_empty() {
                            text.push_str(&get_addition_text(&annotate_whitespace(line)));
                        }
                    }
                },
                Change::Removal(removal) => {
                    for (i, line) in removal.removed_text.split('\n').enumerate() {
                        if i > 0 {
                            text.push_str("\n");
                            let line_text = format!("{}| ", " ".repeat(max_line_num_width));
                            if line.is_empty() {
                                text.push_str(&get_removal_text(&line_text));
                            } else {
                                text.push_str(&line_text);
                            }
                        }

                        if !line.is_empty() {
                            text.push_str(&get_removal_text(&annotate_whitespace(line)));
                        }
                    }
                }
            }
        }

        text.push_str(&annotate_whitespace(&get_line_end_text(&text1, grouped_change.end_index)));
    }

    Ok(text)
}

fn get_line_start_text<'a>(text: &'a str, index: usize) -> Result<&'a str, ErrBox> {
    let new_line_byte = '\n' as u8;
    let text_bytes = text.as_bytes();
    let mut start_index = 0;
    let mut length = 0;

    if index > text.len() {
        // this should never happen
        return err!("The byte index was {}, but the text byte length is {}.", index, text.len());
    }

    for i in (0..index).rev() {
        if text_bytes[i] == new_line_byte || length > 50 {
            start_index = i + 1;
            break;
        }
        length += 1
    }

    Ok(&text[start_index..index])
}

fn get_line_end_text<'a>(text: &'a str, index: usize) -> &'a str {
    let new_line_byte = '\n' as u8;
    let text_bytes = text.as_bytes();
    let mut end_index = index;
    let mut length = 0;

    for i in index..text.len() {
        if text_bytes[i] == new_line_byte || length > 50 {
            end_index = i;
            break;
        }
        length += 1;
    }

    &text[index..end_index]
}

#[derive(Debug)]
struct GroupedChange<'a> {
    start_index: usize,
    end_index: usize,
    start_line_number: usize,
    end_line_number: usize,
    changes: Vec<Change<'a>>,
}

fn get_grouped_changes<'a>(text1: &'a str, text2: &'a str) -> Vec<GroupedChange<'a>> {
    let changes = get_changes(text1, text2);
    let mut grouped_changes: Vec<GroupedChange<'a>> = Vec::new();

    for change in changes {
        if let Some(grouped_change) = grouped_changes.last_mut() {
            // keeps changes together if they are only separated by a single line
            const GROUPED_LINE_COUNT: usize = 2;
            let should_group = change.start_line_number() < GROUPED_LINE_COUNT // prevent overflow
                || grouped_change.end_line_number >= change.start_line_number() - GROUPED_LINE_COUNT;
            if should_group {
                grouped_change.end_index = change.end_index();
                grouped_change.end_line_number = change.end_line_number();
                grouped_change.changes.push(change);
                continue;
            }
        }

        grouped_changes.push(GroupedChange {
            start_index: change.start_index(),
            end_index: change.end_index(),
            start_line_number: change.start_line_number(),
            end_line_number: change.end_line_number(),
            changes: vec![change],
        })
    }

    grouped_changes
}

#[derive(Debug)]
enum Change<'a> {
    Addition(Addition<'a>),
    Removal(Removal<'a>),
}

impl<'a> Change<'a> {
    /// Gets the start index in the original string.
    fn start_index(&self) -> usize {
        match self {
            Change::Addition(addition) => addition.insert_index,
            Change::Removal(removal) => removal.start_index,
        }
    }

    /// Gets the end index in the original string.
    fn end_index(&self) -> usize {
        match self {
            Change::Addition(addition) => addition.insert_index,
            Change::Removal(removal) => removal.end_index,
        }
    }

    /// Gets the start line number in the original string.
    fn start_line_number(&self) -> usize {
        match self {
            Change::Addition(addition) => addition.insert_line_number,
            Change::Removal(removal) => removal.start_line_number,
        }
    }

    /// Gets the end line number in the original string.
    fn end_line_number(&self) -> usize {
        match self {
            Change::Addition(addition) => addition.insert_line_number,
            Change::Removal(removal) => removal.end_line_number,
        }
    }
}

#[derive(Debug)]
struct Addition<'a> {
    new_text: &'a str,
    insert_index: usize,
    insert_line_number: usize,
}

#[derive(Debug)]
struct Removal<'a> {
    removed_text: &'a str,
    start_index: usize,
    end_index: usize,
    start_line_number: usize,
    end_line_number: usize,
}

fn get_changes<'a>(text1: &'a str, text2: &'a str) -> Vec<Change<'a>> {
    let chunks = get_pre_processed_chunks(text1, text2);
    let mut changes: Vec<Change<'a>> = Vec::new();

    let mut line_number = 1;
    let mut byte_index = 0;

    for i in 0..chunks.len() {
        let chunk = chunks[i];
        match chunk {
            Chunk::Insert(inserted_text) => {
                changes.push(Change::Addition(Addition {
                    new_text: inserted_text,
                    insert_index: byte_index,
                    insert_line_number: line_number,
                }));
            },
            Chunk::Delete(deleted_text) => {
                let line_count = deleted_text.split('\n').count() - 1;

                changes.push(Change::Removal(Removal {
                    removed_text: deleted_text,
                    start_index: byte_index,
                    end_index: byte_index + deleted_text.len(),
                    start_line_number: line_number,
                    end_line_number: line_number + line_count,
                }));

                byte_index += deleted_text.len();
                line_number += line_count;
            },
            Chunk::Equal(equal_text) => {
                byte_index += equal_text.len();
                line_number += equal_text.split('\n').count() - 1;
            },
        }
    }

    changes
}

fn get_pre_processed_chunks<'a>(text1: &'a str, text2: &'a str) -> Vec<dissimilar::Chunk<'a>> {
    // What we want to do here is take a collection of chunks like this:
    //   [Equal("class Test"), Delete("\n"), Insert(" "), Equal("{\n"), Delete("\n"), Equal("}"), Insert("\n")]
    // And transform them so the parts that proceed the newline delete end up as inserts on the previous line and deletes on the next:
    //   [Equal("class Test"), Insert(" "), Insert("{"), Equal("\n"), Delete("{"), Delete("\n"), Delete("\n"), Equal("}"), Insert("\n")]
    // Note: It would probably be nice to group like chunks together here... for the future...
    let chunks = dissimilar::diff(text1, text2);

    let mut final_chunks = Vec::new();
    let mut i = 0;
    while i < chunks.len() {
        let chunk = chunks[i];

        let mut was_deleted_with_newline = false;
        if let Chunk::Delete(delete_text) = chunk {
            if let Some(Chunk::Equal(last_change)) = final_chunks.last() {
                was_deleted_with_newline = delete_text.find('\n').is_some() && !last_change.ends_with('\n');
            }
        }

        if was_deleted_with_newline {
            let add_delete_at_end = if let Some(Chunk::Insert(insert_text)) = chunks.get(i + 1) {
                // Do not add a delete newline at the end if the next chunk is inserting a newline.
                // TBH: This doesn't seem exactly right and I did this to fix a bug.
                !insert_text.contains('\n')
            } else {
                true
            };

            let delete_text = if let Chunk::Delete(delete_text) = chunk { delete_text } else { unreachable!() };

            // push delete text previous line
            let delete_text_new_line_index = delete_text.find('\n').unwrap();
            if delete_text_new_line_index > 0 {
                final_chunks.push(dissimilar::Chunk::Delete(&delete_text[0..delete_text_new_line_index]));
            }

            // push delete text next line for next line chunks
            let mut next_line_chunks = Vec::new();
            if delete_text_new_line_index + 1 < delete_text.len() {
                next_line_chunks.push(dissimilar::Chunk::Delete(&delete_text[delete_text_new_line_index + 1..]));
            }

            i += 1;

            // move the next equal or insert chunks to be inserts on the previous line and deletes on the current
            while i < chunks.len() {
                let chunk = chunks[i];
                match chunk {
                    Chunk::Equal(equal_text) => {
                        let new_line_index = equal_text.find('\n');
                        if let Some(new_line_index) = new_line_index {
                            // for previous line
                            final_chunks.push(dissimilar::Chunk::Insert(&equal_text[0..new_line_index]));

                            // for next line
                            next_line_chunks.push(dissimilar::Chunk::Delete(&equal_text[0..new_line_index]));

                            // for remainder of next line equal text
                            let remainder_equal_text = &equal_text[new_line_index + 1..];
                            if remainder_equal_text.len() > 0 {
                                next_line_chunks.push(dissimilar::Chunk::Equal(remainder_equal_text));
                            }
                            break;
                        } else {
                            final_chunks.push(dissimilar::Chunk::Insert(equal_text));
                            next_line_chunks.push(dissimilar::Chunk::Delete(equal_text));
                        }
                    },
                    Chunk::Insert(insert_text) => {
                        let new_line_index = insert_text.find('\n');
                        if let Some(new_line_index) = new_line_index {
                            // for previous line
                            final_chunks.push(dissimilar::Chunk::Insert(&insert_text[0..new_line_index]));
                            // for next line
                            let remainder_text = &insert_text[new_line_index + 1..];
                            if remainder_text.len() > 0 {
                                next_line_chunks.push(dissimilar::Chunk::Insert(remainder_text));
                            }
                            break;
                        } else {
                            final_chunks.push(dissimilar::Chunk::Insert(insert_text));
                        }
                    }
                    Chunk::Delete(delete_text) => {
                        next_line_chunks.push(dissimilar::Chunk::Delete(delete_text));
                    }
                }
                i += 1;
            }

            final_chunks.push(dissimilar::Chunk::Equal(&delete_text[delete_text_new_line_index..delete_text_new_line_index+1]));
            final_chunks.extend(next_line_chunks);
            if add_delete_at_end {
                final_chunks.push(dissimilar::Chunk::Delete(&delete_text[delete_text_new_line_index..delete_text_new_line_index+1]));
            }
        } else {
            final_chunks.push(chunk);
        }

        i += 1;
    }

    final_chunks
}

fn get_addition_text(text: &str) -> String {
    text.white().on_green().to_string()
}

fn get_removal_text(text: &str) -> String {
    let text = text.replace("\t", "\u{21E5}");
    text.white().on_red().to_string()
}

fn annotate_whitespace(text: &str) -> String {
    text.replace("\t", "\u{2192}")
        .replace(" ", "\u{00B7}")
}

#[cfg(test)]
mod test {
    use pretty_assertions::assert_eq;
    use super::*;

    #[test]
    fn it_should_get_when_differs_by_line_endings() {
        assert_eq!(get_difference("test\r\n", "test\n").unwrap(), " | Text differed by line endings.");
    }

    #[test]
    fn it_should_get_difference_on_one_line() {
        assert_eq!(get_difference("test1\n", "test2\n").unwrap(), format!("1| test{}{}", get_removal_text("1"), get_addition_text("2")));
    }

    #[test]
    fn it_should_show_the_addition_of_last_line() {
        assert_eq!(
            get_difference("testing\ntesting", "testing\ntesting\n").unwrap(),
            format!(
                "{}\n{}",
                "2| testing",
                get_addition_text(&format!(" | "))
            )
        );
    }

    #[test]
    fn it_should_get_difference_for_removed_line() {
        assert_eq!(
            get_difference("class Test\n{\n\n}", "class Test {\n}\n").unwrap(),
            format!(
                "{}\n{}\n{}\n{}\n{}",
                format!("1| class\u{00B7}Test{}{}", get_addition_text("\u{00B7}"), get_addition_text("{")),
                format!(" | {}", get_removal_text("{")),
                get_removal_text(" | "),
                format!("{}{}", get_removal_text(" | "), "}"),
                get_addition_text(" | "),
            )
        );
    }

    #[test]
    fn it_should_show_multiple_removals_on_different_lines() {
        assert_eq!(
            get_difference("let t ;\n\n\nlet u ;\n", "let t;\n\n\nlet u;\n").unwrap(),
            format!(
                "{}\n...\n{}",
                format!("1| let\u{00B7}t{};", get_removal_text("\u{00B7}")),
                format!("4| let\u{00B7}u{};", get_removal_text("\u{00B7}")),
            )
        );
    }

    #[test]
    fn it_should_keep_grouped_when_changes_only_separated_by_one_line() {
        assert_eq!(
            get_difference("let t ;\ntest;\nlet u ;\n", "let t;\ntest;\nlet u;\n").unwrap(),
            format!(
                "{}\n{}\n{}",
                format!("1| let\u{00B7}t{};", get_removal_text("\u{00B7}")),
                " | test;",
                format!(" | let\u{00B7}u{};", get_removal_text("\u{00B7}")),
            )
        );
    }

    #[test]
    fn it_should_annotate_whitespace_end_line_text() {
        assert_eq!(
            get_difference("t t t\n", "tt t\n").unwrap(),
            format!(
                "1| t{}t\u{00B7}t",
                get_removal_text("\u{00B7}")
            )
        );
    }

    #[test]
    fn it_should_handle_replacements() {
        assert_eq!(
            get_difference("use::asdf\nuse::test", "use::other\nsomething").unwrap(),
            format!(
                "1| use::{}{}\n | {}{}",
                get_removal_text("asdf"),
                get_addition_text("other"),
                get_removal_text("use::test"),
                get_addition_text("something")
            )
        );
    }
}