From 4c7f6340d6b5aebe02bcd61bb8b966c388ff71cc Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Fri, 2 Aug 2024 21:52:44 +0200 Subject: [PATCH] Add Trim trailing whitespace --- src/text_editor/commands_window.cpp | 32 ++++++++++++++++++++++++++++- src/text_editor/todo.txt | 3 ++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/text_editor/commands_window.cpp b/src/text_editor/commands_window.cpp index f8e6e58..35777b9 100644 --- a/src/text_editor/commands_window.cpp +++ b/src/text_editor/commands_window.cpp @@ -85,6 +85,32 @@ void Command_IndentSelectedLines(View *view, bool shift = false) { view->update_scroll = false; } +void Command_TrimTrailingWhitespace(View *view) { + Scratch scratch; + Buffer *buffer = GetBuffer(view->active_buffer); + + BeforeEdit(buffer, view->carets); + MergeCarets(view); + + Array edits = {scratch}; + for (Int i = 0; i < buffer->line_starts.len; i += 1) { + Range range = GetLineRangeWithoutNL(*buffer, i); + Int whitespace_end = range.max; + for (; whitespace_end > range.min; whitespace_end -= 1) { + U16 w = buffer->data[whitespace_end - 1]; + bool is_whitespace = w == ' ' || w == '\t' || w == '\v' || w == '\r'; + if (!is_whitespace) break; + } + + Range whitespace_range = {whitespace_end, range.max}; + AddEdit(&edits, whitespace_range, L""); + } + + ApplyEdits(buffer, edits); + AfterEdit(buffer, &edits, &view->carets, !KILL_SELECTION); + view->update_scroll = false; +} + bool SHIFT_PRESSED = true; void Command_MoveCursorsByPageSize(Window *window, int direction, bool shift = false) { Assert(direction == DIR_UP || direction == DIR_DOWN); @@ -420,6 +446,10 @@ void WindowCommand(Event event, Window *window, View *view) { search = true; } + if (Ctrl(SDLK_W)) { + Command_TrimTrailingWhitespace(view); + } + if (Ctrl(SDLK_BACKSPACE)) { Command_Delete(view, DIR_LEFT, CTRL_PRESSED); search = true; @@ -572,7 +602,7 @@ void WindowCommand(Event event, Window *window, View *view) { *c = ChangeFront(*c, p); } else if (mouse_in_document && Mouse(LEFT) && event.mouse_double_click) { Caret *c = &view->carets[0]; - if (InBounds({c->range.min - 1, c->range.max + 1}, p)) { + if (InBounds({c->range.min, c->range.max + 1}, p)) { c->range = EncloseWord(*buffer, p); view->selection_anchor = c->range; } diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index 68d133d..7284360 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -2,7 +2,7 @@ - scrolling in fullscreen is busted because no space outside of window - scrolling when clicking on scroller is busted - page up and down should also scroll and leave you in exactly same scroll - - Shift + click is not selecting!!! + - something about mouse stuff is busted and it randomly does big selections and stuff like that - underline the word which can be loaded / executed - search as a command to execute which is going to be in the title bar @@ -13,6 +13,7 @@ - Ctrl + G should select the line number in bar - How to make search into bar? - command window should close on esacpe but make it more coherent + - auto register commands - simulation: - make the editor replayable, store events and then replay