Rust代码拼写检查工具typos-cli的使用,typos-cli可高效检测和修复代码中的拼写错误
Rust代码拼写检查工具typos-cli的使用,typos-cli可高效检测和修复代码中的拼写错误
简介
typos是一个源代码拼写检查工具,可以高效地查找和纠正代码中的拼写错误:
- 速度足够快,可以在大型代码库上运行
- 误报率低,可以在PR中运行
安装
有多种安装方式:
使用Rust安装:
$ 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 = [
# *sigh* this just isn't worth the cost of fixing
"AttributeID.*Supress.*",
]
[default.extend-identifiers]
# *sigh* this just isn't worth the cost of fixing
AttributeIDSupressMenu = "AttributeIDSupressMenu"
[default.extend-words]
# Don't correct the surname "Teh"
teh = "teh"
对于本地化内容,可以禁用文件内容检查而只检查文件名:
[type.po]
extend-glob = ["*.po"]
check-file = false
完全排除某些文件:
[files]
extend-exclude = ["localized/*.po"]
集成
typos-cli支持多种集成方式:
- GitHub Actions
- pre-commit
- Visual Studio Code扩展
- 语言服务器协议(LSP)服务器
自定义集成
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-cli使用示例:
- 首先安装typos-cli:
cargo install typos-cli
- 创建一个Rust项目并故意加入一些拼写错误:
// src/main.rs
fn main() {
// 拼写错误的单词
let misspelled_word = "accomodate"; // 正确应为"accommodate"
let another_error = "recieve"; // 正确应为"receive"
println!("{} {}", misspelled_word, another_error);
// 故意拼写错误的标识符
let mut counterr = 0; // 多了一个r
counterr += 1;
}
- 运行typos检查:
typos
输出会显示检测到的拼写错误。
- 自动修复拼写错误:
typos --write-changes
- 如果要保留某些拼写(如专有名词),创建
_typos.toml
文件:
[default.extend-words]
# 保留特定拼写
accomodate = "accomodate"
- 将typos集成到CI流程中,例如在GitHub Actions中:
name: Spell Checking
on: [push, pull_request]
jobs:
typos:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: crate-ci/typos@v1
通过这种方式,你可以在代码提交或PR时自动检查拼写错误,保持代码库的专业性和一致性。
Rust代码拼写检查工具typos-cli使用指南
工具介绍
typos-cli是一个专为代码设计的拼写检查工具,特别适合Rust项目使用。它能高效检测和修复代码中的拼写错误,包括变量名、函数名、注释和文档中的拼写问题。
安装方法
使用Cargo安装typos-cli:
cargo install typos-cli
或者使用Homebrew(macOS):
brew install typos-cli
基本使用
检查当前目录下的代码
typos
检查特定文件或目录
typos src/main.rs
typos src/
自动修复拼写错误
typos --write-changes
高级功能
排除特定文件或目录
typos --exclude *.md --exclude tests/
仅检查特定文件类型
typos --type rust --type markdown
创建配置文件
typos --dump-config > typos.toml
示例
假设有以下Rust代码(src/main.rs
):
// This functoin calculates the sum of two numbers
fn add_two_numbers(a: i32, b: i32) -> i32 {
a + b
}
fn main() {
let resutl = add_two_numbers(5, 7);
println!("The resutl is: {}", resutl);
}
运行typos检查:
typos src/main.rs
输出结果会显示:
src/main.rs:1:12-1:20: "functoin" is a typo. Did you mean "function"?
src/main.rs:6:8-6:14: "resutl" is a typo. Did you mean "result"?
src/main.rs:7:28-7:34: "resutl" is a typo. Did you mean "result"?
使用--write-changes
自动修复:
typos --write-changes src/main.rs
修复后的代码:
// This function calculates the sum of two numbers
fn add_two_numbers(a: i32, b: i32) -> i32 {
a + b
}
fn main() {
let result = add_two_numbers(5, 7);
println!("The result is: {}", result);
}
完整示例demo
下面是一个完整的项目示例,展示如何在Rust项目中使用typos-cli:
- 首先创建一个新的Rust项目:
cargo new typos-demo
cd typos-demo
- 修改src/main.rs文件,故意加入一些拼写错误:
// This is a smaple Rust program with some typos
fn calculte_sum(a: i32, b: i32) -> i32 {
a + b
}
fn main() {
let numbr1 = 10;
let numbr2 = 20;
let tottal = calculte_sum(numbr1, numbr2);
println!("The tottal is: {}", tottal);
}
- 安装typos-cli并运行检查:
cargo install typos-cli
typos src/main.rs
输出结果:
src/main.rs:1:12-1:18: "smaple" is a typo. Did you mean "sample"?
src/main.rs:2:4-2:13: "calculte_sum" is a typo. Did you mean "calculate_sum"?
src/main.rs:7:8-7:14: "numbr1" is a typo. Did you mean "number1"?
src/main.rs:8:8-8:14: "numbr2" is a typo. Did you mean "number2"?
src/main.rs:9:8-9:14: "tottal" is a typo. Did you mean "total"?
src/main.rs:10:22-10:28: "tottal" is a typo. Did you mean "total"?
- 使用自动修复功能:
typos --write-changes src/main.rs
修复后的代码:
// This is a sample Rust program with some typos
fn calculate_sum(a: i32, b: i32) -> i32 {
a + b
}
fn main() {
let number1 = 10;
let number2 = 20;
let total = calculate_sum(number1, number2);
println!("The total is: {}", total);
}
集成到CI/CD
可以在项目的CI流程中添加typos检查,例如在GitHub Actions中:
name: Spell Check
on: [push, pull_request]
jobs:
typos:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
profile: minimal
override: true
- run: cargo install typos-cli
- run: typos
typos-cli是一个强大的工具,可以帮助开发者保持代码和文档的拼写正确,提高项目的专业性和可读性。