Add Trim trailing whitespace

This commit is contained in:
Krzosa Karol
2024-08-02 21:52:44 +02:00
parent f3059ec8ea
commit 4c7f6340d6
2 changed files with 33 additions and 2 deletions

View File

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

View File

@@ -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