mirror of
https://github.com/crate-ci/typos.git
synced 2025-01-10 16:54:51 -05:00
Merge pull request #798 from epage/refactor
refactor(cli): Abstract away regex ignores
This commit is contained in:
commit
6d31a7ef84
1 changed files with 16 additions and 24 deletions
|
@ -47,14 +47,7 @@ impl FileChecker for Typos {
|
||||||
reporter.report(msg.into())?;
|
reporter.report(msg.into())?;
|
||||||
} else {
|
} else {
|
||||||
let mut accum_line_num = AccumulateLineNum::new();
|
let mut accum_line_num = AccumulateLineNum::new();
|
||||||
let mut ignores: Option<Ignores> = None;
|
for typo in check_bytes(&buffer, policy) {
|
||||||
for typo in typos::check_bytes(&buffer, policy.tokenizer, policy.dict) {
|
|
||||||
if ignores
|
|
||||||
.get_or_insert_with(|| Ignores::new(&buffer, policy.ignore))
|
|
||||||
.is_ignored(typo.span())
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let line_num = accum_line_num.line_num(&buffer, typo.byte_offset);
|
let line_num = accum_line_num.line_num(&buffer, typo.byte_offset);
|
||||||
let (line, line_offset) = extract_line(&buffer, typo.byte_offset);
|
let (line, line_offset) = extract_line(&buffer, typo.byte_offset);
|
||||||
let msg = report::Typo {
|
let msg = report::Typo {
|
||||||
|
@ -92,14 +85,7 @@ impl FileChecker for FixTypos {
|
||||||
} else {
|
} else {
|
||||||
let mut fixes = Vec::new();
|
let mut fixes = Vec::new();
|
||||||
let mut accum_line_num = AccumulateLineNum::new();
|
let mut accum_line_num = AccumulateLineNum::new();
|
||||||
let mut ignores: Option<Ignores> = None;
|
for typo in check_bytes(&buffer, policy) {
|
||||||
for typo in typos::check_bytes(&buffer, policy.tokenizer, policy.dict) {
|
|
||||||
if ignores
|
|
||||||
.get_or_insert_with(|| Ignores::new(&buffer, policy.ignore))
|
|
||||||
.is_ignored(typo.span())
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if is_fixable(&typo) {
|
if is_fixable(&typo) {
|
||||||
fixes.push(typo.into_owned());
|
fixes.push(typo.into_owned());
|
||||||
} else {
|
} else {
|
||||||
|
@ -176,14 +162,7 @@ impl FileChecker for DiffTypos {
|
||||||
} else {
|
} else {
|
||||||
let mut fixes = Vec::new();
|
let mut fixes = Vec::new();
|
||||||
let mut accum_line_num = AccumulateLineNum::new();
|
let mut accum_line_num = AccumulateLineNum::new();
|
||||||
let mut ignores: Option<Ignores> = None;
|
for typo in check_bytes(&buffer, policy) {
|
||||||
for typo in typos::check_bytes(&buffer, policy.tokenizer, policy.dict) {
|
|
||||||
if ignores
|
|
||||||
.get_or_insert_with(|| Ignores::new(&buffer, policy.ignore))
|
|
||||||
.is_ignored(typo.span())
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if is_fixable(&typo) {
|
if is_fixable(&typo) {
|
||||||
fixes.push(typo.into_owned());
|
fixes.push(typo.into_owned());
|
||||||
} else {
|
} else {
|
||||||
|
@ -553,6 +532,19 @@ fn write_file(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn check_bytes<'a>(
|
||||||
|
buffer: &'a [u8],
|
||||||
|
policy: &'a crate::policy::Policy<'a, 'a, 'a>,
|
||||||
|
) -> impl Iterator<Item = typos::Typo<'a>> {
|
||||||
|
let mut ignores: Option<Ignores> = None;
|
||||||
|
|
||||||
|
typos::check_bytes(buffer, policy.tokenizer, policy.dict).filter(move |typo| {
|
||||||
|
!ignores
|
||||||
|
.get_or_insert_with(|| Ignores::new(buffer, policy.ignore))
|
||||||
|
.is_ignored(typo.span())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn report_result<T: Default, E: ToString>(
|
fn report_result<T: Default, E: ToString>(
|
||||||
value: Result<T, E>,
|
value: Result<T, E>,
|
||||||
reporter: &dyn report::Report,
|
reporter: &dyn report::Report,
|
||||||
|
|
Loading…
Reference in a new issue