Skip to content

Commit

Permalink
Fixed create_cursors_around behaviour.
Browse files Browse the repository at this point in the history
Now handles starting with multiple cursors.
Now handles being at start of line & being on empty line correctly.
  • Loading branch information
onelivesleft authored and focus-editor committed Oct 1, 2024
1 parent bd036d4 commit 2337837
Showing 1 changed file with 45 additions and 29 deletions.
74 changes: 45 additions & 29 deletions src/editors.jai
Original file line number Diff line number Diff line change
Expand Up @@ -1581,43 +1581,59 @@ create_cursor :: (using editor: *Editor, buffer: Buffer, where: enum { above; be
}

create_cursors_around :: (using editor: *Editor, buffer: Buffer) {
organise_cursors(editor);
// (a) If cursor is mid-line, add cursors above-and-below it on all contiguous lines which are at least as long.
// (b) If the cursor is at the start of a line, add cursors above-and-below, bounded by empty lines.
// (c) If the cursor is on an empty line, add cursors above-and-below on all contiguous empty lines.

top_cursor := cursors[0];
bottom_cursor := cursors[cursors.count-1];
original := cursors[original_cursor];
original_pos_coords := offset_to_coords(editor, buffer, original.pos);
original_sel_coords := offset_to_coords(editor, buffer, original.sel);

add_cursors :: (position: Coords, direction: s32, terminator_line: int) #expand {
while position.line != terminator_line {
position.line += direction;
start := get_line_start_offset(editor, buffer, position.line);
end := get_line_end_offset(editor, buffer, position.line);
if end - start < original_pos_coords.col
break;
organise_cursors(editor);

cursor := add(*cursors);
<<cursor = top_cursor;
cursor.pos = coords_to_offset(editor, buffer, position);
cursor.col_wanted = original_pos_coords.col;
from_cursors := NewArray(cursors.count, Cursor, initialized = false,, temp);
memcpy(from_cursors.data, cursors.data, cursors.count * size_of(Cursor));

for from_cursor : from_cursors {
from_pos_coords := offset_to_coords(editor, buffer, from_cursor.pos);
from_sel_coords := offset_to_coords(editor, buffer, from_cursor.sel);
from_cursor_is_at_line_end := from_cursor.pos == get_line_end_offset(editor, buffer, from_pos_coords.line);
from_line_is_empty := from_pos_coords.col == 0 && from_cursor_is_at_line_end;

add_cursors :: (position: Coords, direction: s32, terminator_line: int) #expand {
while position.line != terminator_line {
position.line += direction;
start := get_line_start_offset(editor, buffer, position.line);
end := get_line_end_offset(editor, buffer, position.line);
if !from_line_is_empty {
if from_pos_coords.col > 0 {
if !from_cursor_is_at_line_end then end -= 1; // Only expand into line-ends if the original cursor was at the line-end.
if end - start < from_pos_coords.col break; // (a)
} else {
if end == start break; // (b)
}
} else {
if end > start break; // (c)
}

if has_selection(original) {
sel_coords := position;
sel_coords.line += original_sel_coords.line - original_pos_coords.line;
sel_coords.col += original_sel_coords.col - original_pos_coords.col;
cursor.sel = coords_to_offset(editor, buffer, sel_coords);
} else {
cursor.sel = cursor.pos;
cursor := add(*cursors);
cursor.* = from_cursor;
cursor.pos = coords_to_offset(editor, buffer, position);
cursor.col_wanted = from_pos_coords.col;

if has_selection(from_cursor) {
sel_coords := position;
sel_coords.line += from_sel_coords.line - from_pos_coords.line;
sel_coords.col += from_sel_coords.col - from_pos_coords.col;
cursor.sel = coords_to_offset(editor, buffer, sel_coords);
cursor.sel = clamp(cursor.sel, start, end);
} else {
cursor.sel = cursor.pos;
}
}
}
}

add_cursors(offset_to_coords(editor, buffer, top_cursor.pos), -1, 0);
add_cursors(offset_to_coords(editor, buffer, top_cursor.pos), +1, buffer.line_starts.count - 1);
add_cursors(offset_to_coords(editor, buffer, from_cursor.pos), -1, 0);
add_cursors(offset_to_coords(editor, buffer, from_cursor.pos), +1, buffer.line_starts.count - 1);
}

main_cursor = cursors.count - 1;

organise_cursors(editor);

cursor_moved = .refresh_only;
Expand Down

0 comments on commit 2337837

Please sign in to comment.