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
use std::ops::Range;
use smol_str::SmolStr;
/// A stored reference to a fix in the non-templated file.
#[derive(Hash, Debug, Clone, PartialEq, Eq)]
pub struct SourceFix {
pub(crate) edit: SmolStr,
pub(crate) source_slice: Range<usize>,
// TODO: It might be possible to refactor this to not require
// a templated_slice (because in theory it's unnecessary).
// However much of the fix handling code assumes we need
// a position in the templated file to interpret it.
// More work required to achieve that if desired.
pub(crate) templated_slice: Range<usize>,
}
impl SourceFix {
pub fn new(edit: SmolStr, source_slice: Range<usize>, templated_slice: Range<usize>) -> Self {
SourceFix {
edit,
source_slice,
templated_slice,
}
}
}
/// An edit patch for a source file.
#[derive(Clone, Debug)]
#[allow(dead_code)]
pub struct FixPatch {
templated_slice: Range<usize>,
pub fixed_raw: SmolStr,
// The patch category, functions mostly for debugging and explanation
// than for function. It allows traceability of *why* this patch was
// generated. It has no significance for processing. Brought over from sqlfluff
// patch_category: FixPatchCategory,
pub source_slice: Range<usize>,
templated_str: String,
source_str: String,
}
impl FixPatch {
pub fn new(
templated_slice: Range<usize>,
fixed_raw: SmolStr,
source_slice: Range<usize>,
templated_str: String,
source_str: String,
) -> Self {
FixPatch {
templated_slice,
fixed_raw,
source_slice,
templated_str,
source_str,
}
}
/// Generate a tuple of this fix for deduping.
pub fn dedupe_tuple(&self) -> Range<usize> {
self.source_slice.clone()
}
/// Whether this patch is a source-only fix (e.g. jinja tag spacing).
pub fn is_source_fix(&self) -> bool {
!self.source_str.is_empty() && self.templated_str != self.source_str
}
/// Whether this is a zero-length insertion at a point.
pub fn is_zero_length_source(&self) -> bool {
self.source_slice.start == self.source_slice.end
}
}