Skip to content

Commit

Permalink
feat(parser): Give control over identifier detection
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Aug 8, 2019
1 parent 76c6303 commit 1b5ea86
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ pub trait FileSource {
fn ignore_hex(&self) -> Option<bool> {
None
}

/// Allow identifiers to include digits, in addition to letters
fn identifier_include_digits(&self) -> Option<bool> {
None
}

/// Specify additional characters to be included in identifiers
fn identifier_include_chars(&self) -> Option<&str> {
None
}
}

#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
Expand Down Expand Up @@ -227,6 +237,8 @@ pub struct FileConfig {
pub check_filename: Option<bool>,
pub check_file: Option<bool>,
pub ignore_hex: Option<bool>,
pub identifier_include_digits: Option<bool>,
pub identifier_include_chars: Option<String>,
}

impl FileConfig {
Expand All @@ -240,6 +252,12 @@ impl FileConfig {
if let Some(source) = source.ignore_hex() {
self.ignore_hex = Some(source);
}
if let Some(source) = source.identifier_include_digits() {
self.identifier_include_digits = Some(source);
}
if let Some(source) = source.identifier_include_chars() {
self.identifier_include_chars = Some(source.to_owned());
}
}

pub fn check_filename(&self) -> bool {
Expand All @@ -253,6 +271,17 @@ impl FileConfig {
pub fn ignore_hex(&self) -> bool {
self.ignore_hex.unwrap_or(true)
}

pub fn identifier_include_digits(&self) -> bool {
self.identifier_include_digits.unwrap_or(true)
}

pub fn identifier_include_chars(&self) -> &str {
self.identifier_include_chars
.as_ref()
.map(|s| s.as_str())
.unwrap_or("_'")
}
}

impl FileSource for FileConfig {
Expand All @@ -267,6 +296,14 @@ impl FileSource for FileConfig {
fn ignore_hex(&self) -> Option<bool> {
self.ignore_hex
}

fn identifier_include_digits(&self) -> Option<bool> {
self.identifier_include_digits
}

fn identifier_include_chars(&self) -> Option<&str> {
self.identifier_include_chars.as_ref().map(|s| s.as_str())
}
}

fn find_project_file(dir: std::path::PathBuf, name: &str) -> Option<std::path::PathBuf> {
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ fn run() -> Result<i32, failure::Error> {

let parser = typos::tokens::ParserBuilder::new()
.ignore_hex(config.default.ignore_hex())
.include_digits(config.default.identifier_include_digits())
.include_chars(config.default.identifier_include_chars().to_owned())
.build();

let checks = typos::checks::CheckSettings::new()
Expand Down

0 comments on commit 1b5ea86

Please sign in to comment.