hitting spacebar always adds a parameter to the first ancestor that is a CallBlock

This commit is contained in:
Joeri Exelmans 2025-05-20 09:02:19 +02:00
parent 5b6bcf5ffa
commit 5c3018b8c7
8 changed files with 140 additions and 43 deletions

View file

@ -10,7 +10,7 @@ import { LambdaBlock, type LambdaBlockState } from "./LambdaBlock";
import { LetInBlock, type LetInBlockState } from "./LetInBlock";
import { Type } from "./Type";
import { initialEditorState } from "./configurations";
import { evalEditorBlock, type ResolvedType } from "./eval";
import { evalEditorBlock, removeFocus, type ResolvedType } from "./eval";
import { focusNextElement, focusPrevElement } from "./util/dom_trickery";
import "./ExprBlock.css";
@ -31,6 +31,7 @@ export interface State2Props<InType, OutType = InType> {
interface ExprBlockProps extends State2Props<ExprBlockState> {
onCancel: () => void;
addParam: (e: ExprBlockState) => void;
}
function getCommands(type) {
@ -47,20 +48,7 @@ function getShortCommands(type) {
return 'Tab|.';
}
function removeFocus(state: ExprBlockState): ExprBlockState {
if (state.kind === "input") {
return {...state, focus: false};
}
if (state.kind === "call") {
return {...state,
fn: removeFocus(state.fn),
input: removeFocus(state.input),
};
}
return state;
}
export function ExprBlock({state, setState, onCancel, suggestionPriority}: ExprBlockProps) {
export function ExprBlock({state, setState, suggestionPriority, onCancel, addParam}: ExprBlockProps) {
const env = useContext(EnvContext);
const [needCommand, setNeedCommand] = useState(false);
const commandInputRef = useRef<HTMLInputElement>(null);
@ -95,7 +83,6 @@ export function ExprBlock({state, setState, onCancel, suggestionPriority}: ExprB
kind: "call",
fn: removeFocus(state),
input: initialEditorState,
resolved: undefined,
}));
globalContext?.doHighlight.call();
// focusNextElement();
@ -108,7 +95,6 @@ export function ExprBlock({state, setState, onCancel, suggestionPriority}: ExprB
kind: "call",
fn: initialEditorState,
input: removeFocus(state),
resolved: undefined,
}));
globalContext?.doHighlight.transform();
return;
@ -127,9 +113,10 @@ export function ExprBlock({state, setState, onCancel, suggestionPriority}: ExprB
// we become LetInBlock
setState(state => ({
kind: "let",
inner: removeFocus(initialEditorState),
name: "",
focus: true,
value: removeFocus(state),
inner: removeFocus(initialEditorState),
}));
globalContext?.doHighlight.let();
return;
@ -137,9 +124,10 @@ export function ExprBlock({state, setState, onCancel, suggestionPriority}: ExprB
if (e.key === 'L' || e.key === '=' && e.shiftKey) {
setState(state => ({
kind: "let",
inner: removeFocus(state),
name: "",
focus: true,
value: removeFocus(initialEditorState),
inner: removeFocus(state),
}));
}
// a -> lAmbdA
@ -147,6 +135,7 @@ export function ExprBlock({state, setState, onCancel, suggestionPriority}: ExprB
setState(state => ({
kind: "lambda",
paramName: "",
focus: true,
expr: removeFocus(state),
}));
globalContext?.doHighlight.lambda();
@ -162,6 +151,7 @@ export function ExprBlock({state, setState, onCancel, suggestionPriority}: ExprB
setState={setState as (callback:(p:InputBlockState)=>ExprBlockState)=>void}
suggestionPriority={suggestionPriority}
onCancel={onCancel}
addParam={addParam}
/>;
case "call":
return <CallBlock
@ -174,12 +164,14 @@ export function ExprBlock({state, setState, onCancel, suggestionPriority}: ExprB
state={state}
setState={setState as (callback:(p:LetInBlockState)=>ExprBlockState)=>void}
suggestionPriority={suggestionPriority}
addParam={addParam}
/>;
case "lambda":
return <LambdaBlock
state={state}
setState={setState as (callback:(p:LambdaBlockState)=>ExprBlockState)=>void}
suggestionPriority={suggestionPriority}
addParam={addParam}
/>;
}
}