From 5680db1d07871e5c0166f49e8c0ad3ff68dfb362 Mon Sep 17 00:00:00 2001 From: Axel Kappel <69117984+Kl4rry@users.noreply.github.com> Date: Thu, 4 Jul 2024 19:18:02 +0200 Subject: [PATCH] fix trimming and make indent heuristic simpler --- crates/ferrite-core/src/buffer.rs | 46 ++++-------------------------- crates/ferrite-utility/src/trim.rs | 8 +++--- 2 files changed, 9 insertions(+), 45 deletions(-) diff --git a/crates/ferrite-core/src/buffer.rs b/crates/ferrite-core/src/buffer.rs index 4c4436a..45d2256 100644 --- a/crates/ferrite-core/src/buffer.rs +++ b/crates/ferrite-core/src/buffer.rs @@ -1674,51 +1674,15 @@ impl Buffer { } pub fn guess_indent(&self, byte_index: usize) -> String { - let mut indent = String::new(); - let line_idx = self.rope.byte_to_line(byte_index); - while line_idx > 0 { - let line = self.rope.line(line_idx); - /*line_idx -= 1; - if line.is_whitespace() { - continue; - }*/ - - let mut new_indent = String::new(); - for grapheme in line.grapehemes() { - if !grapheme.is_whitespace() { - break; - } - new_indent.extend(grapheme.chunks()); - } - - indent = new_indent; - break; - } + let line = self.rope.line(line_idx); - let line_idx = self.rope.byte_to_line(byte_index) + 1; - while line_idx > 0 { - let Some(line) = self.rope.get_line(line_idx) else { + let mut indent = String::new(); + for grapheme in line.grapehemes() { + if !grapheme.is_whitespace() { break; - }; - /*line_idx += 1; - if line.is_whitespace() { - continue; - }*/ - - let mut new_indent = String::new(); - for grapheme in line.grapehemes() { - if !grapheme.is_whitespace() { - break; - } - new_indent.extend(grapheme.chunks()); } - - if Rope::from_str(&new_indent).width(0) > Rope::from_str(&indent).width(0) { - indent = new_indent; - } - - break; + indent.extend(grapheme.chunks()); } self.indent.from_width(Rope::from_str(&indent).width(0)) diff --git a/crates/ferrite-utility/src/trim.rs b/crates/ferrite-utility/src/trim.rs index e340f12..10bee0e 100644 --- a/crates/ferrite-utility/src/trim.rs +++ b/crates/ferrite-utility/src/trim.rs @@ -2,12 +2,12 @@ use std::path::Path; pub fn trim_path(start: &str, path: &Path) -> String { let path_str = path.to_string_lossy(); - let without_start = path_str.trim_start_matches(start); - if without_start < start { - without_start + let trimmed = path_str.trim_start_matches(start); + if trimmed.len() < path_str.len() { + trimmed .trim_start_matches(std::path::MAIN_SEPARATOR) .to_string() } else { - without_start.to_string() + trimmed.to_string() } }