From dfcdaf91674461a5150902cb3fdb8f198367ff20 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 23 Oct 2021 12:41:30 +0530 Subject: add suggestion application logic --- lib/src/lib.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'lib/src') 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 { .flat_map(|d| Some(d.suggestion.as_ref()?.at)) .reduce(|acc, next| acc.cover(next)) } + /// Unsafe but handy replacement for above + pub fn range(&self) -> TextRange { + self.total_suggestion_range().unwrap() + } + /// Apply all diagnostics. Assumption: diagnostics do not overlap + pub fn apply(&self, src: &mut String) { + for d in self.diagnostics.iter() { + d.apply(src); + } + } } /// Mapping from a bytespan to an error message. @@ -77,6 +87,12 @@ impl Diagnostic { suggestion: Some(suggestion), } } + /// Apply a diagnostic to a source file + pub fn apply(&self, src: &mut String) { + if let Some(s) = &self.suggestion { + s.apply(src); + } + } } /// Suggested fix for a diagnostic, the fix is provided as a syntax element. @@ -95,6 +111,12 @@ impl Suggestion { fix: fix.into(), } } + /// Apply a suggestion to a source file + pub fn apply(&self, src: &mut String) { + let start = usize::from(self.at.start()); + let end = usize::from(self.at.end()); + src.replace_range(start..end, &self.fix.to_string()) + } } /// Lint logic is defined via this trait. Do not implement manually, -- cgit v1.2.3