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
//! Shared source-to-domain diff computation, independent of CLI and transport.
#[cfg(test)]
use crate::config::body_params;
use crate::config::Params;
use crate::constants::Side;
use crate::diff::changes::ChangeMap;
use crate::diff::shortest_path::{mark_syntax, ExceededGraphLimit};
use crate::diff::sliders::fix_all_sliders;
use crate::diff::unchanged;
use crate::line_parser;
use crate::options::{DiffOptions, FileArgument};
use crate::parse::folds;
use crate::parse::guess_language::{guess, language_name, LanguageOverride};
use crate::parse::syntax::{self, init_next_prev};
use crate::parse::tree_sitter_parser as tsp;
use crate::summary::{DiffResult, FallbackCause, FileContent, FileFormat};
use humansize::{format_size, FormatSizeOptions, BINARY};
use std::{env, fmt, path::Path};
use typed_arena::Arena;
/// The fallback reason for a file tagged `generated`, which is always diffed
/// by line.
pub(crate) const GENERATED_FALLBACK: &str = "generated file, diffed by line";
/// A file whose fold query captured one syntax node with two different
/// ranges. The file is not diffed; the stream reports it as a
/// `query_conflict` error.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct QueryConflict {
/// The file's display path.
pub(crate) path: String,
pub(crate) side: Side,
pub(crate) conflict: folds::Conflict,
}
impl fmt::Display for QueryConflict {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (first, second) = &self.conflict.sources;
write!(
f,
"{}:{}{}: {first} and {second} capture the same {} with different fold ranges",
self.path,
self.conflict.line + 1,
match self.side {
Side::Left => " (before)",
Side::Right => "",
},
self.conflict.kind,
)
}
}
impl std::error::Error for QueryConflict {}
impl DiffResult {
#[cfg(test)]
pub(crate) fn from_sources(path: &str, lhs: &str, rhs: &str) -> Self {
Self::from_sources_with_params(path, lhs, rhs, &body_params())
}
/// A diff with the given parameters, for tests whose queries cannot
/// conflict.
#[cfg(test)]
pub(crate) fn from_sources_with_params(
path: &str,
lhs: &str,
rhs: &str,
params: &Params,
) -> Self {
Self::try_from_sources_with_params(path, lhs, rhs, params)
.expect("the test's fold queries do not conflict")
}
#[cfg(test)]
pub(crate) fn try_from_sources_with_params(
path: &str,
lhs: &str,
rhs: &str,
params: &Params,
) -> Result<Self, QueryConflict> {
Self::from_sources_with_options(path, lhs, rhs, params, &DiffOptions::default())
}
pub(crate) fn from_sources_with_options(
path: &str,
lhs: &str,
rhs: &str,
params: &Params,
options: &DiffOptions,
) -> Result<Self, QueryConflict> {
let file = crate::options::FileArgument::NamedPath(path.into());
diff_file_content(params, path, &file, &file, lhs, rhs, options, &[])
}
}
pub(crate) fn diff_file_content(
params: &Params,
display_path: &str,
_lhs_path: &FileArgument,
rhs_path: &FileArgument,
lhs_src: &str,
rhs_src: &str,
diff_options: &DiffOptions,
overrides: &[(LanguageOverride, Vec<glob::Pattern>)],
) -> Result<DiffResult, QueryConflict> {
let guess_src = match rhs_path {
FileArgument::DevNull => &lhs_src,
_ => &rhs_src,
};
let language = guess(Path::new(display_path), guess_src, overrides);
let lang_config = language.map(|lang| (lang, params.language(lang)));
if lhs_src == rhs_src {
let file_format = match language {
Some(language) => FileFormat::SupportedLanguage(language),
None => FileFormat::PlainText,
};
// If the two files are byte-for-byte identical, return early
// rather than doing any more work.
return Ok(DiffResult {
file_format,
lhs_src: FileContent::Text(lhs_src.into()),
rhs_src: FileContent::Text(rhs_src.into()),
lhs_positions: vec![],
rhs_positions: vec![],
lhs_folds: vec![],
rhs_folds: vec![],
});
}
let mut lhs_folds = Vec::new();
let mut rhs_folds = Vec::new();
let (file_format, lhs_positions, rhs_positions) = match lang_config {
_ if diff_options.generated => {
let file_format = FileFormat::TextFallback {
cause: FallbackCause::Generated,
reason: GENERATED_FALLBACK.to_owned(),
};
let (lhs_positions, rhs_positions) = line_parser::change_positions(lhs_src, rhs_src);
(file_format, lhs_positions, rhs_positions)
}
None => {
let file_format = FileFormat::PlainText;
let (lhs_positions, rhs_positions) = line_parser::change_positions(lhs_src, rhs_src);
(file_format, lhs_positions, rhs_positions)
}
Some((language, lang_config)) => {
let arena = Arena::new();
match tsp::to_tree_with_limit(diff_options, lang_config.parser, lhs_src, rhs_src) {
Ok((lhs_tree, rhs_tree)) => {
match tsp::to_syntax_with_limit(
lhs_src,
rhs_src,
&lhs_tree,
&rhs_tree,
&arena,
lang_config,
diff_options,
) {
Ok((lhs, rhs)) => {
let mut change_map = ChangeMap::default();
let possibly_changed = if env::var("DFT_DBG_KEEP_UNCHANGED").is_ok() {
vec![(lhs.clone(), rhs.clone())]
} else {
unchanged::mark_unchanged(&lhs, &rhs, &mut change_map)
};
let mut exceeded_graph_limit = false;
for (lhs_section_nodes, rhs_section_nodes) in possibly_changed {
init_next_prev(&lhs_section_nodes);
init_next_prev(&rhs_section_nodes);
match mark_syntax(
lhs_section_nodes.first().copied(),
rhs_section_nodes.first().copied(),
&mut change_map,
diff_options.graph_limit,
) {
Ok(()) => {}
Err(ExceededGraphLimit {}) => {
exceeded_graph_limit = true;
break;
}
}
}
if exceeded_graph_limit {
// The parse still stands, so its folds do,
// each unpaired: nothing matched the nodes
// they belong to.
folds::unmatched(&lhs, &mut lhs_folds);
folds::unmatched(&rhs, &mut rhs_folds);
let (lhs_positions, rhs_positions) =
line_parser::change_positions(lhs_src, rhs_src);
(
FileFormat::TextFallback {
cause: FallbackCause::GraphLimit,
reason: format!(
"structural diff exceeded diff.graph_limit ({}); raise it in diffr config",
diff_options.graph_limit
),
},
lhs_positions,
rhs_positions,
)
} else {
fix_all_sliders(language, &lhs, &mut change_map);
fix_all_sliders(language, &rhs, &mut change_map);
let mut lhs_positions =
syntax::change_positions(&lhs, &change_map, &mut lhs_folds);
let mut rhs_positions =
syntax::change_positions(&rhs, &change_map, &mut rhs_folds);
if diff_options.ignore_comments {
let lhs_comments =
tsp::comment_positions(&lhs_tree, lhs_src, lang_config);
lhs_positions.extend(lhs_comments);
let rhs_comments =
tsp::comment_positions(&rhs_tree, rhs_src, lang_config);
rhs_positions.extend(rhs_comments);
}
(
FileFormat::SupportedLanguage(language),
lhs_positions,
rhs_positions,
)
}
}
Err(tsp::ToSyntaxError::QueryConflict(conflict, side)) => {
return Err(QueryConflict {
path: display_path.to_owned(),
side,
conflict,
});
}
Err(tsp::ToSyntaxError::ExceededParseErrorLimit(
tsp::ExceededParseErrorLimit {
error_count,
first_error_pos,
},
)) => {
let location = match first_error_pos {
Some((line, column, side)) => {
let in_initial = match side {
Side::Left => " in initial file",
Side::Right => "",
};
format!(
", first at {}:{}{}",
line.display(),
column,
in_initial
)
}
None => "".to_owned(),
};
let file_format = FileFormat::TextFallback {
cause: FallbackCause::ParseErrorLimit,
reason: format!(
"{} {} parse error{}{}, exceeded diff.parse_error_limit ({}); raise it in diffr config",
error_count,
language_name(language),
if error_count == 1 { "" } else { "s" },
location,
diff_options.parse_error_limit
),
};
// The trees parsed, only with too many errors to
// match on. Folds and context still come from them.
let conflict = |side| {
move |conflict| QueryConflict {
path: display_path.to_owned(),
side,
conflict,
}
};
let (lhs, _) = tsp::to_syntax(
&lhs_tree,
lhs_src,
&arena,
lang_config,
diff_options.ignore_comments,
)
.map_err(conflict(Side::Left))?;
let (rhs, _) = tsp::to_syntax(
&rhs_tree,
rhs_src,
&arena,
lang_config,
diff_options.ignore_comments,
)
.map_err(conflict(Side::Right))?;
// Folds are identified by syntax id, which only
// exists once both sides are numbered.
syntax::init_all_info(&lhs, &rhs);
folds::unmatched(&lhs, &mut lhs_folds);
folds::unmatched(&rhs, &mut rhs_folds);
let (lhs_positions, rhs_positions) =
line_parser::change_positions(lhs_src, rhs_src);
(file_format, lhs_positions, rhs_positions)
}
}
}
Err(tsp::ExceededByteLimit(num_bytes)) => {
let format_options = FormatSizeOptions::from(BINARY).decimal_places(1);
let file_format = FileFormat::TextFallback {
cause: FallbackCause::ByteLimit,
reason: format!(
"{} exceeded diff.byte_limit ({}); raise it in diffr config",
format_size(num_bytes, format_options),
diff_options.byte_limit
),
};
let (lhs_positions, rhs_positions) =
line_parser::change_positions(lhs_src, rhs_src);
(file_format, lhs_positions, rhs_positions)
}
}
}
};
// Folds are regions of the file, so two nodes that cover the same lines
// hold one fold between them.
folds::merge_spans(
&mut lhs_folds,
&lhs_src.split_terminator('\n').collect::<Vec<_>>(),
);
folds::merge_spans(
&mut rhs_folds,
&rhs_src.split_terminator('\n').collect::<Vec<_>>(),
);
Ok(DiffResult {
file_format,
lhs_src: FileContent::Text(lhs_src.to_owned()),
rhs_src: FileContent::Text(rhs_src.to_owned()),
lhs_positions,
rhs_positions,
lhs_folds,
rhs_folds,
})
}