use continuity_buffer::Buffer;
use continuity_text::{Position, Selection};
use ropey::Rope;
use crate::edit_planning::{finalize_specs, line_content_end, EditSpec};
use crate::selection_edit::SelectionEditPlan;
use crate::Error;
pub(crate) fn plan_insert_newline_above(
buffer: &Buffer,
) -> Result<Option<SelectionEditPlan>, Error> {
let rope = buffer.rope();
let selections_before = buffer.selections().to_vec();
let mut specs = Vec::new();
let mut selections_after = Vec::new();
for selection in &selections_before {
let line = selection.head.line as usize;
let line_start = rope.line_to_byte(line);
specs.push(EditSpec::insert(rope, line_start, "\n".into())?);
selections_after.push(Selection::caret_at(Position::new(line as u32, 0)));
}
Ok(finalize_specs(specs, selections_before, selections_after))
}
pub(crate) fn plan_insert_newline_below(
buffer: &Buffer,
) -> Result<Option<SelectionEditPlan>, Error> {
let rope = buffer.rope();
let selections_before = buffer.selections().to_vec();
let mut specs = Vec::new();
let mut selections_after = Vec::new();
for selection in &selections_before {
let line = selection.head.line as usize;
let end = line_content_end(rope, line);
specs.push(EditSpec::insert(rope, end, "\n".into())?);
selections_after.push(Selection::caret_at(Position::new((line as u32) + 1, 0)));
}
Ok(finalize_specs(specs, selections_before, selections_after))
}
pub(crate) fn plan_insert_newline_smart(
buffer: &Buffer,
) -> Result<Option<SelectionEditPlan>, Error> {
crate::edit_list::plan_insert_newline_smart_list_aware(buffer)
}
pub(crate) fn leading_whitespace_for(rope: &Rope, line: usize) -> String {
leading_whitespace(rope, line)
}
mod toggle_bullet;
pub(crate) use toggle_bullet::{
plan_toggle_bullet_at_line_start, plan_toggle_bullet_with_continuation_indent,
};
pub(crate) fn plan_delete_to_line_start(
buffer: &Buffer,
) -> Result<Option<SelectionEditPlan>, Error> {
let rope = buffer.rope();
let selections_before = buffer.selections().to_vec();
let mut specs = Vec::new();
let mut selections_after = Vec::new();
for selection in &selections_before {
let head = selection.head;
let line = head.line as usize;
let line_start = rope.line_to_byte(line);
let head_byte = head.to_byte_offset(rope)?;
if head_byte > line_start {
specs.push(EditSpec::delete(rope, line_start, head_byte)?);
selections_after.push(Selection::caret_at(Position::new(line as u32, 0)));
} else {
selections_after.push(*selection);
}
}
Ok(finalize_specs(specs, selections_before, selections_after))
}
pub(crate) fn plan_delete_to_line_end(buffer: &Buffer) -> Result<Option<SelectionEditPlan>, Error> {
let rope = buffer.rope();
let selections_before = buffer.selections().to_vec();
let mut specs = Vec::new();
let mut selections_after = Vec::new();
for selection in &selections_before {
let head = selection.head;
let line = head.line as usize;
let end = line_content_end(rope, line);
let head_byte = head.to_byte_offset(rope)?;
if end > head_byte {
specs.push(EditSpec::delete(rope, head_byte, end)?);
}
selections_after.push(*selection);
}
Ok(finalize_specs(specs, selections_before, selections_after))
}
pub(crate) fn plan_duplicate_line(buffer: &Buffer) -> Result<Option<SelectionEditPlan>, Error> {
let rope = buffer.rope();
let selections_before = buffer.selections().to_vec();
let lines = lines_covered(buffer);
if lines.is_empty() {
return Ok(None);
}
let mut specs = Vec::new();
for &line in &lines {
let start = rope.line_to_byte(line);
let end = if line + 1 < rope.len_lines() {
rope.line_to_byte(line + 1)
} else {
rope.len_bytes()
};
let mut text = rope.byte_slice(start..end).to_string();
if !text.ends_with('\n') {
text.push('\n');
}
specs.push(EditSpec::insert(rope, end, text)?);
}
let selections_after = selections_before.clone();
Ok(finalize_specs(specs, selections_before, selections_after))
}
pub(crate) fn plan_duplicate_selection(
buffer: &Buffer,
) -> Result<Option<SelectionEditPlan>, Error> {
let rope = buffer.rope();
let selections_before = buffer.selections().to_vec();
let mut specs = Vec::new();
let mut selections_after = Vec::new();
for selection in &selections_before {
if selection.is_collapsed() {
selections_after.push(*selection);
continue;
}
let range = selection.ordered_range();
let start = range.start.to_byte_offset(rope)?;
let end = range.end.to_byte_offset(rope)?;
let text = rope.byte_slice(start..end).to_string();
specs.push(EditSpec::insert(rope, end, text)?);
selections_after.push(*selection);
}
Ok(finalize_specs(specs, selections_before, selections_after))
}
pub(crate) use crate::edit_lines_movement::{
plan_join_lines, plan_join_selected_lines, plan_move_line_down, plan_move_line_up,
};
pub(crate) fn rewrite_covered_lines<F>(
buffer: &Buffer,
f: F,
) -> Result<Option<SelectionEditPlan>, Error>
where
F: FnOnce(Vec<String>) -> Vec<String>,
{
let rope = buffer.rope();
let selections_before = buffer.selections().to_vec();
let lines = lines_covered(buffer);
if lines.is_empty() {
return Ok(None);
}
let first = *lines.first().expect("non-empty");
let last = *lines.last().expect("non-empty");
let block_start = rope.line_to_byte(first);
let block_end = if last + 1 < rope.len_lines() {
rope.line_to_byte(last + 1)
} else {
rope.len_bytes()
};
let block_text = rope.byte_slice(block_start..block_end).to_string();
let trailing_newline = block_text.ends_with('\n');
let body = if trailing_newline {
&block_text[..block_text.len() - 1]
} else {
&block_text[..]
};
let original: Vec<String> = body.split('\n').map(str::to_string).collect();
let rewritten = f(original.clone());
if rewritten == original {
return Ok(None);
}
let mut combined = rewritten.join("\n");
if trailing_newline {
combined.push('\n');
}
let specs = vec![EditSpec::replace(rope, block_start, block_end, combined)?];
Ok(finalize_specs(
specs,
selections_before.clone(),
selections_before,
))
}
pub(crate) fn lines_covered(buffer: &Buffer) -> Vec<usize> {
let mut lines: Vec<usize> = Vec::new();
let len_lines = buffer.rope().len_lines();
for selection in buffer.selections() {
let range = selection.ordered_range();
let start = range.start.line as usize;
let end = range.end.line as usize;
for line in start..=end {
if line < len_lines && !lines.contains(&line) {
lines.push(line);
}
}
}
lines.sort_unstable();
lines
}
fn leading_whitespace(rope: &Rope, line: usize) -> String {
let start = rope.line_to_byte(line);
let end = line_content_end(rope, line);
let text = rope.byte_slice(start..end).to_string();
let count: usize = text
.chars()
.take_while(|c| *c == ' ' || *c == '\t')
.map(char::len_utf8)
.sum();
text[..count].to_string()
}
#[cfg(test)]
mod tests;