Skip to content

Commit

Permalink
Solved issues with overlapping lints on web client
Browse files Browse the repository at this point in the history
  • Loading branch information
elijah-potter committed Jan 28, 2024
1 parent 5030a2b commit 3359b78
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 6 deletions.
2 changes: 1 addition & 1 deletion harper-core/benches/parse_demo.rs
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions harper-core/dictionary.dict
Original file line number Diff line number Diff line change
Expand Up @@ -49592,3 +49592,5 @@ TODO
Todo
raytracer
viewport
backend
frontend
1 change: 1 addition & 0 deletions harper-core/src/lexing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ fn lex_punctuation(source: &[char]) -> Option<FoundToken> {
'(' => OpenRound,
')' => CloseRound,
'#' => Hash,
'*' => Star,
_ => return None,
};

Expand Down
1 change: 0 additions & 1 deletion harper-core/src/linting/repeated_words.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use hashbrown::HashSet;
use smallvec::smallvec;

use crate::{
spell::DictWord,
Expand Down
4 changes: 2 additions & 2 deletions harper-core/src/spell/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:.
Expand Down Expand Up @@ -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 length
pub 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());
Expand Down
4 changes: 3 additions & 1 deletion harper-core/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ pub enum Punctuation {
LessThan,
/// >
GreaterThan,
/// Equal
/// =
Equal,
/// *
Star,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, PartialOrd)]
Expand Down
4 changes: 4 additions & 0 deletions harper-ls/README.md
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 6 additions & 0 deletions harper-serve/README.md
Original file line number Diff line number Diff line change
@@ -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).
2 changes: 1 addition & 1 deletion harper-serve/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
30 changes: 30 additions & 0 deletions web/src/lib/analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}
}

0 comments on commit 3359b78

Please sign in to comment.