From 0c2683afaa7763da460e1fa7abc6239bdf48f79b Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sun, 28 Jul 2024 11:42:12 +0200 Subject: [PATCH] Fixing stuff --- src/text_editor/commands_window.cpp | 83 ++++++++++++++++++++++------- src/text_editor/lua_api.cpp | 3 +- src/text_editor/text_editor.cpp | 58 +++----------------- src/text_editor/todo.txt | 3 +- src/text_editor/window.cpp | 3 +- src/text_editor/window_draw.cpp | 2 +- 6 files changed, 77 insertions(+), 75 deletions(-) diff --git a/src/text_editor/commands_window.cpp b/src/text_editor/commands_window.cpp index 7e27f6a..b160306 100644 --- a/src/text_editor/commands_window.cpp +++ b/src/text_editor/commands_window.cpp @@ -166,14 +166,9 @@ void Command_EvalLua(View *view, String16 string) { Command_SelectRangeOneCursor(view, {}); Command_Replace(view, L"\n"); Command_SelectRangeOneCursor(view, {}); - } else { - { - Window *window = GetWindow(GetLastActiveWindow()); - SetActiveWindow(window->id); - // View *view = GetView(window->active_view); - // Command_Replace(view, eval_result); - } + } + else { Range range = GetLineRangeWithoutNL(*buffer, 0); Command_SelectRangeOneCursor(view, range); Command_Replace(view, {}); @@ -551,15 +546,15 @@ void WindowCommand(Event event, Window *window, View *view) { view->carets[0] = caret; } - if (Ctrl(SDLK_Q) || Mouse(MIDDLE)) { - // @todo: Consider applying this to all cursors - if (GetSize(view->carets[0].range) == 0) { - Command_EvalLuaLine(view); - } else { - String16 string = GetString(*buffer, view->carets[0].range); - Command_EvalLua(view, string); - } - } + // if (Ctrl(SDLK_Q) || Mouse(MIDDLE)) { + // // @todo: Consider applying this to all cursors + // if (GetSize(view->carets[0].range) == 0) { + // Command_EvalLuaLine(view); + // } else { + // String16 string = GetString(*buffer, view->carets[0].range); + // Command_EvalLua(view, string); + // } + // } if (window->execute_line) { if (Press(SDLK_RETURN)) { @@ -579,12 +574,13 @@ void WindowCommand(Event event, Window *window, View *view) { String16 string16 = GetString(*buffer); Scratch scratch; String string = ToString(scratch, string16); - bool success = WriteFile(buffer->name, string); + bool success = false; + if (!StartsWith(string, "*")) success = WriteFile(buffer->name, string); + if (success) { buffer->dirty = false; } else { - String msg = Format(scratch, "Failed to save file with name: %.*s", FmtString(buffer->name)); - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Failed to save!", msg.data, NULL); + ReportWarningf("Failed to save file with name: %.*s", FmtString(buffer->name)); } } @@ -670,4 +666,53 @@ void WindowCommand(Event event, Window *window, View *view) { WaitForEvents = false; } } +} + +void UpdateScroll(Window *window, bool update_caret_scrolling) { + ProfileFunction(); + View *view = GetActiveView(window); + Buffer *buffer = GetBuffer(view->active_buffer); + + // Scrolling with caret + if (update_caret_scrolling) { + Caret c = view->carets[0]; + Int front = GetFront(c); + XY xy = PosToXY(*buffer, front); + + Rect2I visible = GetVisibleCells(window); + Vec2I visible_cells = GetSize(visible); + Vec2I visible_size = visible_cells * Vec2I{FontCharSpacing, FontLineSpacing}; + Vec2I rect_size = GetSize(window->document_rect); + + if (xy.line >= visible.max.y - 2) { + Int set_view_at_line = xy.line - (visible_cells.y - 1); + Int cut_off_y = Max((Int)0, visible_size.y - rect_size.y); + view->scroll.y = (set_view_at_line * FontLineSpacing) + cut_off_y; + } + + if (xy.line < visible.min.y + 1) { + view->scroll.y = xy.line * FontLineSpacing; + } + + if (xy.col >= visible.max.x - 1) { + Int set_view_at_line = xy.col - (visible_cells.x - 1); + Int cut_off_x = Max((Int)0, visible_size.x - rect_size.x); + view->scroll.x = (set_view_at_line * FontCharSpacing) + cut_off_x; + } + + if (xy.col <= visible.min.x) { + view->scroll.x = xy.col * FontCharSpacing; + } + } + + // Clip scroll + { + Int last_line = LastLine(*buffer); + view->scroll.y = Clamp(view->scroll.y, (Int)0, Max((Int)0, (last_line - 1) * FontLineSpacing)); + + // @note: + // GetCharCountOfLongestLine is a bottleneck, there is probably an algorithm for + // calculating this value incrementally but do we even need X scrollbar or x clipping? + view->scroll.x = ClampBottom(view->scroll.x, (Int)0); + } } \ No newline at end of file diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index fe8a2e3..d461867 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -52,7 +52,8 @@ int LuaOpen(lua_State *L) { Window *window = GetWindow(GetLastActiveWindow()); View *view = ViewOpenFile(window, file_path); Buffer *buffer = GetBuffer(view->active_buffer); - view->carets[0] = MakeCaret(XYToPos(*buffer, {line - 1, col - 1})); + view->carets[0] = MakeCaret(XYToPos(*buffer, {col - 1, line - 1})); + UpdateScroll(window, true); SetActiveWindow(window->id); } else { // Window *window = GetWindow(GetLastActiveWindow()); diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 058d7fa..7982eec 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -172,11 +172,9 @@ int main() ReloadFont(16); InitLua(); - { - Allocator sys_allocator = GetSystemAllocator(); - Buffer *buffer = CreateBuffer(sys_allocator, "*scratch*"); - View *view = CreateView(buffer->id); - } + Allocator sys_allocator = GetSystemAllocator(); + Buffer *null_buffer = CreateBuffer(sys_allocator, "*scratch*"); + View *null_view = CreateView(null_buffer->id); Buffer *lua_buffer = NULL; Int lua_buffer_change_id = 0; @@ -216,7 +214,7 @@ int main() lua_buffer_change_id = lua_buffer->change_id; } - InitWindows(); + InitWindows(null_view); while (AppIsRunning) { FrameID += 1; @@ -289,52 +287,8 @@ int main() if (!window->visible) continue; } - View *view = GetActiveView(window); - Buffer *buffer = GetBuffer(view->active_buffer); - - // Scrolling with caret - if (!AreEqual(view->main_caret_on_begin_frame, view->carets[0]) && view->update_scroll) { - Caret c = view->carets[0]; - Int front = GetFront(c); - XY xy = PosToXY(*buffer, front); - - Rect2I visible = GetVisibleCells(window); - Vec2I visible_cells = GetSize(visible); - Vec2I visible_size = visible_cells * Vec2I{FontCharSpacing, FontLineSpacing}; - Vec2I rect_size = GetSize(window->document_rect); - - if (xy.line >= visible.max.y - 2) { - Int set_view_at_line = xy.line - (visible_cells.y - 1); - Int cut_off_y = Max((Int)0, visible_size.y - rect_size.y); - view->scroll.y = (set_view_at_line * FontLineSpacing) + cut_off_y; - } - - if (xy.line < visible.min.y + 1) { - view->scroll.y = xy.line * FontLineSpacing; - } - - if (xy.col >= visible.max.x - 1) { - Int set_view_at_line = xy.col - (visible_cells.x - 1); - Int cut_off_x = Max((Int)0, visible_size.x - rect_size.x); - view->scroll.x = (set_view_at_line * FontCharSpacing) + cut_off_x; - } - - if (xy.col <= visible.min.x) { - view->scroll.x = xy.col * FontCharSpacing; - } - } - - // Clip scroll - { - ProfileScope(clip_scroll); - Int last_line = LastLine(*buffer); - view->scroll.y = Clamp(view->scroll.y, (Int)0, Max((Int)0, (last_line - 1) * FontLineSpacing)); - - // @note: - // GetCharCountOfLongestLine is a bottleneck, there is probably an algorithm for - // calculating this value incrementally but do we even need X scrollbar or x clipping? - view->scroll.x = ClampBottom(view->scroll.x, (Int)0); - } + View *view = GetView(window->active_view); + UpdateScroll(window, !AreEqual(view->main_caret_on_begin_frame, view->carets[0]) && view->update_scroll); DrawWindow(window); } diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index 4de46c8..b4355e5 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -1,7 +1,7 @@ - Save file (utf16->utf8) - make sure we only save file buffers -- Some kind of plumbing, linking - resize windows +- page up and down should also scroll and leave you in exactly same scroll - laying out windows, more choice - window borders - file dock on left side @@ -13,6 +13,7 @@ - word completion - Colored strings - open project files in folder and only show open views in Ctrl+P +- Set scroll centered - font cache and on demand unicode loads diff --git a/src/text_editor/window.cpp b/src/text_editor/window.cpp index cffeea1..0016918 100644 --- a/src/text_editor/window.cpp +++ b/src/text_editor/window.cpp @@ -6,7 +6,7 @@ Array GetWindowZOrder(Allocator allocator) { return order; } -void InitWindows() { +void InitWindows(View *null_view) { Allocator sys_allocator = GetSystemAllocator(); { @@ -15,6 +15,7 @@ void InitWindows() { View *v = CreateView(b->id); LoadTextA(b); LoadUnicode(b); + AddView(w, null_view->id); AddView(w, v->id); } diff --git a/src/text_editor/window_draw.cpp b/src/text_editor/window_draw.cpp index 75a8acf..376b0e8 100644 --- a/src/text_editor/window_draw.cpp +++ b/src/text_editor/window_draw.cpp @@ -173,7 +173,7 @@ void DrawWindow(Window *window) { Vec2I pos = {0, line * FontLineSpacing}; pos.y -= view->scroll.y; pos += window->line_numbers_rect.min; - String s = Format(scratch, "%lld", (long long)line); + String s = Format(scratch, "%lld", (long long)line + 1ll); String16 string = ToString16(scratch, s); float x = GetStringSize(&MainFont, string).x; Vec2 p = ToVec2(pos);