Continuing refactor

This commit is contained in:
Krzosa Karol
2026-01-15 22:24:05 +01:00
parent 3864060699
commit 61fc0cd339
6 changed files with 0 additions and 2843 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,39 +0,0 @@
void LayoutBuildWindow(HookParam param) {
auto p = param.layout;
Rect2I copy_rect = *p.rect;
if (!p.window->visible) {
p.rect = &copy_rect;
}
Int barsize = p.window->font->line_spacing * 10;
p.window->document_rect = p.window->total_rect = CutBottom(p.rect, barsize);
}
void CMD_ShowBuildWindow(HookParam param) {
BSet main = GetBSet(BuildWindowID);
if (ActiveWindowID != BuildWindowID) {
main.window->visible = true;
NextActiveWindowID = BuildWindowID;
} else {
main.window->visible = false;
}
} RegisterCommand(CMD_ShowBuildWindow, "ctrl-grave");
void InitBuildWindow(HookParam param) {
Window *window = CreateWind();
BuildWindowID = window->id;
Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "build"));
buffer->special = true;
buffer->no_history = true;
BuildBufferID = buffer->id;
View *view = CreateView(buffer->id);
view->special = true;
BuildViewID = view->id;
window->active_view = view->id;
window->secondary_window_style = true;
window->draw_line_highlight = true;
window->primary = false;
window->visible = false;
window->lose_visibility_on_escape = true;
window->jump_history = false;
AddHook(&window->hooks, HookKind_HandleWindowLayout, "LayoutBuildWindow", "", LayoutBuildWindow);
} RegisterHook(InitBuildWindow, HookKind_AppInit, "", "Init the build window");

View File

@@ -1,252 +0,0 @@
void CMD_ShowCommands(HookParam param) {
// @todo: maybe redo this, similar behavior but use View stored information
// if (ActiveWindowID == CommandWindowID && LastExecutedManualCommand == CMD_ShowCommands) {
// NextActiveWindowID = PrimaryWindowID;
// return;
// }
BSet command_bar = GetBSet(CommandWindowID);
command_bar.window->visible = true;
NextActiveWindowID = command_bar.window->id;
ResetBuffer(command_bar.buffer);
For (GlobalHooks) {
if (it.name == "OpenCommand") {
continue;
}
// RawAppendf(command_bar.buffer, "\n:%-30S <|| :Set %-30S '%-30S'", it.name, it.name, it.binding);
RawAppendf(command_bar.buffer, "\n:%-30S <|| ", it.name);
if (it.docs.len) {
RawAppendf(command_bar.buffer, "%S", it.docs);
}
}
command_bar.view->update_scroll = true;
SelectRange(command_bar.view, GetBufferBeginAsRange(command_bar.buffer));
} RegisterCommand(CMD_ShowCommands, "ctrl-shift-p", "List available commands and their documentation inside the command window");
void CMD_ShowDebugBufferList(HookParam param) {
// @todo: maybe redo this, similar behavior but use View stored information
// if (ActiveWindowID == CommandWindowID && LastExecutedManualCommand == CMD_ShowDebugBufferList) {
// NextActiveWindowID = PrimaryWindowID;
// return;
// }
BSet command_bar = GetBSet(CommandWindowID);
command_bar.window->visible = true;
NextActiveWindowID = command_bar.window->id;
ResetBuffer(command_bar.buffer);
For (Buffers) {
bool is_special = it->special || it->temp || it->is_dir || it->dont_try_to_save_in_bulk_ops;
if (!is_special) {
continue;
}
RawAppendf(command_bar.buffer, "\n%S", it->name);
}
command_bar.view->update_scroll = true;
SelectRange(command_bar.view, GetBufferBeginAsRange(command_bar.buffer));
} RegisterCommand(CMD_ShowDebugBufferList, "ctrl-shift-alt-p", "Show full list of buffers, including the special ones that normally just clutter list");
void CMD_ShowBufferList(HookParam param) {
// @todo: maybe redo this, similar behavior but use View stored information
// if (ActiveWindowID == CommandWindowID && LastExecutedManualCommand == CMD_ShowBufferList) {
// NextActiveWindowID = PrimaryWindowID;
// return;
// }
BSet command_bar = GetBSet(CommandWindowID);
command_bar.window->visible = true;
NextActiveWindowID = command_bar.window->id;
ResetBuffer(command_bar.buffer);
For (Buffers) {
bool is_special = it->special || it->temp || it->is_dir || it->dont_try_to_save_in_bulk_ops;
if (is_special) {
continue;
}
RawAppendf(command_bar.buffer, "\n%S", it->name);
}
command_bar.view->update_scroll = true;
SelectRange(command_bar.view, GetBufferBeginAsRange(command_bar.buffer));
} RegisterCommand(CMD_ShowBufferList, "ctrl-p", "List open buffers inside the command window that you can fuzzy search over");
void OpenCommand(BSet active) {
String16 string = FetchFuzzyViewLoadLine(active.view);
Open(string);
}
struct SearchProjectParams {
String16 needle;
BufferID buffer;
};
void Coro_SearchProject(mco_coro *co) {
SearchProjectParams *param = (SearchProjectParams *)CoCurr->user_ctx;
Array<BufferID> buffers = {CoCurr->arena};
For (Buffers) {
Add(&buffers, it->id);
}
ForItem (id, buffers) {
Buffer *it = GetBuffer(id, NULL);
if (it == NULL || it->special || it->is_dir || it->temp || it->dont_try_to_save_in_bulk_ops) {
continue;
}
{
Scratch scratch;
Array<Caret> occurences = FindAll(scratch, it, param->needle);
Buffer *out_buffer = GetBuffer(param->buffer);
ForItem (caret, occurences) {
Int pos = caret.range.min;
Int line = PosToLine(it, pos);
Range range = GetLineRangeWithoutNL(it, line);
Int column = pos - range.min;
String16 line_string = GetString(it, range);
String line_string8 = ToString(scratch, line_string);
RawAppendf(out_buffer, "%S ||> %S:%lld:%lld\n", line_string8, it->name, (long long)line + 1, (long long)column + 1);
}
}
CoYield(co);
}
}
float NewFuzzyRate(String16 s, String16 p) {
float score = 0;
// try to do this: https://github.com/junegunn/fzf/blob/master/src/algo/algo.go
return score;
}
float FuzzyRate(String16 s, String16 p) {
float score = 0;
for (Int outer_pi = 0; outer_pi < p.len; outer_pi += 1) {
String16 pit = Skip(p, outer_pi);
if (IsWhitespace(At(pit, 0))) {
continue;
}
float matching = 0;
for (Int outer_si = 0; outer_si < s.len; outer_si += 1) {
String16 sit = Skip(s, outer_si);
if (IsWhitespace(At(sit, 0))) {
continue;
}
Int si = 0;
Int pi = 0;
for (;si < sit.len && pi < pit.len;) {
while (si < sit.len && IsWhitespace(sit[si])) si += 1;
while (pi < pit.len && IsWhitespace(pit[pi])) pi += 1;
if (pi >= pit.len) break;
if (si >= sit.len) break;
if (ToLowerCase(sit[si]) == ToLowerCase(pit[pi])) {
matching += 1.0f;
} else {
break;
}
si += 1;
pi += 1;
}
}
score += matching;
}
score = score / (float)s.len;
return score;
}
inline bool MergeSortCompare(FuzzyPair *a, FuzzyPair *b) {
bool result = a->rating > b->rating;
return result;
}
Array<FuzzyPair> FuzzySearchLines(Allocator allocator, Buffer *buffer, Int line_min, Int line_max, String16 needle) {
ProfileFunction();
if (line_min < 0 || line_min >= buffer->line_starts.len) return {};
if (line_max < 0 || line_min > buffer->line_starts.len) return {};
Array<FuzzyPair> ratings = {allocator};
Reserve(&ratings, line_max - line_min + 4);
for (Int i = line_min; i < line_max; i += 1) {
String16 s = GetLineStringWithoutNL(buffer, i);
Int idx = 0;
if (Seek(s, u"||>", &idx, SeekFlag_None)) {
s = GetPrefix(s, idx);
} else if (Seek(s, u"<||", &idx, SeekFlag_None)) {
s = GetPrefix(s, idx);
}
s = Trim(s);
float rating = FuzzyRate(s, needle);
Add(&ratings, {(int32_t)i, rating});
}
Array<FuzzyPair> temp = Copy(allocator, ratings);
MergeSort(ratings.len, ratings.data, temp.data);
return ratings;
}
void UpdateFuzzySearchView(HookParam param) {
Scratch scratch;
BSet active = GetBSet(ActiveWindowID);
String16 line_string = GetLineStringWithoutNL(active.buffer, 0);
uint64_t hash = HashBytes(line_string.data, line_string.len * sizeof(char16_t));
if (active.view->prev_search_line_hash != hash) {
active.view->prev_search_line_hash = hash;
Array<FuzzyPair> ratings = FuzzySearchLines(scratch, active.buffer, 1, active.buffer->line_starts.len, line_string);
Buffer *scratch_buff = CreateScratchBuffer(scratch, active.buffer->cap);
RawAppend(scratch_buff, line_string);
For(IterateInReverse(&ratings)) {
String16 s = GetLineStringWithoutNL(active.buffer, it.index);
if (s.len == 0) continue;
RawAppend(scratch_buff, u"\n");
RawAppend(scratch_buff, s);
}
Caret caret = active.view->carets[0];
SaveCaretHistoryBeforeBeginEdit(active.buffer, active.view->carets);
SelectEntireBuffer(active.view);
Replace(active.view, GetString(scratch_buff));
active.view->carets[0] = caret;
}
}
void SetFuzzy(View *view) {
AddCommand(&view->hooks, "Open", "ctrl-q | enter | f12", [](HookParam){
BSet active = GetBSet(ActiveWindowID);
BSet main = GetBSet(PrimaryWindowID);
NextActiveWindowID = main.window->id;
String16 string = FetchFuzzyViewLoadLine(active.view);
Open(string);
});
AddHook(&view->hooks, HookKind_AppUpdate, "UpdateFuzzySearchView", "", UpdateFuzzySearchView);
}
void LayoutCommandWindow(HookParam param) {
auto p = param.layout;
Rect2I copy_rect = *p.rect;
if (!p.window->visible) {
p.rect = &copy_rect;
}
Int barsize = Clamp((Int)p.window->font->line_spacing*10, (Int)0, (Int)p.wx - 100);
p.window->document_rect = p.window->total_rect = CutBottom(p.rect, barsize);
}
void InitCommandWindow(HookParam param) {
Window *window = CreateWind();
CommandWindowID = window->id;
Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "command_bar"));
buffer->special = true;
buffer->no_history = true;
View *view = CreateView(buffer->id);
view->special = true;
SetFuzzy(view);
window->active_view = view->id;
window->draw_line_numbers = false;
window->draw_scrollbar = false;
window->secondary_window_style = true;
window->draw_line_highlight = true;
window->primary = false;
window->visible = false;
window->sync_visibility_with_focus = true;
window->lose_focus_on_escape = true;
window->jump_history = false;
AddHook(&window->hooks, HookKind_HandleWindowLayout, "LayoutCommandWindow", "", LayoutCommandWindow);
} RegisterHook(InitCommandWindow, HookKind_AppInit, "", "Init command window");

