Make the code formatter take its own medicine
This commit is contained in:
parent
94a4e6032d
commit
fca87793cf
1 changed files with 18 additions and 12 deletions
|
|
@ -17,8 +17,7 @@ fn format_file(path: &Path) -> Result<bool, String> {
|
|||
|
||||
if trimmed == "{"
|
||||
&& !line.trim_start().starts_with("//")
|
||||
&& !line.trim_start().starts_with("/*")
|
||||
{
|
||||
&& !line.trim_start().starts_with("/*") {
|
||||
if let Some(last) = out.last() {
|
||||
let merged = format!("{} {{", last.trim_end());
|
||||
out.pop();
|
||||
|
|
@ -30,8 +29,7 @@ fn format_file(path: &Path) -> Result<bool, String> {
|
|||
}
|
||||
|
||||
if (trimmed.starts_with("} else ") || trimmed == "} else" || trimmed.starts_with("} else if"))
|
||||
&& !line.trim_start().starts_with("//")
|
||||
{
|
||||
&& !line.trim_start().starts_with("//") {
|
||||
let idx = line.rfind('}').unwrap();
|
||||
let before = &line[..idx + 1];
|
||||
let after = &line[idx + 1..];
|
||||
|
|
@ -44,16 +42,20 @@ fn format_file(path: &Path) -> Result<bool, String> {
|
|||
if rest.ends_with('{') {
|
||||
let header = rest.trim_end_matches('{').trim_end();
|
||||
format!("{}{} {{", indent, header)
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
format!("{}{}", indent, rest.trim())
|
||||
}
|
||||
} else if after.trim() == "else {" || after.trim() == "else" {
|
||||
}
|
||||
else if after.trim() == "else {" || after.trim() == "else" {
|
||||
if after.trim().ends_with('{') {
|
||||
format!("{}else {{", indent)
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
format!("{}else", indent)
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
format!("{}{}", indent, after.trim())
|
||||
};
|
||||
out.push(next_line);
|
||||
|
|
@ -69,7 +71,8 @@ fn format_file(path: &Path) -> Result<bool, String> {
|
|||
let result = out.join("\n");
|
||||
let final_content = if content.ends_with('\n') {
|
||||
format!("{}\n", result)
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
result
|
||||
};
|
||||
|
||||
|
|
@ -88,7 +91,8 @@ fn find_rs_files(dir: &Path) -> Vec<std::path::PathBuf> {
|
|||
let path = entry.path();
|
||||
if path.is_dir() {
|
||||
files.extend(find_rs_files(&path));
|
||||
} else if path.extension().map_or(false, |e| e == "rs") {
|
||||
}
|
||||
else if path.extension().map_or(false, |e| e == "rs") {
|
||||
if let Some(name) = path.file_name() {
|
||||
if name != "fmt-tssr.rs" {
|
||||
files.push(path);
|
||||
|
|
@ -107,11 +111,13 @@ fn collect_targets(args: &[String]) -> Vec<std::path::PathBuf> {
|
|||
let p = std::path::PathBuf::from(arg);
|
||||
if p.is_file() {
|
||||
targets.push(p);
|
||||
} else if p.is_dir() {
|
||||
}
|
||||
else if p.is_dir() {
|
||||
targets.extend(find_rs_files(&p));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
let src_dir = std::path::PathBuf::from("src");
|
||||
if src_dir.exists() {
|
||||
targets.extend(find_rs_files(&src_dir));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue