From 3ba9a9380e74b25988c0aa2d56959227100b9465 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Tue, 6 Jan 2026 14:57:43 +0100 Subject: [PATCH] FormatSelection --- src/backup/todo.txt | 1 + src/text_editor/buffer.cpp | 17 +---------------- src/text_editor/commands.cpp | 24 ++++++++++++++++++++++++ src/text_editor/globals.cpp | 1 - 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/backup/todo.txt b/src/backup/todo.txt index 52f416d..2391afe 100644 --- a/src/backup/todo.txt +++ b/src/backup/todo.txt @@ -1,6 +1,7 @@ ! What precise workflow do I need for me to be viable to use this? ! From a user (novice) point of view, how does it look like? +- Buffer edit history separate from caret history (tagging history blocks with carets???) - Search and replace - We need regex for: [FormatCode matching, IsCode matching, - IndentKind has issues with stuff still like cleaning whitespace etc. diff --git a/src/text_editor/buffer.cpp b/src/text_editor/buffer.cpp index 5788dbe..a1ea1a5 100644 --- a/src/text_editor/buffer.cpp +++ b/src/text_editor/buffer.cpp @@ -1546,22 +1546,7 @@ void BasicSaveBuffer(Buffer *buffer) { } void SaveBuffer(Buffer *buffer) { - bool formatted = false; - if (FormatCode) { - bool c = EndsWith(buffer->name, ".c") || EndsWith(buffer->name, ".cpp") || EndsWith(buffer->name, ".h") || EndsWith(buffer->name, ".hpp"); - if (c) { - ApplyFormattingTool(buffer, "clang-format"); - formatted = true; - } - - bool go = EndsWith(buffer->name, ".go"); - if (go) { - ApplyFormattingTool(buffer, "gofmt"); - formatted = true; - } - } - - if (TrimTrailingWhitespace && formatted == false) { + if (TrimTrailingWhitespace) { TrimWhitespace(buffer); } diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index 87d934c..92197a5 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -336,6 +336,30 @@ void ApplyFormattingTool(Buffer *buffer, String tool) { } } +void CMD_FormatSelection() { + Scratch scratch; + BSet primary = GetBSet(PrimaryWindowID); + + String tool = ""; + if (EndsWith(primary.buffer->name, ".c") || EndsWith(primary.buffer->name, ".cpp") || EndsWith(primary.buffer->name, ".h") || EndsWith(primary.buffer->name, ".hpp")) { + tool = "clang-format"; + } else if (EndsWith(primary.buffer->name, ".go")) { + tool = "gofmt"; + } else { + return; + } + + Array edits = BeginEdit(scratch, primary.buffer, primary.view->carets); + MergeCarets(primary.buffer, &primary.view->carets); + For (primary.view->carets) { + String input_string = ToString(scratch, GetString(primary.buffer, it.range)); + ExecResult exec_result = ExecAndWait(scratch, tool, GetDir(primary.buffer), input_string); + String16 string16 = {exec_result.buffer->str, exec_result.buffer->len}; + AddEdit(&edits, it.range, string16); + } + EndEdit(primary.buffer, &edits, &primary.view->carets, KILL_SELECTION); +} RegisterCommand(CMD_FormatSelection, ""); + void GotoNextInList(Window *window, Int line_offset = 1) { Assert(line_offset == 1 || line_offset == -1); View *active_view = GetView(window->active_view); diff --git a/src/text_editor/globals.cpp b/src/text_editor/globals.cpp index e13013d..dae2cfa 100644 --- a/src/text_editor/globals.cpp +++ b/src/text_editor/globals.cpp @@ -170,4 +170,3 @@ RegisterVariable(String, InternetBrowser, "firefox"); RegisterVariable(String, OpenCodePatterns, ".c .h .cpp .hpp .cc .cxx .rs .go .zig .py .lua .js .ts .jsx .tsx .java .kt .swift .cs .rb .php .html .css .scss .bat .sh .bash .zsh .sql .asm .s .cmake .make .json .yaml .toml .ini .txt .md .rst .Makefile .Dockerfile .gitignore .bashrc .zshrc"); RegisterVariable(String, OpenCodeExcludePatterns, ""); RegisterVariable(Int, TrimTrailingWhitespace, 1); -RegisterVariable(Int, FormatCode, 0); \ No newline at end of file