Refactoring Add BSet

This commit is contained in:
Krzosa Karol
2024-08-13 09:49:34 +02:00
parent b3e95a705f
commit 7b21cba834
8 changed files with 107 additions and 144 deletions

View File

@@ -81,28 +81,21 @@ void ToggleFullscreen() {
IsInFullscreen = !IsInFullscreen;
}
void CheckpointBeforeGoto(WindowID window_id, ViewID view_id) {
Window *window = GetWindow(window_id);
View *view = GetView(view_id);
void CheckpointBeforeGoto(Window *window, View *view) {
Buffer *buffer = GetBuffer(view->active_buffer);
Add(&window->goto_history, {buffer->id, view->carets[0]});
window->goto_redo.len = 0;
}
void CheckpointBeforeGoto(WindowID window_id) {
Window *window = GetWindow(window_id);
CheckpointBeforeGoto(window_id, window->active_view);
void CheckpointBeforeGoto(Window *window) {
CheckpointBeforeGoto(window, GetView(window->active_view));
}
void GotoBackward(WindowID window_id) {
Window *window = GetWindow(window_id);
void GotoBackward(Window *window) {
BSet set = GetBSet(window);
if (window->goto_history.len <= 0) return;
{
Window *window = GetWindow(window_id);
View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer);
Add(&window->goto_redo, {buffer->id, view->carets[0]});
}
Add(&window->goto_redo, {set.buffer->id, set.view->carets[0]});
GotoCrumb c = Pop(&window->goto_history);
Buffer *buffer = GetBuffer(c.buffer_id);
View *view = WindowOpenBufferView(window, buffer->name);
@@ -110,15 +103,11 @@ void GotoBackward(WindowID window_id) {
UpdateScroll(window, true);
}
void GotoForward(WindowID window_id) {
Window *window = GetWindow(window_id);
void GotoForward(Window *window) {
BSet set = GetBSet(window);
if (window->goto_redo.len <= 0) return;
{
Window *window = GetWindow(window_id);
View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer);
Add(&window->goto_history, {buffer->id, view->carets[0]});
}
Add(&window->goto_history, {set.buffer->id, set.view->carets[0]});
GotoCrumb c = Pop(&window->goto_redo);
Buffer *buffer = GetBuffer(c.buffer_id);
View *view = WindowOpenBufferView(window, buffer->name);
@@ -163,10 +152,8 @@ void MouseExecWord(Event event) {
}
void Command_ListBuffers() {
Window *window = GetActiveMainWindow();
View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer);
CheckpointBeforeGoto(window->id);
BSet main = GetActiveMainSet();
CheckpointBeforeGoto(main.window);
Scratch scratch;
Array<String> strings = {scratch};
@@ -177,8 +164,8 @@ void Command_ListBuffers() {
String result = Merge(scratch, strings, "\n");
String16 string16 = ToString16(scratch, result);
String buffer_name = GetUniqueBufferName(scratch, GetDir(buffer), "+list_buffers-");
View *new_view = WindowOpenBufferView(window, buffer_name);
String buffer_name = GetUniqueBufferName(scratch, GetDir(main.buffer), "+list_buffers-");
View *new_view = WindowOpenBufferView(main.window, buffer_name);
Command_SelectEntireBuffer(new_view);
Command_Replace(new_view, string16);
@@ -335,10 +322,10 @@ bool GlobalCommand(Event event) {
}
if (event.ctrl && event.shift && Mouse(RIGHT)) {
GotoForward(GetActiveMainWindowID());
GotoForward(GetActiveMainSet().window);
} else if (event.alt && event.ctrl && Mouse(RIGHT)) {
} else if (event.ctrl && Mouse(RIGHT)) {
GotoBackward(GetActiveMainWindowID());
GotoBackward(GetActiveMainSet().window);
} else if (event.alt && Mouse(RIGHT)) {
} else if (Mouse(RIGHT)) {
Vec2I mouse = MouseVec2I();
@@ -555,11 +542,8 @@ void ReportWarningf(const char *fmt, ...) {
STRING_FORMAT(scratch, fmt, string);
Command_Append(NullViewID, string, true);
Window *window = GetWindowWithView(NullViewID);
if (!window) {
WindowID last_active_window_id = GetActiveMainWindowID();
window = GetWindow(last_active_window_id);
}
CheckpointBeforeGoto(window->id);
if (!window) window = GetActiveMainSet().window;
CheckpointBeforeGoto(window);
window->active_view = NullViewID;
ActiveWindow = window->id;
}

View File

@@ -619,7 +619,7 @@ void Command_Find(View *seek_view, String16 needle, bool forward = true) {
void Command_GotoNextInList(Window *window, Int line_offset = 1) {
Assert(line_offset == 1 || line_offset == -1);
ViewID active_view = window->active_view;
View *active_view = GetView(window->active_view);
View *view_goto = GetView(window->active_goto_list);
window->active_view = view_goto->id;
@@ -639,13 +639,13 @@ void Command_GotoNextInList(Window *window, Int line_offset = 1) {
IF_DEBUG(AssertRanges(view_goto->carets));
if (line.len == 0) continue;
CheckpointBeforeGoto(window->id, active_view);
CheckpointBeforeGoto(window, active_view);
Open(line);
opened = true;
break;
}
if (!opened) window->active_view = active_view;
if (!opened) window->active_view = active_view->id;
}
void Command_FuzzySort(View *view, String16 needle) {
@@ -682,7 +682,7 @@ void WindowCommand(Event event, Window *window, View *view) {
if (Press(SDLK_ESCAPE)) {
if (window->deactivate_on_escape) {
ActiveWindow = GetActiveMainWindowID();
ActiveWindow = GetActiveMainSet().window->id;
} else {
view->carets.len = 1;
}
@@ -838,22 +838,18 @@ void WindowCommand(Event event, Window *window, View *view) {
if (Shift(SDLK_F3)) {
Scratch scratch;
Window *window = GetActiveMainWindow();
View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer);
String16 search_string = ToString16(scratch, window->search_string);
Caret caret = FindPrev(buffer, search_string, view->carets[0]);
view->carets.len = 1;
view->carets[0] = caret;
BSet main = GetActiveMainSet();
String16 search_string = ToString16(scratch, main.window->search_string);
Caret caret = FindPrev(main.buffer, search_string, main.view->carets[0]);
main.view->carets.len = 1;
main.view->carets[0] = caret;
} else if (Press(SDLK_F3)) {
Scratch scratch;
Window *window = GetActiveMainWindow();
View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer);
String16 search_string = ToString16(scratch, window->search_string);
Caret caret = FindNext(buffer, search_string, view->carets[0]);
view->carets.len = 1;
view->carets[0] = caret;
BSet main = GetActiveMainSet();
String16 search_string = ToString16(scratch, main.window->search_string);
Caret caret = FindNext(main.buffer, search_string, main.view->carets[0]);
main.view->carets.len = 1;
main.view->carets[0] = caret;
}
if (Shift(SDLK_F4)) {
@@ -878,11 +874,8 @@ void WindowCommand(Event event, Window *window, View *view) {
}
if (Ctrl(SDLK_PERIOD)) {
Window *window = GetActiveMainWindow();
View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer);
String name = ChopLastSlash(buffer->name);
BSet main = GetActiveMainSet();
String name = ChopLastSlash(main.buffer->name);
Open(name);
}
@@ -912,9 +905,9 @@ void WindowCommand(Event event, Window *window, View *view) {
}
if (CtrlShift(SDLK_W)) {
GotoForward(GetActiveMainWindowID());
GotoForward(GetActiveMainSet().window);
} else if (Ctrl(SDLK_W)) {
GotoBackward(GetActiveMainWindowID());
GotoBackward(GetActiveMainSet().window);
}
if (CtrlShift(SDLK_Q)) {

View File

@@ -29,15 +29,18 @@ String FieldString(lua_State *L, String name) {
}
void ExecInNewBuffer(String cmd, String working_dir) {
BSet main = GetActiveMainSet();
CheckpointBeforeGoto(main.window);
Scratch scratch;
CheckpointBeforeGoto(GetActiveMainWindowID());
String buffer_name = GetUniqueBufferName(scratch, working_dir, "+cmd-");
Window *window = GetActiveMainWindow();
View *view = WindowOpenBufferView(window, buffer_name);
Buffer *buffer = GetBuffer(view->active_buffer);
view->carets[0] = MakeCaret({});
View *view = WindowOpenBufferView(main.window, buffer_name);
Buffer *buffer = GetBuffer(view->active_buffer);
view->carets[0] = MakeCaret({});
Exec(view->id, false, cmd, working_dir);
ActiveWindow = window->id;
ActiveWindow = main.window->id;
}
void Open(String path) {
@@ -59,17 +62,16 @@ void Open(String path) {
String col_string = FieldString(LuaState, "col");
Int col = strtoll(col_string.data, NULL, 10);
WindowID window_id = GetActiveMainWindowID();
CheckpointBeforeGoto(window_id);
Window *window = GetWindow(window_id);
View *view = WindowOpenBufferView(window, file_path);
BSet main = GetActiveMainSet();
CheckpointBeforeGoto(main.window);
View *view = WindowOpenBufferView(main.window, file_path);
Buffer *buffer = GetBuffer(view->active_buffer);
if (line != -1 && col != -1) {
Int pos = XYToPos(*buffer, {col - 1, line - 1});
view->carets[0] = MakeCaret(pos);
}
UpdateScroll(window, true);
ActiveWindow = window->id;
UpdateScroll(main.window, true);
ActiveWindow = main.window->id;
} else if (FieldString(LuaState, "kind") == "exec") {
String cmd = FieldString(LuaState, "cmd");
String working_dir = FieldString(LuaState, "working_dir");
@@ -91,9 +93,8 @@ int Lua_FuzzySort(lua_State *L) {
Scratch scratch;
String16 string16 = ToString16(scratch, string);
Window *window = GetActiveMainWindow();
View *view = GetView(window->active_view);
Command_FuzzySort(view, string16);
BSet main = GetActiveMainSet();
Command_FuzzySort(main.view, string16);
return 0;
}
@@ -115,8 +116,8 @@ int Lua_NewCmd(lua_State *L) {
}
int Lua_Kill(lua_State *L) {
Window *window = GetActiveMainWindow();
KillProcess(window->active_view);
BSet main = GetActiveMainSet();
KillProcess(main.window->active_view);
return 0;
}
@@ -181,32 +182,37 @@ int Lua_GetActiveMainWindowBufferDir(lua_State *L) {
}
int Lua_OpenFilesHere(lua_State *L) {
Window *window = GetActiveMainWindow();
BSet main = GetActiveMainSet();
Scratch scratch;
for (FileIter it = IterateFiles(scratch, GetActiveMainWindowBufferDir()); IsValid(it); Advance(&it)) {
WindowOpenBufferView(window, it.absolute_path);
WindowOpenBufferView(main.window, it.absolute_path);
}
return 0;
}
int Lua_Search(lua_State *L) {
Window *seek_window = GetActiveMainWindow();
seek_window->search_string = lua_tostring(L, 1);
BSet main = GetActiveMainSet();
main.window->search_string = lua_tostring(L, 1);
lua_pop(L, 1);
View *seek_view = GetView(seek_window->active_view);
Scratch scratch;
Command_Find(seek_view, ToString16(scratch, seek_window->search_string), true);
Command_Find(main.view, ToString16(scratch, main.window->search_string), true);
return 0;
}
int Lua_SearchB(lua_State *L) {
Window *seek_window = GetActiveMainWindow();
seek_window->search_string = lua_tostring(L, 1);
BSet main = GetActiveMainSet();
main.window->search_string = lua_tostring(L, 1);
lua_pop(L, 1);
View *seek_view = GetView(seek_window->active_view);
Scratch scratch;
Command_Find(seek_view, ToString16(scratch, seek_window->search_string), false);
Command_Find(main.view, ToString16(scratch, main.window->search_string), false);
return 0;
}
int Lua_Rename(lua_State *L) {
String string = lua_tostring(L, 1);
lua_pop(L, 1);
return 0;
}

View File

@@ -14,5 +14,6 @@ luaL_Reg LuaFunctions[] = {
{"OpenFilesHere", Lua_OpenFilesHere},
{"Search", Lua_Search},
{"SearchB", Lua_SearchB},
{"Rename", Lua_Rename},
{NULL, NULL},
};

View File

@@ -77,8 +77,6 @@ inline View *GetView(ViewID id) {
}
inline bool IsNull(Buffer *buffer) { return buffer->id.id == NullBufferID.id; }
inline bool IsActive(Window *window) { return window->id.id == ActiveWindow.id; }
inline bool IsActive(Window *window, View *view) { return window->active_view.id == view->id.id; }
inline Window *GetActiveWindow() { return GetWindow(ActiveWindow); }
inline View *GetActiveView(Window *window) { return GetView(window->active_view); }
@@ -174,16 +172,35 @@ Window *GetTitlebarWindow(WindowID id) {
return window;
}
Window *GetActiveMainWindow() {
Window *window = GetWindow(ActiveWindow);
if (window->is_title_bar) {
window = GetWindow(window->title_bar_window);
}
return window;
BSet GetBSet(Window *window) {
BSet set = {window};
set.view = GetView(set.window->active_view);
set.buffer = GetBuffer(set.view->active_buffer);
return set;
}
WindowID GetActiveMainWindowID() {
return GetActiveMainWindow()->id;
BSet GetTitleSet(Window *window) {
if (!window->is_title_bar) window = GetWindow(window->title_bar_window);
BSet result = GetBSet(window);
return result;
}
BSet GetMainSet(Window *window) {
if (window->is_title_bar) window = GetWindow(window->title_bar_window);
BSet result = GetBSet(window);
return result;
}
BSet GetActiveMainSet() {
Window *window = GetWindow(ActiveWindow);
if (window->is_title_bar) window = GetWindow(window->title_bar_window);
return GetBSet(window);
}
BSet GetActiveTitleSet() {
Window *window = GetWindow(ActiveWindow);
if (window->is_title_bar) window = GetWindow(window->title_bar_window);
return GetBSet(window);
}
RandomSeed UniqueBufferNameSeed = {13};
@@ -194,10 +211,8 @@ String GetUniqueBufferName(Allocator allocator, String working_dir, String p
}
String GetActiveMainWindowBufferName() {
Window *window = GetActiveMainWindow();
View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer);
return buffer->name;
BSet set = GetActiveMainSet();
return set.buffer->name;
}
String GetDir(Buffer *buffer) {
@@ -206,10 +221,8 @@ String GetDir(Buffer *buffer) {
}
String GetActiveMainWindowBufferDir() {
Window *window = GetActiveMainWindow();
View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer);
String name = buffer->is_directory ? buffer->name : ChopLastSlash(buffer->name);
BSet set = GetActiveMainSet();
String name = set.buffer->is_directory ? set.buffer->name : ChopLastSlash(set.buffer->name);
return name;
}

View File

@@ -100,7 +100,7 @@ struct Scroller {
Int line_count;
};
struct ActiveSet {
struct BSet {
Window *window;
View *view;
Buffer *buffer;

View File

@@ -1,29 +1,3 @@
String DebugViewList(Allocator allocator) {
Scratch scratch((Arena *)allocator.object);
Array<String> strings = {scratch};
For(Views) {
View *view = it.o;
Buffer *buffer = GetBuffer(view->active_buffer);
String string = Format(scratch, "view = %lld buffer = %lld name = %.*s", (long long)view->id.id, (long long)buffer->id.id, FmtString(buffer->name));
Add(&strings, string);
}
String result = Merge(allocator, strings, "\n");
return result;
}
String DebugWindowList(Allocator allocator) {
Scratch scratch((Arena *)allocator.object);
Array<String> strings = {scratch};
For(Windows) {
View *view = GetActiveView(it.o);
Buffer *buffer = GetBuffer(view->active_buffer);
String string = Format(scratch, "window = %lld active_view = %lld buffer_name = %.*s", (long long)it.id, (long long)view->id.id, FmtString(buffer->name));
Add(&strings, string);
}
String result = Merge(allocator, strings, "\n");
return result;
}
void UpdateDebugBuffer() {
Buffer *buffer = GetBuffer(DebugBufferID);
@@ -31,12 +5,10 @@ void UpdateDebugBuffer() {
View *view = GetActiveView(window);
if (view->active_buffer.id == buffer->id.id) return;
Window *last_window = GetActiveMainWindow();
View *last_view = GetActiveView(last_window);
Buffer *last_buffer = GetBuffer(last_view->active_buffer);
BSet main = GetActiveMainSet();
Scratch scratch;
String s = Format(scratch, "wid: %d\nvid: %d\nbid: %d\nframe: %lld\n", (int)last_window->id.id, (int)last_view->id.id, (int)last_buffer->id.id, (long long)FrameID);
String s = Format(scratch, "wid: %d\nvid: %d\nbid: %d\nframe: %lld\n", (int)main.window->id.id, (int)main.view->id.id, (int)main.buffer->id.id, (long long)FrameID);
String16 string = ToString16(scratch, s);
IKnowWhatImDoing_ReplaceText(buffer, GetRange(*buffer), string);
@@ -48,13 +20,6 @@ void UpdateDebugBuffer() {
IKnowWhatImDoing_Appendf(buffer, "working dir: %.*s\n", FmtString(WorkingDir));
IKnowWhatImDoing_Appendf(buffer, "exe dir: %.*s\n", FmtString(ExeDir));
IKnowWhatImDoing_Appendf(buffer, "config dir: %.*s\n", FmtString(ConfigDir));
// String view_list = DebugViewList(scratch);
// Append(buffer, ToString16(scratch, view_list));
// Append(buffer, L"\n");
// String window_list = DebugWindowList(scratch);
// Append(buffer, ToString16(scratch, window_list));
}
void ReplaceTitleBarData(Window *window) {

View File

@@ -109,7 +109,8 @@ void DrawWindow(Window *window, Event &event) {
View *view = GetActiveView(window);
Buffer *buffer = GetBuffer(view->active_buffer);
SetScissor(GetScreenRectF());
bool is_active = IsActive(window) || window->id.id == GetActiveMainWindowID().id;
bool is_active = window->id == ActiveWindow || window->title_bar_window == ActiveWindow;
Color color_whitespace_during_selection = ColorWhitespaceDuringSelection;
Color color_background = ColorBackground;