Compare commits
2 commits
92863d60bb
...
595a0f6060
| Author | SHA1 | Date | |
|---|---|---|---|
| 595a0f6060 | |||
| 0206d6f68d |
1 changed files with 32 additions and 30 deletions
56
src/state.rs
56
src/state.rs
|
|
@ -140,6 +140,15 @@ impl Text {
|
||||||
}
|
}
|
||||||
self.chars().get(line_start..line_start + i).unwrap_or(&[])
|
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)]
|
#[derive(Default)]
|
||||||
|
|
@ -639,6 +648,10 @@ impl Buffer {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
let line_start = self.text.to_pos([0, self.text.to_coord(cursor.pos)[1]]);
|
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() {
|
if let Some(selection) = cursor.selection() {
|
||||||
self.remove(selection);
|
self.remove(selection);
|
||||||
} else
|
} else
|
||||||
|
|
@ -648,12 +661,12 @@ impl Buffer {
|
||||||
self.remove(line_start..cursor.pos);
|
self.remove(line_start..cursor.pos);
|
||||||
self.backspace(cursor_id); // Remove the newline too
|
self.backspace(cursor_id); // Remove the newline too
|
||||||
} else*/
|
} 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 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
|
// Ensure there's only whitespace to our left
|
||||||
&& self.text.chars().get(line_start..pos).unwrap_or(&[]).iter().all(|c| "\t ".contains(*c))
|
if cursor.pos == line_text_start {
|
||||||
{
|
|
||||||
self.indent_at(cursor.pos, false);
|
self.indent_at(cursor.pos, false);
|
||||||
} else {
|
} else {
|
||||||
self.remove(pos..pos + 1);
|
self.remove(pos..pos + 1);
|
||||||
|
|
@ -700,35 +713,24 @@ impl Buffer {
|
||||||
.iter()
|
.iter()
|
||||||
.find(|(l, _)| l == last_char)
|
.find(|(l, _)| l == last_char)
|
||||||
&& let next_pos = cursor.selection().map_or(cursor.pos, |s| s.end)
|
&& 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..)
|
let (end_of_block, end_needs_indent) = (cursor.pos..)
|
||||||
.take_while(|pos| self.text.chars().get(*pos) != Some(&'\n'))
|
.map(|pos| (pos, self.text.chars().get(pos).copied().unwrap_or('\n')))
|
||||||
.find(|pos| {
|
.take_while(|(_, c)| *c != '\n')
|
||||||
self.text
|
.find(|(pos, c)| c == r)
|
||||||
.chars()
|
.map(|(pos, _)| (pos, true))
|
||||||
.get(*pos)
|
|
||||||
.map_or(true, |c| *c == '\n' || c == r)
|
|
||||||
})
|
|
||||||
.map(|pos| (pos, true))
|
|
||||||
.or_else(|| {
|
.or_else(|| {
|
||||||
let end_of_block = next_line_start + next_indent.len();
|
let end_of_block = self.text.start_of_line_text(coord[1] + 1).ok()?;
|
||||||
// panic!("{:?}", self.text.chars().get(next_line_start + next_indent.len()));
|
|
||||||
(self.text.chars().get(next_line_start + next_indent.len()) == Some(&r)
|
(self.text.chars().get(next_line_start + next_indent.len()) == Some(&r)
|
||||||
&& prev_indent == next_indent)
|
&& prev_indent == next_indent)
|
||||||
.then_some((end_of_block, false))
|
.then_some((end_of_block, false))
|
||||||
})
|
})
|
||||||
.unwrap_or((cursor.pos, true));
|
.unwrap_or((cursor.pos, true));
|
||||||
let needs_closing = self.text.chars().get(end_of_block) != Some(&r);
|
let needs_closing = self.text.chars().get(end_of_block) != Some(&r);
|
||||||
let creating_block = next_indent
|
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)
|
.strip_prefix(&*prev_indent)
|
||||||
.map_or(false, |i| i.is_empty())
|
.map_or(false, |i| i.is_empty())
|
||||||
|| (needs_closing
|
|| (needs_closing
|
||||||
|
|
@ -738,10 +740,10 @@ impl Buffer {
|
||||||
(
|
(
|
||||||
(creating_block && needs_closing).then_some(*r),
|
(creating_block && needs_closing).then_some(*r),
|
||||||
creating_block.then_some((end_of_block, end_needs_indent)),
|
creating_block.then_some((end_of_block, end_needs_indent)),
|
||||||
if prev_indent.len() > next_indent.len() {
|
if prev_indent.len() < next_indent.len() && !creating_block {
|
||||||
prev_indent
|
|
||||||
} else {
|
|
||||||
next_indent
|
next_indent
|
||||||
|
} else {
|
||||||
|
prev_indent
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue