]> git.lizzy.rs Git - rust.git/blob - src/rustfmt_diff.rs
Merge pull request #1785 from topecongiro/rfc/import
[rust.git] / src / rustfmt_diff.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use diff;
12 use std::collections::VecDeque;
13 use std::io;
14 use term;
15
16 #[derive(Debug, PartialEq)]
17 pub enum DiffLine {
18     Context(String),
19     Expected(String),
20     Resulting(String),
21 }
22
23 #[derive(Debug, PartialEq)]
24 pub struct Mismatch {
25     pub line_number: u32,
26     pub lines: Vec<DiffLine>,
27 }
28
29 impl Mismatch {
30     fn new(line_number: u32) -> Mismatch {
31         Mismatch {
32             line_number: line_number,
33             lines: Vec::new(),
34         }
35     }
36 }
37
38 // Produces a diff between the expected output and actual output of rustfmt.
39 pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec<Mismatch> {
40     let mut line_number = 1;
41     let mut context_queue: VecDeque<&str> = VecDeque::with_capacity(context_size);
42     let mut lines_since_mismatch = context_size + 1;
43     let mut results = Vec::new();
44     let mut mismatch = Mismatch::new(0);
45
46     for result in diff::lines(expected, actual) {
47         match result {
48             diff::Result::Left(str) => {
49                 if lines_since_mismatch >= context_size {
50                     results.push(mismatch);
51                     mismatch = Mismatch::new(line_number - context_queue.len() as u32);
52                 }
53
54                 while let Some(line) = context_queue.pop_front() {
55                     mismatch.lines.push(DiffLine::Context(line.to_owned()));
56                 }
57
58                 mismatch.lines.push(DiffLine::Resulting(str.to_owned()));
59                 lines_since_mismatch = 0;
60             }
61             diff::Result::Right(str) => {
62                 if lines_since_mismatch >= context_size {
63                     results.push(mismatch);
64                     mismatch = Mismatch::new(line_number - context_queue.len() as u32);
65                 }
66
67                 while let Some(line) = context_queue.pop_front() {
68                     mismatch.lines.push(DiffLine::Context(line.to_owned()));
69                 }
70
71                 mismatch.lines.push(DiffLine::Expected(str.to_owned()));
72                 line_number += 1;
73                 lines_since_mismatch = 0;
74             }
75             diff::Result::Both(str, _) => {
76                 if context_queue.len() >= context_size {
77                     let _ = context_queue.pop_front();
78                 }
79
80                 if lines_since_mismatch < context_size {
81                     mismatch.lines.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
100     F: Fn(u32) -> String,
101 {
102     match term::stdout() {
103         Some(ref t) if isatty() && t.supports_color() => {
104             print_diff_fancy(diff, get_section_title, term::stdout().unwrap())
105         }
106         _ => print_diff_basic(diff, get_section_title),
107     }
108
109     // isatty shamelessly adapted from cargo.
110     #[cfg(unix)]
111     fn isatty() -> bool {
112         extern crate libc;
113
114         unsafe { libc::isatty(libc::STDOUT_FILENO) != 0 }
115     }
116     #[cfg(windows)]
117     fn isatty() -> bool {
118         extern crate kernel32;
119         extern crate winapi;
120
121         unsafe {
122             let handle = kernel32::GetStdHandle(winapi::winbase::STD_OUTPUT_HANDLE);
123             let mut out = 0;
124             kernel32::GetConsoleMode(handle, &mut out) != 0
125         }
126     }
127 }
128
129 fn print_diff_fancy<F>(
130     diff: Vec<Mismatch>,
131     get_section_title: F,
132     mut t: Box<term::Terminal<Output = io::Stdout>>,
133 ) where
134     F: Fn(u32) -> String,
135 {
136     for mismatch in diff {
137         let title = get_section_title(mismatch.line_number);
138         writeln!(t, "{}", title).unwrap();
139
140         for line in mismatch.lines {
141             match line {
142                 DiffLine::Context(ref str) => {
143                     t.reset().unwrap();
144                     writeln!(t, " {}⏎", str).unwrap();
145                 }
146                 DiffLine::Expected(ref str) => {
147                     t.fg(term::color::GREEN).unwrap();
148                     writeln!(t, "+{}⏎", str).unwrap();
149                 }
150                 DiffLine::Resulting(ref str) => {
151                     t.fg(term::color::RED).unwrap();
152                     writeln!(t, "-{}⏎", str).unwrap();
153                 }
154             }
155         }
156         t.reset().unwrap();
157     }
158 }
159
160 pub fn print_diff_basic<F>(diff: Vec<Mismatch>, get_section_title: F)
161 where
162     F: Fn(u32) -> String,
163 {
164     for mismatch in diff {
165         let title = get_section_title(mismatch.line_number);
166         println!("{}", title);
167
168         for line in mismatch.lines {
169             match line {
170                 DiffLine::Context(ref str) => {
171                     println!(" {}⏎", str);
172                 }
173                 DiffLine::Expected(ref str) => {
174                     println!("+{}⏎", str);
175                 }
176                 DiffLine::Resulting(ref str) => {
177                     println!("-{}⏎", str);
178                 }
179             }
180         }
181     }
182 }