View File

@@ -1,87 +0,0 @@
void LayoutDebugWindow(HookParam param) {
auto p = param.layout;
Rect2 screen_rect = Rect0Size((float)p.wx, (float)p.wy);
Vec2 size = GetSize(screen_rect);
Rect2 a = CutRight(&screen_rect, 0.3f * size.x);
Rect2 b = CutTop(&a, 0.4f * size.y);
Rect2 c = Shrink(b, 20);
p.window->document_rect = p.window->total_rect = ToRect2I(c);
}
void UpdateDebugWindow(HookParam param) {
ProfileFunction();
BSet set = GetBSet(DebugWindowID);
if (!set.window->visible) {
return;
}
BSet active = GetBSet(ActiveWindowID);
if (active.buffer->id.id == set.buffer->id.id) {
return;
}
BSet main = GetBSet(PrimaryWindowID);
Scratch scratch;
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);
RawReplaceText(set.buffer, GetRange(set.buffer), string);
float xmouse, ymouse;
SDL_GetMouseState(&xmouse, &ymouse);
RawAppendf(set.buffer, "mouse: [%f, %f]\n", roundf(DPIScale * xmouse), roundf(DPIScale * ymouse));
RawAppendf(set.buffer, "BufferID id = %d\n", main.buffer->id.id);
RawAppendf(set.buffer, "String name = %S\n", main.buffer->name);
RawAppendf(set.buffer, "Int change_id = %lld\n", (long long)main.buffer->change_id);
RawAppendf(set.buffer, "Int user_change_id = %lld\n", (long long)main.buffer->user_change_id);
RawAppendf(set.buffer, "Int file_mod_time = %lld\n", (long long)main.buffer->file_mod_time);
RawAppendf(set.buffer, "\n");
RawAppendf(set.buffer, "U16 *data = %zu\n", main.buffer->data);
RawAppendf(set.buffer, "Int len = %lld\n", (long long)main.buffer->len);
RawAppendf(set.buffer, "Int cap = %lld\n", (long long)main.buffer->cap);
RawAppendf(set.buffer, "Array<Int> line_starts = {len = %lld, cap = %lld, data = %zu}\n", (long long)main.buffer->line_starts.len, (long long)main.buffer->line_starts.cap, main.buffer->line_starts.data);
RawAppendf(set.buffer, "\n");
RawAppendf(set.buffer, "Array<HistoryEntry> undo_stack = {len = %lld, cap = %lld, data = %zu}\n", (long long)main.buffer->undo_stack.len, (long long)main.buffer->undo_stack.cap, main.buffer->undo_stack.data);
RawAppendf(set.buffer, "Array<HistoryEntry> redo_stack = {len = %lld, cap = %lld, data = %zu}\n", (long long)main.buffer->redo_stack.len, (long long)main.buffer->redo_stack.cap, main.buffer->redo_stack.data);
RawAppendf(set.buffer, "int edit_phase = %d", main.buffer->edit_phase);
RawAppendf(set.buffer, "\n");
RawAppendf(set.buffer, "int no_history = %d\n", main.buffer->no_history);
RawAppendf(set.buffer, "int no_line_starts = %d\n", main.buffer->no_line_starts);
RawAppendf(set.buffer, "int dirty = %d\n", main.buffer->dirty);
RawAppendf(set.buffer, "int changed_on_disk = %d\n", main.buffer->changed_on_disk);
RawAppendf(set.buffer, "int temp = %d\n", main.buffer->temp);
} RegisterHook(UpdateDebugWindow, HookKind_AppUpdate, "", "Update the debug window");
void InitDebugWindow(HookParam param) {
Window *window = CreateWind();
DebugWindowID = window->id;
window->draw_line_numbers = false;
window->draw_scrollbar = false;
window->visible = false;
window->z = 2;
window->primary = false;
window->jump_history = false;
Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "debug"));
DebugBufferID = buffer->id;
buffer->no_history = true;
buffer->special = true;
View *view = CreateView(buffer->id);
view->special = true;
DebugViewID = view->id;
window->active_view = view->id;
window->visible = false;
AddHook(&window->hooks, HookKind_HandleWindowLayout, "@todo: LayoutDebugWindow", "", LayoutDebugWindow);
} RegisterHook(InitDebugWindow, HookKind_AppInit, "", "Init the debug window");
void CMD_ToggleDebug(HookParam param) {
Window *window = GetWindow(DebugWindowID);
window->visible = !window->visible;
} RegisterCommand(CMD_ToggleDebug, "ctrl-0", "Open a floating window that might become useful for debugging");

