Compare commits

...

2 commits

View file

@ -140,6 +140,15 @@ impl Text {
}
self.chars().get(line_start..line_start + i).unwrap_or(&[])
}
fn start_of_line_text(&self, line: isize) -> Result<usize, usize> {
let start = self.to_pos([0, line]) + self.indent_of_line(line).len();
if self.chars().get(start) == Some(&'\n') {
Err(start)
} else {
Ok(start)
}
}
}
#[derive(Default)]
@ -639,6 +648,10 @@ impl Buffer {
return;
};
let line_start = self.text.to_pos([0, self.text.to_coord(cursor.pos)[1]]);
let coord = self.text.to_coord(cursor.pos);
// At start of line, remove entire line
let line_text_start = self.text.start_of_line_text(coord[1]).unwrap_or_else(|s| s);
if let Some(selection) = cursor.selection() {
self.remove(selection);
} else
@ -648,12 +661,12 @@ impl Buffer {
self.remove(line_start..cursor.pos);
self.backspace(cursor_id); // Remove the newline too
} else*/
if let Some(pos) = cursor.pos.checked_sub(1) {
if cursor.pos == line_text_start {
self.remove(line_start.saturating_sub(1)..cursor.pos);
} else if let Some(pos) = cursor.pos.checked_sub(1) {
// If a backspace is performed on a space, a deindent takes place instead
if self.text.chars().get(pos) == Some(&' ')
// Ensure there's only whitespace to our left
&& self.text.chars().get(line_start..pos).unwrap_or(&[]).iter().all(|c| "\t ".contains(*c))
{
// Ensure there's only whitespace to our left
if cursor.pos == line_text_start {
self.indent_at(cursor.pos, false);
} else {
self.remove(pos..pos + 1);
@ -700,37 +713,26 @@ impl Buffer {
.iter()
.find(|(l, _)| l == last_char)
&& let next_pos = cursor.selection().map_or(cursor.pos, |s| s.end)
&& let next_tok = self
.text
.chars()
.get(next_pos..)
.unwrap_or(&[])
.iter()
.filter(|c| !c.is_ascii_whitespace())
.next()
&& let next_char = self.text.chars().get(next_pos)
{
let (end_of_block, end_needs_indent) = (cursor.pos..)
.take_while(|pos| self.text.chars().get(*pos) != Some(&'\n'))
.find(|pos| {
self.text
.chars()
.get(*pos)
.map_or(true, |c| *c == '\n' || c == r)
})
.map(|pos| (pos, true))
.map(|pos| (pos, self.text.chars().get(pos).copied().unwrap_or('\n')))
.take_while(|(_, c)| *c != '\n')
.find(|(pos, c)| c == r)
.map(|(pos, _)| (pos, true))
.or_else(|| {
let end_of_block = next_line_start + next_indent.len();
// panic!("{:?}", self.text.chars().get(next_line_start + next_indent.len()));
let end_of_block = self.text.start_of_line_text(coord[1] + 1).ok()?;
(self.text.chars().get(next_line_start + next_indent.len()) == Some(&r)
&& prev_indent == next_indent)
.then_some((end_of_block, false))
})
.unwrap_or((cursor.pos, true));
let needs_closing = self.text.chars().get(end_of_block) != Some(&r);
let creating_block = next_indent
.strip_prefix(&*prev_indent)
.map_or(false, |i| i.is_empty())
let creating_block = false
// Case 1: A block is being created from an existing inline one
|| self.text.to_coord(end_of_block)[1] == coord[1]
|| next_indent
.strip_prefix(&*prev_indent)
.map_or(false, |i| i.is_empty())
|| (needs_closing
&& prev_indent
.strip_prefix(&*next_indent)
@ -738,10 +740,10 @@ impl Buffer {
(
(creating_block && needs_closing).then_some(*r),
creating_block.then_some((end_of_block, end_needs_indent)),
if prev_indent.len() > next_indent.len() {
prev_indent
} else {
if prev_indent.len() < next_indent.len() && !creating_block {
next_indent
} else {
prev_indent
},
)
} else {