mirror of
https://github.com/crate-ci/typos.git
synced 2024-11-22 00:51:11 -05:00
feat(parser): Give control over identifier detection
This commit is contained in:
parent
709446821b
commit
6fc61966cc
2 changed files with 39 additions and 0 deletions
|
@ -62,6 +62,16 @@ pub trait FileSource {
|
||||||
fn ignore_hex(&self) -> Option<bool> {
|
fn ignore_hex(&self) -> Option<bool> {
|
||||||
None
|
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)]
|
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
|
||||||
|
@ -223,6 +233,8 @@ pub struct FileConfig {
|
||||||
pub check_filename: Option<bool>,
|
pub check_filename: Option<bool>,
|
||||||
pub check_file: Option<bool>,
|
pub check_file: Option<bool>,
|
||||||
pub ignore_hex: Option<bool>,
|
pub ignore_hex: Option<bool>,
|
||||||
|
pub identifier_include_digits: Option<bool>,
|
||||||
|
pub identifier_include_chars: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FileConfig {
|
impl FileConfig {
|
||||||
|
@ -236,6 +248,12 @@ impl FileConfig {
|
||||||
if let Some(source) = source.ignore_hex() {
|
if let Some(source) = source.ignore_hex() {
|
||||||
self.ignore_hex = Some(source);
|
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 {
|
pub fn check_filename(&self) -> bool {
|
||||||
|
@ -249,6 +267,17 @@ impl FileConfig {
|
||||||
pub fn ignore_hex(&self) -> bool {
|
pub fn ignore_hex(&self) -> bool {
|
||||||
self.ignore_hex.unwrap_or(true)
|
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 {
|
impl FileSource for FileConfig {
|
||||||
|
@ -263,6 +292,14 @@ impl FileSource for FileConfig {
|
||||||
fn ignore_hex(&self) -> Option<bool> {
|
fn ignore_hex(&self) -> Option<bool> {
|
||||||
self.ignore_hex
|
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> {
|
fn find_project_file(dir: std::path::PathBuf, name: &str) -> Option<std::path::PathBuf> {
|
||||||
|
|
|
@ -314,6 +314,8 @@ fn run() -> Result<i32, failure::Error> {
|
||||||
|
|
||||||
let parser = typos::tokens::ParserBuilder::new()
|
let parser = typos::tokens::ParserBuilder::new()
|
||||||
.ignore_hex(config.default.ignore_hex())
|
.ignore_hex(config.default.ignore_hex())
|
||||||
|
.include_digits(config.default.identifier_include_digits())
|
||||||
|
.include_chars(config.default.identifier_include_chars().to_owned())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let checks = typos::checks::CheckSettings::new()
|
let checks = typos::checks::CheckSettings::new()
|
||||||
|
|
Loading…
Reference in a new issue