Skip to content

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

Now searches for unclosed quotes + suggestions are closed by default

Elijah Potterdb0d1a9fc14a
diff --git a/lt-core/src/document.rs b/lt-core/src/document.rs
index ce47a8e85..b6bbdfdd6 100644
--- a/lt-core/src/document.rs
+++ b/lt-core/src/document.rs
@@ -17,11 +17,48 @@ pub struct Document {
}
impl Document {
+ // Lexes and parses text to produce a document.
pub fn new(text: &str) -> Self {
let source: Vec<_> = text.chars().collect();
let tokens = lex_to_end(&source);
- Self { source, tokens }
+ let mut doc = Self { source, tokens };
+ doc.match_quotes();
+
+ doc
+ }
+
+ fn iter_quote_indices(&self) -> impl Iterator<Item = usize> + '_ {
+ self.tokens.iter().enumerate().filter_map(|(idx, token)| {
+ if let TokenKind::Punctuation(Punctuation::Quote(_)) = &token.kind {
+ Some(idx)
+ } else {
+ None
+ }
+ })
+ }
+
+ /// Searches for quotation marks and fills the [`Punctuation::Quote::twin_loc`] field.
+ /// This is on a best effort basis.
+ ///
+ /// Current algorithm is very basic and could use some work.
+ fn match_quotes(&mut self) {
+ let quote_indices: Vec<usize> = self.iter_quote_indices().collect();
+
+ for i in 0..quote_indices.len() / 2 {
+ let a_i = quote_indices[i * 2];
+ let b_i = quote_indices[i * 2 + 1];
+
+ {
+ let a = self.tokens[a_i].kind.as_mut_quote().unwrap();
+ a.twin_loc = Some(b_i);
+ }
+
+ {
+ let b = self.tokens[b_i].kind.as_mut_quote().unwrap();
+ b.twin_loc = Some(a_i);
+ }
+ }
}
pub fn tokens(&self) -> impl Iterator<Item = Token> + '_ {
@@ -32,7 +69,7 @@ impl Document {
self.tokens().map(|token| token.to_fat(&self.source))
}
- /// Create a list of the sentence terminators in the document.
+ /// Iterate over the locations of the sentence terminators in the document.
fn sentence_terminators(&self) -> impl Iterator<Item = usize> + '_ {
self.tokens.iter().enumerate().filter_map(|(index, token)| {
if let Token {
@@ -55,7 +92,7 @@ impl Document {
let first_sentence = self
.sentence_terminators()
.next()
- .map(|first_term| &self.tokens[0..=first_term]);
+ .map(|first_term| &self.tokens[1..=first_term]);
let rest = self
.sentence_terminators()
diff --git a/lt-core/src/linting/lint.rs b/lt-core/src/linting/lint.rs
index b0b94d610..a1225c52e 100644
--- a/lt-core/src/linting/lint.rs
+++ b/lt-core/src/linting/lint.rs
@@ -7,12 +7,14 @@ pub struct Lint {
pub span: Span,
pub lint_kind: LintKind,
pub suggestions: Vec<Suggestion>,
+ pub message: String,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum LintKind {
Spelling,
Capitalization,
+ UnmatchedQuote,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
diff --git a/lt-core/src/linting/mod.rs b/lt-core/src/linting/mod.rs
index dd09f19cd..c5edd19ec 100644
--- a/lt-core/src/linting/mod.rs
+++ b/lt-core/src/linting/mod.rs
@@ -1,6 +1,7 @@
mod lint;
mod sentence_capitalization;
mod spell_check;
+mod unclosed_quotes;
pub use lint::{Lint, LintKind, Suggestion};
@@ -11,9 +12,10 @@ use self::lint::Linter;
pub fn all_linters(document: &Document) -> Vec<Lint> {
let mut lints = Vec::new();
- let linters: [Linter; 2] = [
+ let linters: [Linter; 3] = [
spell_check::spell_check,
sentence_capitalization::sentence_capitalization_lint,
+ unclosed_quotes::unclosed_quotes,
];
for linter in linters {
diff --git a/lt-core/src/linting/sentence_capitalization.rs b/lt-core/src/linting/sentence_capitalization.rs
index c9312a6c5..8479c12b1 100644
--- a/lt-core/src/linting/sentence_capitalization.rs
+++ b/lt-core/src/linting/sentence_capitalization.rs
@@ -20,6 +20,7 @@ pub fn sentence_capitalization_lint(document: &Document) -> Vec<Lint> {
suggestions: vec![Suggestion::ReplaceWith(
first_letter.to_uppercase().collect_vec(),
)],
+ message: "This sentance does not start with a capital letter".to_string(),
})
}
}
diff --git a/lt-core/src/linting/spell_check.rs b/lt-core/src/linting/spell_check.rs
index 14044f88c..173f46b35 100644
--- a/lt-core/src/linting/spell_check.rs
+++ b/lt-core/src/linting/spell_check.rs
@@ -23,6 +23,7 @@ pub fn spell_check(document: &Document) -> Vec<Lint> {
span: word.span,
lint_kind: LintKind::Spelling,
suggestions: suggestions.collect(),
+ message: "Did you mean to spell it this way?".to_string(),
})
}
diff --git a/lt-core/src/linting/unclosed_quotes.rs b/lt-core/src/linting/unclosed_quotes.rs
new file mode 100644
index 000000000..3276c9132
--- /dev/null
+++ b/lt-core/src/linting/unclosed_quotes.rs
@@ -0,0 +1,19 @@
+use crate::{document::Document, parsing::Quote, Lint, LintKind, Punctuation, TokenKind};
+
+pub fn unclosed_quotes(document: &Document) -> Vec<Lint> {
+ let mut lints = Vec::new();
+
+ // TODO: Try zipping quote positions
+ for token in document.tokens() {
+ if let TokenKind::Punctuation(Punctuation::Quote(Quote { twin_loc: None })) = token.kind {
+ lints.push(Lint {
+ span: token.span,
+ lint_kind: LintKind::UnmatchedQuote,
+ suggestions: vec![],
+ message: "This quote has no termination.".to_string(),
+ })
+ }
+ }
+
+ lints
+}
diff --git a/lt-core/src/parsing/lexer.rs b/lt-core/src/parsing/lexer.rs
index 4fc4f1db7..67a555f97 100644
--- a/lt-core/src/parsing/lexer.rs
+++ b/lt-core/src/parsing/lexer.rs
@@ -1,6 +1,9 @@
use crate::span::Span;
-use super::token::{Punctuation, Token, TokenKind};
+use super::{
+ token::{Punctuation, Token, TokenKind},
+ Quote,
+};
#[derive(Debug)]
pub struct FoundToken {
@@ -152,6 +155,10 @@ fn lex_characters(source: &[char], cs: &str, token: TokenKind) -> Option<FoundTo
macro_rules! lex_punctuation {
($($text:literal => $res:ident),*) => {
fn lex_punctuation(source: &[char]) -> Option<FoundToken> {
+ if let Some(found) = lex_quote(source){
+ return Some(found);
+ }
+
$(
if let Some(found) = lex_characters(source, $text, TokenKind::Punctuation(Punctuation::$res)){
return Some(found);
@@ -169,7 +176,6 @@ lex_punctuation! {
"?" => Question,
":" => Colon,
";" => Semicolon,
- "\"" => Quote,
"," => Comma,
"-" => Hyphen,
"[" => OpenSquare,
@@ -179,6 +185,19 @@ lex_punctuation! {
"#" => Hash
}
+fn lex_quote(source: &[char]) -> Option<FoundToken> {
+ let c = *source.first()?;
+
+ if c == '\"' || c == '“' || c == '”' {
+ Some(FoundToken {
+ next_index: 1,
+ token: TokenKind::Punctuation(Punctuation::Quote(Quote { twin_loc: None })),
+ })
+ } else {
+ None
+ }
+}
+
#[cfg(test)]
mod tests {
use crate::{
diff --git a/lt-core/src/parsing/mod.rs b/lt-core/src/parsing/mod.rs
index 4e91bcc1c..71416f1b7 100644
--- a/lt-core/src/parsing/mod.rs
+++ b/lt-core/src/parsing/mod.rs
@@ -1,6 +1,5 @@
mod lexer;
-
mod token;
pub use lexer::{lex_to_end, lex_to_end_str};
-pub use token::{FatToken, Punctuation, Token, TokenKind, TokenStringExt};
+pub use token::{FatToken, Punctuation, Quote, Token, TokenKind, TokenStringExt};
diff --git a/lt-core/src/parsing/token.rs b/lt-core/src/parsing/token.rs
index 22e3c98db..d97ff0c08 100644
--- a/lt-core/src/parsing/token.rs
+++ b/lt-core/src/parsing/token.rs
@@ -40,6 +40,16 @@ pub enum TokenKind {
Newline(usize),
}
+impl TokenKind {
+ pub fn as_mut_quote(&mut self) -> Option<&mut Quote> {
+ self.as_mut_punctuation()?.as_mut_quote()
+ }
+
+ pub fn as_quote(&self) -> Option<&Quote> {
+ self.as_punctuation()?.as_quote()
+ }
+}
+
#[derive(Debug, Is, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(tag = "kind")]
pub enum Punctuation {
@@ -54,7 +64,7 @@ pub enum Punctuation {
/// ;
Semicolon,
/// "
- Quote,
+ Quote(Quote),
/// ,
Comma,
/// -
@@ -71,6 +81,12 @@ pub enum Punctuation {
Hash,
}
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+pub struct Quote {
+ /// The location of the matching quote, if it exists.
+ pub twin_loc: Option<usize>,
+}
+
pub trait TokenStringExt {
fn first_word(&self) -> Option<Token>;
}
diff --git a/web/src/app.css b/web/src/app.css
index 28b8f8a6b..f41cd0105 100644
--- a/web/src/app.css
+++ b/web/src/app.css
@@ -12,3 +12,21 @@
src: url('/fonts/outfit.ttf');
}
+.underlinespecial {
+ position: relative;
+}
+
+.underlinespecial:after {
+ transition-property: all;
+ transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
+ transition-duration: 150ms;
+ content: '';
+ display: block;
+ width: 100%;
+ height: var(--line-width);
+ border-radius: 1000px;
+ background: var(--bg-color);
+ position: absolute;
+ bottom: -3px;
+ left: 0;
+}
diff --git a/web/src/lib/Highlights.svelte b/web/src/lib/Highlights.svelte
deleted file mode 100644
index 3741a127b..000000000
--- a/web/src/lib/Highlights.svelte
+++ /dev/null
@@ -1,40 +0,0 @@
-<script lang="ts">
- import type { Lint, Token, TokenKind } from '$lib/analysis';
- import { parseText, contentToString, lintText } from '$lib/analysis';
-
- export let content: string;
-
- let tokens: Token[] = [];
-
- $: parseText(content).then((parsedTokens) => (tokens = parsedTokens));
-
- /** Convert a given TokenKind to class */
- function kindToClass(tokenKind: TokenKind): string {
- switch (tokenKind.kind) {
- case 'Word':
- return 'bg-red-100';
- case 'Punctuation':
- return 'bg-blue-100';
- case 'Number':
- return 'bg-green-100';
- case 'Space':
- return '';
- case 'Newline':
- return '';
- }
- }
-</script>
-
-<div class="grid">
- <div class="p-0 m-0 indent-0" style="grid-row: 1; grid-column: 1">
- {#each tokens as token}
- {#if token.kind.kind === 'Newline'}
- {#each { length: token.kind.value } as _}
- <br />
- {/each}
- {:else}
- <p class={`inline ${kindToClass(token.kind)}`}>{contentToString(token.content)}</p>
- {/if}
- {/each}
- </div>
-</div>
diff --git a/web/src/lib/Underlines.svelte b/web/src/lib/Underlines.svelte
index ac2faaf5e..2099f036b 100644
--- a/web/src/lib/Underlines.svelte
+++ b/web/src/lib/Underlines.svelte
@@ -1,6 +1,6 @@
<script lang="ts">
- import type { Lint, Token, TokenKind } from '$lib/analysis';
- import { contentToString, lintText, spanContent } from '$lib/analysis';
+ import type { Lint } from '$lib/analysis';
+ import { lintText, spanContent } from '$lib/analysis';
export let content: string;
export let focusLintIndex: number | undefined;
@@ -53,7 +53,7 @@
let lintContent = [
spanContent(lint.span, content).replaceAll(' ', '\u00A0'),
'red',
- lintIndex === focusLintIndex ? '3px' : '1px'
+ lintIndex === focusLintIndex ? '4px' : '2px'
];
return [...prevContent, lintContent];
@@ -80,7 +80,7 @@
</script>
<div class="grid">
- <div class="p-0 m-0 indent-0" style="grid-row: 1; grid-column: 1; color: transparent;">
+ <div class="p-0 m-0 indent-0 text-transparent" style="grid-row: 1; grid-column: 1">
{#each modified as chunk}
{#if chunk == null}
<br />
@@ -88,7 +88,10 @@
<span class="">{chunk}</span>
{:else}
<span style={`margin-right: -4px;`}>
- <span class="transition-all" style={`border-bottom: ${chunk[2]} solid ${chunk[1]};`}>
+ <span
+ class="underlinespecial"
+ style={`--bg-color: ${chunk[1]}; --line-width: ${chunk[2]};`}
+ >
{chunk[0]}
</span>
</span>
diff --git a/web/src/lib/analysis.ts b/web/src/lib/analysis.ts
index ef3f870f8..325e22712 100644
--- a/web/src/lib/analysis.ts
+++ b/web/src/lib/analysis.ts
@@ -56,6 +56,7 @@ export interface Lint {
span: Span;
lint_kind: 'Capitalization' | 'Spelling';
suggestions: Suggestion[];
+ message: string;
}
export interface Suggestion {
diff --git a/web/src/routes/+page.svelte b/web/src/routes/+page.svelte
index 9bdb1a1fd..eb74d967c 100644
--- a/web/src/routes/+page.svelte
+++ b/web/src/routes/+page.svelte
@@ -1,7 +1,6 @@
<script lang="ts">
import { Card } from 'flowbite-svelte';
import alice from '../../../alice.txt?raw';
- import Highlights from '$lib/Highlights.svelte';
import Underlines from '$lib/Underlines.svelte';
import { Button } from 'flowbite-svelte';
import { lintText, applySuggestion, spanContent } from '$lib/analysis';
@@ -36,14 +35,11 @@
$: console.log(focused);
</script>
-<div class="flex flex-row w-full h-screen">
- <div class="flex-auto h-full p-5 grid z-10 text-lg">
+<div class="flex flex-row w-full h-screen [&>*]:m-5">
+ <Card class="flex-auto max-w-full h-full p-5 grid z-10 text-lg">
<div class="overflow-auto p-0" style="grid-row: 1; grid-column: 1">
<Underlines {content} focusLintIndex={focused} />
</div>
- <div class="overflow-auto p-0" style="grid-row: 1; grid-column: 1">
- <Highlights {content} />
- </div>
<textarea
class="w-full h-full m-0 rounded-none p-0 z-0 bg-transparent border-none text-lg"
spellcheck="false"
@@ -51,24 +47,30 @@
bind:value={content}
bind:this={editor}
></textarea>
- </div>
- <div class="flex flex-col flex-grow">
+ </Card>
+ <Card class="flex flex-col flex-grow">
{#each lints as lint, i}
- <Card
- class="m-1 hover:translate-x-3 transition-all"
- on:mouseenter={() => (focused = i)}
- on:mouseleave={() => (focused = undefined)}
- >
- <div style={`border-left: 3px solid ${categoryToColor('mild')}`}>
+ <Card class="m-1 hover:translate-x-3 transition-all" on:click={() => (focused = i)}>
+ <div class="pl-2" style={`border-left: 3px solid ${categoryToColor('mild')}`}>
<div class="flex flex-row">
- <div class="rounded-full w-2 aspect-square"></div>
- <h3 class="font-bold">{`${lint.lint_kind} - "${spanContent(lint.span, content)}"`}</h3>
+ <h3 class="font-bold">
+ {lint.lint_kind} - “<span class="italic">
+ {spanContent(lint.span, content)}
+ </span> ”
+ </h3>
</div>
- <div>
+ <div
+ class="transition-all overflow-hidden"
+ style={`height: ${
+ focused === i ? `calc(55px * ${lint.suggestions.length + 1})` : '0px'
+ }`}
+ >
+ <p style="height: 50px">{lint.message}</p>
{#each lint.suggestions as suggestion}
<Button
color="alternative"
- class="w-full h-full m-1 "
+ class="w-full mb-1"
+ style="height: 40px; margin: 5px 0px;"
on:click={() =>
applySuggestion(content, suggestion, lint.span).then(
(edited) => (content = edited)
@@ -83,5 +85,5 @@
</div>
</Card>
{/each}
- </div>
+ </Card>
</div>