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) {
if (buffer->no_history) return;
HistoryEntry *entry = Alloc(stack);
entry->carets = TightCopy(GetSystemAllocator(), carets);
}
void SaveHistoryBeforeApplyEdits(Buffer *buffer, Array<HistoryEntry> *stack, Array<Edit> &edits) {
ProfileFunction();
if (buffer->no_history) return;
HistoryEntry *entry = GetLast(*stack);
Allocator sys_allocator = GetSystemAllocator();
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) {
ProfileFunction();
if (buffer->no_history) return;
if (buffer->redo_stack.len <= 0) return;
HistoryEntry entry = Pop(&buffer->redo_stack);
@@ -57,6 +60,7 @@ void RedoEdit(Buffer *buffer, Array<Caret> *carets) {
void UndoEdit(Buffer *buffer, Array<Caret> *carets) {
ProfileFunction();
if (buffer->no_history) return;
if (buffer->undo_stack.len <= 0) return;
HistoryEntry entry = Pop(&buffer->undo_stack);
@@ -102,13 +106,15 @@ void AfterEdit(Buffer *buffer, Array<Edit> *edits, Array<Caret> *carets) {
ProfileFunction();
Assert(buffer->debug_edit_phase == 2);
buffer->debug_edit_phase -= 2;
HistoryEntry *entry = GetLast(buffer->undo_stack);
#if BUFFER_DEBUG
Assert(entry->carets.len);
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);
if (buffer->no_history == false) {
HistoryEntry *entry = GetLast(buffer->undo_stack);
Assert(entry->carets.len);
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

View File

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

View File

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

View File

@@ -3,7 +3,7 @@ String16 LuaCommandResult = {};
int LuaOpenFile(lua_State *L) {
const char *text = luaL_checkstring(L, 1);
Window *window = GetWindow(LastActiveWindow);
Window *window = GetWindow(GetLastActiveWindow());
View *view = ViewOpenFile(window, text);
SetActiveWindow(window->id);
return 0; // number of results
@@ -71,13 +71,6 @@ void InitLua() {
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) {
Scratch scratch((Arena *)allocator.object);
String string = ToString(scratch, string16);

View File

@@ -50,11 +50,21 @@ View *CreateView(BufferID buffer_id) {
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) {
if (LastFrameIDWhenSwitchedActiveWindow != FrameID) {
if (ActiveWindow.id != CommandWindowID.id) {
LastActiveWindow = ActiveWindow;
}
if (window.id != ActiveWindow.id && LastFrameIDWhenSwitchedActiveWindow != FrameID) {
Add(&WindowSwitchHistory, window);
ActiveWindow = window;
LastFrameIDWhenSwitchedActiveWindow = FrameID;
}

View File

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

View File

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