Refactor Command_s

This commit is contained in:
Krzosa Karol
2025-12-08 09:23:07 +01:00
parent 9d29a1c187
commit b600361278
7 changed files with 61 additions and 66 deletions

View File

@@ -140,8 +140,8 @@ void PlayTestOpen(mco_coro *co) {
} }
void Test(mco_coro *co) { void Test(mco_coro *co) {
Wait(co); // First phase starts immediately but stuff is not initialized so Command_Open acts weird Wait(co); // First phase starts immediately but stuff is not initialized so Open acts weird
Command_Open(TestDir); Open(TestDir);
PlayTestOpen(co); PlayTestOpen(co);
Release(&TestArena); Release(&TestArena);

View File

@@ -112,7 +112,7 @@ void MouseLoadWord(Event event, String meta = "") {
active.view->carets.len = 1; active.view->carets.len = 1;
active.view->carets[0] = MakeCaret(p); active.view->carets[0] = MakeCaret(p);
Command_Open(string, meta); Open(string, meta);
} }
} }
} }
@@ -480,7 +480,7 @@ void Replace(View *view, String16 string) {
ReplaceEx(scratch, view, string); ReplaceEx(scratch, view, string);
} }
void Command_DuplicateLine(View *view, int direction) { void DuplicateLine(View *view, int direction) {
Assert(direction == DIR_UP || direction == DIR_DOWN); Assert(direction == DIR_UP || direction == DIR_DOWN);
Scratch scratch; Scratch scratch;
@@ -546,7 +546,7 @@ Array<Range> GetSelectedLinesSorted(Allocator allocator, View *view) {
return result; return result;
} }
void Command_IndentSelectedLines(View *view, bool shift = false) { void IndentSelectedLines(View *view, bool shift = false) {
Scratch scratch; Scratch scratch;
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
@@ -703,7 +703,7 @@ void SaveBuffer(Buffer *buffer) {
} }
void Command_Save() { void Command_Save() {
BSet active = GetBSet(ActiveWindowID); BSet active = GetBSet(LastActiveLayoutWindowID);
SaveBuffer(active.buffer); SaveBuffer(active.buffer);
} }
@@ -725,7 +725,7 @@ int Lua_SaveAll(lua_State *L) {
return 0; return 0;
} }
void Command_KillSelectedLines(View *view) { void KillSelectedLines(View *view) {
Scratch scratch; Scratch scratch;
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
SaveCaretHistoryBeforeBeginEdit(buffer, view->carets); SaveCaretHistoryBeforeBeginEdit(buffer, view->carets);
@@ -765,7 +765,7 @@ void EncloseSpace(View *view) {
} }
} }
void Command_Delete(View *view, int direction, bool ctrl = false) { void Delete(View *view, int direction, bool ctrl = false) {
Assert(direction == DIR_LEFT || direction == DIR_RIGHT); Assert(direction == DIR_LEFT || direction == DIR_RIGHT);
Scratch scratch; Scratch scratch;
@@ -801,7 +801,7 @@ void Command_Delete(View *view, int direction, bool ctrl = false) {
EndEdit(buffer, &edits, &view->carets, true); EndEdit(buffer, &edits, &view->carets, true);
} }
void Command_SelectAll(View *view, String16 needle) { void SelectAll(View *view, String16 needle) {
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
String16 string_buffer = GetString(buffer); String16 string_buffer = GetString(buffer);
@@ -818,7 +818,7 @@ void Command_SelectAll(View *view, String16 needle) {
MergeCarets(buffer, &view->carets); MergeCarets(buffer, &view->carets);
} }
void Command_CreateCursorVertical(View *view, int direction) { void CreateCursorVertical(View *view, int direction) {
Assert(direction == DIR_UP || direction == DIR_DOWN); Assert(direction == DIR_UP || direction == DIR_DOWN);
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
@@ -908,7 +908,7 @@ void IdentedNewLine(View *view) {
EndEdit(buffer, &edits, &view->carets, KILL_SELECTION); EndEdit(buffer, &edits, &view->carets, KILL_SELECTION);
} }
void Command_Find(View *seek_view, String16 needle, bool forward = true) { void Find(View *seek_view, String16 needle, bool forward = true) {
Buffer *seek_buffer = GetBuffer(seek_view->active_buffer); Buffer *seek_buffer = GetBuffer(seek_view->active_buffer);
Caret caret = seek_view->carets[0]; Caret caret = seek_view->carets[0];
if (forward) caret = FindNext(seek_buffer, needle, caret); if (forward) caret = FindNext(seek_buffer, needle, caret);
@@ -918,7 +918,7 @@ void Command_Find(View *seek_view, String16 needle, bool forward = true) {
IF_DEBUG(AssertRanges(seek_view->carets)); IF_DEBUG(AssertRanges(seek_view->carets));
} }
void Command_GotoNextInList(Window *window, Int line_offset = 1) { void GotoNextInList(Window *window, Int line_offset = 1) {
Assert(line_offset == 1 || line_offset == -1); Assert(line_offset == 1 || line_offset == -1);
View *active_view = GetView(window->active_view); View *active_view = GetView(window->active_view);
@@ -943,7 +943,7 @@ void Command_GotoNextInList(Window *window, Int line_offset = 1) {
continue; continue;
} }
BSet set = Command_Open(line, "goto_build"); BSet set = Open(line, "goto_build");
if (set.window == NULL) { if (set.window == NULL) {
continue; continue;
} }
@@ -955,7 +955,7 @@ void Command_GotoNextInList(Window *window, Int line_offset = 1) {
if (!opened) window->active_view = active_view->id; if (!opened) window->active_view = active_view->id;
} }
void Command_FuzzySort(View *view, String16 needle) { void FuzzySortView(View *view, String16 needle) {
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
Scratch scratch; Scratch scratch;
@@ -1044,7 +1044,7 @@ void NewDir(Window *window, String name = "") {
} }
MakeDir(name); MakeDir(name);
Command_Open(name); Open(name);
} }
int Lua_NewDir(lua_State *L) { int Lua_NewDir(lua_State *L) {
@@ -1095,32 +1095,27 @@ void ListFilesRecursive(Buffer *buffer, String dir) {
} }
} }
void Command_ListCode(String dir = WorkDir) { void Command_ListCode() {
BSet main = GetBSet(LastActiveLayoutWindowID); BSet main = GetBSet(LastActiveLayoutWindowID);
JumpGarbageBuffer(&main); JumpGarbageBuffer(&main);
ListFilesRecursive(main.buffer, dir); ListFilesRecursive(main.buffer, WorkDir);
main.view->fuzzy_search = true; main.view->fuzzy_search = true;
main.view->update_scroll = true; main.view->update_scroll = true;
SelectRange(main.view, GetBufferEndAsRange(main.buffer)); SelectRange(main.view, GetBufferEndAsRange(main.buffer));
} }
int Lua_ListCode(lua_State *L) { int Lua_ListCode(lua_State *L) {
String string = lua_tostring(L, 1); Command_ListCode();
lua_pop(L, 1);
if (string.len == 0) {
string = WorkDir;
}
Command_ListCode(string);
return 0; return 0;
} }
View *Command_ExecHidden(String buffer_name, String cmd, String working_dir) { View *ExecHidden(String buffer_name, String cmd, String working_dir) {
View *view = OpenBufferView(buffer_name); View *view = OpenBufferView(buffer_name);
Exec(view->id, true, cmd, working_dir); Exec(view->id, true, cmd, working_dir);
return view; return view;
} }
BSet Command_Exec(String cmd, String working_dir, bool set_active = true) { BSet Exec(String cmd, String working_dir, bool set_active = true) {
BSet main = GetBSet(LastActiveLayoutWindowID); BSet main = GetBSet(LastActiveLayoutWindowID);
if (set_active) { if (set_active) {
NextActiveWindowID = main.window->id; NextActiveWindowID = main.window->id;
@@ -1133,11 +1128,11 @@ BSet Command_Exec(String cmd, String working_dir, bool set_active = true) {
int Lua_C(lua_State *L) { int Lua_C(lua_State *L) {
String string = lua_tostring(L, 1); String string = lua_tostring(L, 1);
lua_pop(L, 1); lua_pop(L, 1);
Command_Exec(string, GetMainDir()); Exec(string, GetMainDir());
return 0; return 0;
} }
BSet Command_Open(Window *window, String path, String meta, bool set_active = true) { BSet Open(Window *window, String path, String meta, bool set_active = true) {
Scratch scratch; Scratch scratch;
BSet set = GetBSet(window); BSet set = GetBSet(window);
path = Trim(path); path = Trim(path);
@@ -1182,23 +1177,23 @@ BSet Command_Open(Window *window, String path, String meta, bool set_active = tr
return set; return set;
} }
BSet Command_Open(String path, String meta) { BSet Open(String path, String meta) {
BSet main = GetBSet(LastActiveLayoutWindowID); BSet main = GetBSet(LastActiveLayoutWindowID);
main = Command_Open(main.window, path, meta); main = Open(main.window, path, meta);
return main; return main;
} }
BSet Command_Open(String16 path, String meta) { BSet Open(String16 path, String meta) {
Scratch scratch; Scratch scratch;
String string = ToString(scratch, path); String string = ToString(scratch, path);
return Command_Open(string, meta); return Open(string, meta);
} }
int Lua_Open(lua_State *L) { int Lua_Open(lua_State *L) {
Scratch scratch; Scratch scratch;
String path = luaL_checkstring(L, 1); String path = luaL_checkstring(L, 1);
lua_pop(L, 1); lua_pop(L, 1);
Command_Open(path); Open(path);
return 0; return 0;
} }
@@ -1291,7 +1286,7 @@ int Lua_ListViews(lua_State *L) {
return 0; return 0;
} }
void Command_Eval(String string) { void Eval(String string) {
if (luaL_dostring(LuaState, string.data) != LUA_OK) { if (luaL_dostring(LuaState, string.data) != LUA_OK) {
const char *error_message = lua_tostring(LuaState, -1); const char *error_message = lua_tostring(LuaState, -1);
ReportWarningf("Execution error! %s", error_message); ReportWarningf("Execution error! %s", error_message);
@@ -1299,15 +1294,15 @@ void Command_Eval(String string) {
} }
} }
void Command_Eval(String16 string) { void Eval(String16 string) {
Scratch scratch; Scratch scratch;
Command_Eval(ToString(scratch, string)); Eval(ToString(scratch, string));
} }
int Lua_Eval(lua_State *L) { int Lua_Eval(lua_State *L) {
String string = lua_tostring(L, 1); String string = lua_tostring(L, 1);
lua_pop(L, 1); lua_pop(L, 1);
Command_Eval(string); Eval(string);
return 0; return 0;
} }

View File

@@ -59,13 +59,13 @@ void FuzzySearchOpen(BSet active) {
Int idx = 0; Int idx = 0;
if (Seek(string, u"||", &idx)) { if (Seek(string, u"||", &idx)) {
string = Skip(string, idx + 3); string = Skip(string, idx + 3);
Command_Open(string); Open(string);
success = true; success = true;
} }
} }
if (!success) { if (!success) {
Command_Open(FetchLoadWord()); Open(FetchLoadWord());
} }
} }
@@ -362,9 +362,9 @@ void OnCommand(Event event) {
} }
if (CtrlAltPress(SDLK_DOWN)) { if (CtrlAltPress(SDLK_DOWN)) {
Command_DuplicateLine(active.view, DIR_DOWN); DuplicateLine(active.view, DIR_DOWN);
} else if (AltShiftPress(SDLK_DOWN)) { } else if (AltShiftPress(SDLK_DOWN)) {
Command_CreateCursorVertical(active.view, DIR_DOWN); CreateCursorVertical(active.view, DIR_DOWN);
} else if (CtrlShiftPress(SDLK_DOWN)) { } else if (CtrlShiftPress(SDLK_DOWN)) {
MoveCarets(active.view, DIR_DOWN, CTRL_PRESSED, SHIFT_PRESS); MoveCarets(active.view, DIR_DOWN, CTRL_PRESSED, SHIFT_PRESS);
} else if (AltPress(SDLK_DOWN)) { } else if (AltPress(SDLK_DOWN)) {
@@ -378,9 +378,9 @@ void OnCommand(Event event) {
} }
if (CtrlAltPress(SDLK_UP)) { if (CtrlAltPress(SDLK_UP)) {
Command_DuplicateLine(active.view, DIR_UP); DuplicateLine(active.view, DIR_UP);
} else if (AltShiftPress(SDLK_UP)) { } else if (AltShiftPress(SDLK_UP)) {
Command_CreateCursorVertical(active.view, DIR_UP); CreateCursorVertical(active.view, DIR_UP);
} else if (CtrlShiftPress(SDLK_UP)) { } else if (CtrlShiftPress(SDLK_UP)) {
MoveCarets(active.view, DIR_UP, CTRL_PRESSED, SHIFT_PRESS); MoveCarets(active.view, DIR_UP, CTRL_PRESSED, SHIFT_PRESS);
} else if (AltPress(SDLK_UP)) { } else if (AltPress(SDLK_UP)) {
@@ -424,12 +424,12 @@ void OnCommand(Event event) {
} }
if (CtrlPress(SDLK_C)) { if (CtrlPress(SDLK_C)) {
Command_Copy(active.view); ClipboardCopy(active.view);
} else if (CtrlPress(SDLK_V)) { } else if (CtrlPress(SDLK_V)) {
Command_Paste(active.view); ClipboardPaste(active.view);
} else if (CtrlPress(SDLK_X)) { } else if (CtrlPress(SDLK_X)) {
SaveCaretHistoryBeforeBeginEdit(active.buffer, active.view->carets); SaveCaretHistoryBeforeBeginEdit(active.buffer, active.view->carets);
Command_Copy(active.view); ClipboardCopy(active.view);
Replace(active.view, u""); Replace(active.view, u"");
} }
@@ -478,33 +478,33 @@ void OnCommand(Event event) {
if (CtrlShiftPress(SDLK_TAB)) { if (CtrlShiftPress(SDLK_TAB)) {
} else if (ShiftPress(SDLK_TAB)) { } else if (ShiftPress(SDLK_TAB)) {
Command_IndentSelectedLines(active.view, SHIFT_PRESS); IndentSelectedLines(active.view, SHIFT_PRESS);
} else if (CtrlPress(SDLK_TAB)) { } else if (CtrlPress(SDLK_TAB)) {
} else if (Press(SDLK_TAB)) { } else if (Press(SDLK_TAB)) {
Command_IndentSelectedLines(active.view); IndentSelectedLines(active.view);
} }
if (CtrlPress(SDLK_LEFTBRACKET)) { if (CtrlPress(SDLK_LEFTBRACKET)) {
Command_IndentSelectedLines(active.view, SHIFT_PRESS); IndentSelectedLines(active.view, SHIFT_PRESS);
} }
if (CtrlPress(SDLK_RIGHTBRACKET)) { if (CtrlPress(SDLK_RIGHTBRACKET)) {
Command_IndentSelectedLines(active.view); IndentSelectedLines(active.view);
} }
if (CtrlShiftPress(SDLK_K)) { if (CtrlShiftPress(SDLK_K)) {
Command_KillSelectedLines(active.view); KillSelectedLines(active.view);
} }
if (CtrlPress(SDLK_BACKSPACE)) { if (CtrlPress(SDLK_BACKSPACE)) {
Command_Delete(active.view, DIR_LEFT, CTRL_PRESSED); Delete(active.view, DIR_LEFT, CTRL_PRESSED);
} else if (Press(SDLK_BACKSPACE)) { } else if (Press(SDLK_BACKSPACE)) {
Command_Delete(active.view, DIR_LEFT); Delete(active.view, DIR_LEFT);
} }
if (CtrlPress(SDLK_DELETE)) { if (CtrlPress(SDLK_DELETE)) {
Command_Delete(active.view, DIR_RIGHT, CTRL_PRESSED); Delete(active.view, DIR_RIGHT, CTRL_PRESSED);
} else if (Press(SDLK_DELETE)) { } else if (Press(SDLK_DELETE)) {
Command_Delete(active.view, DIR_RIGHT); Delete(active.view, DIR_RIGHT);
} }
if (event.kind == EVENT_TEXT_INPUT) { if (event.kind == EVENT_TEXT_INPUT) {
@@ -589,7 +589,7 @@ void OnCommand(Event event) {
if (EndsWith(main.buffer->name, "dirlisting")) { if (EndsWith(main.buffer->name, "dirlisting")) {
name = ChopLastSlash(name); name = ChopLastSlash(name);
} }
Command_Open(name); Open(name);
} }
if (CtrlShiftPress(SDLK_L)) { if (CtrlShiftPress(SDLK_L)) {
@@ -612,7 +612,7 @@ void OnCommand(Event event) {
if (active.view->fuzzy_search) { if (active.view->fuzzy_search) {
FuzzySearchOpen(active); FuzzySearchOpen(active);
} else { } else {
Command_Open(FetchLoadWord()); Open(FetchLoadWord());
} }
} }

View File

@@ -33,7 +33,7 @@ void SaveStringInClipboard(String16 string) {
_SetClipboardText(ToString(scratch, SavedClipboardString).data); _SetClipboardText(ToString(scratch, SavedClipboardString).data);
} }
void Command_Copy(View *view) { void ClipboardCopy(View *view) {
Scratch scratch; Scratch scratch;
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
@@ -59,7 +59,7 @@ void Command_Copy(View *view) {
_SetClipboardText(ToString(scratch, SavedClipboardString).data); _SetClipboardText(ToString(scratch, SavedClipboardString).data);
} }
void Command_Paste(View *view) { void ClipboardPaste(View *view) {
Scratch scratch; Scratch scratch;
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
const char *text = GetClipboardText(); const char *text = GetClipboardText();

View File

@@ -253,7 +253,7 @@ BSet GetConsoleSet() {
return result; return result;
} }
String Command_GetFilename() { String GetCurrentFilename() {
BSet main = GetBSet(LastActiveLayoutWindowID); BSet main = GetBSet(LastActiveLayoutWindowID);
return main.buffer->name; return main.buffer->name;
} }

View File

@@ -299,7 +299,7 @@ void Windows_SetupVCVarsall(mco_coro *co) {
String working_dir = WorkDir; String working_dir = WorkDir;
String buffer_name = GetUniqueBufferName(working_dir, "vcvarsall-"); String buffer_name = GetUniqueBufferName(working_dir, "vcvarsall-");
String cmd = Format(scratch, "\"%S\" && set", StyleVCVarsall); String cmd = Format(scratch, "\"%S\" && set", StyleVCVarsall);
view = Command_ExecHidden(buffer_name, cmd, working_dir); view = ExecHidden(buffer_name, cmd, working_dir);
} }
for (;;) { for (;;) {
if (!ProcessIsActive(view->id)) { if (!ProcessIsActive(view->id)) {

View File

@@ -84,8 +84,8 @@ float DPIScale = 1.0f;
Rect2I GetVisibleCells(Window *window); Rect2I GetVisibleCells(Window *window);
void AfterEdit(View *view, Array<Edit> edits); void AfterEdit(View *view, Array<Edit> edits);
Scroller ComputeScrollerRect(Window *window); Scroller ComputeScrollerRect(Window *window);
BSet Command_Open(String path, String meta = ""); BSet Open(String path, String meta = "");
BSet Command_Open(String16 path, String meta = ""); BSet Open(String16 path, String meta = "");
void UpdateScroll(Window *window, bool update_caret_scrolling); void UpdateScroll(Window *window, bool update_caret_scrolling);
void SelectEntireBuffer(View *view); void SelectEntireBuffer(View *view);
@@ -94,13 +94,13 @@ void SelectRange(View *view, Range range);
void Append(View *view, String16 string, bool scroll_to_end_if_cursor_on_last_line); 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 Append(View *view, String string, bool scroll_to_end_if_cursor_on_last_line);
Array<Edit> ReplaceEx(Allocator scratch, View *view, String16 string); Array<Edit> ReplaceEx(Allocator scratch, View *view, String16 string);
void Command_Eval(String string); void Eval(String string);
void Command_Eval(String16 string); void Eval(String16 string);
void ReportDebugf(const char *fmt, ...); void ReportDebugf(const char *fmt, ...);
void ReplaceWithoutMovingCarets(Buffer *buffer, Range range, String16 string); void ReplaceWithoutMovingCarets(Buffer *buffer, Range range, String16 string);
void Command_Copy(View *view); void ClipboardCopy(View *view);
void Command_Paste(View *view); void ClipboardPaste(View *view);
void ReportConsolef(const char *fmt, ...); void ReportConsolef(const char *fmt, ...);
void ReportErrorf(const char *fmt, ...); void ReportErrorf(const char *fmt, ...);