Skip to content

Commit

Permalink
changed the put editor function to accept any editor state. Still did…
Browse files Browse the repository at this point in the history
… not fix the refresh issue
  • Loading branch information
reevafaisal committed Sep 23, 2024
1 parent ab7f94c commit a0ab52c
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 74 deletions.
97 changes: 75 additions & 22 deletions src/haz3lweb/Editors.re
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ type t =
| Documentation(string, list((string, DocumentationEnv.state)))
| Exercises(int, list(Exercise.spec), Exercise.state);

[@deriving (show({with_path: false}), sexp, yojson)]
type cur_state =
| ScratchState(ScratchSlide.state)
| DocumentationState(DocumentationEnv.state)
| ExerciseState(Exercise.state);

let get_editor = (editors: t): Editor.t =>
switch (editors) {
| Scratch(n, slides) =>
Expand All @@ -34,42 +40,93 @@ let get_editor = (editors: t): Editor.t =>
| Exercises(_, _, exercise) => Exercise.editor_of_state(exercise)
};

let put_editor = (ed: DocumentationEnv.state, eds: t): t =>
// let put_editor = (ed: DocumentationEnv.state, eds: t): t =>
// switch (eds) {
// | Scratch(n, slides) =>
// assert(n < List.length(slides));
// let convert_to_state = (doc_state: DocumentationEnv.state): state => {
// title: doc_state.eds.title,
// description: doc_state.eds.description,
// // your_impl: doc_state.eds.your_impl, // or however this field maps
// hidden_tests: {
// tests: doc_state.eds.hidden_tests.tests, // or however this field maps
// hints: doc_state.eds.hidden_tests.hints,
// },
// };
// let new_ed = convert_to_state(ed);
// Scratch(n, Util.ListUtil.put_nth(n, new_ed, slides));

// | Documentation(name, slides) =>
// assert(List.mem_assoc(name, slides));

// // NEW //
// let update_slide =
// (hint: string, state: DocumentationEnv.state)
// : (string, DocumentationEnv.state) =>
// if (hint == name) {
// let updatedState =
// switch (ed.pos) {
// | DocumentationEnv.HiddenTests =>
// DocumentationEnv.put_editor(state, ed.eds.hidden_tests.tests) // Update hidden_tests
// | DocumentationEnv.YourImpl =>
// DocumentationEnv.put_editor(state, ed.eds.your_impl) // Update your_impl
// };
// (hint, updatedState);
// } else {
// (hint, state);
// };

// let updatedSlides =
// List.map(
// slide => {
// let (hint, state) = slide;
// update_slide(hint, state);
// },
// slides,
// );

// Documentation(name, updatedSlides);
// // //

// | Exercises(n, specs, exercise) =>
// Exercises(
// n,
// specs,
// Exercise.put_editor(exercise, ed.eds.hidden_tests.tests),
// )
// };

