-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add for support Java, Kotlin, Objective-C, Swift, PHP, CSharp, Dart
- Loading branch information
Showing
27 changed files
with
756 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Oops, something went wrong.