diff --git a/src/CallBlock.tsx b/src/CallBlock.tsx index 79834b1..db165d3 100644 --- a/src/CallBlock.tsx +++ b/src/CallBlock.tsx @@ -1,11 +1,12 @@ import { useContext } from "react"; -import "./CallBlock.css"; import { Editor, type EditorState, type SetStateFn, type State2Props } from "./Editor"; -import { EnvContext, functionWith3Params, functionWith4Params } from "./EnvContext"; +import { EnvContext } from "./EnvContext"; import { evalCallBlock2, evalEditorBlock, scoreResolved, type ResolvedType } from "./eval"; -import { Value } from "./Value"; import { GlobalContext } from "./GlobalContext"; +import { Value } from "./Value"; + +import "./CallBlock.css"; export interface CallBlockState { kind: "call"; @@ -18,20 +19,36 @@ interface CallBlockProps< InputState=EditorState, > extends State2Props {} -function headlessCallBlock(setState: (callback: SetStateFn) => void) { +function nestedFnProperties({state, setState, suggestionPriority}: CallBlockProps, env) { const setFn = (callback: SetStateFn) => { setState(state => ({...state, fn: callback(state.fn)})); } - const setInput = (callback: SetStateFn) => { - setState(state => ({...state, input: callback(state.input)})); - } const onFnCancel = () => { setState(state => state.input); // we become our input } + const fnSuggestionPriority = (fnSuggestion: ResolvedType) => computePriority( + fnSuggestion, + evalEditorBlock(state.input, env), + suggestionPriority, + env, + ); + return {state: state.fn, setState: setFn, onCancel: onFnCancel, suggestionPriority: fnSuggestionPriority}; +} + +function nestedInputProperties({state, setState, suggestionPriority}: CallBlockProps, env) { + const setInput = (callback: SetStateFn) => { + setState(state => ({...state, input: callback(state.input)})); + } const onInputCancel = () => { setState(state => state.fn); // we become our function } - return {setFn, setInput, onFnCancel, onInputCancel}; + const inputSuggestionPriorirty = (inputSuggestion: ResolvedType) => computePriority( + evalEditorBlock(state.fn, env), // fn *may* be set + inputSuggestion, // suggestions will be for input + suggestionPriority, // priority function we get from parent block + env, + ) + return {state: state.input, setState: setInput, onCancel: onInputCancel, suggestionPriority: inputSuggestionPriorirty}; } export function CallBlock({ state, setState, suggestionPriority }: CallBlockProps) { @@ -70,37 +87,17 @@ function computePriority(fn: ResolvedType, input: ResolvedType, outPriority: (s: function FunctionHeader({ state, setState, suggestionPriority }) { const env = useContext(EnvContext); const globalContext = useContext(GlobalContext); - const {setFn, onFnCancel} = headlessCallBlock(setState); + const nestedProperties = nestedFnProperties({state, setState, suggestionPriority}, env); if (state.fn.kind === "call" && globalContext?.syntacticSugar) { // if the function we're calling is itself the result of a function call, // then we are anonymous, and so we don't draw a function name - return computePriority( - fnSuggestion, - evalEditorBlock(state.input, env), - suggestionPriority, - env, - )} - />; + return ; } else { // end of recursion - draw function name return -  𝑓𝑛  - computePriority( - fnSuggestion, // suggestions will be for function - evalEditorBlock(state.input, env), // input *may* be set - suggestionPriority, // priority function we get from parent block - env, - )} - /> +  𝑓𝑛  + ; } } @@ -108,42 +105,18 @@ function FunctionHeader({ state, setState, suggestionPriority }) { function InputParams({ state, setState, suggestionPriority, depth, errorDepth }) { const env = useContext(EnvContext); const globalContext = useContext(GlobalContext); - const {setFn, setInput, onInputCancel} = headlessCallBlock(setState); - let nestedParams; - if (state.fn.kind === "call" && globalContext?.syntacticSugar) { - // Nest input of nested function - nestedParams = computePriority( - evalEditorBlock(state.fn.fn, env), - inputSuggestion, - suggestionPriority, - env, - )} - depth={depth+1} - errorDepth={errorDepth} - />; - } - else { - nestedParams = <>; - } const isOffending = depth === errorDepth; return
- {nestedParams} + {state.fn.kind === "call" + && globalContext?.syntacticSugar + && } {/* Our own input param */} computePriority( - evalEditorBlock(state.fn, env), // fn *may* be set - inputSuggestion, // suggestions will be for input - suggestionPriority, // priority function we get from parent block - env, - )} + {...nestedInputProperties({state, setState, suggestionPriority}, env)} />
; }