Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

Merge pull request #186 from fisherdarling/fisher/stable-rustc

Elijah Potterb2b00e691a5a
diff --cc harper-core/src/linting/lint.rs
index 08b42bb12,a9445ed05..e4053181b
--- a/harper-core/src/linting/lint.rs
+++ b/harper-core/src/linting/lint.rs
@@@ -57,37 -57,9 +57,37 @@@ impl Display for LintKind
#[derive(Debug, Clone, Serialize, Deserialize, Is)]
pub enum Suggestion {
ReplaceWith(Vec<char>),
- Remove
+ Remove,
}
+impl Suggestion {
+ /// Apply a suggestion to a given text.
+ pub fn apply(&self, span: Span, source: &mut Vec<char>) {
+ match self {
+ Self::ReplaceWith(chars) => {
+ // Avoid allocation if possible
+ if chars.len() == span.len() {
+ for (index, c) in chars.iter().enumerate() {
+ source[index + span.start] = *c
+ }
+ } else {
+ let popped = source.split_off(span.start);
+
+ source.extend(chars);
+ source.extend(popped.into_iter().skip(span.len()));
+ }
+ }
+ Self::Remove => {
+ for i in span.end..source.len() {
+ source[i - span.len()] = source[i];
+ }
+
+ source.truncate(source.len() - span.len());
+ }
+ }
+ }
+}
+
impl Display for Suggestion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {