Rust拼写检查库typos的使用,高效识别和修复代码中的拼写错误
Rust拼写检查库typos的使用,高效识别和修复代码中的拼写错误
typos简介
typos是一个源代码拼写检查工具,能够快速发现并纠正源代码中的拼写错误:
- 速度足够快,可以在大型代码库上运行
- 低误报率,适合在PR提交时运行
安装方式
有多种安装typos的方法:
使用Cargo安装:
$ cargo install typos-cli
使用Homebrew安装:
$ brew install typos-cli
使用Conda安装:
$ conda install typos
使用Pacman安装:
$ sudo pacman -S typos
基本使用
查看代码中的拼写错误:
$ typos
自动修复拼写错误:
$ typos --write-changes
# 或简写
$ typos -w
如果存在多个可能的修正方案,typos会报告给用户并继续处理。
处理误报
有时看起来像拼写错误的内容是有意为之的,比如人名、缩写或本地化内容。
可以通过_typos.toml
配置文件标记单词或标识符为有效:
[default]
extend-ignore-identifiers-re = [
# 不值得花时间修复
"AttributeID.*Supress.*",
]
[default.extend-identifiers]
# 不值得花时间修复
AttributeIDSupressMenu = "AttributeIDSupressMenu"
[default.extend-words]
# 不纠正姓氏"Teh"
teh = "teh"
对于本地化内容,可以禁用文件内容检查但仍检查文件名:
[type.po]
extend-glob = ["*.po"]
check-file = false
完全排除某些文件:
[files]
extend-exclude = ["localized/*.po"]
自定义集成
typos提供了几种自定义集成的构建块:
# 从stdin读取,将修正版本写入stdout
$ typos - --write-changes
# 创建将要更改的diff
$ typos dir/file --diff
# 完全编程控制
$ typos dir/file --format json
调试
查看有效配置:
$ typos --dump-config -
查看typos如何处理项目:
$ typos --files
$ typos --identifiers
$ typos --words
启用调试日志:
$ typos -v
完整示例
以下是一个完整的typos使用示例:
- 首先创建示例Rust项目:
$ cargo new typos-demo
$ cd typos-demo
- 添加一些故意拼写错误的代码到
src/main.rs
:
fn main() {
let misspelled_var = "This is a testt"; // 拼写错误:testt
println!("{}", misspelled_var);
if misspelled_var == "testt" { // 拼写错误:testt
println!("Found typoo"); // 拼写错误:typoo
}
}
- 安装并运行typos:
$ cargo install typos-cli
$ typos
- 输出将显示拼写错误建议:
src/main.rs:2:23: "testt" is a typo. Did you mean "test"?
src/main.rs:5:28: "typoo" is a typo. Did you mean "typo"?
- 自动修复错误:
$ typos --write-changes
- 修正后的
src/main.rs
:
fn main() {
let misspelled_var = "This is a test"; // 已修正为test
println!("{}", misspelled_var);
if misspelled_var == "test" { // 已修正为test
println!("Found typo"); // 已修正为typo
}
}
- 如果有需要忽略的单词,创建
_typos.toml
:
[default.extend-words]
# 忽略特定术语
testt = "testt" # 保持原样不修正
typoo = "typoo" # 保持原样不修正
通过这种方式,typos可以帮助你保持代码中的拼写正确性,提高代码质量。
1 回复
Rust拼写检查库typos的使用:高效识别和修复代码中的拼写错误
介绍
typos是一个用Rust编写的拼写检查工具,专门用于识别和修复源代码中的拼写错误。它支持多种编程语言,能够智能地跳过代码中的变量名、函数名等技术术语,专注于检查注释、字符串字面量等内容的拼写。
主要特性
- 高性能:基于Rust实现,检查速度快
- 低误报率:智能区分代码标识符和可读文本
- 多语言支持:支持多种编程语言的源代码
- 可配置:允许自定义词典和忽略规则
- 多种输出格式:支持多种输出格式便于集成
安装方法
使用cargo安装
cargo install typos-cli
作为项目依赖
在Cargo.toml中添加:
[dependencies]
typos = "1.0"
基本使用方法
检查当前目录
typos
检查指定目录
typos path/to/your/code
自动修复错误
typos --write-changes
示例输出
Found 3 typos in 2 files
src/main.rs:
12:6-12:14 warning `definately` is a misspelling of `definitely`
45:23-45:30 warning `recieve` is a misspelling of `receive`
docs/README.md:
8:14-8:18 warning `thier` is a misspelling of `their`
高级配置
自定义配置文件
在项目根目录创建typos.toml
文件:
[default.extend-words]
# 添加项目特定词汇
"mycrate" = "mycrate"
"serde" = "serde"
[default.extend-identifiers]
# 允许特定的标识符拼写
"colour" = "colour"
[files]
# 忽略特定文件
ignore = ["target/**", "*.lock"]
集成到CI/CD
可以在项目的CI流程中添加typos检查,例如GitHub Actions:
name: Spell Check
on: [push, pull_request]
jobs:
typos:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: baptiste0928/cargo-install@v1
with:
crate: typos-cli
- run: typos
Rust代码中使用示例
use typos::check_file;
fn main() {
let result = check_file("src/main.rs", &Default::default());
match result {
Ok(errors) => {
if errors.is_empty() {
println!("No spelling errors found!");
} else {
for error in errors {
println!("Found typo at {}:{} - {}",
error.line, error.column, error.suggestion);
}
}
}
Err(e) => eprintln!("Error checking file: {}", e),
}
}
完整示例demo
下面是一个完整的项目示例,展示如何在Rust项目中使用typos:
- 首先创建项目并添加typos依赖:
cargo new typos-demo
cd typos-demo
- 在Cargo.toml中添加依赖:
[dependencies]
typos = "1.0"
- 创建src/main.rs文件:
use typos::check_file;
use std::path::Path;
fn main() {
// 检查当前目录下的所有文件
let dir = Path::new(".");
let config = typos::Config::default();
// 递归检查目录中的所有文件
let results = typos::check_dir(dir, &config);
match results {
Ok(report) => {
if report.is_empty() {
println!("恭喜!没有发现拼写错误");
} else {
println!("发现 {} 处拼写错误:", report.len());
// 输出详细的拼写错误信息
for (file_path, errors) in report.iter() {
println!("\n文件: {}", file_path.display());
for error in errors {
println!("行 {} 列 {}: {} -> {}",
error.line,
error.column,
error.typo,
error.correction);
}
}
}
}
Err(e) => eprintln!("检查错误: {}", e),
}
}
- 创建typos.toml配置文件:
[default.extend-words]
# 添加项目特定词汇
"typosdemo" = "typosdemo"
"rustlang" = "rustlang"
[default.extend-identifiers]
# 允许特定的标识符拼写
"colour" = "colour"
[files]
# 忽略特定文件
ignore = ["target/**", "*.lock"]
- 运行示例:
cargo run
实用技巧
- 对于大型项目,可以使用
--no-check-filenames
和--no-check-files
选项提高速度 - 使用
--diff
选项只检查git变更的文件 - 结合
jq
工具可以处理JSON输出格式的结果 - 在VSCode等编辑器中可以配置typos作为拼写检查器
typos是提高代码质量和专业度的实用工具,特别适合开源项目和技术文档维护。