Skip to content

Commit

Permalink
feat: swapped out LintSet for LintGroup and made linters configur…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
elijah-potter committed Mar 8, 2024
1 parent 5c0f23f commit 8ff3182
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 158 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 3 additions & 12 deletions harper-core/benches/parse_demo.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use divan::{black_box, AllocProfiler, Bencher};
use harper_core::{Document, FullDictionary, LintSet, Linter};
use harper_core::{Document, FullDictionary, LintGroup, LintGroupConfig, Linter};

#[global_allocator]
static ALLOC: AllocProfiler = AllocProfiler::system();
Expand All @@ -13,19 +13,10 @@ fn parse_demo(bencher: Bencher) {
});
}

#[divan::bench]
fn create_lint_set(bencher: Bencher) {
let dictionary = FullDictionary::create_from_curated();

bencher.bench_local(|| {
let _lint_set = LintSet::new().with_standard(dictionary.clone());
});
}

#[divan::bench]
fn lint_demo(bencher: Bencher) {
let dictionary = FullDictionary::create_from_curated();
let mut lint_set = LintSet::new().with_standard(dictionary);
let mut lint_set = LintGroup::new(Default::default(), dictionary);
let document = Document::new_markdown(black_box(DEMO));

bencher.bench_local(|| {
Expand All @@ -37,7 +28,7 @@ fn lint_demo(bencher: Bencher) {
fn lint_demo_uncached(bencher: Bencher) {
let dictionary = FullDictionary::create_from_curated();
bencher.bench_local(|| {
let mut lint_set = LintSet::new().with_standard(dictionary.clone());
let mut lint_set = LintGroup::new(LintGroupConfig::default(), dictionary.clone());
let document = Document::new_markdown(black_box(DEMO));

lint_set.lint(&document);
Expand Down
4 changes: 0 additions & 4 deletions harper-core/src/lexing/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ fn lex_ip_schemepart(source: &[char]) -> Option<usize> {

// Parse endpoint path
while cursor != rest.len() {
dbg!(&rest[cursor..]);

if rest[cursor] != '/' {
break;
}
Expand Down Expand Up @@ -84,8 +82,6 @@ fn lex_login(source: &[char]) -> Option<usize> {
fn lex_hostport(source: &[char]) -> Option<usize> {
let hostname_end = lex_hostname(source)?;

dbg!(&source[..hostname_end]);

if source.get(hostname_end) == Some(&':') {
Some(
source
Expand Down
2 changes: 1 addition & 1 deletion harper-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod spell;
mod token;

pub use document::Document;
pub use linting::{Lint, LintKind, LintSet, Linter, Suggestion};
pub use linting::{Lint, LintGroup, LintGroupConfig, LintKind, Linter, Suggestion};
pub use span::Span;
pub use spell::{Dictionary, FullDictionary, MergedDictionary};
pub use token::{FatToken, Punctuation, Token, TokenKind, TokenStringExt};
Expand Down
107 changes: 107 additions & 0 deletions harper-core/src/linting/lint_group.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
use paste::paste;
use serde::{Deserialize, Serialize};

use super::an_a::AnA;
use super::long_sentences::LongSentences;
use super::matcher::Matcher;
use super::repeated_words::RepeatedWords;
use super::sentence_capitalization::SentenceCapitalization;
use super::spaces::Spaces;
use super::spell_check::SpellCheck;
use super::spelled_numbers::SpelledNumbers;
use super::unclosed_quotes::UnclosedQuotes;
use super::wrong_quotes::WrongQuotes;
use super::{Lint, Linter};
use crate::{Dictionary, Document};

macro_rules! create_lint_group_config {
($($linter:ident => $default:expr),*) => {
paste! {
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
pub struct LintGroupConfig {
$(
#[doc = "Configures the use of the `" $linter "` linter.
If set to [`None`], the default configuration will be used."]
pub [<$linter:snake>]: Option<bool>,
)*
pub spell_check: Option<bool>
}

impl LintGroupConfig {
/// Fills the [`None`] values in the configuration with the default values.
pub fn fill_default_values(&mut self){
$(
if self.[<$linter:snake>].is_none() {
self.[<$linter:snake>] = Some($default);
}
)*

if self.spell_check.is_none() {
self.spell_check = Some(true);
}
}
}

pub struct LintGroup<T: Dictionary> {
$(
[<$linter:snake>]: $linter,
)*
spell_check: SpellCheck<T>,
pub config: LintGroupConfig
}


impl<T: Dictionary> LintGroup<T> {
pub fn new(config: LintGroupConfig, dictionary: T) -> Self {
Self {
$(
[<$linter:snake>]: $linter::default(),
)*
spell_check: SpellCheck::new(dictionary),
config,
}
}
}

impl<T: Dictionary> Linter for LintGroup<T> {
fn lint(&mut self, document: &Document) -> Vec<Lint>{
let mut lints = Vec::new();

let mut config = self.config.clone();
config.fill_default_values();

$(
if config.[<$linter:snake>].unwrap() {
lints.append(&mut self.[<$linter:snake>].lint(document));
}
)*

if config.spell_check.unwrap() {
lints.append(&mut self.spell_check.lint(document));
}


lints
}
}
}
};
}

create_lint_group_config!(
SpelledNumbers => false,
AnA => true,
SentenceCapitalization => true,
UnclosedQuotes => true,
WrongQuotes => true,
LongSentences => true,
RepeatedWords => true,
Spaces => true,
Matcher => true
);

impl<T: Dictionary + Default> Default for LintGroup<T> {
fn default() -> Self {
Self::new(LintGroupConfig::default(), T::default())
}
}
113 changes: 0 additions & 113 deletions harper-core/src/linting/lint_set.rs

This file was deleted.

4 changes: 2 additions & 2 deletions harper-core/src/linting/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod an_a;
mod lint;
mod lint_set;
mod lint_group;
mod long_sentences;
mod matcher;
mod repeated_words;
Expand All @@ -12,7 +12,7 @@ mod unclosed_quotes;
mod wrong_quotes;

pub use lint::{Lint, LintKind, Suggestion};
pub use lint_set::LintSet;
pub use lint_group::{LintGroup, LintGroupConfig};

use crate::Document;

Expand Down
1 change: 1 addition & 0 deletions harper-ls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ tracing = "0.1.40"
tracing-subscriber = "0.3.18"
resolve-path = "0.1.0"
open = "5.1.1"
futures = "0.3.30"
26 changes: 25 additions & 1 deletion harper-ls/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Refer to the linked documentation for more information.
If you happen to use [`mason.nvim`](https://github.com/williamboman/mason.nvim), installation will be pretty straightforward.
`harper-ls` is in the official Mason registry, so you can install it the same way you install anything through Mason.

If you __don't__ install your LSPs through Mason, we also have binary releases are available on [GitHub](https://github.com/elijah-potter/harper/releases).
If you __don't__ install your LSPs through Mason, we also have binary releases available on [GitHub](https://github.com/elijah-potter/harper/releases).

Finally, if you have [Rust installed](https://www.rust-lang.org/tools/install), you're in luck!
To install `harper-ls`, simply run:
Expand Down Expand Up @@ -54,6 +54,30 @@ lspconfig.harper_ls.setup {
}
```

You can also toggle any particular linter.
The default values are shown below:

```lua
lspconfig.harper_ls.setup {
settings = {
["harper-ls"] = {
linters = {
spell_check = true,
spelled_numbers = false,
an_a = true,
sentence_capitalization = true,
unclosed_quotes = true,
wrong_quotes = true,
long_sentences = true,
repeated_words = true,
spaces = true,
matcher = true
}
}
},
}
```

### File-Local Dictionary

Sometimes, you'll encounter a word (or name) that is only valid within the context of a specific file.
Expand Down
Loading

0 comments on commit 8ff3182

Please sign in to comment.