Skip to content

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

feat: added suggestion variant `Remove`

Elijah Potter64d233b5c376
diff --git a/demo.md b/demo.md
index 3dc005f18..daf6deaee 100644
--- a/demo.md
+++ b/demo.md
@@ -2,7 +2,7 @@ There are some cases where the the standard grammar checkers
don't cut it. That s where Harper comes in handy.
Harper is a language checker for developers. it can detect
-improper capitalization and mispelled words.
+improper capitalization and misspellled words.
Harper works everywhere, even offline. Since you r data
never leaves your device, you don't ned to worry aout us
diff --git a/harper-core/src/document.rs b/harper-core/src/document.rs
index 9abbd146b..091efe440 100644
--- a/harper-core/src/document.rs
+++ b/harper-core/src/document.rs
@@ -156,6 +156,13 @@ impl Document {
self.source.extend(popped.into_iter().skip(span.len()));
}
}
+ Suggestion::Remove => {
+ for i in span.end..self.source.len() {
+ self.source[i - span.len()] = self.source[i];
+ }
+
+ self.source.truncate(self.source.len() - span.len());
+ }
}
self.parse();
diff --git a/harper-core/src/linting/lint.rs b/harper-core/src/linting/lint.rs
index 7f095cee6..e557e45ac 100644
--- a/harper-core/src/linting/lint.rs
+++ b/harper-core/src/linting/lint.rs
@@ -41,7 +41,8 @@ pub enum LintKind {
#[derive(Debug, Clone, Serialize, Deserialize, Is)]
pub enum Suggestion {
- ReplaceWith(Vec<char>)
+ ReplaceWith(Vec<char>),
+ Remove
}
impl Display for Suggestion {
@@ -50,6 +51,7 @@ impl Display for Suggestion {
Suggestion::ReplaceWith(with) => {
write!(f, "Replace with: “{}”", with.iter().collect::<String>())
}
+ Suggestion::Remove => write!(f, "Remove error")
}
}
}
diff --git a/harper-core/src/linting/repeated_words.rs b/harper-core/src/linting/repeated_words.rs
index 75b1e5a1b..c95c80ae4 100644
--- a/harper-core/src/linting/repeated_words.rs
+++ b/harper-core/src/linting/repeated_words.rs
@@ -85,7 +85,7 @@ impl Linter for RepeatedWords {
lints.push(Lint {
span: Span::new(remove_start, remove_end),
lint_kind: LintKind::Repetition,
- suggestions: vec![Suggestion::ReplaceWith(Vec::new())],
+ suggestions: vec![Suggestion::Remove],
message: "Did you mean to repeat this word?".to_string(),
..Default::default()
})
diff --git a/harper-ls/src/diagnostics.rs b/harper-ls/src/diagnostics.rs
index c6f201523..1b5b93936 100644
--- a/harper-ls/src/diagnostics.rs
+++ b/harper-ls/src/diagnostics.rs
@@ -34,7 +34,11 @@ pub fn lint_to_code_actions<'a>(
.flat_map(|suggestion| {
let range = span_to_range(source, lint.span);
- let Suggestion::ReplaceWith(with) = suggestion;
+ let replace_string = match suggestion {
+ Suggestion::ReplaceWith(with) => with.iter().collect(),
+ Suggestion::Remove => "".to_string()
+ };
+
Some(CodeAction {
title: suggestion.to_string(),
kind: Some(CodeActionKind::QUICKFIX),
@@ -44,8 +48,7 @@ pub fn lint_to_code_actions<'a>(
url.clone(),
vec![TextEdit {
range,
-
- new_text: with.iter().collect()
+ new_text: replace_string
}]
)])),
document_changes: None,
diff --git a/web/src/lib/Editor.svelte b/web/src/lib/Editor.svelte
index 2efc0268e..4af5e21bb 100644
--- a/web/src/lib/Editor.svelte
+++ b/web/src/lib/Editor.svelte
@@ -86,10 +86,14 @@
(edited) => (content = edited)
)}
>
- Replace "{content.substring(lint.span.start, lint.span.end)}" with "{suggestion.ReplaceWith.reduce(
- (p, c) => p + c,
- ''
- )}"
+ {#if suggestion == 'Remove'}
+ Remove "{content.substring(lint.span.start, lint.span.end)}"
+ {:else}
+ Replace "{content.substring(lint.span.start, lint.span.end)}" with "{suggestion.ReplaceWith.reduce(
+ (p, c) => p + c,
+ ''
+ )}"
+ {/if}
</Button>
</div>
{/each}
diff --git a/web/src/lib/analysis.ts b/web/src/lib/analysis.ts
index d1cf198d6..182d70f30 100644
--- a/web/src/lib/analysis.ts
+++ b/web/src/lib/analysis.ts
@@ -67,9 +67,11 @@ export interface Lint {
message: string;
}
-export interface Suggestion {
- ReplaceWith: string[];
-}
+export type Suggestion =
+ | {
+ ReplaceWith: string[];
+ }
+ | 'Remove';
export interface Span {
start: number;