Skip to content

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

feat: improved style

Elijah Potter9c7032017854
diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dict
index 55f7e9dda..63b4e8464 100644
--- a/harper-core/dictionary.dict
+++ b/harper-core/dictionary.dict
@@ -49609,3 +49609,4 @@ LiDAR
LADAR
TODO
struct
+WASM
diff --git a/harper-core/src/linting/matcher.rs b/harper-core/src/linting/matcher.rs
index 2c0cc460f..9745fa381 100644
--- a/harper-core/src/linting/matcher.rs
+++ b/harper-core/src/linting/matcher.rs
@@ -4,7 +4,7 @@ use crate::{Document, Lint, LintKind, Linter, Punctuation, Span, Suggestion, Tok
#[derive(Debug, PartialEq, PartialOrd, Clone)]
struct PatternToken {
kind: TokenKind,
- content: Option<DictWord>
+ content: Option<DictWord>,
}
impl PatternToken {
@@ -12,12 +12,12 @@ impl PatternToken {
if token.kind.is_word() {
Self {
kind: token.kind,
- content: Some(document.get_span_content(token.span).into())
+ content: Some(document.get_span_content(token.span).into()),
}
} else {
Self {
kind: token.kind,
- content: None
+ content: None,
}
}
}
@@ -85,13 +85,13 @@ macro_rules! pt {
struct Rule {
pattern: Vec<PatternToken>,
- replace_with: Vec<char>
+ replace_with: Vec<char>,
}
/// A linter that uses a variety of curated pattern matches to find and fix
/// common grammatical issues.
pub struct Matcher {
- triggers: Vec<Rule>
+ triggers: Vec<Rule>,
}
impl Matcher {
@@ -99,6 +99,8 @@ impl Matcher {
// This match list needs to be automatically expanded instead of explicitly
// defined like it is now.
let mut triggers = pt! {
+ "dep" => "dependency",
+ "deps" => "dependencies",
"off","the","cuff" => "off-the-cuff",
"an","in" => "and in",
"repo" => "repository",
@@ -197,24 +199,24 @@ impl Matcher {
// We need to be more explicit that we are replacing with an Em dash
triggers.push(Rule {
pattern: vec![pt!(Hyphen), pt!(Hyphen), pt!(Hyphen)],
- replace_with: vecword!("—")
+ replace_with: vecword!("—"),
});
// Same goes for this En dash
triggers.push(Rule {
pattern: vec![pt!(Hyphen), pt!(Hyphen)],
- replace_with: vecword!("–")
+ replace_with: vecword!("–"),
});
// And this ellipsis
triggers.push(Rule {
pattern: vec![pt!(Period), pt!(Period), pt!(Period)],
- replace_with: vecword!("…")
+ replace_with: vecword!("…"),
});
triggers.push(Rule {
pattern: vec![pt!("L"), pt!(Period), pt!("L"), pt!(Period), pt!("M")],
- replace_with: vecword!("large language model")
+ replace_with: vecword!("large language model"),
});
triggers.push(Rule {
@@ -226,7 +228,7 @@ impl Matcher {
pt!("M"),
pt!(Period),
],
- replace_with: vecword!("large language model")
+ replace_with: vecword!("large language model"),
});
Self { triggers }
@@ -266,7 +268,7 @@ impl Linter for Matcher {
if match_tokens.len() == trigger.pattern.len() && !match_tokens.is_empty() {
let span = Span::new(
match_tokens.first().unwrap().span.start,
- match_tokens.last().unwrap().span.end
+ match_tokens.last().unwrap().span.end,
);
lints.push(Lint {
@@ -277,7 +279,7 @@ impl Linter for Matcher {
"Did you mean “{}”?",
trigger.replace_with.iter().collect::<String>()
),
- priority: 15
+ priority: 15,
})
}
}
diff --git a/harper-ls/src/tree_sitter_parser.rs b/harper-ls/src/tree_sitter_parser.rs
index 8051b6689..45aaaff0a 100644
--- a/harper-ls/src/tree_sitter_parser.rs
+++ b/harper-ls/src/tree_sitter_parser.rs
@@ -10,7 +10,7 @@ use super::comment_parsers::{Go, Unit};
/// exclusively parses comments in any language supported by [`tree_sitter`].
pub struct TreeSitterParser {
language: Language,
- comment_parser: Box<dyn Parser>
+ comment_parser: Box<dyn Parser>,
}
impl TreeSitterParser {
@@ -21,6 +21,8 @@ impl TreeSitterParser {
"ts" => tree_sitter_typescript::language_typescript(),
"py" => tree_sitter_python::language(),
"js" => tree_sitter_javascript::language(),
+ "mjs" => tree_sitter_javascript::language(),
+ "cjs" => tree_sitter_javascript::language(),
"go" => tree_sitter_go::language(),
"c" => tree_sitter_c::language(),
"cpp" => tree_sitter_cpp::language(),
@@ -31,17 +33,17 @@ impl TreeSitterParser {
"cs" => tree_sitter_c_sharp::language(),
"toml" => tree_sitter_toml::language(),
"lua" => tree_sitter_lua::language(),
- _ => return None
+ _ => return None,
};
let comment_parser: Box<dyn Parser> = match file_extension {
"go" => Box::new(Go),
- _ => Box::new(Unit)
+ _ => Box::new(Unit),
};
Some(Self {
language,
- comment_parser
+ comment_parser,
})
}
diff --git a/harper-wasm/src/lib.rs b/harper-wasm/src/lib.rs
index 766650e3d..4041fdfc1 100644
--- a/harper-wasm/src/lib.rs
+++ b/harper-wasm/src/lib.rs
@@ -9,7 +9,7 @@ use wasm_bindgen::JsValue;
static LINTER: Lazy<Mutex<LintGroup<FullDictionary>>> = Lazy::new(|| {
Mutex::new(LintGroup::new(
Default::default(),
- FullDictionary::create_from_curated()
+ FullDictionary::create_from_curated(),
))
});
@@ -29,6 +29,12 @@ pub fn setup() {
tracing_wasm::set_as_global_default();
}
+#[wasm_bindgen]
+pub fn use_spell_check(set: bool) {
+ let mut linter = LINTER.lock().unwrap();
+ linter.config.spell_check = Some(set);
+}
+
#[wasm_bindgen]
pub fn lint(text: String) -> Vec<JsValue> {
let document = Document::new_markdown(&text);
@@ -57,7 +63,7 @@ pub fn parse(text: String) -> Vec<JsValue> {
pub fn apply_suggestion(
text: String,
span: JsValue,
- suggestion: JsValue
+ suggestion: JsValue,
) -> Result<String, String> {
let span = serde_wasm_bindgen::from_value(span).map_err(|e| e.to_string())?;
let suggestion = serde_wasm_bindgen::from_value(suggestion).map_err(|e| e.to_string())?;
diff --git a/web/package.json b/web/package.json
index 72ee6899e..24996fe72 100644
--- a/web/package.json
+++ b/web/package.json
@@ -1,47 +1,47 @@
{
- "name": "harper-web",
- "version": "0.0.1",
- "private": true,
- "scripts": {
- "dev": "vite dev",
- "build": "vite build",
- "preview": "vite preview",
- "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
- "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
- "lint": "prettier --check . && eslint .",
- "format": "prettier --write ."
- },
- "devDependencies": {
- "@flydotio/dockerfile": "^0.5.0",
- "@sveltejs/adapter-node": "^3.0.3",
- "@sveltejs/kit": "^1.20.4",
- "@typescript-eslint/eslint-plugin": "^6.0.0",
- "@typescript-eslint/parser": "^6.0.0",
- "autoprefixer": "^10.4.16",
- "eslint": "^8.28.0",
- "eslint-config-prettier": "^9.0.0",
- "eslint-plugin-prettier": "^5.0.1",
- "eslint-plugin-svelte": "^2.34.0",
- "flowbite": "^1.8.1",
- "flowbite-svelte": "^0.44.18",
- "postcss": "^8.4.31",
- "prettier": "^3.0.3",
- "prettier-plugin-svelte": "^3.0.3",
- "svelte": "^4.0.5",
- "svelte-check": "^3.4.3",
- "tailwindcss": "^3.3.3",
- "tslib": "^2.4.1",
- "typescript": "^5.0.0",
- "vite": "^4.4.2",
- "vite-plugin-top-level-await": "^1.4.1",
- "vite-plugin-wasm": "^3.3.0",
- "wasm": "link:../harper-wasm/pkg"
- },
- "type": "module",
- "dependencies": {
- "@codemirror/lint": "^6.5.0",
- "@codemirror/state": "^6.4.1",
- "codemirror": "^6.0.1",
- "lodash-es": "^4.17.21"
- }
+ "name": "harper-web",
+ "version": "0.0.1",
+ "private": true,
+ "scripts": {
+ "dev": "vite dev",
+ "build": "vite build",
+ "preview": "vite preview",
+ "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
+ "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
+ "lint": "prettier --check . && eslint .",
+ "format": "prettier --write ."
+ },
+ "devDependencies": {
+ "@flydotio/dockerfile": "^0.5.0",
+ "@sveltejs/adapter-node": "^3.0.3",
+ "@sveltejs/kit": "^1.20.4",
+ "@typescript-eslint/eslint-plugin": "^6.0.0",
+ "@typescript-eslint/parser": "^6.0.0",
+ "autoprefixer": "^10.4.16",
+ "eslint": "^8.28.0",
+ "eslint-config-prettier": "^9.0.0",
+ "eslint-plugin-prettier": "^5.0.1",
+ "eslint-plugin-svelte": "^2.34.0",
+ "flowbite": "^1.8.1",
+ "flowbite-svelte": "^0.44.18",
+ "postcss": "^8.4.31",
+ "prettier": "^3.0.3",
+ "prettier-plugin-svelte": "^3.0.3",
+ "svelte": "^4.0.5",
+ "svelte-check": "^3.4.3",
+ "tailwindcss": "^3.3.3",
+ "tslib": "^2.4.1",
+ "typescript": "^5.0.0",
+ "vite": "^4.4.2",
+ "vite-plugin-top-level-await": "^1.4.1",
+ "vite-plugin-wasm": "^3.3.0"
+ },
+ "type": "module",
+ "dependencies": {
+ "wasm": "link:../harper-wasm/pkg",
+ "@codemirror/lint": "^6.5.0",
+ "@codemirror/state": "^6.4.1",
+ "codemirror": "^6.0.1",
+ "lodash-es": "^4.17.21"
+ }
}
diff --git a/web/src/app.css b/web/src/app.css
index b05030561..a95825fe5 100644
--- a/web/src/app.css
+++ b/web/src/app.css
@@ -12,30 +12,6 @@
src: url('/fonts/outfit.ttf');
}
-.underlinespecial {
- position: relative;
- background-color: var(--bg-color);
-}
-
-.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(--line-color);
- position: absolute;
- bottom: -3px;
- left: 0;
-}
-
-textarea {
- --tw-ring-shadow: 0 0 #000 !important;
-}
-
.animate-bigbounce {
animation: bigbounce 1s infinite;
}
@@ -55,3 +31,31 @@ textarea {
animation-timing-function: cubic-bezier(0, 0, 0.2, 1);
}
}
+
+.cm-editor {
+}
+
+.cm-diagnostic-error {
+}
+
+.cm-tooltip {
+ border-radius: 10px;
+ border: 1px solid black;
+ margin-top: 5px;
+ background: white !important;
+}
+
+.cm-diagnostic {
+ border-left: none !important;
+ display: flex !important;
+ flex-direction: column;
+ padding: 10px !important;
+ align-items: start;
+}
+
+.cm-diagnostic-error > * {
+ margin: 4px 0px !important;
+}
+
+.cm-diagnosticAction {
+}
diff --git a/web/src/lib/NewEditor.svelte b/web/src/lib/NewEditor.svelte
index e9ad90e90..970e35881 100644
--- a/web/src/lib/NewEditor.svelte
+++ b/web/src/lib/NewEditor.svelte
@@ -3,6 +3,8 @@
import { minimalSetup, EditorView } from 'codemirror';
import { contentToString, lintText, type Suggestion } from './analysis';
import { linter } from '@codemirror/lint';
+ import demoText from '../../../demo.md?raw';
+ import { Card } from 'flowbite-svelte';
let editorDiv: HTMLDivElement;
@@ -16,7 +18,7 @@
const harperLinter = linter(
async (view) => {
- let text = view.state.doc.sliceString(0);
+ let text = view.state.doc.sliceString(-1);
let lints = await lintText(text);
@@ -24,7 +26,7 @@
return {
from: lint.span.start,
to: lint.span.end,
- severity: 'warning',
+ severity: 'error',
message: lint.message,
actions: lint.suggestions.map((sug, i) => {
return {
@@ -49,16 +51,18 @@
};
});
},
- { delay: 0 }
+ { delay: -1 }
);
onMount(() => {
new EditorView({
- doc: 'You cannot add new lines in this editor\n\n\n\n',
+ doc: demoText,
extensions: [minimalSetup, harperLinter],
parent: editorDiv
});
});
</script>
-<div bind:this={editorDiv} />
+<Card class="w-full max-w-full">
+ <div bind:this={editorDiv} class="text-lg" />
+</Card>
diff --git a/web/src/routes/+page.svelte b/web/src/routes/+page.svelte
index 32374bf61..5c08db37f 100644
--- a/web/src/routes/+page.svelte
+++ b/web/src/routes/+page.svelte
@@ -1,5 +1,4 @@
<script>
- import Editor from '$lib/Editor.svelte';
import GutterCenter from '$lib/GutterCenter.svelte';
import Logo from '$lib/Logo.svelte';
import NewEditor from '$lib/NewEditor.svelte';
@@ -48,7 +47,13 @@
</div>
<div class="w-full h-[600px] overflow-hidden">
- <NewEditor />
+ {#if width > 570}
+ <NewEditor />
+ {:else}
+ <p class="italic text-lg w-full text-center dark:text-white">
+ Open this page on a bigger screen to start checking your work.
+ </p>
+ {/if}
</div>
</GutterCenter>