]> git.lizzy.rs Git - rust.git/blob - src/rustfmt_diff.rs
fallout - source reformatting
[rust.git] / src / rustfmt_diff.rs
1 use std::collections::VecDeque;
2 use diff;
3 use term;
4 use std::io;
5
6 #[derive(Debug, PartialEq)]
7 pub enum DiffLine {
8     Context(String),
9     Expected(String),
10     Resulting(String),
11 }
12
13 #[derive(Debug, PartialEq)]
14 pub struct Mismatch {
15     pub line_number: u32,
16     pub lines: Vec<DiffLine>,
17 }
18
19 impl Mismatch {
20     fn new(line_number: u32) -> Mismatch {
21         Mismatch {
22             line_number: line_number,
23             lines: Vec::new(),
24         }
25     }
26 }
27
28 // Produces a diff between the expected output and actual output of rustfmt.
29 pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec<Mismatch> {
30     let mut line_number = 1;
31     let mut context_queue: VecDeque<&str> = VecDeque::with_capacity(context_size);
32     let mut lines_since_mismatch = context_size + 1;
33     let mut results = Vec::new();
34     let mut mismatch = Mismatch::new(0);
35
36     for result in diff::lines(expected, actual) {
37         match result {
38             diff::Result::Left(str) => {
39                 if lines_since_mismatch >= context_size {
40                     results.push(mismatch);
41                     mismatch = Mismatch::new(line_number - context_queue.len() as u32);
42                 }
43
44                 while let Some(line) = context_queue.pop_front() {
45                     mismatch
46                         .lines
47                         .push(DiffLine::Context(line.to_owned()));
48                 }
49
50                 mismatch
51                     .lines
52                     .push(DiffLine::Resulting(str.to_owned()));
53                 lines_since_mismatch = 0;
54             }
55             diff::Result::Right(str) => {
56                 if lines_since_mismatch >= context_size {
57                     results.push(mismatch);
58                     mismatch = Mismatch::new(line_number - context_queue.len() as u32);
59                 }
60
61                 while let Some(line) = context_queue.pop_front() {
62                     mismatch
63                         .lines
64                         .push(DiffLine::Context(line.to_owned()));
65                 }
66
67                 mismatch
68                     .lines
69                     .push(DiffLine::Expected(str.to_owned()));
70                 line_number += 1;
71                 lines_since_mismatch = 0;
72             }
73             diff::Result::Both(str, _) => {
74                 if context_queue.len() >= context_size {
75                     let _ = context_queue.pop_front();
76                 }
77
78                 if lines_since_mismatch < context_size {
79                     mismatch
80                         .lines
81                         .push(DiffLine::Context(str.to_owned()));
82                 } else {
83                     context_queue.push_back(str);
84                 }
85
86                 line_number += 1;
87                 lines_since_mismatch += 1;
88             }
89         }
90     }
91
92     results.push(mismatch);
93     results.remove(0);
94
95     results
96 }
97
98 pub fn print_diff<F>(diff: Vec<Mismatch>, get_section_title: F)
99     where F: Fn(u32) -> String
100 {
101     match term::stdout() {
102         Some(ref t) if isatty() && t.supports_color() => {
103             print_diff_fancy(diff, get_section_title, term::stdout().unwrap())
104         }
105         _ => print_diff_basic(diff, get_section_title),
106     }
107
108     // isatty shamelessly adapted from cargo.
109     #[cfg(unix)]
110     fn isatty() -> bool {
111         extern crate libc;
112
113         unsafe { libc::isatty(libc::STDOUT_FILENO) != 0 }
114     }
115     #[cfg(windows)]
116     fn isatty() -> bool {
117         extern crate kernel32;
118         extern crate winapi;
119
120         unsafe {
121             let handle = kernel32::GetStdHandle(winapi::winbase::STD_OUTPUT_HANDLE);
122             let mut out = 0;
123             kernel32::GetConsoleMode(handle, &mut out) != 0
124         }
125     }
126 }
127
128 fn print_diff_fancy<F>(diff: Vec<Mismatch>,
129                        get_section_title: F,
130                        mut t: Box<term::Terminal<Output = io::Stdout>>)
131     where F: Fn(u32) -> String
132 {
133     for mismatch in diff {
134         let title = get_section_title(mismatch.line_number);
135         writeln!(t, "{}", title).unwrap();
136
137         for line in mismatch.lines {
138             match line {
139                 DiffLine::Context(ref str) => {
140                     t.reset().unwrap();
141                     writeln!(t, " {}⏎", str).unwrap();
142                 }
143                 DiffLine::Expected(ref str) => {
144                     t.fg(term::color::GREEN).unwrap();
145                     writeln!(t, "+{}⏎", str).unwrap();
146                 }
147                 DiffLine::Resulting(ref str) => {
148                     t.fg(term::color::RED).unwrap();
149                     writeln!(t, "-{}⏎", str).unwrap();
150                 }
151             }
152         }
153         t.reset().unwrap();
154     }
155 }
156
157 pub fn print_diff_basic<F>(diff: Vec<Mismatch>, get_section_title: F)
158     where F: Fn(u32) -> String
159 {
160     for mismatch in diff {
161         let title = get_section_title(mismatch.line_number);
162         println!("{}", title);
163
164         for line in mismatch.lines {
165             match line {
166                 DiffLine::Context(ref str) => {
167                     println!(" {}⏎", str);
168                 }
169                 DiffLine::Expected(ref str) => {
170                     println!("+{}⏎", str);
171                 }
172                 DiffLine::Resulting(ref str) => {
173                     println!("-{}⏎", str);
174                 }
175             }
176         }
177     }
178 }