Skip to content

Commit

Permalink
Amended creating cursor above/below to skip lines, added create_curso…
Browse files Browse the repository at this point in the history
…rs_around

If the current cursor is not at the start of the line, when the user creates a cursor above or below it, empty lines will be skipped.  This is a lot more ergonomic; almost always this is what the user wants to do.
The old behaviour is preserved when the cursor is at the start of the line (for instance, when on an empty line).

create_cursors_around:
Cursors will be added to every contiguous line above and below the current cursor as long as the line in question reaches to the cursor column.
  • Loading branch information
onelivesleft authored and focus-editor committed Oct 1, 2024
1 parent 0c0717a commit 8ea0384
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
57 changes: 53 additions & 4 deletions src/editors.jai
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ active_editor_handle_event :: (editor: *Editor, buffer: *Buffer, event: Input.Ev
case .select_all; select_all (editor, buffer); keep_selection = true;
case .create_cursor_above; create_cursor (editor, buffer, .above); keep_selection = true;
case .create_cursor_below; create_cursor (editor, buffer, .below); keep_selection = true;
case .create_cursors_around; create_cursors_around (editor, buffer); keep_selection = true;
case .strip_trailing_whitespace; strip_trailing_whitespace (buffer, editor.buffer_id); keep_selection = true;

case .choose_language; show_dialog (*language_dialog);
Expand Down Expand Up @@ -1552,11 +1553,16 @@ create_cursor :: (using editor: *Editor, buffer: Buffer, where: enum { above; be
cursor := *cursors[cursors.count-1];

pos_coords := offset_to_coords(editor, buffer, cursor.pos);
if where == .above then pos_coords.line -= 1; else pos_coords.line += 1;
pos_coords.col = original_pos_coords.col;
while true {
if where == .above then pos_coords.line -= 1; else pos_coords.line += 1;
pos_coords.col = original_pos_coords.col;

cursor.pos = coords_to_offset(editor, buffer, pos_coords);
cursor.col_wanted = original_pos_coords.col;

cursor.pos = coords_to_offset(editor, buffer, pos_coords);
cursor.col_wanted = original_pos_coords.col;
if cursor.pos <= 0 || cursor.pos >= buffer.bytes.count || original_pos_coords.col == 0 || offset_to_coords(editor, buffer, cursor.pos).col > 0
break;
}

if has_selection(original) {
sel_coords := pos_coords;
Expand All @@ -1574,6 +1580,49 @@ create_cursor :: (using editor: *Editor, buffer: Buffer, where: enum { above; be
cursor_moved = .refresh_only;
}

create_cursors_around :: (using editor: *Editor, buffer: Buffer) {
organise_cursors(editor);

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;

cursor := add(*cursors);
<<cursor = top_cursor;
cursor.pos = coords_to_offset(editor, buffer, position);
cursor.col_wanted = original_pos_coords.col;

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;
}
}
}

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);

main_cursor = cursors.count - 1;

organise_cursors(editor);

cursor_moved = .refresh_only;
}

restore_cursor_state :: (using editor: *Editor, cursor_state: [] Cursor) {
if cursor_state.count == 0 return;

Expand Down
1 change: 1 addition & 0 deletions src/keymap.jai
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ ACTIONS_EDITORS :: #run arrays_concat(ACTIONS_COMMON, string.[
"select_all_occurrences",
"create_cursor_above",
"create_cursor_below",
"create_cursors_around",
"align_cursors",
"change_case_to_upper",
"change_case_to_lower",
Expand Down

0 comments on commit 8ea0384

Please sign in to comment.