diff options
author | Akshay <[email protected]> | 2021-10-23 08:11:30 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2021-10-23 08:11:30 +0100 |
commit | dfcdaf91674461a5150902cb3fdb8f198367ff20 (patch) | |
tree | 94fe87e89fb4b1e4f50e0917cfd4a138ddea133a | |
parent | e8f5ed3a5ce9ba10e900b97dc95d3961dfa60718 (diff) |
add suggestion application logic
-rw-r--r-- | lib/src/lib.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 6bb16b6..d8a3859 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs | |||
@@ -49,6 +49,16 @@ impl Report { | |||
49 | .flat_map(|d| Some(d.suggestion.as_ref()?.at)) | 49 | .flat_map(|d| Some(d.suggestion.as_ref()?.at)) |
50 | .reduce(|acc, next| acc.cover(next)) | 50 | .reduce(|acc, next| acc.cover(next)) |
51 | } | 51 | } |
52 | /// Unsafe but handy replacement for above | ||
53 | pub fn range(&self) -> TextRange { | ||
54 | self.total_suggestion_range().unwrap() | ||
55 | } | ||
56 | /// Apply all diagnostics. Assumption: diagnostics do not overlap | ||
57 | pub fn apply(&self, src: &mut String) { | ||
58 | for d in self.diagnostics.iter() { | ||
59 | d.apply(src); | ||
60 | } | ||
61 | } | ||
52 | } | 62 | } |
53 | 63 | ||
54 | /// Mapping from a bytespan to an error message. | 64 | /// Mapping from a bytespan to an error message. |
@@ -77,6 +87,12 @@ impl Diagnostic { | |||
77 | suggestion: Some(suggestion), | 87 | suggestion: Some(suggestion), |
78 | } | 88 | } |
79 | } | 89 | } |
90 | /// Apply a diagnostic to a source file | ||
91 | pub fn apply(&self, src: &mut String) { | ||
92 | if let Some(s) = &self.suggestion { | ||
93 | s.apply(src); | ||
94 | } | ||
95 | } | ||
80 | } | 96 | } |
81 | 97 | ||
82 | /// Suggested fix for a diagnostic, the fix is provided as a syntax element. | 98 | /// Suggested fix for a diagnostic, the fix is provided as a syntax element. |
@@ -95,6 +111,12 @@ impl Suggestion { | |||
95 | fix: fix.into(), | 111 | fix: fix.into(), |
96 | } | 112 | } |
97 | } | 113 | } |
114 | /// Apply a suggestion to a source file | ||
115 | pub fn apply(&self, src: &mut String) { | ||
116 | let start = usize::from(self.at.start()); | ||
117 | let end = usize::from(self.at.end()); | ||
118 | src.replace_range(start..end, &self.fix.to_string()) | ||
119 | } | ||
98 | } | 120 | } |
99 | 121 | ||
100 | /// Lint logic is defined via this trait. Do not implement manually, | 122 | /// Lint logic is defined via this trait. Do not implement manually, |