View File

@@ -1,103 +0,0 @@
void CMD_Search(HookParam param) {
BSet main = GetBSet(ActiveWindowID);
String16 string = {};
if (main.view->carets.len == 1 && GetSize(main.view->carets[0]) > 0) {
string = GetString(main.buffer, main.view->carets[0].range);
}
BSet set = GetBSet(SearchWindowID);
set.window->visible = true;
NextActiveWindowID = SearchWindowID;
SelectEntireBuffer(set.view);
if (string.len > 0) {
Replace(set.view, string);
SelectEntireBuffer(set.view);
}
} RegisterCommand(CMD_Search, "ctrl-f", "Open up a search window");
void SearchWindowFindNext(bool forward = true) {
BSet main = GetBSet(PrimaryWindowID);
BSet set = GetBSet(SearchWindowID);
String16 seek = GetString(set.buffer, GetRange(set.buffer));
Find(main.view, seek, forward);
main.window->search_bar_anchor = main.view->carets[0];
CenterView(PrimaryWindowID);
}
void CMD_SearchNextInSearch(HookParam param) {
SearchWindowFindNext(true);
}
void CMD_SearchPrevInSearch(HookParam param) {
SearchWindowFindNext(false);
}
void CMD_SearchNext(HookParam param) {
SearchWindowFindNext(true);
} RegisterCommand(CMD_SearchNext, "f3", "Go to the next occurence of the search window needle");
void CMD_SearchPrev(HookParam param) {
SearchWindowFindNext(false);
} RegisterCommand(CMD_SearchPrev, "shift-f3", "Go to the previous occurence of the search window needle");
void CMD_SearchAll(HookParam param) {
BSet main = GetBSet(PrimaryWindowID);
BSet set = GetBSet(SearchWindowID);
String16 needle = GetString(set.buffer, GetRange(set.buffer));
SelectAllOccurences(main.view, needle);
set.window->visible = false;
} RegisterCommand(CMD_SearchAll, "alt-f3", "Use the search window needle and seek all the possible occurences in current buffer");
void CMD_ToggleCaseSensitiveSearch(HookParam param) {
SearchCaseSensitive = !SearchCaseSensitive;
} RegisterCommand(CMD_ToggleCaseSensitiveSearch, "alt-c", "Text editor wide search toggle, should apply to most search things");
void CMD_ToggleSearchWordBoundary(HookParam param) {
SearchWordBoundary = !SearchWordBoundary;
} RegisterCommand(CMD_ToggleSearchWordBoundary, "alt-w", "Text editor wide search toggle, should apply to most search things");
void UpdateSearchWindow(HookParam param) {
BSet active = GetBSet(ActiveWindowID);
if (active.window->id == SearchWindowID && active.buffer->begin_frame_change_id != active.buffer->change_id) {
BSet main = GetBSet(PrimaryWindowID);
BSet set = GetBSet(SearchWindowID);
main.view->carets[0] = main.window->search_bar_anchor;
String16 seek = GetString(set.buffer, GetRange(set.buffer));
Find(main.view, seek, true);
CenterView(main.window->id);
}
} RegisterHook(UpdateSearchWindow, HookKind_AppUpdate, "", "Update the search window");
void LayoutSearchWindow(HookParam param) {
auto p = param.layout;
Rect2I copy_rect = *p.rect;
if (!p.window->visible) {
p.rect = &copy_rect;
}
Int barsize = GetExpandingBarSize(p.window);
p.window->document_rect = p.window->total_rect = CutBottom(p.rect, barsize);
p.window->line_numbers_rect = CutLeft(&p.window->document_rect, p.window->font->char_spacing * 6);
}
void InitSearchWindow(HookParam param) {
Window *window = CreateWind();
SearchWindowID = window->id;
Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "search"));
buffer->special = true;
SearchBufferID = buffer->id;
View *view = CreateView(buffer->id);
view->special = true;
SearchViewID = view->id;
window->active_view = view->id;
window->draw_line_numbers = false;
window->draw_scrollbar = false;
window->secondary_window_style = true;
window->draw_line_highlight = false;
window->primary = false;
window->visible = false;
window->lose_visibility_on_escape = true;
window->jump_history = false;
AddCommand(&view->hooks, "SearchAll", "alt-enter", CMD_SearchAll);
AddCommand(&view->hooks, "SearchPrevInSearch", "shift-enter", CMD_SearchPrevInSearch);
AddCommand(&view->hooks, "SearchNextInSearch", "enter", CMD_SearchNextInSearch);
AddHook(&window->hooks, HookKind_HandleWindowLayout, "LayoutSearchWindow", "", LayoutSearchWindow);
} RegisterHook(InitSearchWindow, HookKind_AppInit, "", "Init the search window");

