Skip to content

Commit

Permalink
Add for support Java, Kotlin, Objective-C, Swift, PHP, CSharp, Dart
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Jul 1, 2021
1 parent f27c1e4 commit 2a7a64a
Show file tree
Hide file tree
Showing 27 changed files with 756 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT"
name = "autocorrect"
readme = "README.md"
repository = "https://github.com/huacnlee/autocorrect"
version = "0.6.0"
version = "0.7.0"

[lib]
name = "autocorrect"
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ release\:arm:
cargo +stable build --release --features bin --target aarch64-apple-darwin
cd target/aarch64-apple-darwin/release; tar czvf autocorrect-darwin-arm64.tar.gz autocorrect; mv autocorrect-darwin-arm64.tar.gz ~/Downloads/
ls -lh ~/Downloads/autocorrect*.tar.gz
release\:local:
build:
cargo +stable build --release --features bin --target aarch64-apple-darwin
sudo ln -f target/aarch64-apple-darwin/release/autocorrect /usr/local/bin/autocorrect
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ https://marketplace.visualstudio.com/items?itemName=huacnlee.auto-correct
## Features

- Auto add spacings between CJK (Chinese, Japanese, Korean) and English words.
- Multiple file content support (HTML, YAML, Rust, Go, SQL, Ruby, JavaScript ...).
- Multiple file content support (HTML, YAML, Rust, Go, SQL, Ruby, Python, Objective-C, Swift, Java, Kotlin, Dart, JavaScript, CSharp ...).
- Fullwidth -> halfwidth (only for [a-zA-Z0-9], and `` in time).
- Correct punctuations into Fullwidth near the CJK.

Expand Down
81 changes: 81 additions & 0 deletions src/csharp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// autocorrect: false
use super::*;
use pest::iterators::Pair;
use pest::Parser as P;
use pest_derive::Parser;

#[derive(Parser)]
#[grammar = "peg/csharp.pest"]
struct CSharpParser;

pub fn format_csharp(text: &str) -> String {
let result = CSharpParser::parse(Rule::item, text);
match result {
Ok(items) => {
let mut out = String::new();
for item in items {
format_csharp_pair(&mut out, item);
}
return out;
}
Err(_err) => {
return String::from(text);
}
}
}

