From 9ddfed92ff1b64d7921be21a7d61bb1fed0d661b Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Mon, 5 May 2025 21:21:31 +0200 Subject: [PATCH] title bar renaming --- src/basic/string16.cpp | 26 +++++++++++++++ src/text_editor/title_bar.cpp | 59 ++++++++++++++++++++++++++++++----- 2 files changed, 78 insertions(+), 7 deletions(-) diff --git a/src/basic/string16.cpp b/src/basic/string16.cpp index 29aeeb3..a43fdb0 100644 --- a/src/basic/string16.cpp +++ b/src/basic/string16.cpp @@ -313,3 +313,29 @@ String16 SkipWhitespace(String16 *string) { return begin; } +// chop this - :324 +String16 ChopNumberEx(String16 *string) { + String16 col = {}; + for (int64_t i = string->len - 1; i >= 0; i -= 1) { + if (IsDigit(string->data[i])) { + col.data = string->data + i; + col.len += 1; + } else if (string->data[i] == L':') { + break; + } else { + return {}; + } + } + *string = Chop(*string, col.len + 1); + return col; +} + +Int ChopNumber(String16 *string) { + Scratch scratch; + String16 col = ChopNumberEx(string); + if (col.len == 0) return -1; + String num_string = ToString(scratch, col); + Int result = strtoll(num_string.data, NULL, 10) - 1; + + return result; +} \ No newline at end of file diff --git a/src/text_editor/title_bar.cpp b/src/text_editor/title_bar.cpp index 3dd8eef..c0d432c 100644 --- a/src/text_editor/title_bar.cpp +++ b/src/text_editor/title_bar.cpp @@ -22,13 +22,58 @@ void UpdateDebugBuffer() { } void ReplaceTitleBarData(Window *window) { - BSet title = GetBSet(window); + Scratch scratch; + BSet main = GetMainSet(window); + BSet title = GetBSet(window); title.view->scroll.y = 0; - if (window->id == ActiveWindow) return; + String16 buffer_string = GetString(title.buffer); + Range replace_range = {0, title.buffer->len}; + bool found_separator = Seek(buffer_string, u" |", &replace_range.max); + + if (window->id == ActiveWindow) { + if (title.buffer->change_id == title.window->title_bar_last_buffer_change_id) { + return; + } + String16 buffer_name = GetString(title.buffer, replace_range); + buffer_name = Trim(buffer_name); + + Int column = ChopNumber(&buffer_name); + if (column == -1) return; + + Int line = ChopNumber(&buffer_name); + if (line == -1) { + line = column; + column = 0; + } + + String name = ToString(scratch, buffer_name); + if (name != main.buffer->name) { + name = GetAbsolutePath(scratch, name); + if (GetBufferStrict(name)) { + title.window->title_bar_last_buffer_change_id = title.buffer->change_id; + ReportConsolef("there is already buffer with name: %.*s", FmtString(name)); + return; + } + + main.buffer->name = Intern(&GlobalInternTable, name); + } + + Int buffer_pos = XYToPos(main.buffer, {column, line}); + + Caret &caret = main.view->carets[0]; + if (GetFront(caret) != buffer_pos) { + caret = MakeCaret(buffer_pos); + } + + title.window->title_bar_last_buffer_change_id = title.buffer->change_id; + main.buffer->file_mod_time = 0; + main.buffer->changed_on_disk = false; + main.buffer->dirty = true; + + return; + } - BSet main = GetMainSet(window); - Scratch scratch; Caret caret = main.view->carets[0]; XY xy = PosToXY(main.buffer, GetFront(caret)); @@ -38,14 +83,14 @@ void ReplaceTitleBarData(Window *window) { title.view->carets = caret_copy; }; - String16 buffer_string = GetString(title.buffer); - Range replace_range = {0, title.buffer->len}; - if (!Seek(buffer_string, u" |", &replace_range.max)) { + // add separator at the end of buffer + if (!found_separator) { Command_SelectRangeOneCursor(title.view, GetEndAsRange(title.buffer)); Array edits = Command_ReplaceEx(scratch, title.view, u" |"); AdjustCarets(edits, &caret_copy); } + // replace data up to separator with filename and stuff char *reopen = main.buffer->changed_on_disk ? " Reopen()" : ""; String s = Format(scratch, "%.*s:%lld:%lld%s", FmtString(main.buffer->name), (long long)xy.line + 1ll, (long long)xy.col + 1ll, reopen); String16 string = ToString16(scratch, s);