From ecbc800fdd72968bab5a4c550e7ed522a204609f Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Mon, 5 Jan 2026 19:40:05 +0100 Subject: [PATCH] Indent on open scope --- src/backup/todo.txt | 1 - src/text_editor/buffer.cpp | 47 +++++++----------------------- src/text_editor/buffer.h | 1 - src/text_editor/view.cpp | 24 +++++---------- src/text_editor/window_command.cpp | 2 +- 5 files changed, 19 insertions(+), 56 deletions(-) diff --git a/src/backup/todo.txt b/src/backup/todo.txt index 3016b76..c31d87c 100644 --- a/src/backup/todo.txt +++ b/src/backup/todo.txt @@ -9,7 +9,6 @@ - Window position: vscode opens in fullscreen and then remembers what position it was close in (could be a config option) - On Linux: Try to implement is_debugger_present() - Probably shouldn't emit (Key pressed when ctrl etc. is not clicked!!) -- Brace, bracket, paren indenting? - OnUpdate view hooks! - OnSave buffer hooks which will execute the config on save diff --git a/src/text_editor/buffer.cpp b/src/text_editor/buffer.cpp index e4c9893..f58c23a 100644 --- a/src/text_editor/buffer.cpp +++ b/src/text_editor/buffer.cpp @@ -519,47 +519,20 @@ API Int SkipSpaces(Buffer *buffer, Int seek) { return seek; } -API Int FindScopeEnd(Buffer *buffer, Int seek, Int max_seek, char16_t open, char16_t close) { - char16_t right = GetChar(buffer, seek); - if (right == open) { - int scope = 1; - Int i = seek + 1; - for (; i < seek + max_seek && i < buffer->len; i += 1) { - char16_t c = GetChar(buffer, i); - - if (open == close && c == '\\') { - i += 1; - } else if (open == close && c == open) { - scope -= 1; - } else if (c == open) { - scope += 1; - } else if (c == close) { - scope -= 1; +API Int FindAnyScopeBegin(Buffer *buffer, Int pos) { + int inner_scope = 0; + for (Int i = pos - 1; i >= 0; i -= 1) { + if (buffer->str[i] == '{' || buffer->str[i] == '[' || buffer->str[i] == '(') { + if (inner_scope == 0) { + return i; } - - if (c == u'\n' || scope == 0) break; + inner_scope -= 1; } - - if (scope == 0) seek = i; - } - return seek; -} - -API Range EncloseScope(Buffer *buffer, Int pos_min, Int pos_max, char16_t open, char16_t close) { - Range result = {pos_min, pos_max}; - for (Int i = pos_min - 1; i >= 0; i -= 1) { - if (buffer->str[i] == open) { - result.min = i; - break; + if (buffer->str[i] == '}' || buffer->str[i] == ']' || buffer->str[i] == ')') { + inner_scope += 1; } } - for (Int i = pos_max; i < buffer->len; i += 1) { - if (buffer->str[i] == close) { - result.max = i + 1; - break; - } - } - return result; + return 0; } API Range EncloseLine(Buffer *buffer, Int pos) { diff --git a/src/text_editor/buffer.h b/src/text_editor/buffer.h index f413b85..0b1884a 100644 --- a/src/text_editor/buffer.h +++ b/src/text_editor/buffer.h @@ -120,7 +120,6 @@ API Int GetPrevEmptyLineStart(Buffer *buffer, Int pos); API Range EncloseWord(Buffer *buffer, Int pos); API Int SkipSpaces(Buffer *buffer, Int seek); API Int FindScopeEnd(Buffer *buffer, Int seek, Int max_seek, char16_t open, char16_t close); -API Range EncloseScope(Buffer *buffer, Int pos_min, Int pos_max, char16_t open, char16_t close); API Range EncloseLine(Buffer *buffer, Int pos); API Range EncloseFullLine(Buffer *buffer, Int pos); API Int OffsetByLine(Buffer *buffer, Int pos, Int line_offset); diff --git a/src/text_editor/view.cpp b/src/text_editor/view.cpp index 5dd06b5..8be56e6 100644 --- a/src/text_editor/view.cpp +++ b/src/text_editor/view.cpp @@ -134,16 +134,17 @@ String16 FetchFuzzyViewLoadLine(View *view) { return string; } -void IdentedNewLine(View *view) { - Buffer *buffer = GetBuffer(view->active_buffer); - Scratch scratch; +void IndentedNewLine(View *view) { + Buffer *buffer = GetBuffer(view->active_buffer); + Scratch scratch; Array edits = BeginEdit(scratch, buffer, view->carets); MergeCarets(buffer, &view->carets); For(view->carets) { - Int front = GetFront(it); - Int line = PosToLine(buffer, front); - Int indent = GetLineIndent(buffer, line); - String string = Format(scratch, "\n%.*s", (int)indent, " "); + Int front = GetFront(it); + Int scope_begin = FindAnyScopeBegin(buffer, front); // @todo: this could be problematic in large source files! + Int line = PosToLine(buffer, scope_begin); + Int indent = GetLineIndent(buffer, line) + IndentSize; + String string = Format(scratch, "\n%.*s", (int)indent, " "); String16 string16 = ToString16(scratch, string); AddEdit(&edits, it.range, string16); } @@ -662,15 +663,6 @@ void EncloseLine(View *view) { } } -void EncloseScope(View *view) { - Buffer *buffer = GetBuffer(view->active_buffer); - For (view->carets) { - it.range = EncloseScope(buffer, it.range.min - 1, it.range.max + 1, u'(', u')'); - it.range.min = Clamp(buffer, it.range.min + 1); - it.range.max = Clamp(buffer, it.range.max - 1); - } -} - void SelectAllOccurences(View *view, String16 needle) { Buffer *buffer = GetBuffer(view->active_buffer); Scratch scratch; diff --git a/src/text_editor/window_command.cpp b/src/text_editor/window_command.cpp index eb24fe1..7f338af 100644 --- a/src/text_editor/window_command.cpp +++ b/src/text_editor/window_command.cpp @@ -43,7 +43,7 @@ void CMD_ShowDebugBufferList() { } command_bar.view->update_scroll = true; SelectRange(command_bar.view, GetBufferBeginAsRange(command_bar.buffer)); -} RegisterCommand(CMD_ShowDebugBufferList, "", "Show full list of buffers, including the special ones that normally just clutter list"); +} RegisterCommand(CMD_ShowDebugBufferList, "ctrl-shift-alt-p", "Show full list of buffers, including the special ones that normally just clutter list"); void CMD_ShowBufferList() { if (ActiveWindowID == CommandWindowID && LastExecutedManualCommand == CMD_ShowBufferList) {