fn format_csharp_pair(text: &mut String, item: Pair<Rule>) {
match item.as_rule() {
Rule::string | Rule::comment => text.push_str(format(item.as_str()).as_str()),
Rule::item => {
for sub in item.into_inner() {
format_csharp_pair(text, sub);
}
}
_ => text.push_str(item.as_str()),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_format_csharp() {
let example = r###"
/**
* 第1行注释
* 第2行注释
*/
public String helloWorld(stirng name) {
// 第3行注释
string singleLineString = "第1个字符串string";
string stringLiteral = $"这是stringLiteral {name}!";
string quotation = @"
这是多行string第1行
这是多行string第2行
";
}
"###;

let expect = r###"
/**
* 第 1 行注释
* 第 2 行注释
*/
public String helloWorld(stirng name) {
// 第 3 行注释
string singleLineString = "第 1 个字符串 string";
string stringLiteral = $"这是 stringLiteral {name}!";
string quotation = @"
这是多行 string 第 1 行
这是多行 string 第 2 行
";
}
"###;

assert_eq!(expect, format_csharp(example));
}
}
91 changes: 91 additions & 0 deletions src/dart.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// autocorrect: false
use super::*;
use pest::iterators::Pair;
use pest::Parser as P;
use pest_derive::Parser;

#[derive(Parser)]
#[grammar = "peg/dart.pest"]
struct DartParser;

pub fn format_dart(text: &str) -> String {
let result = DartParser::parse(Rule::item, text);
match result {
Ok(items) => {
let mut out = String::new();
for item in items {
format_dart_pair(&mut out, item);
}
return out;
}
Err(_err) => {
return String::from(text);
}
}
}

fn format_dart_pair(text: &mut String, item: Pair<Rule>) {
match item.as_rule() {
Rule::string | Rule::comment => text.push_str(format(item.as_str()).as_str()),
Rule::item => {
for sub in item.into_inner() {
format_dart_pair(text, sub);
}
}
_ => text.push_str(item.as_str()),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_format_dart() {
let example = r###"
/**
* 第1行注释
* 第2行注释
*/
String helloWorld(String name) {
// 第3行注释
var singleLineString = "第1个字符串string";
var singleLineString = '第2个字符串string';
var quotation = """
这是第3行字符串
这是第4行
""";
let quotation = '''
这是第5行字符串
这是第6行
''';
}
"###;

let expect = r###"
/**
* 第 1 行注释
* 第 2 行注释
*/
String helloWorld(String name) {
// 第 3 行注释
var singleLineString = "第 1 个字符串 string";
var singleLineString = '第 2 个字符串 string';
var quotation = """
这是第 3 行字符串
这是第 4 行
""";
let quotation = '''
这是第 5 行字符串
这是第 6 行
''';
}
"###;

assert_eq!(expect, format_dart(example));
}
}
83 changes: 83 additions & 0 deletions src/java.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// autocorrect: false
use super::*;
use pest::iterators::Pair;
use pest::Parser as P;
use pest_derive::Parser;

#[derive(Parser)]
#[grammar = "peg/java.pest"]
struct JavaParser;

pub fn format_java(text: &str) -> String {
let result = JavaParser::parse(Rule::item, text);
match result {
Ok(items) => {
let mut out = String::new();
for item in items {
format_java_pair(&mut out, item);
}
return out;
}
Err(_err) => {
return String::from(text);
}
}
}

fn format_java_pair(text: &mut String, item: Pair<Rule>) {
match item.as_rule() {
Rule::string | Rule::comment => text.push_str(format(item.as_str()).as_str()),
Rule::item => {
for sub in item.into_inner() {
format_java_pair(text, sub);
}
}
_ => text.push_str(item.as_str()),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_format_java() {
let example = r###"
// 第1行注释
// 第2行注释
public String helloWorld() {
// 第3行注释
String singleLineString = "第1个字符串string"
/**
* 第4行注释
* 第5行注释
*/
String quotation = """
这是多行string里面包含"双引号"
"Begin at the beginning," the King said gravely.
"""
}
"###;

let expect = r###"
// 第 1 行注释
// 第 2 行注释
public String helloWorld() {
// 第 3 行注释
String singleLineString = "第 1 个字符串 string"
/**
* 第 4 行注释
* 第 5 行注释
*/
String quotation = """
这是多行 string 里面包含"双引号"
"Begin at the beginning," the King said gravely.
"""
}
"###;

assert_eq!(expect, format_java(example));
}
}
79 changes: 79 additions & 0 deletions src/kotlin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// autocorrect: false
use super::*;
use pest::iterators::Pair;
use pest::Parser as P;
use pest_derive::Parser;

#[derive(Parser)]
#[grammar = "peg/kotlin.pest"]
struct KotlinParser;

pub fn format_kotlin(text: &str) -> String {
let result = KotlinParser::parse(Rule::item, text);
match result {
Ok(items) => {
let mut out = String::new();
for item in items {
format_kotlin_pair(&mut out, item);
}
return out;
}
Err(_err) => {
return String::from(text);
}
}
}

fn format_kotlin_pair(text: &mut String, item: Pair<Rule>) {
match item.as_rule() {
Rule::string | Rule::comment => text.push_str(format(item.as_str()).as_str()),
Rule::item => {
for sub in item.into_inner() {
format_kotlin_pair(text, sub);
}
}
_ => text.push_str(item.as_str()),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_format_kotlin() {
let example = r###"
/**
* 第1行注释
* 第2行注释
*/
fun helloWorld(name: String) {
// 第3行注释
var singleLineString = "第1个字符串string"
var quotation = """
这是多行string里面包含"双引号"
"Begin at the beginning," the King said gravely.
"""
}
"###;

let expect = r###"
/**
* 第 1 行注释
* 第 2 行注释
*/
fun helloWorld(name: String) {
// 第 3 行注释
var singleLineString = "第 1 个字符串 string"
var quotation = """
这是多行 string 里面包含"双引号"
"Begin at the beginning," the King said gravely.
"""
}
"###;

assert_eq!(expect, format_kotlin(example));
}
}
Loading

0 comments on commit 2a7a64a

Please sign in to comment.