Skip to content

Commit

Permalink
Integrate shortcut and remove local undo
Browse files Browse the repository at this point in the history
  • Loading branch information
Negabinary committed Feb 25, 2025
1 parent a627b37 commit 494d987
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 136 deletions.
60 changes: 5 additions & 55 deletions src/haz3lcore/zipper/Editor.re
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,11 @@ module State = {
};
};

module History = {
[@deriving (show({with_path: false}), sexp, yojson)]
type affix = list((Action.t, State.t));
[@deriving (show({with_path: false}), sexp, yojson)]
type t = (affix, affix);

let empty = ([], []);

let add = (a: Action.t, state: State.t, (pre, _): t): t => (
[(a, state), ...pre],
[],
);
};

module Model = {
[@deriving (show({with_path: false}), sexp, yojson)]
type t = {
// Updated
state: State.t,
history: History.t,
// Calculated
[@opaque]
syntax: CachedSyntax.t,
Expand All @@ -94,7 +79,6 @@ module Model = {
zipper,
col_target: None,
},
history: History.empty,
syntax: CachedSyntax.init(zipper, Id.Map.empty),
};

Expand Down Expand Up @@ -140,7 +124,7 @@ module Update = {
~settings: CoreSettings.t,
a: Action.t,
old_statics,
{state, history, syntax}: Model.t,
{state, syntax}: Model.t,
)
: Action.Result.t(Model.t) => {
open Result.Syntax;
Expand All @@ -154,7 +138,7 @@ module Update = {
~settings,
old_statics,
Buffer(Clear),
Model.to_move_s({state, history, syntax}),
Model.to_move_s({state, syntax}),
state.zipper,
)
|> Action.Result.ok
Expand All @@ -168,10 +152,6 @@ module Update = {
syntax;
};

// 2. Add to undo history
let history =
Action.is_historic(a) ? History.add(a, state, history) : history;

// 3. Record target column if moving up/down
let col_target =
switch (a) {
Expand All @@ -191,7 +171,7 @@ module Update = {
~settings,
old_statics,
a,
Model.to_move_s({state, history, syntax}),
Model.to_move_s({state, syntax}),
state.zipper,
);

Expand All @@ -201,45 +181,16 @@ module Update = {
zipper,
col_target,
},
history,
syntax,
};
};

let undo = (ed: Model.t) =>
switch (ed.history) {
| ([], _) => None
| ([(a, prev), ...before], after) =>
Some(
Model.{
state: prev,
history: (before, [(a, ed.state), ...after]),
syntax: ed.syntax // Will be recalculated in calculate
},
)
};
let redo = (ed: Model.t) =>
switch (ed.history) {
| (_, []) => None
| (before, [(a, next), ...after]) =>
Some(
Model.{
state: next,
history: ([(a, ed.state), ...before], after),
syntax: ed.syntax // Will be recalculated in calculate
},
)
};

let can_undo = ed => Option.is_some(undo(ed));
let can_redo = ed => Option.is_some(redo(ed));

let calculate =
(
~settings: CoreSettings.t,
~is_edited,
new_statics,
{syntax, state, history}: Model.t,
{syntax, state}: Model.t,
) => {
// 1. Recalculate the autocomplete buffer if necessary
let zipper =
Expand All @@ -249,7 +200,7 @@ module Update = {
~settings,
new_statics,
Buffer(Set(TyDi)),
Model.to_move_s({syntax, state, history}),
Model.to_move_s({syntax, state}),
state.zipper,
)
) {
Expand All @@ -266,7 +217,6 @@ module Update = {

// Recombine
Model.{
history,
state: {
...state,
zipper,
Expand Down
8 changes: 7 additions & 1 deletion src/haz3lweb/Main.re
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ let apply =
(
model: Page.Model.t,
action: Page.Update.t,
~schedule_effect,
~schedule_action,
~schedule_autosave,
)
Expand All @@ -36,6 +37,7 @@ let apply =
Page.Update.update(
~import_log=Log.import,
~get_log_and=Log.get_and,
~schedule_effect,
~schedule_action,
action,
model,
Expand Down Expand Up @@ -95,7 +97,11 @@ let start = {
schedule_event(alarm_inject(action))
| Inactive => ()
};
apply(~schedule_action, ~schedule_autosave);
apply(
~schedule_action,
~schedule_effect=schedule_event,
~schedule_autosave,
);
},
~default_model=Page.Store.load(),
~can_undo=Page.Update.can_undo,
Expand Down
36 changes: 0 additions & 36 deletions src/haz3lweb/app/editors/code/CodeEditable.re
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ module Update = {
[@deriving (show({with_path: false}), sexp, yojson)]
type t =
| Perform(Action.t)
| Undo
| Redo
| TAB
| DebugConsole(string);

Expand All @@ -23,8 +21,6 @@ module Update = {
let can_undo = (action: t) => {
switch (action) {
| Perform(action) => Action.is_historic(action)
| Undo => false
| Redo => false
| TAB => true
| DebugConsole(_) => false
};
Expand Down Expand Up @@ -71,16 +67,6 @@ module Update = {
);
switch (action) {
| Perform(action) => perform(action, model)
| Undo =>
switch (Editor.Update.undo(model.editor)) {
| Some(editor) => Model.{...model, editor} |> Updated.return
| None => model |> Updated.return_quiet
}
| Redo =>
switch (Editor.Update.redo(model.editor)) {
| Some(editor) => Model.{...model, editor} |> Updated.return
| None => model |> Updated.return_quiet
}
| DebugConsole(key) =>
DebugConsole.print(~settings, model, key);
model |> Updated.return_quiet;
Expand Down Expand Up @@ -117,36 +103,14 @@ module Selection = {
CodeWithStatics.Model.get_cursor_info(model)
|> map(x => Update.Perform(x)),
editor_read_only: false,
undo_action: Some(Update.Undo),
redo_action: Some(Update.Redo),
};
};

let handle_key_event =
(~selection as (), _: Model.t): (Key.t => option(Update.t)) =>
fun
| {
key: D("Z" | "z"),
sys: Mac,
shift: Down,
meta: Down,
ctrl: Up,
alt: Up,
}
| {
key: D("Z" | "z"),
sys: PC,
shift: Down,
meta: Up,
ctrl: Down,
alt: Up,
} =>
Some(Update.Redo)
| {key: D("Tab"), sys: _, shift: Up, meta: Up, ctrl: Up, alt: Up} =>
Some(Update.TAB)
| {key: D("Z" | "z"), sys: Mac, shift: Up, meta: Down, ctrl: Up, alt: Up}
| {key: D("Z" | "z"), sys: PC, shift: Up, meta: Up, ctrl: Down, alt: Up} =>
Some(Update.Undo)
| {key: D(key), sys: Mac | PC, shift: Down, meta: Up, ctrl: Up, alt: Up}
when Keyboard.is_f_key(key) =>
Some(Update.DebugConsole(key))
Expand Down
2 changes: 0 additions & 2 deletions src/haz3lweb/app/editors/code/CodeSelectable.re
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ module Update = {
Buffer(_) |
Project(_),
)
| Undo
| Redo
| DebugConsole(_)
| TAB => None;

Expand Down
88 changes: 46 additions & 42 deletions src/haz3lweb/view/Page.re
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ module Update = {
(
~import_log,
~schedule_action,
~schedule_effect,
~globals: Globals.Model.t,
action: Globals.Update.t,
model: Model.t,
Expand Down Expand Up @@ -166,40 +167,24 @@ module Update = {
{...model, editors};
};
| Undo =>
let cursor_info =
Editors.Selection.get_cursor_info(
~selection=model.selection,
model.editors,
);
switch (cursor_info.undo_action) {
| None => model |> return_quiet
| Some(action) =>
let* editors =
Editors.Update.update(
~globals=model.globals,
~schedule_action=a => schedule_action(Editors(a)),
action,
model.editors,
);
{...model, editors};
let undo = BonsaiUndo.UndoController.get_undo();
switch (undo) {
| None =>
print_endline("Cannot undo");
model |> return_quiet;
| Some(effect) =>
schedule_effect(effect);
model |> return_quiet;
};
| Redo =>
let cursor_info =
Editors.Selection.get_cursor_info(
~selection=model.selection,
model.editors,
);
switch (cursor_info.redo_action) {
| None => model |> return_quiet
| Some(action) =>
let* editors =
Editors.Update.update(
~globals=model.globals,
~schedule_action=a => schedule_action(Editors(a)),
action,
model.editors,
);
{...model, editors};
let redo = BonsaiUndo.UndoController.get_redo();
switch (redo) {
| None =>
print_endline("Cannot redo");
model |> return_quiet;
| Some(effect) =>
schedule_effect(effect);
model |> return_quiet;
};
};
};
Expand All @@ -208,6 +193,7 @@ module Update = {
(
~import_log,
~get_log_and,
~schedule_effect,
~schedule_action: t => unit,
action: t,
model: Model.t,
Expand All @@ -219,7 +205,14 @@ module Update = {
};
switch (action) {
| Globals(action) =>
update_global(~globals, ~import_log, ~schedule_action, action, model)
update_global(
~globals,
~import_log,
~schedule_effect,
~schedule_action,
action,
model,
)
| Editors(action) =>
let* editors =
Editors.Update.update(
Expand Down Expand Up @@ -300,6 +293,26 @@ module Selection = {
Some(Update.Globals(SetShowBackpackTargets(false)))
| {key: D("F7"), sys: Mac | PC, shift: Down, meta: Up, ctrl: Up, alt: Up} =>
Some(Update.Benchmark(Start))
| {
key: D("Z" | "z"),
sys: Mac,
shift: Down,
meta: Down,
ctrl: Up,
alt: Up,
}
| {
key: D("Z" | "z"),
sys: PC,
shift: Down,
meta: Up,
ctrl: Down,
alt: Up,
} =>
Some(Update.Globals(Redo))
| {key: D("Z" | "z"), sys: Mac, shift: Up, meta: Down, ctrl: Up, alt: Up}
| {key: D("Z" | "z"), sys: PC, shift: Up, meta: Up, ctrl: Down, alt: Up} =>
Some(Update.Globals(Undo))
| _ =>
Editors.Selection.handle_key_event(~selection, ~event, model.editors)
|> Option.map(x => Update.Editors(x))
Expand Down Expand Up @@ -423,15 +436,6 @@ module View = {
++ Keyboard.meta(Os.is_mac^ ? Mac : PC)
++ " + k)",
),
{
let undo = BonsaiUndo.UndoController.get_undo();
button_d(
Icons.undo,
~disabled=undo == None,
undo |> Option.value(~default=Effect.Ignore),
~tooltip="Undo (Test)",
);
},
link(
Icons.github,
"https://github.com/hazelgrove/hazel",
Expand Down

0 comments on commit 494d987

Please sign in to comment.