use continuity_buffer::Buffer;
use continuity_text::{Position, Selection, SelectionKind};
use crate::edit_planning::{finalize_specs, line_content_end, EditSpec};
use crate::selection_edit::SelectionEditPlan;
use crate::Error;
pub(crate) fn plan_join_selected_lines(
buffer: &Buffer,
) -> Result<Option<SelectionEditPlan>, Error> {
let rope = buffer.rope();
let selections_before = buffer.selections().to_vec();
let lines = crate::edit_lines::lines_covered(buffer);
if lines.is_empty() {
return Ok(None);
}
let first = *lines.first().expect("lines is non-empty by check");
let last = *lines.last().expect("lines is non-empty by check");
if first == last {
return Ok(None);
}
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 mut covered = body.split('\n');
let mut joined = covered.next().unwrap_or("").to_string();
let mut pending_blank_lines = 0usize;
for fragment in covered {
let raw = fragment.trim_end_matches('\r');
let trimmed = raw.trim_start();
if trimmed.is_empty() {
pending_blank_lines += 1;
continue;
}
if pending_blank_lines > 0 {
for _ in 1..pending_blank_lines {
joined.push('\n');
}
joined.push('\n');
joined.push_str(raw);
pending_blank_lines = 0;
continue;
}
let content = strip_joined_line_marker(trimmed);
if !content.is_empty() {
joined.push(' ');
joined.push_str(content);
}
}
for _ in 1..pending_blank_lines.max(1) {
joined.push('\n');
}
if trailing_newline {
joined.push('\n');
}
if joined == block_text {
return Ok(None);
}
let joined_body = if trailing_newline {
&joined[..joined.len() - 1]
} else {
&joined[..]
};
let extra_lines = joined_body.matches('\n').count();
let last_line_text = joined_body.rsplit('\n').next().unwrap_or("");
let selections_after = vec![Selection::new(
Position::new(first as u32, 0),
Position::new((first + extra_lines) as u32, last_line_text.len() as u32),
SelectionKind::Caret,
)];
let specs = vec![EditSpec::replace(rope, block_start, block_end, joined)?];
Ok(finalize_specs(specs, selections_before, selections_after))
}
fn strip_joined_line_marker(text: &str) -> &str {
let rest = if let Some(rest) = text
.strip_prefix("- ")
.or_else(|| text.strip_prefix("* "))
.or_else(|| text.strip_prefix("+ "))
{
rest
} else {
let bytes = text.as_bytes();
let digits = bytes.iter().take_while(|b| b.is_ascii_digit()).count();
if digits > 0
&& digits + 1 < bytes.len()
&& (bytes[digits] == b'.' || bytes[digits] == b')')
&& bytes[digits + 1] == b' '
{
&text[digits + 2..]
} else {
return text;
}
};
for checkbox in ["[ ] ", "[x] ", "[X] "] {
if let Some(after) = rest.strip_prefix(checkbox) {
return after;
}
}
rest
}
pub(crate) fn plan_join_lines(buffer: &Buffer) -> Result<Option<SelectionEditPlan>, Error> {
let rope = buffer.rope();
let selections_before = buffer.selections().to_vec();
let mut specs = Vec::new();
let mut handled = std::collections::HashSet::new();
let mut selections_after = Vec::new();
for selection in &selections_before {
let line = selection.head.line as usize;
if !handled.insert(line) {
selections_after.push(*selection);
continue;
}
if line + 1 >= rope.len_lines() {
selections_after.push(*selection);
continue;
}
let line_end = line_content_end(rope, line);
let next_start = rope.line_to_byte(line + 1);
let next_text = rope
.byte_slice(next_start..line_content_end(rope, line + 1))
.to_string();
let trimmed = next_text.trim_start();
let separator = if trimmed.is_empty() { "" } else { " " };
let replacement = format!("{separator}{trimmed}");
let consume_end = next_start + (next_text.len() - trimmed.len()) + trimmed.len();
specs.push(EditSpec::replace(rope, line_end, consume_end, replacement)?);
selections_after.push(*selection);
}
Ok(finalize_specs(specs, selections_before, selections_after))
}
pub(crate) fn plan_move_line_up(buffer: &Buffer) -> Result<Option<SelectionEditPlan>, Error> {
move_line_block(buffer, -1)
}
pub(crate) fn plan_move_line_down(buffer: &Buffer) -> Result<Option<SelectionEditPlan>, Error> {
move_line_block(buffer, 1)
}
fn move_line_block(buffer: &Buffer, delta: i32) -> Result<Option<SelectionEditPlan>, Error> {
let rope = buffer.rope();
let selections_before = buffer.selections().to_vec();
let lines = crate::edit_lines::lines_covered(buffer);
if lines.is_empty() {
return Ok(None);
}
let first = *lines.first().expect("lines is non-empty by check");
let last = *lines.last().expect("lines is non-empty by check");
if delta < 0 && first == 0 {
return Ok(None);
}
if delta > 0 && last + 1 >= rope.len_lines() {
return Ok(None);
}
if let Some(plan) = try_move_within_ordered_run(buffer, first, last, delta)? {
return Ok(Some(plan));
}
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 (replace_start, replace_end, combined_raw) = if delta < 0 {
let prev_start = rope.line_to_byte(first - 1);
let prev_text = rope.byte_slice(prev_start..block_start).to_string();
let mut block = block_text.clone();
if !block.ends_with('\n') {
block.push('\n');
}
(prev_start, block_end, format!("{block}{prev_text}"))
} else {
let next_end = if last + 2 < rope.len_lines() {
rope.line_to_byte(last + 2)
} else {
rope.len_bytes()
};
let next_text = rope.byte_slice(block_end..next_end).to_string();
let mut next_pad = next_text.clone();
if !next_pad.ends_with('\n') {
next_pad.push('\n');
}
(block_start, next_end, format!("{next_pad}{block_text}"))
};
let original_len = replace_end - replace_start;
let mut replacement = combined_raw;
while replacement.len() > original_len && replacement.ends_with('\n') {
replacement.pop();
}
let specs = vec![EditSpec::replace(
rope,
replace_start,
replace_end,
replacement,
)?];
let shift = |p: Position| -> Position {
let new_line = if delta < 0 {
p.line.saturating_sub(1)
} else {
p.line.saturating_add(1)
};
Position::new(new_line, p.byte_in_line)
};
let mut selections_after = Vec::new();
for selection in &selections_before {
selections_after.push(Selection::new(
shift(selection.anchor),
shift(selection.head),
selection.kind,
));
}
Ok(finalize_specs(specs, selections_before, selections_after))
}
fn try_move_within_ordered_run(
buffer: &Buffer,
first: usize,
last: usize,
delta: i32,
) -> Result<Option<SelectionEditPlan>, Error> {
let rope = buffer.rope();
let partner = if delta < 0 { first - 1 } else { last + 1 };
let Some((run_start, run_end, indent)) = crate::edit_list::ordered_list_range_for(rope, first)
else {
return Ok(None);
};
if first < run_start || last > run_end || partner < run_start || partner > run_end {
return Ok(None);
}
for line in first..=last {
if !is_ordered_line_at(rope, line, &indent) {
return Ok(None);
}
}
if !is_ordered_line_at(rope, partner, &indent) {
return Ok(None);
}
let mut bodies: Vec<String> = Vec::with_capacity(run_end - run_start + 1);
for line in run_start..=run_end {
bodies.push(ordered_body_at(rope, line, &indent));
}
let block_lo = first - run_start;
let block_hi = last - run_start;
let block: Vec<String> = bodies[block_lo..=block_hi].to_vec();
bodies.drain(block_lo..=block_hi);
let insert_at = if delta < 0 {
block_lo - 1
} else {
block_lo + 1
};
for (offset, body) in block.into_iter().enumerate() {
bodies.insert(insert_at + offset, body);
}
let mut rebuilt = String::new();
for (idx, body) in bodies.iter().enumerate() {
if idx > 0 {
rebuilt.push('\n');
}
rebuilt.push_str(&format!("{indent}{}. {body}", idx + 1));
}
let block_start = rope.line_to_byte(run_start);
let block_end = if run_end + 1 < rope.len_lines() {
rope.line_to_byte(run_end + 1)
} else {
rope.len_bytes()
};
let original = rope.byte_slice(block_start..block_end).to_string();
let trailing_newline = original.ends_with('\n');
if trailing_newline {
rebuilt.push('\n');
}
if rebuilt == original {
return Ok(None);
}
let selections_before = buffer.selections().to_vec();
let shift = |p: Position| -> Position {
let new_line = if delta < 0 {
p.line.saturating_sub(1)
} else {
p.line.saturating_add(1)
};
Position::new(new_line, p.byte_in_line)
};
let selections_after: Vec<Selection> = selections_before
.iter()
.map(|sel| Selection::new(shift(sel.anchor), shift(sel.head), sel.kind))
.collect();
let specs = vec![EditSpec::replace(rope, block_start, block_end, rebuilt)?];
Ok(finalize_specs(specs, selections_before, selections_after))
}
fn ordered_body_at(rope: &ropey::Rope, line: usize, indent: &str) -> String {
let start = rope.line_to_byte(line);
let end = line_content_end(rope, line);
let text = rope.byte_slice(start..end).to_string();
if text.len() < indent.len() {
return text;
}
let body = &text[indent.len()..];
match crate::edit_list::detect_list_marker(body) {
Some(marker) => body[marker.prefix_len..].to_string(),
None => body.to_string(),
}
}
fn is_ordered_line_at(rope: &ropey::Rope, line: usize, indent: &str) -> bool {
let start = rope.line_to_byte(line);
let end = line_content_end(rope, line);
let text = rope.byte_slice(start..end).to_string();
if text.len() < indent.len() || !text.starts_with(indent) {
return false;
}
let line_indent: String = text
.chars()
.take_while(|c| *c == ' ' || *c == '\t')
.collect();
if line_indent != indent {
return false;
}
let body = &text[indent.len()..];
crate::edit_list::detect_list_marker(body)
.is_some_and(|m| crate::edit_list::is_ordered_marker(&m))
}
#[cfg(test)]
mod tests {
use continuity_buffer::Buffer;
use continuity_text::{Position, Selection};
use crate::selection_edit::{apply_plan, plan, SelectionEdit};
fn build(text: &str, line: u32, col: u32) -> Buffer {
let mut b = Buffer::from_text(text);
b.set_selections(vec![Selection::caret_at(Position::new(line, col))]);
b
}
fn run(b: &mut Buffer, edit: SelectionEdit) {
let p = plan(b, &edit).expect("plan ok").expect("plan some");
apply_plan(b, &p).expect("apply ok");
}
#[test]
fn move_up_within_ordered_run_renumbers() {
let mut b = build("1. a\n2. b\n3. c", 1, 0);
run(&mut b, SelectionEdit::MoveLineUp);
assert_eq!(b.rope().to_string(), "1. b\n2. a\n3. c");
}
#[test]
fn move_down_within_ordered_run_renumbers() {
let mut b = build("1. a\n2. b\n3. c", 1, 0);
run(&mut b, SelectionEdit::MoveLineDown);
assert_eq!(b.rope().to_string(), "1. a\n2. c\n3. b");
}
#[test]
fn move_first_ordered_item_down_renumbers() {
let mut b = build("1. a\n2. b\n3. c", 0, 0);
run(&mut b, SelectionEdit::MoveLineDown);
assert_eq!(b.rope().to_string(), "1. b\n2. a\n3. c");
}
#[test]
fn move_non_ordered_block_falls_through_to_plain_move() {
let mut b = build("a\nb\nc", 1, 0);
run(&mut b, SelectionEdit::MoveLineUp);
assert_eq!(b.rope().to_string(), "b\na\nc");
}
#[test]
fn move_ordered_item_out_to_plain_line_does_not_corrupt() {
let mut b = build("intro\n1. a\n2. b", 1, 0);
run(&mut b, SelectionEdit::MoveLineUp);
assert_eq!(b.rope().to_string(), "1. a\nintro\n2. b");
}
}