Skip to content

Commit

Permalink
Will now lint incorrect quote characters
Browse files Browse the repository at this point in the history
  • Loading branch information
elijah-potter committed Jan 15, 2024
1 parent db0d1a9 commit e3d8001
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lt-core/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Document {
doc
}

fn iter_quote_indices(&self) -> impl Iterator<Item = usize> + '_ {
pub 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)
Expand All @@ -38,6 +38,10 @@ impl Document {
})
}

pub fn iter_quotes(&self) -> impl Iterator<Item = Token> + '_ {
self.iter_quote_indices().map(|idx| self.tokens[idx])
}

/// Searches for quotation marks and fills the [`Punctuation::Quote::twin_loc`] field.
/// This is on a best effort basis.
///
Expand Down
1 change: 1 addition & 0 deletions lt-core/src/linting/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub enum LintKind {
Spelling,
Capitalization,
UnmatchedQuote,
WrongQuotes,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
4 changes: 3 additions & 1 deletion lt-core/src/linting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod lint;
mod sentence_capitalization;
mod spell_check;
mod unclosed_quotes;
mod wrong_quotes;

pub use lint::{Lint, LintKind, Suggestion};

Expand All @@ -12,10 +13,11 @@ use self::lint::Linter;
pub fn all_linters(document: &Document) -> Vec<Lint> {
let mut lints = Vec::new();

let linters: [Linter; 3] = [
let linters: [Linter; 4] = [
spell_check::spell_check,
sentence_capitalization::sentence_capitalization_lint,
unclosed_quotes::unclosed_quotes,
wrong_quotes::wrong_quotes,
];

for linter in linters {
Expand Down
33 changes: 33 additions & 0 deletions lt-core/src/linting/wrong_quotes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::{
document::Document, parsing::Quote, Lint, LintKind, Punctuation, Suggestion, Token, TokenKind,
};

pub fn wrong_quotes(document: &Document) -> Vec<Lint> {
document
.iter_quote_indices()
.zip(document.iter_quotes())
.filter_map(|(quote_idx, quote_token)| lint_quote(document, quote_idx, quote_token))
.collect()
}

fn lint_quote(document: &Document, quote_idx: usize, quote_token: Token) -> Option<Lint> {
let quote = quote_token.kind.as_quote().unwrap();

let twin_loc = quote.twin_loc?;
let is_left = twin_loc > quote_idx;

let quote_char = *document.get_span_content(quote_token.span).first()?;

let should_be = if is_left { '“' } else { '”' };

if quote_char != should_be {
Some(Lint {
span: quote_token.span,
lint_kind: LintKind::WrongQuotes,
suggestions: vec![Suggestion::ReplaceWith(vec![should_be])],
message: "Use the better-formatted quote character.".to_string(),
})
} else {
None
}
}

0 comments on commit e3d8001

Please sign in to comment.