Skip to content

Commit

Permalink
fix: broken match-indent behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
sminez committed Feb 20, 2025
1 parent 3fef214 commit 631e9b4
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,18 +772,18 @@ impl Buffer {

match k {
Input::Return => {
let mut s = if match_indent {
let mut s = "\n".to_string();
if match_indent {
let cur = self.dot.first_cur();
let y = self.txt.char_to_line(cur.idx);
let line = self.txt.line(y).to_string();
line.find(|c: char| !c.is_whitespace())
.map(|ix| line.split_at(ix).0.to_string())
.unwrap_or_default()
} else {
"".to_string()
};

s.push('\n');
s.push_str(
&line
.find(|c: char| !c.is_whitespace())
.map(|ix| line.split_at(ix).0.to_string())
.unwrap_or_default(),
);
}

let c = self.insert_string(self.dot, s, Some(Source::Keyboard)).0;

Expand Down Expand Up @@ -1453,4 +1453,12 @@ pub(crate) mod tests {
assert_eq!(b.txt.to_string(), "a");
assert_eq!(b.dot, Dot::Cur { c: Cur { idx: 1 } });
}

#[test]
fn match_indent_works() {
let mut b = Buffer::new_virtual(0, "test", " foo");
b.set_dot(TextObject::BufferEnd, 1);
b.handle_raw_input(Input::Return);
assert_eq!(b.txt.to_string(), " foo\n ");
}
}

0 comments on commit 631e9b4

Please sign in to comment.