Compare commits
No commits in common. "595a0f606097e80997dbedb6e160f4e8f798da5b" and "92863d60bbe926f565ed1e8b0fa71e51dfe93eb5" have entirely different histories.
595a0f6060
...
92863d60bb
1 changed files with 30 additions and 32 deletions
62
src/state.rs
62
src/state.rs
|
|
@ -140,15 +140,6 @@ 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)]
|
||||||
|
|
@ -648,10 +639,6 @@ 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
|
||||||
|
|
@ -661,12 +648,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 cursor.pos == line_text_start {
|
if let Some(pos) = cursor.pos.checked_sub(1) {
|
||||||
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
|
||||||
// Ensure there's only whitespace to our left
|
if self.text.chars().get(pos) == Some(&' ')
|
||||||
if cursor.pos == line_text_start {
|
// Ensure there's only whitespace to our left
|
||||||
|
&& self.text.chars().get(line_start..pos).unwrap_or(&[]).iter().all(|c| "\t ".contains(*c))
|
||||||
|
{
|
||||||
self.indent_at(cursor.pos, false);
|
self.indent_at(cursor.pos, false);
|
||||||
} else {
|
} else {
|
||||||
self.remove(pos..pos + 1);
|
self.remove(pos..pos + 1);
|
||||||
|
|
@ -713,26 +700,37 @@ 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..)
|
||||||
.map(|pos| (pos, self.text.chars().get(pos).copied().unwrap_or('\n')))
|
.take_while(|pos| self.text.chars().get(*pos) != Some(&'\n'))
|
||||||
.take_while(|(_, c)| *c != '\n')
|
.find(|pos| {
|
||||||
.find(|(pos, c)| c == r)
|
self.text
|
||||||
.map(|(pos, _)| (pos, true))
|
.chars()
|
||||||
|
.get(*pos)
|
||||||
|
.map_or(true, |c| *c == '\n' || c == r)
|
||||||
|
})
|
||||||
|
.map(|pos| (pos, true))
|
||||||
.or_else(|| {
|
.or_else(|| {
|
||||||
let end_of_block = self.text.start_of_line_text(coord[1] + 1).ok()?;
|
let end_of_block = next_line_start + next_indent.len();
|
||||||
|
// 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 = false
|
let creating_block = next_indent
|
||||||
// Case 1: A block is being created from an existing inline one
|
.strip_prefix(&*prev_indent)
|
||||||
|| self.text.to_coord(end_of_block)[1] == coord[1]
|
.map_or(false, |i| i.is_empty())
|
||||||
|| next_indent
|
|
||||||
.strip_prefix(&*prev_indent)
|
|
||||||
.map_or(false, |i| i.is_empty())
|
|
||||||
|| (needs_closing
|
|| (needs_closing
|
||||||
&& prev_indent
|
&& prev_indent
|
||||||
.strip_prefix(&*next_indent)
|
.strip_prefix(&*next_indent)
|
||||||
|
|
@ -740,10 +738,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() && !creating_block {
|
if prev_indent.len() > next_indent.len() {
|
||||||
next_indent
|
|
||||||
} else {
|
|
||||||
prev_indent
|
prev_indent
|
||||||
|
} else {
|
||||||
|
next_indent
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue