simplify suggestions ordering

This commit is contained in:
Joeri Exelmans 2025-05-20 16:53:04 +02:00
parent fdbf43a4e9
commit bb6e742f5f
8 changed files with 109 additions and 110 deletions

View file

@ -2,14 +2,14 @@ import { useContext } from "react";
import { getType } from "dope2";
import { CallBlock, type CallBlockState } from "./CallBlock";
import { CallBlock, type CallBlockProps, type CallBlockState } from "./CallBlock";
import { EnvContext } from "./EnvContext";
import { GlobalContext } from "./GlobalContext";
import { InputBlock, type InputBlockState } from "./InputBlock";
import { LambdaBlock, type LambdaBlockState } from "./LambdaBlock";
import { LetInBlock, type LetInBlockState } from "./LetInBlock";
import { InputBlock, type InputBlockProps, type InputBlockState } from "./InputBlock";
import { LambdaBlock, type LambdaBlockProps, type LambdaBlockState } from "./LambdaBlock";
import { LetInBlock, type LetInBlockProps, type LetInBlockState } from "./LetInBlock";
import { Type } from "./Type";
import { evalEditorBlock, type ResolvedType } from "./eval";
import { evalExprBlock, type ResolvedType } from "./eval";
import "./ExprBlock.css";
import { Input } from "./Input";
@ -26,48 +26,31 @@ export type SetStateFn<InType = ExprBlockState, OutType = InType> = (state: InTy
export interface State2Props<InType, OutType = InType> {
state: InType;
setState: (callback: SetStateFn<InType, OutType>) => void;
suggestionPriority: (suggestion: ResolvedType) => number;
score: (suggestion: ResolvedType) => number;
}
interface ExprBlockProps extends State2Props<ExprBlockState> {
onCancel: () => void;
}
export function ExprBlock({state, setState, suggestionPriority, onCancel}: ExprBlockProps) {
export function ExprBlock(props: ExprBlockProps) {
const env = useContext(EnvContext);
const globalContext = useContext(GlobalContext);
const renderBlock = {
input: () => <InputBlock
state={state as InputBlockState}
setState={setState as (callback:(p:InputBlockState)=>ExprBlockState)=>void}
suggestionPriority={suggestionPriority}
onCancel={onCancel}
/>,
call: () => <CallBlock
state={state as CallBlockState}
setState={setState as (callback:(p:CallBlockState)=>ExprBlockState)=>void}
suggestionPriority={suggestionPriority}
/>,
let: () => <LetInBlock
state={state as LetInBlockState}
setState={setState as (callback:(p:LetInBlockState)=>ExprBlockState)=>void}
suggestionPriority={suggestionPriority}
/>,
lambda: () => <LambdaBlock
state={state as LambdaBlockState}
setState={setState as (callback:(p:LambdaBlockState)=>ExprBlockState)=>void}
suggestionPriority={suggestionPriority}
/>,
input: () => <InputBlock {...props as InputBlockProps} />,
call: () => <CallBlock {...props as CallBlockProps} />,
let: () => <LetInBlock {...props as LetInBlockProps} />,
lambda: () => <LambdaBlock {...props as LambdaBlockProps} />,
};
const [resolved] = evalEditorBlock(state, env);
const actions = getActions(globalContext, setState);
const [resolved] = evalExprBlock(props.state, env);
const actions = getActions(globalContext, props.setState);
const extraHandlers = Object.fromEntries(Object.entries(actions).map(([shortcut, action]) =>
[shortcut, (e) => { e.preventDefault(); action(); }]))
return <span className={"editor" + ((resolved.kind!=="value") ? " "+resolved.kind : "")}>
{renderBlock[state.kind]()}
{renderBlock[props.state.kind]()}
<div className="typeSignature">
&nbsp;::&nbsp;<Type type={getType(resolved)} />
</div>
@ -76,7 +59,7 @@ export function ExprBlock({state, setState, suggestionPriority, onCancel}: ExprB
text=""
suggestion=""
onEnter={() => {}}
onCancel={onCancel}
onCancel={props.onCancel}
onTextChange={() => {}}
extraHandlers={extraHandlers}
/>