From 7ed46b9724206e054e93a022fb9d2fb876679a95 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Thu, 15 Aug 2024 11:54:32 +0200 Subject: [PATCH] Report errors idea using titlebar --- data/test_change_buffer_and_close.project.lua | 44 ++++++++--------- src/text_editor/commands.cpp | 48 ++++++++++++------- src/text_editor/commands_bindings.cpp | 4 ++ src/text_editor/lua_api.cpp | 2 +- src/text_editor/management.cpp | 2 +- src/text_editor/test.cpp | 1 - src/text_editor/text_editor.cpp | 5 +- src/text_editor/todo.txt | 2 +- 8 files changed, 62 insertions(+), 46 deletions(-) diff --git a/data/test_change_buffer_and_close.project.lua b/data/test_change_buffer_and_close.project.lua index 6023ad0..4d3c95b 100644 --- a/data/test_change_buffer_and_close.project.lua +++ b/data/test_change_buffer_and_close.project.lua @@ -1,25 +1,25 @@ -Style.WaitForEvents = 0 +-- Style.WaitForEvents = 0 -AddCo(function() - Play{ - {kind = 10, key = KEY_DOWN, xwindow = 1280, ywindow = 720}, - {kind = 10, key = KEY_DOWN, xwindow = 1280, ywindow = 720}, - {kind = 10, key = KEY_DOWN, xwindow = 1280, ywindow = 720}, - {kind = 10, key = KEY_DOWN, xwindow = 1280, ywindow = 720}, - {kind = 10, key = KEY_DOWN, xwindow = 1280, ywindow = 720}, - {kind = 10, key = KEY_DOWN, xwindow = 1280, ywindow = 720}, - {kind = 10, key = KEY_DOWN, xwindow = 1280, ywindow = 720}, - {kind = 10, key = KEY_Q , xwindow = 1280, ywindow = 720, ctrl = 1}, - {kind = 10, key = KEY_PAGE_DOWN, xmouse = 0, ymouse = 0, xwindow = 1280, ywindow = 720}, - {kind = 111}, - } - -- for i = 1,8 do coroutine.yield() end - while coroutine.yield().kind ~= 111 do end +-- AddCo(function() +-- Play{ +-- {kind = 10, key = KEY_DOWN, xwindow = 1280, ywindow = 720}, +-- {kind = 10, key = KEY_DOWN, xwindow = 1280, ywindow = 720}, +-- {kind = 10, key = KEY_DOWN, xwindow = 1280, ywindow = 720}, +-- {kind = 10, key = KEY_DOWN, xwindow = 1280, ywindow = 720}, +-- {kind = 10, key = KEY_DOWN, xwindow = 1280, ywindow = 720}, +-- {kind = 10, key = KEY_DOWN, xwindow = 1280, ywindow = 720}, +-- {kind = 10, key = KEY_DOWN, xwindow = 1280, ywindow = 720}, +-- {kind = 10, key = KEY_Q , xwindow = 1280, ywindow = 720, ctrl = 1}, +-- {kind = 10, key = KEY_PAGE_DOWN, xmouse = 0, ymouse = 0, xwindow = 1280, ywindow = 720}, +-- {kind = 111}, +-- } +-- -- for i = 1,8 do coroutine.yield() end +-- while coroutine.yield().kind ~= 111 do end - local buffer_name = GetActiveMainWindowBufferName() - Print("buffer name = "..buffer_name) - if buffer_name == "C:/Work/text_editor/src/text_editor/buffer_history.cpp" then - Play{{kind = 2}} - end -end) +-- local buffer_name = GetActiveMainWindowBufferName() +-- Print("buffer name = "..buffer_name) +-- if buffer_name == "C:/Work/text_editor/src/text_editor/buffer_history.cpp" then +-- Play{{kind = 2}} +-- end +-- end) diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index 7cb145a..1f910fa 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -210,12 +210,9 @@ void ReportWarningf(const char *fmt, ...) { View *null_view = GetView(NullViewID); Command_Append(null_view, string, true); - // @todo: proper warning - // Window *window = GetWindowWithView(null_view->id); - // if (!window) window = GetActiveMainSet().window; - // CheckpointBeforeGoto(window); - // window->active_view = null_view->id; - // ActiveWindow = window->id; + void Command_InsertTitlebarCommand(BSet title, String16 needle, String16 string, bool select_entire); + BSet title = GetActiveTitleSet(); + Command_InsertTitlebarCommand(title, L"#Error(\"", ToString16(scratch, string), true); } void Command_MoveCursorsByPageSize(Window *window, int direction, bool shift = false) { @@ -887,26 +884,41 @@ void Command_FuzzySort(View *view, String16 needle) { Command_SelectRangeOneCursor(view, Rng(0)); } -void Command_SelectTitlebarCommand(Window *window, String16 needle) { - BSet title = GetTitleSet(window); - String16 buffer_string = GetString(title.buffer); - ActiveWindow = title.window->id; - title.window->auto_enclose = true; - +void Command_InsertTitlebarCommand(BSet title, String16 needle, String16 initial_value, bool select_entire_thing) { Scratch scratch; String16 quoted16 = {}; { - String needle8 = ToString(scratch, needle); - String quoted = Format(scratch, "%.*s\")", FmtString(needle8)); - quoted16 = ToString16(scratch, quoted); + String needle8 = ToString(scratch, needle); + String initial_value8 = ToString(scratch, initial_value); + String quoted = Format(scratch, "%.*s%.*s\")", FmtString(needle8), FmtString(initial_value8)); + quoted16 = ToString16(scratch, quoted); } - int64_t index = 0; + Range needle_range = {}; + int64_t index = 0; + String16 buffer_string = GetString(title.buffer); if (Seek(buffer_string, needle, &index)) { - Command_SelectRangeOneCursor(title.view, Rng(index + needle.len)); + Range range = EncloseExecWord(title.buffer, index); + Command_SelectRangeOneCursor(title.view, Rng(range.min - 1, range.max)); + Command_Replace(title.view, quoted16); + needle_range = {index + needle.len, index + needle.len + initial_value.len}; } else { Command_SelectRangeOneCursor(title.view, GetEndAsRange(title.buffer)); Command_Replace(title.view, quoted16); - Command_SelectRangeOneCursor(title.view, GetEndAsRange(title.buffer) - 2); + Int end = GetEndAsRange(title.buffer).max - 2; + needle_range = {end - initial_value.len, end}; } + + if (select_entire_thing) { + needle_range.min -= needle.len; + needle_range.max += 2; + } + Command_SelectRangeOneCursor(title.view, MakeCaret(needle_range.max, needle_range.min)); +} + +void Command_SelectTitlebarCommand(Window *window, String16 needle) { + BSet title = GetTitleSet(window); + ActiveWindow = title.window->id; + title.window->auto_enclose = true; + Command_InsertTitlebarCommand(title, needle, L"", false); } diff --git a/src/text_editor/commands_bindings.cpp b/src/text_editor/commands_bindings.cpp index f4d2ec7..90a242c 100644 --- a/src/text_editor/commands_bindings.cpp +++ b/src/text_editor/commands_bindings.cpp @@ -449,6 +449,10 @@ void OnCommand(Event event) { Open(name); } + if (CtrlPress(SDLK_T)) { + ActiveWindow = GetWindow(active.window->title_bar_window)->id; + } + if (CtrlShiftPress(SDLK_G)) { } else if (CtrlPress(SDLK_G)) { Command_SelectTitlebarCommand(active.window, L"#FuzzySort(\""); diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index a19cdd5..87adcf1 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -469,7 +469,7 @@ bool EvalString(Allocator allocator, String16 string16) { String string = ToString(scratch, string16); if (luaL_dostring(LuaState, string.data) != LUA_OK) { const char *error_message = lua_tostring(LuaState, -1); - ReportConsolef("Execution error! %s", error_message); + ReportWarningf("Execution error! %s", error_message); lua_pop(LuaState, 1); return false; } diff --git a/src/text_editor/management.cpp b/src/text_editor/management.cpp index a3ca4bf..107adec 100644 --- a/src/text_editor/management.cpp +++ b/src/text_editor/management.cpp @@ -225,7 +225,7 @@ BSet GetActiveMainSet() { BSet GetActiveTitleSet() { Window *window = GetWindow(ActiveWindow); - if (window->is_title_bar) window = GetWindow(window->title_bar_window); + if (!window->is_title_bar) window = GetWindow(window->title_bar_window); return GetBSet(window); } diff --git a/src/text_editor/test.cpp b/src/text_editor/test.cpp index ef3e407..ddc811e 100644 --- a/src/text_editor/test.cpp +++ b/src/text_editor/test.cpp @@ -8,7 +8,6 @@ void Test(mco_coro *co) { Add(&EventPlayback, {EVENT_KEY_PRESS, SDLK_DOWN, 1280, 720}); Add(&EventPlayback, {EVENT_KEY_PRESS, SDLK_Q, 1280, 720}); GetLast(EventPlayback)->ctrl = 1; - Add(&EventPlayback, {EVENT_KEY_PRESS, SDLK_PAGEDOWN, 1280, 720}); Add(&EventPlayback, {111}); for (Event *event = Yield(co); event->kind != 111; event = Yield(co)) { diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 176a46c..baf44be 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -317,8 +317,9 @@ int main(int argc, char **argv) InitLuaConfig(); - StyleWaitForEvents = false; - AddCo(Test); + // :Tests + // StyleWaitForEvents = false; + // AddCo(Test); Serializer ser = {EventBuffer}; while (AppIsRunning) { diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index 5b70e95..6cfee20 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -5,8 +5,8 @@ - Find matches using grep, change things in that buffer then apply those changes to all items - group history entries so the you can rollback through multiple ones at once and not waste time on skipping whitespace trimming or deleting every character -- Fuzzy search buffer which uses titlebar as query! - ReportWarning should be signaled visibly but you should be able to do things! no focus switching +- fix fucking conversions between strings!!!!!! we need to have stb_sprintf 16 - OnWindowCommand allow config user to overwrite the WindowCommand keybinding, introduce his own