From cfad821e8eee842198b7f492bcf41d2334177f2f Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sun, 4 Aug 2024 07:10:36 +0200 Subject: [PATCH] Command_Move down --- src/text_editor/buffer_helpers.cpp | 10 ++++ src/text_editor/buffer_ops.cpp | 2 +- src/text_editor/commands_window.cpp | 74 ++++++++++++++++++++--------- src/text_editor/todo.txt | 2 +- 4 files changed, 63 insertions(+), 25 deletions(-) diff --git a/src/text_editor/buffer_helpers.cpp b/src/text_editor/buffer_helpers.cpp index 4c79e31..2ca080a 100644 --- a/src/text_editor/buffer_helpers.cpp +++ b/src/text_editor/buffer_helpers.cpp @@ -366,4 +366,14 @@ Int OffsetByLine(Buffer *buffer, Int pos, Int line_offset) { XY xy = PosToXY(*buffer, pos); Int result = XYToPosWithoutNL(*buffer, {xy.col, xy.line + line_offset}); return result; +} + +Int GetNextChar(Buffer *buffer, Int pos) { + Int result = Clamp(pos + 1, (Int)0, buffer->len); + return result; +} + +Int GetPrevChar(Buffer *buffer, Int pos) { + Int result = Clamp(pos - 1, (Int)0, buffer->len); + return result; } \ No newline at end of file diff --git a/src/text_editor/buffer_ops.cpp b/src/text_editor/buffer_ops.cpp index 59079f5..c199213 100644 --- a/src/text_editor/buffer_ops.cpp +++ b/src/text_editor/buffer_ops.cpp @@ -2,6 +2,7 @@ const int DIR_RIGHT = 0; const int DIR_LEFT = 1; const int DIR_DOWN = 2; const int DIR_UP = 3; +const int DIR_COUNT = 4; const bool CTRL_PRESSED = true; @@ -67,7 +68,6 @@ Int MoveOnWhitespaceBoundaryVertical(Buffer &buffer, Int pos, int direction) { } Int MovePos(Buffer &buffer, Int pos, int direction, bool ctrl_pressed = false) { - ProfileFunction(); Assert(direction >= 0 && direction <= 3); if (ctrl_pressed) { switch (direction) { diff --git a/src/text_editor/commands_window.cpp b/src/text_editor/commands_window.cpp index 69a6f27..7ef8032 100644 --- a/src/text_editor/commands_window.cpp +++ b/src/text_editor/commands_window.cpp @@ -206,17 +206,16 @@ void Command_MoveCursorsByPageSize(Window *window, int direction, bool shift = f } } -void Command_MoveCursorsToSide(Window *window, int direction, bool shift = false) { +void Command_MoveCursorsToSide(View *view, int direction, bool shift = false) { Assert(direction == DIR_LEFT || direction == DIR_RIGHT); - View &view = *GetActiveView(window); - Buffer *buffer = GetBuffer(view.active_buffer); + Buffer *buffer = GetBuffer(view->active_buffer); - For(view.carets) { - Range line_range = GetLineRangeWithoutNL(*buffer, PosToLine(*buffer, GetFront(it))); - - Int pos = line_range.min; + For(view->carets) { + Int pos = GetFront(it); if (direction == DIR_RIGHT) { - pos = line_range.max; + pos = GetLineEnd(buffer, pos); + } else { + pos = GetLineStart(buffer, pos); } if (shift) { @@ -227,6 +226,41 @@ void Command_MoveCursorsToSide(Window *window, int direction, bool shift = false } } +void Command_Move(View *view, int direction, bool ctrl = false, bool shift = false) { + Assert(direction < DIR_COUNT); + Buffer *buffer = GetBuffer(view->active_buffer); + For(view->carets) { + Int front = GetFront(it); + switch (direction) { + case DIR_UP: { + } break; + case DIR_DOWN: { + if (ctrl && shift) { + Int pos = GetNextEmptyLineStart(buffer, front); + it = ChangeFront(it, pos); + } else if (ctrl) { + Int pos = GetNextEmptyLineStart(buffer, it.range.max); + it = MakeCaret(pos); + } else if (shift) { + Int pos = OffsetByLine(buffer, front, 1); + it = ChangeFront(it, pos); + } else { + if (GetSize(it.range) == 0) { + Int pos = OffsetByLine(buffer, it.range.max, 1); + it = MakeCaret(pos); + } else { + it = MakeCaret(it.range.max); + } + } + } break; + case DIR_LEFT: { + } break; + case DIR_RIGHT: { + } break; + } + } +} + void Command_Delete(View *view, int direction, bool ctrl = false) { Assert(direction == DIR_LEFT || direction == DIR_RIGHT); Scratch scratch; @@ -407,19 +441,13 @@ void WindowCommand(Event event, Window *window, View *view) { } else if (AltShift(SDLK_DOWN)) { Command_CreateCursorVertical(view, DIR_DOWN); } else if (CtrlShift(SDLK_DOWN)) { - For(view->carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_DOWN, true)); + Command_Move(view, DIR_DOWN, CTRL_PRESSED, SHIFT_PRESSED); } else if (Ctrl(SDLK_DOWN)) { - For(view->carets) it = MakeCaret(MovePos(*buffer, it.range.max, DIR_DOWN, true)); + Command_Move(view, DIR_DOWN, CTRL_PRESSED); } else if (Shift(SDLK_DOWN)) { - For(view->carets) it = ChangeFront(it, MovePos(*buffer, GetFront(it), DIR_DOWN, false)); + Command_Move(view, DIR_DOWN, false, SHIFT_PRESSED); } else if (Press(SDLK_DOWN)) { - For(view->carets) { - if (GetSize(it.range) == 0) { - it = MakeCaret(MovePos(*buffer, it.range.max, DIR_DOWN, false)); - } else { - it = MakeCaret(it.range.max); - } - } + Command_Move(view, DIR_DOWN); } if (CtrlAlt(SDLK_UP)) { @@ -519,15 +547,15 @@ void WindowCommand(Event event, Window *window, View *view) { } if (Shift(SDLK_HOME)) { - Command_MoveCursorsToSide(window, DIR_LEFT, SHIFT_PRESSED); + Command_MoveCursorsToSide(view, DIR_LEFT, SHIFT_PRESSED); } else if (Press(SDLK_HOME)) { - Command_MoveCursorsToSide(window, DIR_LEFT); + Command_MoveCursorsToSide(view, DIR_LEFT); } if (Shift(SDLK_END)) { - Command_MoveCursorsToSide(window, DIR_RIGHT, SHIFT_PRESSED); + Command_MoveCursorsToSide(view, DIR_RIGHT, SHIFT_PRESSED); } else if (Press(SDLK_END)) { - Command_MoveCursorsToSide(window, DIR_RIGHT); + Command_MoveCursorsToSide(view, DIR_RIGHT); } bool search = false; @@ -664,7 +692,7 @@ void WindowCommand(Event event, Window *window, View *view) { } else if (window->id.id == SearchWindowID.id) { } else { if (Ctrl(SDLK_RETURN)) { - Command_MoveCursorsToSide(window, DIR_RIGHT); + Command_MoveCursorsToSide(view, DIR_RIGHT); Command_Replace(view, L"\n"); } else if (Press(SDLK_RETURN)) { Command_Replace(view, L"\n"); diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index 9b9969d..d660532 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -2,11 +2,11 @@ - page up and down should also scroll and leave you in exactly same scroll - I think the way sublime text and we display line highlights is confusing with multiple cursors (line highlight can be confused with selection) +- save location on open and allow for revert (buffer id? or buffer name? - buffer name can change, buffer id is harder to serialize, I guess if internal only then buffer id) - delete till indent - indent on enter - maybe my mouse selection is wrong? seems like on double click lite and sublime start to enclosing words!! - click 3 times to select line -- save location on open and allow for revert - we could rewrite kill lines with simpler commands - extend selection to encompass lines->replace - improve cursor movement, it's too clunky, too much stopping - should be able click on title bar of windows which disappear on losing focus