Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
Solved issues with overlapping lints on web client
Elijah Potter3359b7819738
diff --git a/harper-core/benches/parse_demo.rs b/harper-core/benches/parse_demo.rsindex 04e7d57b6..da1c1fa49 100644--- a/harper-core/benches/parse_demo.rs+++ b/harper-core/benches/parse_demo.rs@@ -1,4 +1,4 @@-use criterion::{black_box, criterion_group, criterion_main, Criterion};+use criterion::{criterion_group, criterion_main, Criterion};use harper_core::{Dictionary, Document, LintSet, Linter};fn parse_demo(c: &mut Criterion) {diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dictindex 3018d8657..59b486302 100644--- a/harper-core/dictionary.dict+++ b/harper-core/dictionary.dict@@ -49592,3 +49592,5 @@ TODOTodoraytracerviewport+backend+frontenddiff --git a/harper-core/src/lexing/mod.rs b/harper-core/src/lexing/mod.rsindex e2946bf69..ef34578dd 100644--- a/harper-core/src/lexing/mod.rs+++ b/harper-core/src/lexing/mod.rs@@ -137,6 +137,7 @@ fn lex_punctuation(source: &[char]) -> Option<FoundToken> {'(' => OpenRound,')' => CloseRound,'#' => Hash,+ '*' => Star,_ => return None,};diff --git a/harper-core/src/linting/repeated_words.rs b/harper-core/src/linting/repeated_words.rsindex 6f8338169..e81ae78e9 100644--- a/harper-core/src/linting/repeated_words.rs+++ b/harper-core/src/linting/repeated_words.rs@@ -1,5 +1,4 @@use hashbrown::HashSet;-use smallvec::smallvec;use crate::{spell::DictWord,diff --git a/harper-core/src/spell/dictionary.rs b/harper-core/src/spell/dictionary.rsindex 03fde7f1f..9646ce749 100644--- a/harper-core/src/spell/dictionary.rs+++ b/harper-core/src/spell/dictionary.rs@@ -9,7 +9,7 @@ use super::{#[derive(Debug, Clone)]pub struct Dictionary {- /// Storing a separate [Vec] for iterations speeds up spellchecking by ~16% at the cost of+ /// Storing a separate [`Vec`] for iterations speeds up spellchecking by ~16% at the cost of/// additional memory.////// This is likely due to increased locality :shrug:.@@ -63,7 +63,7 @@ impl Dictionary {DICT.clone()}- /// Iterate over all the words in the dicitonary of a given length+ /// Iterate over all the words in the dictionary of a given lengthpub fn words_with_len_iter(&self, len: usize) -> Box<dyn Iterator<Item = &'_ [char]> + '_> {if len == 0 || len >= self.word_len_starts.len() {return Box::new(std::iter::empty());diff --git a/harper-core/src/token.rs b/harper-core/src/token.rsindex 7db10c28e..7a49933e8 100644--- a/harper-core/src/token.rs+++ b/harper-core/src/token.rs@@ -100,8 +100,10 @@ pub enum Punctuation {LessThan,/// >GreaterThan,- /// Equal+ /// =Equal,+ /// *+ Star,}#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, PartialOrd)]diff --git a/harper-ls/README.md b/harper-ls/README.mdnew file mode 100644index 000000000..00f7ff776--- /dev/null+++ b/harper-ls/README.md@@ -0,0 +1,4 @@+# `harper-ls`++`harper-ls` is the [Language Server Protocol](https://microsoft.github.io/language-server-protocol/) frontend for [Harper](https://harper.elijahpotter.dev).+Out of the box, it has built-in support for parsing the comments of most programming languages.diff --git a/harper-serve/README.md b/harper-serve/README.mdnew file mode 100644index 000000000..93383e9ea--- /dev/null+++ b/harper-serve/README.md@@ -0,0 +1,6 @@+# `harper-serve`++This crates exists as a way to debug both the web interface and the underlying engine at the same time.+To set the web interface to use a server as the backend, edit the first line of `analysis.ts`.++When I'm debugging, I then start the `harper-serve` binary with [`bacon`](https://github.com/Canop/bacon).diff --git a/harper-serve/src/main.rs b/harper-serve/src/main.rsindex c4e2632ba..f604127b0 100644--- a/harper-serve/src/main.rs+++ b/harper-serve/src/main.rs@@ -1,7 +1,7 @@#![allow(dead_code)]use harper_core::{Dictionary, Document, FatToken, Lint, LintSet, Linter, Span, Suggestion};-use std::{marker, net::SocketAddr};+use std::net::SocketAddr;use tokio::time::Instant;use tracing::{info, Level};use tracing_subscriber::FmtSubscriber;diff --git a/web/src/lib/analysis.ts b/web/src/lib/analysis.tsindex 1f9e2b360..263f2cb8b 100644--- a/web/src/lib/analysis.ts+++ b/web/src/lib/analysis.ts@@ -81,6 +81,7 @@ export function spanContent(span: Span, source: string): string {}export async function lintText(text: string, useWasm = defaultUseWasm): Promise<Lint[]> {+ console.time('lint');let lints;if (useWasm) {@@ -103,6 +104,10 @@ export async function lintText(text: string, useWasm = defaultUseWasm): Promise<// We only want to show fixable errors.lints = lints.filter((lint) => lint.suggestions.length > 0);+ // The `Underlines` component assumes the lints do not overlap.+ lints = removeOverlaps(lints);++ console.timeEnd('lint');return lints;}@@ -130,3 +135,28 @@ export async function applySuggestion(return res.text;}}++/** Removes lints whose spans overlap.+ * NOTE: __Will__ reorder the lints. */+function removeOverlaps(lints: Lint[]) {+ const sorted = lints.sort((a, b) => a.span.start - b.span.start);++ let overlapsFound = false;++ const filtered = sorted.filter((value, idx, arr) => {+ const next = arr[idx + 1];++ if (next != null && next.span.start < value.span.end) {+ overlapsFound = true;+ return false;+ } else {+ return true;+ }+ });++ if (overlapsFound) {+ return removeOverlaps(filtered);+ } else {+ return filtered;+ }+}