Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evaluate all code blocks below current block with same scope #35

Merged
merged 2 commits into from
Jul 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion client/src_analyzer/Worker_Analyze.re
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ module Make = (ESig: Worker_Evaluator.EvaluatorSig) => {
}
)
);
let execute: (. bool, string) => list(Worker_Types.blockData) =
let execute: (. bool, string) => list(blockData) =
(. reset, code) => {
if (reset) {
Evaluator.reset();
Expand All @@ -213,4 +213,12 @@ module Make = (ESig: Worker_Evaluator.EvaluatorSig) => {
});
result;
};

let executeMany:
(. Belt.Map.String.t(string)) => Belt.Map.String.t(list(blockData)) =
(. codeMap) => {
/* Reset before evaluating several blocks */
Evaluator.reset();
codeMap |. Belt.Map.String.map(code => execute(. false, code));
};
};
2 changes: 1 addition & 1 deletion client/src_analyzer/Worker_Index.re
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ Worker.importScripts("/reason.js");

module Analyze = Worker_Analyze.Make(Worker_BrowserEvaluator);

let obj = {"execute": Analyze.execute};
let obj = {"execute": Analyze.execute, "executeMany": Analyze.executeMany};

Comlink.(comlink |. expose(obj, Worker.self));
107 changes: 63 additions & 44 deletions client/src_editor/Editor_Blocks.re
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type state = {

type action =
| Block_Add(id, blockData)
| Block_Execute(id)
| Block_Execute
| Block_Delete(id)
| Block_Focus(id, blockTyp)
| Block_Blur(id)
Expand Down Expand Up @@ -38,11 +38,10 @@ let blockControlsButtons = (b_id, send) =>
</button>
</div>;

let blockHint = (b_id, send) =>
let blockHint = send =>
<div className="cell__controls-hint">
<button
onClick=(_ => send(Block_Execute(b_id)))
className="cell__controls-hint--run">
onClick=(_ => send(Block_Execute)) className="cell__controls-hint--run">
("run" |. str)
</button>
<span>
Expand All @@ -59,8 +58,7 @@ let make = (~blocks: array(block), _children) => {
...component,
initialState: () => {blocks, focusedBlock: None},
didMount: self => {
blocks
|. Belt.Array.forEachU((. {b_id}) => self.send(Block_Execute(b_id)));
self.send(Block_Execute);
();
},
reducer: (action, state) =>
Expand All @@ -85,44 +83,57 @@ let make = (~blocks: array(block), _children) => {
};
}),
})
| Block_Execute(blockId) =>
let block = state.blocks |. arrayFind(({b_id}) => b_id == blockId);
switch (block) {
| None => ReasonReact.NoUpdate
| Some(block) =>
switch (block.b_data) {
| B_Text(_) => ReasonReact.NoUpdate
| B_Code({bc_value}) =>
ReasonReact.SideEffects(
(
self =>
Js.Promise.(
Editor_Worker.execute(. true, bc_value)
|> then_(result => {
let widgets =
Editor_Blocks_Utils.executeRessultToWidget(result);
resolve(
self.send(Block_AddWidgets(blockId, widgets)),
);
})
|> catch(error => resolve(Js.log(error)))
|> ignore
)
),
)
}
};
| Block_Execute =>
module MS = Belt.Map.String;

let allCodeBlocks =
state.blocks
|. Belt.Array.reduceU(MS.empty, (. map, {b_id, b_data}) =>
switch (b_data) {
| B_Text(_) => map
| B_Code({bc_value}) => map |. MS.set(b_id, bc_value)
}
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whaaaaaaat. This really works huh?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It actually does! 💯


/* Clear all widgets and execute all blocks */
ReasonReact.SideEffects(
(
self =>
Js.Promise.(
Editor_Worker.executeMany(. allCodeBlocks)
|> then_(results => {
results
|. MS.forEachU((. blockId, result) => {
let widgets =
Editor_Blocks_Utils.executeResultToWidget(result);
self.send(Block_AddWidgets(blockId, widgets));
});

resolve();
})
|> catch(error => resolve(Js.log(error)))
|> ignore
)
),
);
| Block_UpdateValue(blockId, newValue, diff) =>
let foundIndex =
arrayFindIndex(state.blocks, ({b_id}) => b_id == blockId);
let blockIndex =
switch (foundIndex) {
| None => (-1)
| Some(i) => i
};

ReasonReact.Update({
...state,
blocks:
state.blocks
|. Belt.Array.mapU((. block) => {
|. Belt.Array.mapWithIndexU((. i, block) => {
let {b_id, b_data} = block;
if (b_id != blockId) {
if (i < blockIndex) {
block;
} else {
} else if (i == blockIndex) {
switch (b_data) {
| B_Code(bcode) => {
b_id,
Expand All @@ -144,10 +155,18 @@ let make = (~blocks: array(block), _children) => {
}
| B_Text(_) => {b_id, b_data: B_Text(newValue)}
};
} else {
switch (b_data) {
| B_Text(_) => block
| B_Code(bcode) => {
...block,
b_data: B_Code({...bcode, bc_widgets: [||]}),
}
};
};
})
|. Editor_Blocks_Utils.syncLineNumber,
})
});
| Block_Delete(blockId) =>
ReasonReact.Update({
blocks:
Expand Down Expand Up @@ -199,7 +218,7 @@ let make = (~blocks: array(block), _children) => {
|. Editor_Blocks_Utils.syncLineNumber,
})
| Block_FocusUp(blockId) =>
let upperBlockId = {
let upperBlock = {
let rec loop = i =>
if (i >= 0) {
let {b_id} = state.blocks[i];
Expand All @@ -217,7 +236,7 @@ let make = (~blocks: array(block), _children) => {
};
loop((state.blocks |. Belt.Array.length) - 1);
};
switch (upperBlockId) {
switch (upperBlock) {
| None => ReasonReact.NoUpdate
| Some((upperBlockId, blockTyp)) =>
ReasonReact.Update({
Expand All @@ -226,7 +245,7 @@ let make = (~blocks: array(block), _children) => {
})
};
| Block_FocusDown(blockId) =>
let lowerBlockId = {
let lowerBlock = {
let length = state.blocks |. Belt.Array.length;
let rec loop = i =>
if (i < length) {
Expand All @@ -245,7 +264,7 @@ let make = (~blocks: array(block), _children) => {
};
loop(0);
};
switch (lowerBlockId) {
switch (lowerBlock) {
| None => ReasonReact.NoUpdate
| Some((lowerBlockId, blockTyp)) =>
ReasonReact.Update({
Expand Down Expand Up @@ -277,7 +296,7 @@ let make = (~blocks: array(block), _children) => {
(newValue, diff) =>
send(Block_UpdateValue(b_id, newValue, diff))
)
onExecute=(() => send(Block_Execute(b_id)))
onExecute=(() => send(Block_Execute))
onFocus=(() => send(Block_Focus(b_id, BTyp_Code)))
onBlur=(() => send(Block_Blur(b_id)))
onBlockUp=(() => send(Block_FocusUp(b_id)))
Expand All @@ -296,13 +315,13 @@ let make = (~blocks: array(block), _children) => {
switch (state.focusedBlock) {
| Some((focusedBlock, BTyp_Code, _)) =>
focusedBlock == b_id ?
blockHint(b_id, send) : ReasonReact.null
blockHint(send) : ReasonReact.null
| _ =>
lastCodeBlockId
=>> (
last_b_id =>
last_b_id == b_id ?
blockHint(b_id, send) : ReasonReact.null
blockHint(send) : ReasonReact.null
)
}
)
Expand Down
2 changes: 1 addition & 1 deletion client/src_editor/Editor_Blocks_Utils.re
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let renderErrorIndicator = (colStart, colEnd, content) =>
++ "\n"
++ content;

let executeRessultToWidget = (result: list(Worker_Types.blockData)) => {
let executeResultToWidget = (result: list(Worker_Types.blockData)) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I blame @-jaredly for this. His reason-language-server is so good that I just tab tab tab

open Worker_Types;
open Editor_CodeBlockTypes;

Expand Down
9 changes: 5 additions & 4 deletions client/src_editor/Editor_Worker.re
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ type js_executeResult = {
[@bs.deriving abstract]
type rtop = {
execute: (. bool, string) => Js.Promise.t(list(Worker_Types.blockData)),
/* reset: (. unit) => Js.Promise.t(unit),
reasonSyntax: (. unit) => Js.Promise.t(unit),
mlSyntax: (. unit) => Js.Promise.t(unit),
parseError: (. string, string) => Js.Promise.t(string), */
executeMany:
(. Belt.Map.String.t(string)) =>
Js.Promise.t(Belt.Map.String.t(list(Worker_Types.blockData))),
};
let worker = RtopWorker.make();

let rtop: rtop = Comlink.comlink |. Comlink.proxy(worker);

let execute = rtop |. executeGet;

let executeMany = rtop |. executeManyGet;