From 6121f57c1db649ed6dadedceee8e6ddb75ce0b2a Mon Sep 17 00:00:00 2001 From: Elijah Potter Date: Sun, 14 Jan 2024 12:30:00 -0700 Subject: [PATCH] Underlines now expand when the suggestion is hovered --- lt-core/src/linting/mod.rs | 2 +- .../src/linting/sentence_capitalization.rs | 5 -- lt-core/src/linting/spell_check.rs | 2 +- web/src/lib/Suggestion.svelte | 26 -------- web/src/lib/Underlines.svelte | 54 ++++++++++++---- web/src/routes/+page.svelte | 64 +++++++++++++------ 6 files changed, 89 insertions(+), 64 deletions(-) delete mode 100644 web/src/lib/Suggestion.svelte diff --git a/lt-core/src/linting/mod.rs b/lt-core/src/linting/mod.rs index e3d1d7c5..dd09f19c 100644 --- a/lt-core/src/linting/mod.rs +++ b/lt-core/src/linting/mod.rs @@ -4,7 +4,7 @@ mod spell_check; pub use lint::{Lint, LintKind, Suggestion}; -use crate::{document, Document}; +use crate::Document; use self::lint::Linter; diff --git a/lt-core/src/linting/sentence_capitalization.rs b/lt-core/src/linting/sentence_capitalization.rs index 1e9186d3..c9312a6c 100644 --- a/lt-core/src/linting/sentence_capitalization.rs +++ b/lt-core/src/linting/sentence_capitalization.rs @@ -9,11 +9,6 @@ pub fn sentence_capitalization_lint(document: &Document) -> Vec { let mut lints = Vec::new(); for sentence in document.sentences() { - dbg!(document.get_content_string(crate::Span { - start: sentence.first().unwrap().span.start, - end: sentence.last().unwrap().span.start, - })); - if let Some(first_word) = sentence.first_word() { let letters = document.get_span_content(first_word.span); diff --git a/lt-core/src/linting/spell_check.rs b/lt-core/src/linting/spell_check.rs index 81b17174..14044f88 100644 --- a/lt-core/src/linting/spell_check.rs +++ b/lt-core/src/linting/spell_check.rs @@ -13,7 +13,7 @@ pub fn spell_check(document: &Document) -> Vec { continue; } - let possibilities = suggest_correct_spelling(word_chars, 1, 3, &dictionary); + let possibilities = suggest_correct_spelling(word_chars, 3, 3, &dictionary); let suggestions = possibilities .into_iter() diff --git a/web/src/lib/Suggestion.svelte b/web/src/lib/Suggestion.svelte deleted file mode 100644 index f1ad3b60..00000000 --- a/web/src/lib/Suggestion.svelte +++ /dev/null @@ -1,26 +0,0 @@ - - - -
-

{title}

- {startCase(category)} -
- -
diff --git a/web/src/lib/Underlines.svelte b/web/src/lib/Underlines.svelte index 28d3d36d..ac2faaf5 100644 --- a/web/src/lib/Underlines.svelte +++ b/web/src/lib/Underlines.svelte @@ -3,10 +3,15 @@ import { contentToString, lintText, spanContent } from '$lib/analysis'; export let content: string; + export let focusLintIndex: number | undefined; + + let lints: [Lint, number][] = []; - let lints: Lint[] = []; $: lintText(content).then( - (newLints) => (lints = newLints.toSorted((a, b) => a.span.start - b.span.end)) + (newLints) => + (lints = newLints + .map<[Lint, number]>((lint, index) => [lint, index]) + .toSorted(([a], [b]) => a.span.start - b.span.end)) ); function reOrgString(text: string): (string | undefined)[] { @@ -27,13 +32,16 @@ return output; } - // string | [string, string] | null - $: modified = [ - ...lints - .map((lint, index, arr) => { + function processString(lintMap: [Lint, number][], focusLintIndex?: number) { + let results = lintMap + .map(([lint, lintIndex], index, arr) => { + let prevStart = 0; let prev = arr[index - 1]; - let prevStart = prev?.span.end ?? 0; + if (prev != null) { + prevStart = prev[0].span.end; + } + let prevEnd = lint.span.start; let prevContent = []; @@ -42,17 +50,37 @@ prevContent.push(...reOrgString(content.substring(prevStart, prevEnd))); } - let lintContent = [spanContent(lint.span, content).replaceAll(' ', '\u00A0'), 'red']; + let lintContent = [ + spanContent(lint.span, content).replaceAll(' ', '\u00A0'), + 'red', + lintIndex === focusLintIndex ? '3px' : '1px' + ]; return [...prevContent, lintContent]; }) - .flat(), - ...reOrgString(content.substring(lints.at(-1)?.span.end ?? 0)) - ]; + .flat(); + + let lastLint = lints.at(-1); + + let finalChunk; + + if (lastLint != null) { + finalChunk = content.substring(lastLint[0].span.end); + } else { + finalChunk = content; + } + + results.push(...reOrgString(finalChunk)); + + return results; + } + + // string | [string, string, string] | null + $: modified = processString(lints, focusLintIndex);
-
+
{#each modified as chunk} {#if chunk == null}
@@ -60,7 +88,7 @@ {chunk} {:else} - + {chunk[0]} diff --git a/web/src/routes/+page.svelte b/web/src/routes/+page.svelte index 62125a9b..9bdb1a1f 100644 --- a/web/src/routes/+page.svelte +++ b/web/src/routes/+page.svelte @@ -1,16 +1,17 @@
- +
@@ -39,21 +53,35 @@ >
- {#each lints as lint} - {#each lint.suggestions as suggestion} - - - - {/each} + {#each lints as lint, i} + (focused = i)} + on:mouseleave={() => (focused = undefined)} + > +
+
+
+

{`${lint.lint_kind} - "${spanContent(lint.span, content)}"`}

+
+
+ {#each lint.suggestions as suggestion} + + {/each} +
+
+
{/each}