Skip to content

Commit

Permalink
feat: added suggestion variant Remove
Browse files Browse the repository at this point in the history
  • Loading branch information
elijah-potter committed Feb 29, 2024
1 parent b9092e2 commit 64d233b
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions harper-core/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion harper-core/src/linting/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
}
}
}
2 changes: 1 addition & 1 deletion harper-core/src/linting/repeated_words.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand Down
9 changes: 6 additions & 3 deletions harper-ls/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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,
Expand Down
12 changes: 8 additions & 4 deletions web/src/lib/Editor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
8 changes: 5 additions & 3 deletions web/src/lib/analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 64d233b

Please sign in to comment.