Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: created `CharString` and added config for `diagnosticSeverity`
Elijah Potter6e7cbe184481
diff --git a/Cargo.lock b/Cargo.lockindex f9fd90782..8c5d764d1 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -562,6 +562,7 @@ dependencies = ["once_cell","open","resolve-path",+ "serde","serde_json","tokio","tower-lsp",diff --git a/README.md b/README.mdindex faedc4239..7e9ade36c 100644--- a/README.md+++ b/README.md@@ -31,4 +31,3 @@ Harper is even small enough to load via [WebAssembly.](https://writewithharper.cIf you want to use Harper on your machine, you will want to look at the [documentation for`harper-ls`](./harper-ls/README.md), the Language Server Protocol implementation.-diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dictindex 49966e473..7f66ee172 100644--- a/harper-core/dictionary.dict+++ b/harper-core/dictionary.dict@@ -49622,3 +49622,4 @@ URI/ssuprachiasmaticcannabinoidseudaimonia+whitespacediff --git a/harper-core/src/char_string.rs b/harper-core/src/char_string.rsnew file mode 100644index 000000000..26394f629--- /dev/null+++ b/harper-core/src/char_string.rs@@ -0,0 +1,17 @@+use smallvec::SmallVec;++pub type CharString = SmallVec<[char; 6]>;++pub trait CharStringExt {+ fn to_lower(&self) -> CharString;+}++impl CharStringExt for [char] {+ fn to_lower(&self) -> CharString {+ let mut out = CharString::with_capacity(self.len());++ out.extend(self.iter().flat_map(|v| v.to_lowercase()));++ out+ }+}diff --git a/harper-core/src/lib.rs b/harper-core/src/lib.rsindex 23152d67b..94fc14c07 100644--- a/harper-core/src/lib.rs+++ b/harper-core/src/lib.rs@@ -1,5 +1,6 @@#![allow(dead_code)]+mod char_string;mod document;mod lexing;mod linting;@@ -8,6 +9,7 @@ mod span;mod spell;mod token;+pub use char_string::{CharString, CharStringExt};pub use document::Document;pub use linting::{Lint, LintGroup, LintGroupConfig, LintKind, Linter, Suggestion};pub use span::Span;diff --git a/harper-core/src/linting/an_a.rs b/harper-core/src/linting/an_a.rsindex afb9ccf87..089fad481 100644--- a/harper-core/src/linting/an_a.rs+++ b/harper-core/src/linting/an_a.rs@@ -1,6 +1,6 @@use itertools::Itertools;-use crate::{Document, Lint, LintKind, Linter, Suggestion, TokenStringExt};+use crate::{CharStringExt, Document, Lint, LintKind, Linter, Suggestion, TokenStringExt};#[derive(Debug, Default)]pub struct AnA;@@ -23,7 +23,7 @@ impl Linter for AnA {continue;};- let should_be_a_an = !starts_with_vowel(chars_second);+ let should_be_a_an = !starts_with_vowel(&chars_second.to_lower());if a_an != should_be_a_an {let replacement = match a_an {diff --git a/harper-core/src/linting/matcher.rs b/harper-core/src/linting/matcher.rsindex 03760f48f..8367c431b 100644--- a/harper-core/src/linting/matcher.rs+++ b/harper-core/src/linting/matcher.rs@@ -1,10 +1,20 @@-use crate::spell::DictWord;-use crate::{Document, Lint, LintKind, Linter, Punctuation, Span, Suggestion, Token, TokenKind};+use crate::{+ CharString,+ Document,+ Lint,+ LintKind,+ Linter,+ Punctuation,+ Span,+ Suggestion,+ Token,+ TokenKind+};#[derive(Debug, PartialEq, PartialOrd, Clone)]struct PatternToken {kind: TokenKind,- content: Option<DictWord>+ content: Option<CharString>}impl PatternToken {diff --git a/harper-core/src/linting/repeated_words.rs b/harper-core/src/linting/repeated_words.rsindex c95c80ae4..2ef59733c 100644--- a/harper-core/src/linting/repeated_words.rs+++ b/harper-core/src/linting/repeated_words.rs@@ -1,14 +1,13 @@use hashbrown::HashSet;use super::{Lint, LintKind, Linter};-use crate::spell::DictWord;use crate::token::{Token, TokenKind, TokenStringExt};-use crate::{Document, Span, Suggestion};+use crate::{CharString, Document, Span, Suggestion};#[derive(Debug, Clone)]pub struct RepeatedWords {/// The set of words that can be considered for repetition checking.- set: HashSet<DictWord>+ set: HashSet<CharString>}impl RepeatedWords {diff --git a/harper-core/src/spell/full_dictionary.rs b/harper-core/src/spell/full_dictionary.rsindex 51008242e..434b8cdde 100644--- a/harper-core/src/spell/full_dictionary.rs+++ b/harper-core/src/spell/full_dictionary.rs@@ -4,7 +4,8 @@ use smallvec::{SmallVec, ToSmallVec};use super::dictionary::Dictionary;use super::hunspell::{parse_default_attribute_list, parse_default_word_list};-use super::{seq_to_normalized, DictWord};+use super::seq_to_normalized;+use crate::CharString;/// A full, fat dictionary./// All of the elements are stored in-memory.@@ -16,13 +17,13 @@ pub struct FullDictionary {/// This is likely due to increased locality :shrug:.////// This list is sorted by word length (i.e. the shortest words are first).- words: Vec<DictWord>,+ words: Vec<CharString>,/// A lookup list for each word length./// Each index of this list will return the first index of [`Self::words`]/// that has a word whose index is that length.word_len_starts: Vec<usize>,/// All English words- word_set: HashSet<DictWord>+ word_set: HashSet<CharString>}fn uncached_inner_new() -> FullDictionary {@@ -81,7 +82,7 @@ impl FullDictionary {/// list. NOTE: This function will sort the original word list by its/// length. If the word list's order is changed after creating the/// lookup, it will no longer be valid.- fn create_len_starts(words: &mut [DictWord]) -> Vec<usize> {+ fn create_len_starts(words: &mut [CharString]) -> Vec<usize> {words.sort_by_key(|a| a.len());let mut word_len_starts = vec![0, 0];diff --git a/harper-core/src/spell/hunspell/attributes.rs b/harper-core/src/spell/hunspell/attributes.rsindex 1bc4bf21a..be101a5fd 100644--- a/harper-core/src/spell/hunspell/attributes.rs+++ b/harper-core/src/spell/hunspell/attributes.rs@@ -6,8 +6,7 @@ use smallvec::ToSmallVec;use super::matcher::Matcher;use super::word_list::MarkedWord;use super::Error;-use crate::spell::DictWord;-use crate::Span;+use crate::{CharString, Span};#[derive(Debug, Clone)]struct AffixReplacement {@@ -111,7 +110,7 @@ impl AttributeList {/// Will append to the given `dest`;////// In the future, I want to make this function cleaner and faster.- pub fn expand_marked_word(&self, word: MarkedWord, dest: &mut Vec<DictWord>) {+ pub fn expand_marked_word(&self, word: MarkedWord, dest: &mut Vec<CharString>) {dest.reserve(word.attributes.len() + 1);let start_len = dest.len();@@ -173,7 +172,7 @@ impl AttributeList {pub fn expand_marked_words(&self,words: impl IntoIterator<Item = MarkedWord>,- dest: &mut Vec<DictWord>+ dest: &mut Vec<CharString>) {for word in words {self.expand_marked_word(word, dest);@@ -184,7 +183,7 @@ impl AttributeList {replacement: &AffixReplacement,letters: &[char],suffix: bool- ) -> Option<DictWord> {+ ) -> Option<CharString> {if replacement.condition.len() > letters.len() {return None;}@@ -199,7 +198,7 @@ impl AttributeList {if replacement.condition.matches(target_segment) {let mut replaced_segment = letters.to_smallvec();- let mut remove: DictWord = replacement.remove.to_smallvec();+ let mut remove: CharString = replacement.remove.to_smallvec();if !suffix {replaced_segment.reverse();diff --git a/harper-core/src/spell/hunspell/mod.rs b/harper-core/src/spell/hunspell/mod.rsindex 27e954a8a..fd8c5bbbb 100644--- a/harper-core/src/spell/hunspell/mod.rs+++ b/harper-core/src/spell/hunspell/mod.rs@@ -22,7 +22,7 @@ mod tests {use super::attributes::AttributeList;use super::word_list::parse_word_list;use super::{parse_default_attribute_list, parse_default_word_list};- use crate::spell::DictWord;+ use crate::CharString;pub const TEST_WORD_LIST: &str = "3\nhello\ntry/B\nwork/AB";pub const ATTR_LIST: &str =@@ -65,7 +65,7 @@ mod tests {assert!(expanded.contains(&split("giants")))}- fn build_expanded() -> Vec<DictWord> {+ fn build_expanded() -> Vec<CharString> {let words = parse_default_word_list().unwrap();let attributes = parse_default_attribute_list().unwrap();@@ -91,7 +91,7 @@ mod tests {assert!(build_expanded().contains(&split("deallocate")));}- fn split(text: &str) -> DictWord {+ fn split(text: &str) -> CharString {text.chars().collect()}}diff --git a/harper-core/src/spell/hunspell/word_list.rs b/harper-core/src/spell/hunspell/word_list.rsindex b83204a07..21b915b53 100644--- a/harper-core/src/spell/hunspell/word_list.rs+++ b/harper-core/src/spell/hunspell/word_list.rs@@ -1,9 +1,9 @@use super::Error;-use crate::spell::DictWord;+use crate::CharString;#[derive(Debug, Clone)]pub struct MarkedWord {- pub letters: DictWord,+ pub letters: CharString,pub attributes: Vec<char>}diff --git a/harper-core/src/spell/mod.rs b/harper-core/src/spell/mod.rsindex 6b09c56bd..af51ee5ac 100644--- a/harper-core/src/spell/mod.rs+++ b/harper-core/src/spell/mod.rs@@ -1,7 +1,6 @@use std::borrow::Cow;use itertools::{Itertools, MinMaxResult};-use smallvec::SmallVec;pub use self::dictionary::Dictionary;pub use self::full_dictionary::FullDictionary;@@ -12,9 +11,6 @@ mod full_dictionary;mod hunspell;mod merged_dictionary;-/// A word from a dictionary or other similar structure.-pub type DictWord = SmallVec<[char; 6]>;-/// Suggest a correct spelling for a given misspelled word./// [`misspelled_word`] is assumed to be quite small (n < 100)./// [`max_edit_dist`] relates to an optimization that allows the searchdiff --git a/harper-ls/Cargo.toml b/harper-ls/Cargo.tomlindex 0eb146a8d..218d77254 100644--- a/harper-ls/Cargo.toml+++ b/harper-ls/Cargo.toml@@ -36,3 +36,4 @@ resolve-path = "0.1.0"open = "5.1.1"futures = "0.3.30"unicode-width = "0.1.11"+serde = { version = "1.0.197", features = ["derive"] }diff --git a/harper-ls/README.md b/harper-ls/README.mdindex c3b1ce471..df870071a 100644--- a/harper-ls/README.md+++ b/harper-ls/README.md@@ -78,6 +78,20 @@ lspconfig.harper_ls.setup {}```+By default, `harper-ls` will mark all diagnostics with HINT.+If you want to configure this, refer below:++```lua+lspconfig.harper_ls.setup {+ settings = {+ ["harper-ls"] = {+ diagnosticSeverity = "hint" -- Can also be "information", "warning", or "error"+ }+ },+}+```++### File-Local DictionarySometimes, you'll encounter a word (or name) that is only valid within the context of a specific file.diff --git a/harper-ls/src/backend.rs b/harper-ls/src/backend.rsindex d15a1115e..6cc93420d 100644--- a/harper-ls/src/backend.rs+++ b/harper-ls/src/backend.rs@@ -318,8 +318,13 @@ impl Backend {};let lints = doc_state.linter.lint(&doc_state.document);+ let config = self.config.read().await;- lints_to_diagnostics(doc_state.document.get_full_content(), &lints)+ lints_to_diagnostics(+ doc_state.document.get_full_content(),+ &lints,+ config.diagnostic_severity+ )}#[instrument(skip(self))]@@ -519,6 +524,8 @@ impl LanguageServer for Backend {}};+ dbg!(new_config.diagnostic_severity);+{let mut config = self.config.write().await;*config = new_config;@@ -530,6 +537,7 @@ impl LanguageServer for Backend {doc_state.keys().cloned().collect()};+ // Update documents with new configfutures::future::join_all(keys.iter().map(|key| self.update_document_from_file(key, None))diff --git a/harper-ls/src/config.rs b/harper-ls/src/config.rsindex 19d01ea81..8454e552b 100644--- a/harper-ls/src/config.rs+++ b/harper-ls/src/config.rs@@ -3,13 +3,38 @@ use std::path::PathBuf;use dirs::{config_dir, data_local_dir};use harper_core::LintGroupConfig;use resolve_path::PathResolveExt;+use serde::{Deserialize, Serialize};use serde_json::Value;+#[derive(Debug, Serialize, Deserialize, Clone, Copy)]+#[serde(rename_all = "camelCase")]+pub enum DiagnosticSeverity {+ Error,+ Warning,+ Information,+ Hint+}++impl DiagnosticSeverity {+ /// Converts `self` to the equivalent LSP type.+ pub fn to_lsp(self) -> tower_lsp::lsp_types::DiagnosticSeverity {+ match self {+ DiagnosticSeverity::Error => tower_lsp::lsp_types::DiagnosticSeverity::ERROR,+ DiagnosticSeverity::Warning => tower_lsp::lsp_types::DiagnosticSeverity::WARNING,+ DiagnosticSeverity::Information => {+ tower_lsp::lsp_types::DiagnosticSeverity::INFORMATION+ }+ DiagnosticSeverity::Hint => tower_lsp::lsp_types::DiagnosticSeverity::HINT+ }+ }+}+#[derive(Debug, Clone)]pub struct Config {pub user_dict_path: PathBuf,pub file_dict_path: PathBuf,- pub lint_config: LintGroupConfig+ pub lint_config: LintGroupConfig,+ pub diagnostic_severity: DiagnosticSeverity}impl Config {@@ -38,6 +63,10 @@ impl Config {base.lint_config = serde_json::from_value(v.clone())?;}+ if let Some(v) = value.get("diagnosticSeverity") {+ base.diagnostic_severity = serde_json::from_value(v.clone())?;+ }+Ok(base)}}@@ -49,7 +78,8 @@ impl Default for Config {file_dict_path: data_local_dir().unwrap().join("harper-ls/file_dictionaries/"),- lint_config: LintGroupConfig::default()+ lint_config: LintGroupConfig::default(),+ diagnostic_severity: DiagnosticSeverity::Hint}}}diff --git a/harper-ls/src/diagnostics.rs b/harper-ls/src/diagnostics.rsindex 60ec154dd..190d0b0e9 100644--- a/harper-ls/src/diagnostics.rs+++ b/harper-ls/src/diagnostics.rs@@ -12,12 +12,17 @@ use tower_lsp::lsp_types::{WorkspaceEdit};+use crate::config::DiagnosticSeverity;use crate::pos_conv::span_to_range;-pub fn lints_to_diagnostics(source: &[char], lints: &[Lint]) -> Vec<Diagnostic> {+pub fn lints_to_diagnostics(+ source: &[char],+ lints: &[Lint],+ severity: DiagnosticSeverity+) -> Vec<Diagnostic> {lints.iter()- .map(|lint| lint_to_diagnostic(lint, source))+ .map(|lint| lint_to_diagnostic(lint, source, severity)).collect()}@@ -82,12 +87,12 @@ pub fn lint_to_code_actions<'a>(results}-fn lint_to_diagnostic(lint: &Lint, source: &[char]) -> Diagnostic {+fn lint_to_diagnostic(lint: &Lint, source: &[char], severity: DiagnosticSeverity) -> Diagnostic {let range = span_to_range(source, lint.span);Diagnostic {range,- severity: None,+ severity: Some(severity.to_lsp()),code: None,code_description: None,source: Some("Harper".to_string()),