[Perf] find min indention of the line and remove the ssame indention

from subsequent lines
pull/90/head
Mist 2024-04-25 18:00:56 +08:00 committed by The Mist
parent 13d7118c06
commit 66206496f9
1 changed files with 21 additions and 15 deletions

View File

@ -36,25 +36,31 @@ fn replace_tab_to_space(text: &str) -> String {
str::replace(text, "\t", &spaces)
}
// If the first line have indention, remove the same indention from subsequent lines
// Find min indention of the line, and remove the same indention from subsequent lines
fn trim_space(text: &str) -> String {
let lines = text.split("\n").collect::<Vec<&str>>();
let first_line = lines.first().unwrap();
let head_spaces = Regex::new(r"^(\s*)").unwrap().find(first_line);
let regex = Regex::new(r"(?:^|\n)(\s*)").unwrap();
let captures_iter = regex.captures_iter(text);
let space_lengths = captures_iter
.map(|capture| capture.get(1).unwrap().as_str().len())
.collect::<Vec<usize>>();
match head_spaces {
Some(head_spaces) => lines
if space_lengths.len() < lines.len() {
return text.to_string();
}
let need_to_remove_spaces = " ".repeat(space_lengths.into_iter().min().unwrap());
lines
.into_iter()
.map(|line| {
Regex::new(format!("^{}", head_spaces.as_str()).as_ref())
Regex::new(format!("^{}", need_to_remove_spaces).as_ref())
.unwrap()
.replace(line, "")
.to_string()
})
.collect::<Vec<String>>()
.join("\n"),
None => text.to_string(),
}
.join("\n")
}
pub fn prepare_code(code: &str) -> String {