Skip to content

Commit

Permalink
fix trimming and make indent heuristic simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
Kl4rry committed Jul 4, 2024
1 parent 01b3d5b commit 5680db1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 45 deletions.
46 changes: 5 additions & 41 deletions crates/ferrite-core/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
8 changes: 4 additions & 4 deletions crates/ferrite-utility/src/trim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

0 comments on commit 5680db1

Please sign in to comment.