diff --git a/src/text_editor/buffer.cpp b/src/text_editor/buffer.cpp index 7b5b989..62441d0 100644 --- a/src/text_editor/buffer.cpp +++ b/src/text_editor/buffer.cpp @@ -1410,31 +1410,6 @@ String GetUniqueBufferName(String working_dir, String prepend_name, String exten return buffer_name; } -void InitBuffers() { - Allocator sys_allocator = GetSystemAllocator(); - Scratch scratch; - Buffer *null_buffer = CreateBuffer(sys_allocator, GetUniqueBufferName(ProjectDirectory, "logs", "")); - null_buffer->special = true; - View *null_view = CreateView(null_buffer->id); - null_view->special = true; - Assert(null_buffer->id == NullBufferID && null_view->id == NullViewID); - TraceBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(ProjectDirectory, "trace")); - TraceBuffer->special = true; - TraceBuffer->no_history = true; - GCInfoBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(ProjectDirectory, "gc")); - GCInfoBuffer->special = true; - GCInfoBuffer->no_history = true; - EventBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(ProjectDirectory, "events")); - EventBuffer->no_history = true; - EventBuffer->special = true; -#ifdef PLUGIN_SEARCH_OPEN_BUFFERS - Buffer *search_project = CreateBuffer(sys_allocator, GetUniqueBufferName(ProjectDirectory, "search_project")); - search_project->no_history = true; - search_project->special = true; - SearchOpenBuffersBufferID = search_project->id; -#endif -} - Int ConvertUTF8ToUTF16UnixLine(String string, char16_t *buffer, Int buffer_cap) { if (string.len == 0) { return 0; diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index d793570..8f5aea0 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -91,6 +91,33 @@ View *GetViewForFixingWhenBufferCommand(Buffer *buffer, bool *is_active = NULL) return view; } +void MoveCursorByPageSize(Window *window, int direction, bool shift = false) { + Assert(direction == DIR_UP || direction == DIR_DOWN); + BSet set = GetBSet(window); + + Rect2I visible_cells_rect = GetVisibleCells(window); + Int y = GetSize(visible_cells_rect).y - 2; + if (direction == DIR_UP) y = -y; + + For(set.view->carets) { + XY xy = PosToXY(set.buffer, GetFront(it)); + if (direction == DIR_DOWN && xy.line == set.buffer->line_starts.len - 1) { + Range line_range = GetLineRange(set.buffer, xy.line); + xy.col = line_range.max - line_range.min; + } else if (direction == DIR_UP && xy.line == 0) { + xy.col = 0; + } + xy.line += y; + + Int pos = XYToPos(set.buffer, xy); + if (shift) { + it = SetFront(it, pos); + } else { + it = MakeCaret(pos); + } + } +} + void ReplaceWithoutMovingCarets(Buffer *buffer, Range range, String16 string) { View *view = GetViewForFixingWhenBufferCommand(buffer); Array carets = Copy(GetSystemAllocator(), view->carets); @@ -205,12 +232,6 @@ void ReportWarningf(const char *fmt, ...) { Appendf(null_view, "%S\n", string); } -void ReportDebugf(const char *fmt, ...) { - Scratch scratch; - STRING_FORMAT(scratch, fmt, string); - RawAppendf(TraceBuffer, "%S\n", string); -} - void CenterView(WindowID window) { BSet set = GetBSet(window); Caret c = set.view->carets[0]; @@ -436,16 +457,6 @@ void CMD_SaveAll() { } } RegisterCommand(CMD_SaveAll, "ctrl-shift-s"); -void CMD_GotoNextInList() { - BSet main = GetBSet(PrimaryWindowID); - GotoNextInList(main.window, 1); -} RegisterCommand(CMD_GotoNextInList, "ctrl-e", "For example: when jumping from build panel to build error, a jump point is setup, user can click this button to go over to the next compiler error"); - -void CMD_GotoPrevInList() { - BSet main = GetBSet(PrimaryWindowID); - GotoNextInList(main.window, -1); -} RegisterCommand(CMD_GotoPrevInList, "alt-e", "For example: when jumping from build panel to build error, a jump point is setup, user can click this button to go over to the previous compiler error"); - ResolvedOpen ResolveOpen(Allocator alo, String path, ResolveOpenMeta meta) { ResolvedOpen result = {}; path = Trim(path); diff --git a/src/text_editor/draw.cpp b/src/text_editor/draw.cpp index 64661dc..7c89a12 100644 --- a/src/text_editor/draw.cpp +++ b/src/text_editor/draw.cpp @@ -214,7 +214,7 @@ void DrawWindow(Window *window, Event &event) { } } - if (Ctrl()) { + if (event.ctrl) { if (is_active) { if (GetSize(caret.range) == 0) { Range range = EncloseLoadWord(buffer, caret.range.min); diff --git a/src/text_editor/event.cpp b/src/text_editor/event.cpp index cb7bb1a..1c06438 100644 --- a/src/text_editor/event.cpp +++ b/src/text_editor/event.cpp @@ -503,78 +503,6 @@ Array GetEventsForFrame(Allocator allocator) { return result; } -struct Serializer { - Buffer *buffer; // for writing -}; - -void Serialize(Serializer *s, String name, EventKind *kind) { - RawAppendf(s->buffer, "ev.%S = %s; ", name, EventKindStrings[*kind]); -} - -void Serialize(Serializer *s, String name, Int *datum) { - if (*datum == 0) { - return; - } - if (name == "key") { - RawAppendf(s->buffer, "ev.%S = %s; ", name, SDLKeycodeToName((SDL_Keycode)*datum)); - } else { - RawAppendf(s->buffer, "ev.%S = %lld; ", name, (long long)*datum); - } -} - -void Serialize(Serializer *s, String name, uint32_t *datum) { - Int d = *datum; - Serialize(s, name, &d); - *datum = (uint32_t)d; -} - -void Serialize(Serializer *s, String name, uint8_t *datum) { - Int d = *datum; - Serialize(s, name, &d); - *datum = (uint8_t)d; -} - -void Serialize(Serializer *s, String name, int16_t *datum) { - Int d = *datum; - Serialize(s, name, &d); - *datum = (int16_t)d; -} - -void Serialize(Serializer *s, String name, float *datum) { - if (*datum == 0.f) { - return; - } - RawAppendf(s->buffer, "ev.%S = %f; ", name, *datum); -} - -void Serialize(Serializer *s, String name, char **text) { - String str = *text; - if (str.len == 0) { - return; - } - RawAppendf(s->buffer, "ev.%S = \"%S\"; ", name, str); -} - -void SerializeBegin(Serializer *s) { - RawAppendf(s->buffer, "{Event ev = {};"); -} - -void SerializeEnd(Serializer *s) { - RawAppendf(s->buffer, "Add(&EventPlayback, ev);}\n"); -} - -// :Event -void Serialize(Serializer *s, Event *e) { - SerializeBegin(s); -#define X(TYPE, KIND, NAME) Serialize(s, #NAME, &e->NAME); - EVENT_FIELDS -#undef X - SerializeEnd(s); -} - -#define Ctrl() event.ctrl -#define Alt() event.alt -#define Shift() event.shift #define Press(KEY) (event.key == KEY) #define CtrlPress(KEY) (event.key == KEY && event.ctrl) @@ -583,10 +511,8 @@ void Serialize(Serializer *s, Event *e) { #define CtrlShiftPress(KEY) (event.key == KEY && event.ctrl && event.shift) #define CtrlAltPress(KEY) (event.key == KEY && event.ctrl && event.alt) #define AltShiftPress(KEY) (event.key == KEY && event.shift && event.alt) -#define MouseVec2() \ - Vec2 { (float)event.xmouse, (float)event.ymouse } -#define MouseVec2I() \ - Vec2I { (Int) event.xmouse, (Int)event.ymouse } +#define MouseVec2() Vec2 { (float)event.xmouse, (float)event.ymouse } +#define MouseVec2I() Vec2I { (Int) event.xmouse, (Int)event.ymouse } #define Mouse(x) (event.kind == EVENT_MOUSE_##x) #define MousePress() (Mouse(LEFT) || Mouse(RIGHT) || Mouse(MIDDLE)) #define MouseUp() (Mouse(LEFT_UP) || Mouse(RIGHT_UP) || Mouse(MIDDLE_UP)) diff --git a/src/text_editor/globals.cpp b/src/text_editor/globals.cpp index a875b12..e3db645 100644 --- a/src/text_editor/globals.cpp +++ b/src/text_editor/globals.cpp @@ -59,10 +59,6 @@ WindowID NullWindowID; BufferID BuildBufferID; #endif -#ifdef PLUGIN_SEARCH_OPEN_BUFFERS - BufferID SearchOpenBuffersBufferID; -#endif - BufferID GlobalConfigBufferID; WindowID NextActiveWindowID; @@ -75,11 +71,6 @@ WindowID ResizerHover = {-1}; Caret DocumentAnchor; Vec2I MouseMiddleAnchor; -Buffer *GCInfoBuffer; -Buffer *EventBuffer; -Buffer *TraceBuffer; -View *TraceView; - RandomSeed UniqueBufferNameSeed = {}; Array EventPlayback; BlockArena Perm; @@ -193,6 +184,7 @@ RegisterVariable(String, OpenCodeExcludePatterns, ""); RegisterVariable(Int, TrimTrailingWhitespace, 1); // PROJECT_MANAGEMENT +// Set at the beginning of the program to current directory RegisterVariable(String, ProjectDirectory, ""); // PLUGIN_REMEDYBG diff --git a/src/text_editor/plugin_basic_commands.cpp b/src/text_editor/plugin_basic_commands.cpp index 13e75b4..ad5b8c0 100644 --- a/src/text_editor/plugin_basic_commands.cpp +++ b/src/text_editor/plugin_basic_commands.cpp @@ -1,30 +1,3 @@ -void MoveCursorByPageSize(Window *window, int direction, bool shift = false) { - Assert(direction == DIR_UP || direction == DIR_DOWN); - BSet set = GetBSet(window); - - Rect2I visible_cells_rect = GetVisibleCells(window); - Int y = GetSize(visible_cells_rect).y - 2; - if (direction == DIR_UP) y = -y; - - For(set.view->carets) { - XY xy = PosToXY(set.buffer, GetFront(it)); - if (direction == DIR_DOWN && xy.line == set.buffer->line_starts.len - 1) { - Range line_range = GetLineRange(set.buffer, xy.line); - xy.col = line_range.max - line_range.min; - } else if (direction == DIR_UP && xy.line == 0) { - xy.col = 0; - } - xy.line += y; - - Int pos = XYToPos(set.buffer, xy); - if (shift) { - it = SetFront(it, pos); - } else { - it = MakeCaret(pos); - } - } -} - void CMD_Redo() { BSet active = GetBSet(ActiveWindowID); RedoEdit(active.buffer, &active.view->carets); diff --git a/src/text_editor/plugin_record_events.cpp b/src/text_editor/plugin_record_events.cpp new file mode 100644 index 0000000..a445ad6 --- /dev/null +++ b/src/text_editor/plugin_record_events.cpp @@ -0,0 +1,67 @@ +#define PLUGIN_RECORD_EVENTS +Buffer *EventBuffer; + +void Serialize(Buffer *buffer, String name, EventKind *kind) { + RawAppendf(buffer, "ev.%S = %s; ", name, EventKindStrings[*kind]); +} + +void Serialize(Buffer *buffer, String name, Int *datum) { + if (*datum == 0) { + return; + } + if (name == "key") { + RawAppendf(buffer, "ev.%S = %s; ", name, SDLKeycodeToName((SDL_Keycode)*datum)); + } else { + RawAppendf(buffer, "ev.%S = %lld; ", name, (long long)*datum); + } +} + +void Serialize(Buffer *buffer, String name, uint32_t *datum) { + Int d = *datum; + Serialize(buffer, name, &d); + *datum = (uint32_t)d; +} + +void Serialize(Buffer *buffer, String name, uint8_t *datum) { + Int d = *datum; + Serialize(buffer, name, &d); + *datum = (uint8_t)d; +} + +void Serialize(Buffer *buffer, String name, int16_t *datum) { + Int d = *datum; + Serialize(buffer, name, &d); + *datum = (int16_t)d; +} + +void Serialize(Buffer *buffer, String name, float *datum) { + if (*datum == 0.f) { + return; + } + RawAppendf(buffer, "ev.%S = %f; ", name, *datum); +} + +void Serialize(Buffer *buffer, String name, char **text) { + String str = *text; + if (str.len == 0) { + return; + } + RawAppendf(buffer, "ev.%S = \"%S\"; ", name, str); +} + +void SerializeBegin(Buffer *buffer) { + RawAppendf(buffer, "{Event ev = {};"); +} + +void SerializeEnd(Buffer *buffer) { + RawAppendf(buffer, "Add(&EventPlayback, ev);}\n"); +} + +// :Event +void Serialize(Buffer *buffer, Event *e) { + SerializeBegin(buffer); +#define X(TYPE, KIND, NAME) Serialize(buffer, #NAME, &e->NAME); + EVENT_FIELDS +#undef X + SerializeEnd(buffer); +} diff --git a/src/text_editor/plugin_record_gc.cpp b/src/text_editor/plugin_record_gc.cpp new file mode 100644 index 0000000..c72efb4 --- /dev/null +++ b/src/text_editor/plugin_record_gc.cpp @@ -0,0 +1,2 @@ +#define PLUGIN_RECORD_GC +Buffer *GCInfoBuffer; \ No newline at end of file diff --git a/src/text_editor/plugin_search_open_buffers.cpp b/src/text_editor/plugin_search_open_buffers.cpp index 25822e7..8f3a80f 100644 --- a/src/text_editor/plugin_search_open_buffers.cpp +++ b/src/text_editor/plugin_search_open_buffers.cpp @@ -1,3 +1,6 @@ +#define PLUGIN_SEARCH_OPEN_BUFFERS +BufferID SearchOpenBuffersBufferID; + struct SearchOpenBuffersParams { String16 needle; BufferID buffer; diff --git a/src/text_editor/plugin_search_open_buffers.h b/src/text_editor/plugin_search_open_buffers.h deleted file mode 100644 index f0ee9c5..0000000 --- a/src/text_editor/plugin_search_open_buffers.h +++ /dev/null @@ -1 +0,0 @@ -#define PLUGIN_SEARCH_OPEN_BUFFERS diff --git a/src/text_editor/commands_window_management.cpp b/src/text_editor/plugin_window_management.cpp similarity index 78% rename from src/text_editor/commands_window_management.cpp rename to src/text_editor/plugin_window_management.cpp index 9e0596c..ee28ac6 100644 --- a/src/text_editor/commands_window_management.cpp +++ b/src/text_editor/plugin_window_management.cpp @@ -56,3 +56,13 @@ void CMD_NewWindow() { void CMD_CloseWindow() { Close(PrimaryWindowID); } RegisterCommand(CMD_CloseWindow, "", "Close the last active primary window"); + +void CMD_GotoNextInList() { + BSet main = GetBSet(PrimaryWindowID); + GotoNextInList(main.window, 1); +} RegisterCommand(CMD_GotoNextInList, "ctrl-e", "For example: when jumping from build panel to build error, a jump point is setup, user can click this button to go over to the next compiler error"); + +void CMD_GotoPrevInList() { + BSet main = GetBSet(PrimaryWindowID); + GotoNextInList(main.window, -1); +} RegisterCommand(CMD_GotoPrevInList, "alt-e", "For example: when jumping from build panel to build error, a jump point is setup, user can click this button to go over to the previous compiler error"); diff --git a/src/text_editor/process.cpp b/src/text_editor/process.cpp index 05bda73..1c2ff2c 100644 --- a/src/text_editor/process.cpp +++ b/src/text_editor/process.cpp @@ -15,7 +15,7 @@ void UpdateProcesses() { Append(view, poll, it.scroll_to_end); } if (!IsValid(&it)) { - ReportDebugf("process %lld exit code = %d", it.id, it.exit_code); + ReportConsolef("process %lld exit code = %d", it.id, it.exit_code); remove_item = true; } } @@ -23,7 +23,7 @@ void UpdateProcesses() { void Exec(ViewID view, bool scroll_to_end, String cmd, String working_dir) { Process process = SpawnProcess(cmd, working_dir, {}, ProcessEnviroment); - ReportDebugf("process %lld start. is_valid = %d cmd = %S working_dir = %S", process.id, process.is_valid, cmd, working_dir); + ReportConsolef("process %lld start. is_valid = %d cmd = %S working_dir = %S", process.id, process.is_valid, cmd, working_dir); process.view_id = view.id; process.scroll_to_end = scroll_to_end; if (process.is_valid) { @@ -43,7 +43,7 @@ struct ExecResult { }; ExecResult ExecAndWait(Allocator allocator, String cmd, String working_dir, String stdin_string = {}) { - ReportDebugf("ExecAndWait cmd = %S working_dir = %S stdin_string = %S", cmd, working_dir, stdin_string); + ReportConsolef("ExecAndWait cmd = %S working_dir = %S stdin_string = %S", cmd, working_dir, stdin_string); Buffer *scratch_buff = CreateScratchBuffer(allocator, 4096 * 4); Process process = SpawnProcess(cmd, working_dir, stdin_string, ProcessEnviroment); diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 0c0004b..8517ca0 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -22,7 +22,6 @@ #include "plugin_status_window.h" #include "plugin_build_window.h" #include "plugin_project_management.h" -#include "plugin_search_open_buffers.h" #if OS_WINDOWS #include "plugin_remedybg.h" #endif @@ -36,12 +35,12 @@ #include "event.cpp" #include "config.cpp" #include "commands.cpp" +#include "commands_clipboard.cpp" #include "scratch.cpp" #include "draw.cpp" #include "test/tests.cpp" -#include "commands_window_management.cpp" -#include "commands_clipboard.cpp" +#include "plugin_window_management.cpp" #include "plugin_directory_navigation.cpp" #include "plugin_search_open_buffers.cpp" #include "plugin_project_management.cpp" @@ -51,6 +50,8 @@ #include "plugin_status_window.cpp" #include "plugin_build_window.cpp" #include "plugin_debug_window.cpp" +#include "plugin_record_gc.cpp" +#include "plugin_record_events.cpp" #if OS_WINDOWS #include "plugin_remedybg.cpp" #endif @@ -322,7 +323,7 @@ void OnCommand(Event event) { } } - if (Ctrl() && Mouse(RIGHT)) { + if (event.ctrl && Mouse(RIGHT)) { Vec2I mouse = MouseVec2I(); BSet active = GetBSet(ActiveWindowID); bool mouse_in_document = AreOverlapping(mouse, active.window->document_rect); @@ -367,7 +368,7 @@ void OnCommand(Event event) { } - if (Ctrl() && Mouse(LEFT)) { + if (event.ctrl && Mouse(LEFT)) { MouseLoadWord(event); } else if (Mouse(LEFT)) { // Uses Alt and shift Vec2I mouse = MouseVec2I(); @@ -382,11 +383,11 @@ void OnCommand(Event event) { DocumentSelected = active.window->id; Int p = ScreenSpaceToBufferPos(active.window, active.view, active.buffer, mouse); - if (Alt()) Insert(&active.view->carets, MakeCaret(p, p), 0); - if (!Alt() && !Shift()) active.view->carets.len = 1; + if (event.alt) Insert(&active.view->carets, MakeCaret(p, p), 0); + if (!event.alt && !event.shift) active.view->carets.len = 1; Caret &caret = active.view->carets[0]; - if (Shift()) { + if (event.shift) { if (p <= caret.range.min) { caret.range.min = p; caret.ifront = 0; @@ -545,10 +546,12 @@ void GarbageCollect() { } } +#ifdef PLUGIN_RECORD_GC RawAppendf(GCInfoBuffer, "View %d %S\n", (int)it->id.id, buffer ? buffer->name : String{"NULL"}); - remove_item = true; +#endif Dealloc(&it->commands); Dealloc(it); + remove_item = true; } IterRemove(Buffers) { @@ -569,7 +572,9 @@ void GarbageCollect() { } } +#ifdef PLUGIN_RECORD_GC RawAppendf(GCInfoBuffer, "Buff %d %S\n", (int)it->id.id, it->name); +#endif remove_item = true; DeallocBuffer(it); } @@ -581,7 +586,9 @@ void GarbageCollect() { } if (it->close) { +#ifdef PLUGIN_RECORD_GC RawAppendf(GCInfoBuffer, "Wind %d %d %d %d %d\n", (int)it->id.id, (int)it->total_rect.min.x, (int)it->total_rect.min.y, (int)it->total_rect.max.x, (int)it->total_rect.max.y); +#endif Dealloc(&it->goto_history); Dealloc(&it->goto_redo); Dealloc(&it->commands); @@ -776,11 +783,13 @@ void MainLoop() { Scratch scratch; FrameID += 1; Array frame_events = GetEventsForFrame(scratch); - Serializer ser = {EventBuffer}; For(frame_events) { - if (it.kind != 1) { - if (!Testing) Serialize(&ser, &it); + +#ifdef PLUGIN_RECORD_EVENTS + if (it.kind != EVENT_UPDATE && !Testing) { + Serialize(EventBuffer, &it); } +#endif if (it.xwindow == 0 || it.ywindow == 0) { int xwindow, ywindow; @@ -944,7 +953,36 @@ int main(int argc, char **argv) if (scale != 1.0f) DPIScale = scale; } - InitBuffers(); + // Init buffers + { + Allocator sys_allocator = GetSystemAllocator(); + Scratch scratch; + Buffer *null_buffer = CreateBuffer(sys_allocator, GetUniqueBufferName(ProjectDirectory, "logs", "")); + null_buffer->special = true; + View *null_view = CreateView(null_buffer->id); + null_view->special = true; + Assert(null_buffer->id == NullBufferID && null_view->id == NullViewID); + +#ifdef PLUGIN_RECORD_GC + GCInfoBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(ProjectDirectory, "gc")); + GCInfoBuffer->special = true; + GCInfoBuffer->no_history = true; +#endif + +#ifdef PLUGIN_RECORD_EVENTS + EventBuffer = CreateBuffer(sys_allocator, GetUniqueBufferName(ProjectDirectory, "events")); + EventBuffer->no_history = true; + EventBuffer->special = true; +#endif + +#ifdef PLUGIN_SEARCH_OPEN_BUFFERS + Buffer *search_project = CreateBuffer(sys_allocator, GetUniqueBufferName(ProjectDirectory, "search_project")); + search_project->no_history = true; + search_project->special = true; + SearchOpenBuffersBufferID = search_project->id; +#endif + } + InitRender(); ReloadFont(PathToFont, (U32)FontSize); CreateWind(); diff --git a/src/text_editor/text_editor.h b/src/text_editor/text_editor.h index 61a0c33..8aff172 100644 --- a/src/text_editor/text_editor.h +++ b/src/text_editor/text_editor.h @@ -306,7 +306,6 @@ BSet GetBSet(WindowID window_id); void Append(View *view, String16 string, bool scroll_to_end_if_cursor_on_last_line); void Append(View *view, String string, bool scroll_to_end_if_cursor_on_last_line); void ReplaceWithoutMovingCarets(Buffer *buffer, Range range, String16 string); -void ReportDebugf(const char *fmt, ...); void ReportConsolef(const char *fmt, ...); void ReportErrorf(const char *fmt, ...); void ReportWarningf(const char *fmt, ...);