No history buffer, Active window stack

This commit is contained in:
Krzosa Karol
2024-07-24 13:03:52 +02:00
parent bb3044d168
commit 039d2f9350
7 changed files with 46 additions and 34 deletions

View File

@@ -1,10 +1,12 @@
void SaveHistoryBeforeMergeCursor(Buffer *buffer, Array<HistoryEntry> *stack, Array<Caret> &carets) { void SaveHistoryBeforeMergeCursor(Buffer *buffer, Array<HistoryEntry> *stack, Array<Caret> &carets) {
if (buffer->no_history) return;
HistoryEntry *entry = Alloc(stack); HistoryEntry *entry = Alloc(stack);
entry->carets = TightCopy(GetSystemAllocator(), carets); entry->carets = TightCopy(GetSystemAllocator(), carets);
} }
void SaveHistoryBeforeApplyEdits(Buffer *buffer, Array<HistoryEntry> *stack, Array<Edit> &edits) { void SaveHistoryBeforeApplyEdits(Buffer *buffer, Array<HistoryEntry> *stack, Array<Edit> &edits) {
ProfileFunction(); ProfileFunction();
if (buffer->no_history) return;
HistoryEntry *entry = GetLast(*stack); HistoryEntry *entry = GetLast(*stack);
Allocator sys_allocator = GetSystemAllocator(); Allocator sys_allocator = GetSystemAllocator();
entry->edits = TightCopy(sys_allocator, edits); entry->edits = TightCopy(sys_allocator, edits);
@@ -39,6 +41,7 @@ void SaveHistoryBeforeApplyEdits(Buffer *buffer, Array<HistoryEntry> *stack, Arr
void RedoEdit(Buffer *buffer, Array<Caret> *carets) { void RedoEdit(Buffer *buffer, Array<Caret> *carets) {
ProfileFunction(); ProfileFunction();
if (buffer->no_history) return;
if (buffer->redo_stack.len <= 0) return; if (buffer->redo_stack.len <= 0) return;
HistoryEntry entry = Pop(&buffer->redo_stack); HistoryEntry entry = Pop(&buffer->redo_stack);
@@ -57,6 +60,7 @@ void RedoEdit(Buffer *buffer, Array<Caret> *carets) {
void UndoEdit(Buffer *buffer, Array<Caret> *carets) { void UndoEdit(Buffer *buffer, Array<Caret> *carets) {
ProfileFunction(); ProfileFunction();
if (buffer->no_history) return;
if (buffer->undo_stack.len <= 0) return; if (buffer->undo_stack.len <= 0) return;
HistoryEntry entry = Pop(&buffer->undo_stack); HistoryEntry entry = Pop(&buffer->undo_stack);
@@ -102,13 +106,15 @@ void AfterEdit(Buffer *buffer, Array<Edit> *edits, Array<Caret> *carets) {
ProfileFunction(); ProfileFunction();
Assert(buffer->debug_edit_phase == 2); Assert(buffer->debug_edit_phase == 2);
buffer->debug_edit_phase -= 2; buffer->debug_edit_phase -= 2;
HistoryEntry *entry = GetLast(buffer->undo_stack);
#if BUFFER_DEBUG #if BUFFER_DEBUG
Assert(entry->carets.len); if (buffer->no_history == false) {
Assert(entry->edits.len); HistoryEntry *entry = GetLast(buffer->undo_stack);
for (Int i = 0; i < edits->len - 1; i += 1) { Assert(entry->carets.len);
Assert(edits->data[i].range.min <= edits->data[i + 1].range.min); Assert(entry->edits.len);
for (Int i = 0; i < edits->len - 1; i += 1) {
Assert(edits->data[i].range.min <= edits->data[i + 1].range.min);
}
} }
#endif #endif

View File

@@ -224,7 +224,7 @@ void HandleGlobalCommands() {
} }
if (CtrlPress(KEY_P)) { if (CtrlPress(KEY_P)) {
if (command_window->visible) { if (command_window->visible) {
SetActiveWindow(LastActiveWindow); SetActiveWindow(GetLastActiveWindow());
} else { } else {
SetActiveWindow(command_window->id); SetActiveWindow(command_window->id);
} }

View File

@@ -384,7 +384,7 @@ void HandleActiveWindowBindings(Window *window) {
String16 string = GetString(*buffer, range); String16 string = GetString(*buffer, range);
String16 eval_result = EvalString(scratch, string); String16 eval_result = EvalString(scratch, string);
if (Ctrl()) { if (Ctrl() && eval_result.len) {
Command_SelectEntireBuffer(&view); Command_SelectEntireBuffer(&view);
Command_Replace(&view, eval_result); Command_Replace(&view, eval_result);
Command_SelectRange(&view, {}); Command_SelectRange(&view, {});
@@ -395,11 +395,11 @@ void HandleActiveWindowBindings(Window *window) {
if (window->id.id == CommandWindowID.id) { if (window->id.id == CommandWindowID.id) {
Window *command_window = GetWindow(CommandWindowID); Window *command_window = GetWindow(CommandWindowID);
command_window->visible = !command_window->visible; command_window->visible = !command_window->visible;
ActiveWindow = LastActiveWindow; ActiveWindow = GetLastActiveWindow();
} }
{ {
Window *window = GetWindow(LastActiveWindow); Window *window = GetWindow(GetLastActiveWindow());
View *view = GetView(window->active_view); View *view = GetView(window->active_view);
SetActiveWindow(window->id); SetActiveWindow(window->id);
Command_Replace(view, eval_result); Command_Replace(view, eval_result);
@@ -574,13 +574,15 @@ void ChangeActiveWindowAndScroll(Array<Int> &order) {
void ReplaceInfobarData() { void ReplaceInfobarData() {
Window *window = GetWindow(InfoBarWindowID); Window *window = GetWindow(InfoBarWindowID);
if (IsActive(window)) return;
View *view = GetView(window->active_view); View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->buffer_id); Buffer *buffer = GetBuffer(view->buffer_id);
XY xy = {}; XY xy = {};
String name = {}; String name = {};
{ {
Window *window = GetActiveWindow(); Window *window = GetWindow(GetLastActiveWindow());
View *view = GetActiveView(window); View *view = GetActiveView(window);
Buffer *buffer = GetBuffer(view->buffer_id); Buffer *buffer = GetBuffer(view->buffer_id);
Scratch scratch; Scratch scratch;
@@ -589,10 +591,10 @@ void ReplaceInfobarData() {
name = buffer->name; name = buffer->name;
} }
Scratch scratch; Scratch scratch;
String error = {}; String error = {};
if (InfoBarErrorMessage.len) error = Format(scratch, "| error: %.*s", FmtString(InfoBarErrorMessage)); String s = Format(scratch, "line: %5lld col: %5lld name: %.*s", (long long)xy.line + 1ll, (long long)xy.col + 1ll, FmtString(name));
String s = Format(scratch, "line: %5lld col: %5lld name: %.*s %.*s", (long long)xy.line + 1ll, (long long)xy.col + 1ll, FmtString(name), FmtString(error));
String16 string = ToString16(scratch, s); String16 string = ToString16(scratch, s);
ReplaceText(buffer, {0, buffer->len}, string); ReplaceText(buffer, {0, buffer->len}, string);
Command_SelectRange(view, {});
} }

View File

@@ -3,7 +3,7 @@ String16 LuaCommandResult = {};
int LuaOpenFile(lua_State *L) { int LuaOpenFile(lua_State *L) {
const char *text = luaL_checkstring(L, 1); const char *text = luaL_checkstring(L, 1);
Window *window = GetWindow(LastActiveWindow); Window *window = GetWindow(GetLastActiveWindow());
View *view = ViewOpenFile(window, text); View *view = ViewOpenFile(window, text);
SetActiveWindow(window->id); SetActiveWindow(window->id);
return 0; // number of results return 0; // number of results
@@ -71,13 +71,6 @@ void InitLua() {
lua_setglobal(LuaState, "list_windows"); lua_setglobal(LuaState, "list_windows");
} }
// @todo: also log to buffer or something
void SetInfoBarErrorMessage(String string) {
Allocator sys = GetSystemAllocator();
if (InfoBarErrorMessage.data) Dealloc(sys, &InfoBarErrorMessage.data);
if (string.len) InfoBarErrorMessage = Copy(sys, string);
}
String16 EvalString(Allocator allocator, String16 string16) { String16 EvalString(Allocator allocator, String16 string16) {
Scratch scratch((Arena *)allocator.object); Scratch scratch((Arena *)allocator.object);
String string = ToString(scratch, string16); String string = ToString(scratch, string16);

View File

@@ -50,11 +50,21 @@ View *CreateView(BufferID buffer_id) {
return w; return w;
} }
inline bool IsSpecial(WindowID window) {
bool result = window.id == CommandWindowID.id || window.id == InfoBarWindowID.id;
return result;
}
WindowID GetLastActiveWindow() {
For(IterateInReverse(&WindowSwitchHistory)) {
if (!IsSpecial(it)) return it;
}
return NullWindowID;
}
void SetActiveWindow(WindowID window) { void SetActiveWindow(WindowID window) {
if (LastFrameIDWhenSwitchedActiveWindow != FrameID) { if (window.id != ActiveWindow.id && LastFrameIDWhenSwitchedActiveWindow != FrameID) {
if (ActiveWindow.id != CommandWindowID.id) { Add(&WindowSwitchHistory, window);
LastActiveWindow = ActiveWindow;
}
ActiveWindow = window; ActiveWindow = window;
LastFrameIDWhenSwitchedActiveWindow = FrameID; LastFrameIDWhenSwitchedActiveWindow = FrameID;
} }

View File

@@ -116,7 +116,8 @@ int main(void) {
w->draw_scrollbar = false; w->draw_scrollbar = false;
w->draw_line_numbers = false; w->draw_line_numbers = false;
Buffer *b = CreateBuffer(sys_allocator, "*infobar*"); Buffer *b = CreateBuffer(sys_allocator, "*infobar*");
View *v = CreateView(b->id); b->no_history = true;
View *v = CreateView(b->id);
AddView(w, v->id); AddView(w, v->id);
InfoBarWindowID = w->id; InfoBarWindowID = w->id;
} }

View File

@@ -35,6 +35,7 @@ struct Buffer {
Array<HistoryEntry> undo_stack; Array<HistoryEntry> undo_stack;
Array<HistoryEntry> redo_stack; Array<HistoryEntry> redo_stack;
int debug_edit_phase; int debug_edit_phase;
bool no_history;
}; };
struct View { struct View {
@@ -88,10 +89,9 @@ WindowID CommandWindowID = {0};
WindowID InfoBarWindowID = {0}; WindowID InfoBarWindowID = {0};
// WindowID ExecBarWindowID = {0}; // WindowID ExecBarWindowID = {0};
Array<Buffer> Buffers = {}; Array<Buffer> Buffers = {};
Array<View> Views = {}; Array<View> Views = {};
Array<Window> Windows = {}; Array<Window> Windows = {};
WindowID ActiveWindow = {};
float MenuFontSize; float MenuFontSize;
Font MenuFont; Font MenuFont;
@@ -105,9 +105,9 @@ Int FontCharSpacing;
Int FrameID; Int FrameID;
Int LastFrameIDWhenScrolled; Int LastFrameIDWhenScrolled;
WindowID LastActiveWindow; Int LastFrameIDWhenSwitchedActiveWindow;
Int LastFrameIDWhenSwitchedActiveWindow; Array<WindowID> WindowSwitchHistory;
String InfoBarErrorMessage; WindowID ActiveWindow = {};
String16 EvalString(Allocator allocator, String16 string16); String16 EvalString(Allocator allocator, String16 string16);
Rect2I GetVisibleCells(Window &window); Rect2I GetVisibleCells(Window &window);