FormatSelection

This commit is contained in:
Krzosa Karol
2026-01-06 14:57:43 +01:00
parent a96cead179
commit 3ba9a9380e
4 changed files with 26 additions and 17 deletions

View File

@@ -1,6 +1,7 @@
! What precise workflow do I need for me to be viable to use this? ! 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? ! 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 - Search and replace
- We need regex for: [FormatCode matching, IsCode matching, - We need regex for: [FormatCode matching, IsCode matching,
- IndentKind has issues with stuff still like cleaning whitespace etc. - IndentKind has issues with stuff still like cleaning whitespace etc.

View File

@@ -1546,22 +1546,7 @@ void BasicSaveBuffer(Buffer *buffer) {
} }
void SaveBuffer(Buffer *buffer) { void SaveBuffer(Buffer *buffer) {
bool formatted = false; if (TrimTrailingWhitespace) {
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) {
TrimWhitespace(buffer); TrimWhitespace(buffer);
} }

View File

@@ -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<Edit> 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) { void GotoNextInList(Window *window, Int line_offset = 1) {
Assert(line_offset == 1 || line_offset == -1); Assert(line_offset == 1 || line_offset == -1);
View *active_view = GetView(window->active_view); View *active_view = GetView(window->active_view);

View File

@@ -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, 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(String, OpenCodeExcludePatterns, "");
RegisterVariable(Int, TrimTrailingWhitespace, 1); RegisterVariable(Int, TrimTrailingWhitespace, 1);
RegisterVariable(Int, FormatCode, 0);