Compare commits

..

2 Commits

Author SHA1 Message Date
Krzosa Karol
c970c1c7d6 UndoEdit, RedoEdit fix recursion, search buffers uses temp buffers now 2026-01-17 08:22:32 +01:00
Krzosa Karol
01488e4eca Search all temp buffers opens a new buffer 2026-01-17 08:03:13 +01:00
4 changed files with 56 additions and 60 deletions

View File

@@ -898,53 +898,61 @@ void SaveHistoryBeforeApplyEdits(Buffer *buffer, Array<HistoryEntry> *stack, Arr
API void RedoEdit(Buffer *buffer, Array<Caret> *carets) { API void RedoEdit(Buffer *buffer, Array<Caret> *carets) {
ProfileFunction(); ProfileFunction();
if (buffer->no_history) return; if (buffer->no_history) return;
if (buffer->redo_stack.len <= 0) return;
HistoryEntry entry = Pop(&buffer->redo_stack); for (int i = 0; buffer->redo_stack.len > 0; i += 1) {
HistoryEntry entry = Pop(&buffer->redo_stack);
HistoryEntry *e = SaveHistoryBeforeMergeCursor(buffer, &buffer->undo_stack, *carets);
e->time = entry.time;
SaveHistoryBeforeApplyEdits(buffer, &buffer->undo_stack, entry.edits);
ApplyEditsMultiCursor(buffer, entry.edits);
HistoryEntry *e = SaveHistoryBeforeMergeCursor(buffer, &buffer->undo_stack, *carets); Dealloc(carets);
e->time = entry.time; *carets = entry.carets;
SaveHistoryBeforeApplyEdits(buffer, &buffer->undo_stack, entry.edits);
ApplyEditsMultiCursor(buffer, entry.edits);
Dealloc(carets); Allocator sys_allocator = GetSystemAllocator();
*carets = entry.carets; For(entry.edits) Dealloc(sys_allocator, it.string.data);
Dealloc(&entry.edits);
Allocator sys_allocator = GetSystemAllocator(); if (buffer->redo_stack.len > 0) {
For(entry.edits) Dealloc(sys_allocator, it.string.data); HistoryEntry *next = GetLast(buffer->redo_stack);
Dealloc(&entry.edits); if ((next->time - entry.time) <= UndoMergeTime) {
continue;
if (buffer->redo_stack.len > 0) { }
HistoryEntry *next = GetLast(buffer->redo_stack);
if ((next->time - entry.time) <= UndoMergeTime) {
RedoEdit(buffer, carets);
} }
break;
} }
} }
API void UndoEdit(Buffer *buffer, Array<Caret> *carets) { API void UndoEdit(Buffer *buffer, Array<Caret> *carets) {
ProfileFunction(); ProfileFunction();
if (buffer->no_history) return; if (buffer->no_history) return;
if (buffer->undo_stack.len <= 0) return;
HistoryEntry entry = Pop(&buffer->undo_stack); static bool warning_reported;
HistoryEntry *e = SaveHistoryBeforeMergeCursor(buffer, &buffer->redo_stack, *carets); for (int i = 0;buffer->undo_stack.len > 0; i += 1) {
e->time = entry.time; HistoryEntry entry = Pop(&buffer->undo_stack);
SaveHistoryBeforeApplyEdits(buffer, &buffer->redo_stack, entry.edits); HistoryEntry *e = SaveHistoryBeforeMergeCursor(buffer, &buffer->redo_stack, *carets);
ApplyEditsMultiCursor(buffer, entry.edits); e->time = entry.time;
SaveHistoryBeforeApplyEdits(buffer, &buffer->redo_stack, entry.edits);
ApplyEditsMultiCursor(buffer, entry.edits);
Dealloc(carets); Dealloc(carets);
*carets = entry.carets; *carets = entry.carets;
Allocator sys_allocator = GetSystemAllocator(); Allocator sys_allocator = GetSystemAllocator();
For(entry.edits) Dealloc(sys_allocator, it.string.data); For(entry.edits) Dealloc(sys_allocator, it.string.data);
Dealloc(&entry.edits); Dealloc(&entry.edits);
if (buffer->undo_stack.len > 0) { if (i > 1000 && !warning_reported) {
HistoryEntry *next = GetLast(buffer->undo_stack); ReportConsolef("WARNING: Undoing more then 1000 edits at once, optimizations is needed I think, too much memory usage?");
if ((entry.time - next->time) <= UndoMergeTime) { warning_reported = true;
UndoEdit(buffer, carets);
} }
if (buffer->undo_stack.len > 0) {
HistoryEntry *next = GetLast(buffer->undo_stack);
if ((entry.time - next->time) <= UndoMergeTime) {
continue;
}
}
break;
} }
} }

View File

@@ -443,13 +443,13 @@ BSet Open(Window *window, String path, ResolveOpenMeta meta, bool set_active = t
#if PLUGIN_DIRECTORY_NAVIGATION #if PLUGIN_DIRECTORY_NAVIGATION
OpenDirectoryNavigation(view); OpenDirectoryNavigation(view);
#endif #endif
} else { }
Buffer *buffer = GetBuffer(view->active_buffer);
if (o.line != -1) { Buffer *buffer = GetBuffer(view->active_buffer);
if (o.col == -1) o.col = 1; if (o.line != -1) {
Int pos = XYToPos(buffer, {o.col - 1, o.line - 1}); if (o.col == -1) o.col = 1;
SelectRange(view, MakeCaret(pos)); Int pos = XYToPos(buffer, {o.col - 1, o.line - 1});
} SelectRange(view, MakeCaret(pos));
} }
CenterView(window->id); CenterView(window->id);
} else if (o.kind == OpenKind_Exec) { } else if (o.kind == OpenKind_Exec) {

View File

@@ -1,8 +1,6 @@
BufferID SearchOpenBuffersBufferID;
struct SearchOpenBuffersParams { struct SearchOpenBuffersParams {
String16 needle; String16 needle;
BufferID buffer; ViewID view;
}; };
void Coro_SearchOpenBuffers(mco_coro *co) { void Coro_SearchOpenBuffers(mco_coro *co) {
@@ -27,7 +25,7 @@ void Coro_SearchOpenBuffers(mco_coro *co) {
{ {
Scratch scratch; Scratch scratch;
Array<Caret> occurences = FindAll(scratch, it, param->needle); Array<Caret> occurences = FindAll(scratch, it, param->needle);
Buffer *out_buffer = GetBuffer(param->buffer); View *out_view = GetView(param->view);
ForItem (caret, occurences) { ForItem (caret, occurences) {
Int pos = caret.range.min; Int pos = caret.range.min;
Int line = PosToLine(it, pos); Int line = PosToLine(it, pos);
@@ -35,7 +33,7 @@ void Coro_SearchOpenBuffers(mco_coro *co) {
Int column = pos - range.min; Int column = pos - range.min;
String16 line_string = GetString(it, range); String16 line_string = GetString(it, range);
String line_string8 = ToString(scratch, line_string); 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); Appendf(out_view, "%S ||> %S:%lld:%lld\n", line_string8, it->name, (long long)line + 1, (long long)column + 1);
} }
} }
CoYield(co); CoYield(co);
@@ -50,7 +48,6 @@ void UpdateSearchOpenBuffersView() {
if (active.view->prev_search_line_hash != hash) { if (active.view->prev_search_line_hash != hash) {
active.view->prev_search_line_hash = hash; active.view->prev_search_line_hash = hash;
if (line_string.len > 0) { if (line_string.len > 0) {
// @todo: do we reintroduce history here? like in fuzzy search view
Caret caret = active.view->carets[0]; Caret caret = active.view->carets[0];
SelectEntireBuffer(active.view); SelectEntireBuffer(active.view);
Replace(active.view, line_string); Replace(active.view, line_string);
@@ -60,7 +57,7 @@ void UpdateSearchOpenBuffersView() {
CoData *dat = CoAdd(Coro_SearchOpenBuffers); CoData *dat = CoAdd(Coro_SearchOpenBuffers);
SearchOpenBuffersParams *param = AllocType(dat->arena, SearchOpenBuffersParams); SearchOpenBuffersParams *param = AllocType(dat->arena, SearchOpenBuffersParams);
param->needle = Copy16(dat->arena, line_string); param->needle = Copy16(dat->arena, line_string);
param->buffer = active.buffer->id; param->view = active.view->id;
dat->user_ctx = param; dat->user_ctx = param;
dat->dont_wait_until_resolved = true; dat->dont_wait_until_resolved = true;
CoResume(dat); CoResume(dat);
@@ -78,10 +75,8 @@ void CMD_SearchOpenBuffers() {
} }
NextActiveWindowID = main.window->id; NextActiveWindowID = main.window->id;
Buffer *search_project_buffer = GetBuffer(SearchOpenBuffersBufferID); JumpTempBuffer(&main);
View *view = WindowOpenBufferView(main.window, search_project_buffer->name); AddCommand(&main.view->commands, "Open", "ctrl-q | enter | f12", []() {
view->special = true;
AddCommand(&view->commands, "Open", "ctrl-q | enter | f12", []() {
BSet active = GetBSet(ActiveWindowID); BSet active = GetBSet(ActiveWindowID);
BSet main = GetBSet(PrimaryWindowID); BSet main = GetBSet(PrimaryWindowID);
NextActiveWindowID = main.window->id; NextActiveWindowID = main.window->id;
@@ -90,10 +85,10 @@ void CMD_SearchOpenBuffers() {
main.window->goto_list_pos = active.view->carets[0].range.min; main.window->goto_list_pos = active.view->carets[0].range.min;
Open(string); Open(string);
}); });
view->update_hook = UpdateSearchOpenBuffersView; main.view->update_hook = UpdateSearchOpenBuffersView;
if (string.len) { if (string.len) {
SelectRange(view, GetLineRangeWithoutNL(search_project_buffer, 0)); SelectRange(main.view, GetLineRangeWithoutNL(main.buffer, 0));
Replace(view, string); Replace(main.view, string);
} }
SelectRange(view, GetLineRangeWithoutNL(search_project_buffer, 0)); SelectRange(main.view, GetLineRangeWithoutNL(main.buffer, 0));
} RegisterCommand(CMD_SearchOpenBuffers, "ctrl-shift-f", "Interactive search over the entire project in a new buffer view"); } RegisterCommand(CMD_SearchOpenBuffers, "ctrl-shift-f", "Interactive search over the entire project in a new buffer view");

View File

@@ -962,13 +962,6 @@ int main(int argc, char **argv)
EventBuffer->no_history = true; EventBuffer->no_history = true;
EventBuffer->special = true; EventBuffer->special = true;
#endif #endif
#if 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(); InitRender();