View File

@@ -1,99 +0,0 @@
void StatusWindowUpdate(HookParam param) {
ProfileFunction();
Window *status_bar_window = GetWindow(StatusBarWindowID, NULL);
Scratch scratch;
BSet main = GetBSet(PrimaryWindowID);
BSet title = GetBSet(status_bar_window);
title.view->scroll.y = 0;
String16 buffer_string = GetString(title.buffer);
Range replace_range = {0, title.buffer->len};
bool found_separator = Seek(buffer_string, u" |", &replace_range.max);
// Parse the title and line
if (title.window->id == ActiveWindowID) {
if (title.buffer->change_id == title.buffer->begin_frame_change_id) {
return;
}
String16 buffer_name = GetString(title.buffer, replace_range);
buffer_name = Trim(buffer_name);
Int column = ChopNumber(&buffer_name);
if (column == -1) return;
Chop(&buffer_name, u":");
Int line = ChopNumber(&buffer_name);
if (line == -1) {
line = column;
column = 0;
}
Chop(&buffer_name, u":");
Int buffer_pos = XYToPos(main.buffer, {column, line});
Caret &caret = main.view->carets[0];
if (GetFront(caret) != buffer_pos) {
caret = MakeCaret(buffer_pos);
}
return;
}
Caret caret = main.view->carets[0];
XY xy = PosToXY(main.buffer, GetFront(caret));
// add separator at the end of buffer
if (!found_separator) {
SelectRange(title.view, GetBufferEndAsRange(title.buffer));
ReplaceEx(scratch, title.view, u" | :Prev :Next :Close");
}
// replace data up to separator with filename and stuff
const char *reopen = main.buffer->changed_on_disk ? " :Reopen" : "";
const char *case_sens = SearchCaseSensitive ? " C" : "";
const char *word_bound = SearchWordBoundary ? " W" : "";
const char *dirty = main.buffer->dirty ? " !" : "";
String s = Format(scratch, "%S:%lld:%lld%s%s%s", main.buffer->name, (long long)xy.line + 1ll, (long long)xy.col + 1ll, dirty, case_sens, word_bound, reopen);
For (ActiveProcesses) {
if (it.view_id == main.view->id.id) {
s = Format(scratch, "%S %lld :KillProcess", s, (long long)it.id);
}
}
String16 string = ToString16(scratch, s);
String16 string_to_replace = GetString(title.buffer, replace_range);
if (string_to_replace != string) {
SelectRange(title.view, replace_range);
ReplaceEx(scratch, title.view, string);
}
SelectRange(title.view, MakeRange(0));
ResetHistory(title.buffer);
} RegisterHook(StatusWindowUpdate, HookKind_AppUpdate, "", "");
void LayoutStatusWindow(HookParam param) {
auto p = param.layout;
Rect2I copy_rect = *p.rect;
if (!p.window->visible) {
p.rect = &copy_rect;
}
Int barsize = GetExpandingBarSize(p.window);
p.window->document_rect = p.window->total_rect = CutBottom(p.rect, barsize);
} RegisterHook(LayoutStatusWindow, HookKind_HandleWindowLayout, "", "Layout the status window");
void StatusWindowInit(HookParam param) {
Window *window = CreateWind();
StatusBarWindowID = window->id;
Buffer *buffer = CreateBuffer(SysAllocator, GetUniqueBufferName(WorkDir, "status_bar"));
buffer->special = true;
View *view = CreateView(buffer->id);
view->special = true;
window->active_view = view->id;
// window->font = &SecondaryFont;
window->draw_line_numbers = false;
window->draw_scrollbar = false;
window->draw_line_highlight = true;
window->secondary_window_style = true;
window->primary = false;
window->jump_history = false;
window->lose_focus_on_escape = true;
AddHook(&window->hooks, HookKind_HandleWindowLayout, "@todo LayoutStatusWindow", "", LayoutStatusWindow);
} RegisterHook(StatusWindowInit, HookKind_AppInit, "", "Init the status window");