let put_editor = (editor: Editor.t, eds: t): t =>
switch (eds) {
| Scratch(n, slides) =>
assert(n < List.length(slides));
let convert_to_state = (doc_state: DocumentationEnv.state): state => {
title: doc_state.eds.title,
description: doc_state.eds.description,
// your_impl: doc_state.eds.your_impl, // or however this field maps

let new_ed: state = {
title: "",
description: "",
hidden_tests: {
tests: doc_state.eds.hidden_tests.tests, // or however this field maps
hints: doc_state.eds.hidden_tests.hints,
tests: editor,
hints: [],
},
};
let new_ed = convert_to_state(ed);

Scratch(n, Util.ListUtil.put_nth(n, new_ed, slides));

| Documentation(name, slides) =>
assert(List.mem_assoc(name, slides));

// NEW //
// Function to update the slide based on `editor`
let update_slide =
(hint: string, state: DocumentationEnv.state)
: (string, DocumentationEnv.state) =>
if (hint == name) {
let updatedState =
switch (ed.pos) {
| DocumentationEnv.HiddenTests =>
DocumentationEnv.put_editor(state, ed.eds.hidden_tests.tests) // Update hidden_tests
| DocumentationEnv.YourImpl =>
DocumentationEnv.put_editor(state, ed.eds.your_impl) // Update your_impl
};
let updatedState = DocumentationEnv.put_editor(state, editor);
(hint, updatedState);
} else {
(hint, state);
};

// Map the update function over the slides
let updatedSlides =
List.map(
slide => {
Expand All @@ -80,14 +137,10 @@ let put_editor = (ed: DocumentationEnv.state, eds: t): t =>
);

Documentation(name, updatedSlides);
// //

| Exercises(n, specs, exercise) =>
Exercises(
n,
specs,
Exercise.put_editor(exercise, ed.eds.hidden_tests.tests),
)
// For Exercises, update the hidden_tests with `editor`
Exercises(n, specs, Exercise.put_editor(exercise, editor))
};

let get_zipper = (editors: t): Zipper.t => get_editor(editors).state.zipper;
Expand Down
56 changes: 28 additions & 28 deletions src/haz3lweb/Update.re
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,29 @@ open Haz3lcore;

include UpdateAction; // to prevent circularity

let fromEditor = (editor: Editor.t): DocumentationEnv.state => {
{
pos: DocumentationEnv.YourImpl,
eds: {
title: "",
description: "",
your_impl: Editor.init(Zipper.init()),
hidden_tests: {
tests: editor,
hints: [],
},
},
};
};
// let fromEditor = (editor: Editor.t): ScratchSlide.state => {
// title: "",
// description: "",
// hidden_tests: {
// tests: editor,
// hints: [],
// },
// };

// let fromEditor = (editor: Editor.t): DocumentationEnv.state => {
// {
// pos: DocumentationEnv.YourImpl,
// eds: {
// title: "",
// description: "",
// your_impl: editor,
// hidden_tests: {
// tests: editor,
// hints: [],
// },
// },
// };
// };

let update_settings =
(a: settings_action, {settings, _} as model: Model.t): Model.t =>
Expand Down Expand Up @@ -254,10 +263,7 @@ let perform_action = (model: Model.t, a: Action.t): Result.t(Model.t) =>
) {
| Error(err) => Error(FailedToPerform(err))
| Ok(ed) =>
let model = {
...model,
editors: Editors.put_editor(fromEditor(ed), model.editors),
};
let model = {...model, editors: Editors.put_editor(ed, model.editors)};
/* Note: Not saving here as saving is costly to do each keystroke,
we wait a second after the last edit action (see Main.re) */
Ok(model);
Expand Down Expand Up @@ -471,7 +477,7 @@ let rec apply =
| Some(z) =>
//TODO: add correct action to history (Pick_up is wrong)
let editor = Haz3lcore.Editor.new_state(Pick_up, z, ed);
let editors = Editors.put_editor(fromEditor(editor), model.editors);
let editors = Editors.put_editor(editor, model.editors);
Ok({...model, editors});
};
| Cut =>
Expand All @@ -488,28 +494,22 @@ let rec apply =
| Some(z) =>
//HACK(andrew): below is not strictly a insert action...
let ed = Haz3lcore.Editor.new_state(Insert(clipboard), z, ed);
let editors = Editors.put_editor(fromEditor(ed), model.editors);
let editors = Editors.put_editor(ed, model.editors);
Ok({...model, editors});
};
| Undo =>
let ed = Editors.get_editor(model.editors);
switch (Haz3lcore.Editor.undo(ed)) {
| None => Error(CantUndo)
| Some(ed) =>
Ok({
...model,
editors: Editors.put_editor(fromEditor(ed), model.editors),
})
Ok({...model, editors: Editors.put_editor(ed, model.editors)})
};
| Redo =>
let ed = Editors.get_editor(model.editors);
switch (Haz3lcore.Editor.redo(ed)) {
| None => Error(CantRedo)
| Some(ed) =>
Ok({
...model,
editors: Editors.put_editor(fromEditor(ed), model.editors),
})
Ok({...model, editors: Editors.put_editor(ed, model.editors)})
};
| MoveToNextHole(d) =>
perform_action(model, Move(Goal(Piece(Grout, d))))
Expand Down
50 changes: 26 additions & 24 deletions src/haz3lweb/view/assistant/UpdateAssistant.re
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
open Haz3lcore;
include UpdateAction;

let fromEditor = (editor: Editor.t): DocumentationEnv.state => {
{
pos: DocumentationEnv.YourImpl,
eds: {
title: "",
description: "",
your_impl: Editor.init(Zipper.init()),
hidden_tests: {
tests: editor,
hints: [],
},
},
};
};
// let fromEditor = (editor: Editor.t): ScratchSlide.state => {
// title: "",
// description: "",
// hidden_tests: {
// tests: editor,
// hints: [],
// },
// };

// let fromEditor = (editor: Editor.t): DocumentationEnv.state => {
// {
// pos: DocumentationEnv.YourImpl,
// eds: {
// title: "",
// description: "",
// your_impl: editor,
// hidden_tests: {
// tests: editor,
// hints: [],
// },
// },
// };
// };

/* NOTE: this is duplicated from Update */
let perform_action = (model: Model.t, a: Action.t): Result.t(Model.t) => {
let ed_init = Editors.get_editor(model.editors);
switch (Haz3lcore.Perform.go(~settings=model.settings.core, a, ed_init)) {
| Error(err) => Error(FailedToPerform(err))
| Ok(ed) =>
Ok({
...model,
editors: Editors.put_editor(fromEditor(ed), model.editors),
})
| Ok(ed) => Ok({...model, editors: Editors.put_editor(ed, model.editors)})
};
};

Expand All @@ -39,10 +44,7 @@ let reset_buffer = (model: Model.t) => {
| Ok(z) =>
let ed = Editor.new_state(Destruct(Left), z, ed);
//TODO(andrew): fix double action
{
...model,
editors: Editors.put_editor(fromEditor(ed), model.editors),
};
{...model, editors: Editors.put_editor(ed, model.editors)};
}
| _ => model
};
Expand All @@ -67,7 +69,7 @@ let apply =
| Some(z) =>
let ed = Editor.new_state(Pick_up, z, editor);
//TODO: add correct action to history (Pick_up is wrong)
let editors = Editors.put_editor(fromEditor(ed), model.editors);
let editors = Editors.put_editor(ed, model.editors);
Ok({...model, editors});
};
| AcceptSuggestion =>
Expand Down

0 comments on commit a0ab52c

Please sign in to comment.