UndoEdit, RedoEdit fix recursion, search buffers uses temp buffers now

This commit is contained in:
Krzosa Karol
2026-01-17 08:22:32 +01:00
parent 01488e4eca
commit c970c1c7d6
2 changed files with 43 additions and 36 deletions

View File

@@ -898,10 +898,9 @@ 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;
for (int i = 0; buffer->redo_stack.len > 0; i += 1) {
HistoryEntry entry = Pop(&buffer->redo_stack); HistoryEntry entry = Pop(&buffer->redo_stack);
HistoryEntry *e = SaveHistoryBeforeMergeCursor(buffer, &buffer->undo_stack, *carets); HistoryEntry *e = SaveHistoryBeforeMergeCursor(buffer, &buffer->undo_stack, *carets);
e->time = entry.time; e->time = entry.time;
SaveHistoryBeforeApplyEdits(buffer, &buffer->undo_stack, entry.edits); SaveHistoryBeforeApplyEdits(buffer, &buffer->undo_stack, entry.edits);
@@ -917,16 +916,19 @@ API void RedoEdit(Buffer *buffer, Array<Caret> *carets) {
if (buffer->redo_stack.len > 0) { if (buffer->redo_stack.len > 0) {
HistoryEntry *next = GetLast(buffer->redo_stack); HistoryEntry *next = GetLast(buffer->redo_stack);
if ((next->time - entry.time) <= UndoMergeTime) { if ((next->time - entry.time) <= UndoMergeTime) {
RedoEdit(buffer, carets); continue;
} }
} }
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;
static bool warning_reported;
for (int i = 0;buffer->undo_stack.len > 0; i += 1) {
HistoryEntry entry = Pop(&buffer->undo_stack); HistoryEntry entry = Pop(&buffer->undo_stack);
HistoryEntry *e = SaveHistoryBeforeMergeCursor(buffer, &buffer->redo_stack, *carets); HistoryEntry *e = SaveHistoryBeforeMergeCursor(buffer, &buffer->redo_stack, *carets);
e->time = entry.time; e->time = entry.time;
@@ -940,12 +942,18 @@ API void UndoEdit(Buffer *buffer, Array<Caret> *carets) {
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 (i > 1000 && !warning_reported) {
ReportConsolef("WARNING: Undoing more then 1000 edits at once, optimizations is needed I think, too much memory usage?");
warning_reported = true;
}
if (buffer->undo_stack.len > 0) { if (buffer->undo_stack.len > 0) {
HistoryEntry *next = GetLast(buffer->undo_stack); HistoryEntry *next = GetLast(buffer->undo_stack);
if ((entry.time - next->time) <= UndoMergeTime) { if ((entry.time - next->time) <= UndoMergeTime) {
UndoEdit(buffer, carets); continue;
} }
} }
break;
}
} }
API void DeallocHistoryEntries(Array<HistoryEntry> *entries) { API void DeallocHistoryEntries(Array<HistoryEntry> *entries) {

View File

@@ -1,6 +1,6 @@
struct SearchOpenBuffersParams { struct SearchOpenBuffersParams {
String16 needle; String16 needle;
BufferID buffer; ViewID view;
}; };
void Coro_SearchOpenBuffers(mco_coro *co) { void Coro_SearchOpenBuffers(mco_coro *co) {
@@ -25,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);
@@ -33,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);
@@ -48,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);
@@ -58,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);