fix suggestion order
This commit is contained in:
parent
46baad7cf4
commit
1ff4b181ff
8 changed files with 91 additions and 168 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import './App.css';
|
import './App.css';
|
||||||
import { CommandContext } from './CommandContext';
|
import { GlobalContext } from './GlobalContext';
|
||||||
import { Editor, type EditorState } from './Editor';
|
import { Editor, type EditorState } from './Editor';
|
||||||
import { extendedEnv } from './EnvContext';
|
import { extendedEnv } from './EnvContext';
|
||||||
import { biggerExample, initialEditorState, lambda2Params, nonEmptyEditorState, tripleFunctionCallEditorState } from "./configurations";
|
import { biggerExample, initialEditorState, lambda2Params, nonEmptyEditorState, tripleFunctionCallEditorState } from "./configurations";
|
||||||
|
|
@ -161,14 +161,14 @@ export function App() {
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
<main onKeyDown={onKeyDown}>
|
<main onKeyDown={onKeyDown}>
|
||||||
<CommandContext value={{undo: onUndo, redo: onRedo, doHighlight, syntacticSugar}}>
|
<GlobalContext value={{undo: onUndo, redo: onRedo, doHighlight, syntacticSugar}}>
|
||||||
<Editor
|
<Editor
|
||||||
state={appState.history.at(-1)!}
|
state={appState.history.at(-1)!}
|
||||||
setState={pushHistory}
|
setState={pushHistory}
|
||||||
onCancel={() => {}}
|
onCancel={() => {}}
|
||||||
suggestionPriority={() => 1}
|
suggestionPriority={() => 1}
|
||||||
/>
|
/>
|
||||||
</CommandContext>
|
</GlobalContext>
|
||||||
|
|
||||||
|
|
||||||
</main>
|
</main>
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,11 @@
|
||||||
import { useContext, useInsertionEffect } from "react";
|
import { useContext } from "react";
|
||||||
|
|
||||||
import { Editor, type EditorState } from "./Editor";
|
|
||||||
import { Value } from "./Value";
|
|
||||||
import { type SetStateFn, type State2Props } from "./Editor";
|
|
||||||
import { evalCallBlock, evalCallBlock2, evalEditorBlock, scoreResolved } from "./eval";
|
|
||||||
import { type ResolvedType } from "./eval";
|
|
||||||
import "./CallBlock.css";
|
import "./CallBlock.css";
|
||||||
|
import { Editor, type EditorState, type SetStateFn, type State2Props } from "./Editor";
|
||||||
import { EnvContext } from "./EnvContext";
|
import { EnvContext } from "./EnvContext";
|
||||||
import type { SuggestionType } from "./InputBlock";
|
import { evalCallBlock2, evalEditorBlock, scoreResolved, type ResolvedType } from "./eval";
|
||||||
import { UnifyError } from "dope2";
|
import { Value } from "./Value";
|
||||||
|
import { GlobalContext } from "./GlobalContext";
|
||||||
|
|
||||||
export interface CallBlockState {
|
export interface CallBlockState {
|
||||||
kind: "call";
|
kind: "call";
|
||||||
|
|
@ -19,9 +16,7 @@ export interface CallBlockState {
|
||||||
interface CallBlockProps<
|
interface CallBlockProps<
|
||||||
FnState=EditorState,
|
FnState=EditorState,
|
||||||
InputState=EditorState,
|
InputState=EditorState,
|
||||||
> extends State2Props<CallBlockState,EditorState> {
|
> extends State2Props<CallBlockState,EditorState> {}
|
||||||
suggestionPriority: (suggestion: SuggestionType) => number;
|
|
||||||
}
|
|
||||||
|
|
||||||
function headlessCallBlock(setState: (callback: SetStateFn<CallBlockState,EditorState>) => void) {
|
function headlessCallBlock(setState: (callback: SetStateFn<CallBlockState,EditorState>) => void) {
|
||||||
const setFn = (callback: SetStateFn) => {
|
const setFn = (callback: SetStateFn) => {
|
||||||
|
|
@ -40,97 +35,51 @@ function headlessCallBlock(setState: (callback: SetStateFn<CallBlockState,Editor
|
||||||
}
|
}
|
||||||
|
|
||||||
export function CallBlock({ state, setState, suggestionPriority }: CallBlockProps) {
|
export function CallBlock({ state, setState, suggestionPriority }: CallBlockProps) {
|
||||||
const {setFn, setInput, onFnCancel, onInputCancel}
|
|
||||||
= headlessCallBlock(setState);
|
|
||||||
const env = useContext(EnvContext);
|
const env = useContext(EnvContext);
|
||||||
const resolved = evalEditorBlock(state, env);
|
const resolved = evalEditorBlock(state, env);
|
||||||
return <span className={"functionBlock" + ((resolved.kind === "error") ? " unifyError" : "")}>
|
return <span className={"functionBlock" + ((resolved.kind === "error") ? " unifyError" : "")}>
|
||||||
<FunctionHeader
|
<FunctionHeader
|
||||||
fn={state.fn}
|
state={state}
|
||||||
setFn={setFn}
|
setState={setState}
|
||||||
onFnCancel={onFnCancel}
|
|
||||||
input={state.input}
|
|
||||||
suggestionPriority={suggestionPriority}
|
suggestionPriority={suggestionPriority}
|
||||||
/>
|
/>
|
||||||
<div className="functionParams">
|
<div className="functionParams">
|
||||||
<Output resolved={resolved}>
|
<div className="outputParam">
|
||||||
{/* Sequence of input parameters */}
|
{/* Sequence of input parameters */}
|
||||||
<InputParams
|
<InputParams
|
||||||
fn={state.fn} setFn={setFn}
|
state={state}
|
||||||
input={state.input} setInput={setInput}
|
setState={setState}
|
||||||
onInputCancel={onInputCancel}
|
suggestionPriority={suggestionPriority}
|
||||||
depth={0}
|
depth={0}
|
||||||
errorDepth={(resolved.kind === "error") ? (resolved.depth) : -1}
|
errorDepth={(resolved.kind === "error") ? (resolved.depth) : -1}
|
||||||
suggestionPriority={suggestionPriority}
|
|
||||||
/>
|
/>
|
||||||
</Output>
|
|
||||||
</div>
|
|
||||||
</span>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function Output({resolved, children}) {
|
|
||||||
return <div className="outputParam">
|
|
||||||
{children}
|
|
||||||
{ (resolved.kind === "error") && resolved.e.toString()
|
{ (resolved.kind === "error") && resolved.e.toString()
|
||||||
|| (resolved.kind === "value") && <Value dynamic={resolved} />
|
|| (resolved.kind === "value") && <Value dynamic={resolved} />
|
||||||
|| "unknown" }
|
|| "unknown" }
|
||||||
</div>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function CallBlockNoSugar({ state, setState, suggestionPriority }: CallBlockProps) {
|
|
||||||
const {setFn, setInput, onFnCancel, onInputCancel}
|
|
||||||
= headlessCallBlock(setState);
|
|
||||||
const env = useContext(EnvContext);
|
|
||||||
const resolved = evalEditorBlock(state, env);
|
|
||||||
const isOffending = (resolved.kind === "error") ? (resolved.depth===0) : false;
|
|
||||||
return <span className={"functionBlock" + ((resolved.kind === "error") ? " unifyError" : "")}>
|
|
||||||
<FunctionName fn={state.fn} setFn={setFn} onFnCancel={onFnCancel} suggestionPriority={suggestionPriority} input={state.input} />
|
|
||||||
<div className="functionParams">
|
|
||||||
<Output resolved={resolved}>
|
|
||||||
<div className={"inputParam" + (isOffending ? " offending" : "")}>
|
|
||||||
<Editor
|
|
||||||
state={state.input}
|
|
||||||
setState={setInput}
|
|
||||||
onCancel={onInputCancel}
|
|
||||||
suggestionPriority={
|
|
||||||
(inputSuggestion: SuggestionType) => computePriority(
|
|
||||||
evalEditorBlock(state.fn, env), // fn *may* be set
|
|
||||||
inputSuggestion[2], // suggestions will be for input
|
|
||||||
suggestionPriority, // priority function we get from parent block
|
|
||||||
env,
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
</Output>
|
|
||||||
</div>
|
</div>
|
||||||
</span>;
|
</span>;
|
||||||
}
|
}
|
||||||
|
|
||||||
function computePriority(fn: ResolvedType, input: ResolvedType, outPriority: (s: SuggestionType) => number, env) {
|
function computePriority(fn: ResolvedType, input: ResolvedType, outPriority: (s: ResolvedType) => number, env) {
|
||||||
const resolved = evalCallBlock2(fn, input, env);
|
const resolved = evalCallBlock2(fn, input, env);
|
||||||
return scoreResolved(resolved, outPriority);
|
const score = scoreResolved(resolved, outPriority);
|
||||||
|
return score;
|
||||||
}
|
}
|
||||||
|
|
||||||
function FunctionHeader({ fn, setFn, input, onFnCancel, suggestionPriority }) {
|
function FunctionHeader({ state, setState, suggestionPriority }) {
|
||||||
const env = useContext(EnvContext);
|
const env = useContext(EnvContext);
|
||||||
if (fn.kind === "call") {
|
const globalContext = useContext(GlobalContext);
|
||||||
|
const {setFn, onFnCancel} = headlessCallBlock(setState);
|
||||||
|
if (state.fn.kind === "call" && globalContext?.syntacticSugar) {
|
||||||
// if the function we're calling is itself the result of a function call,
|
// 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
|
// then we are anonymous, and so we don't draw a function name
|
||||||
|
|
||||||
// recurse:
|
|
||||||
const {
|
|
||||||
setFn : setFnFn,
|
|
||||||
onFnCancel : onFnFnCancel,
|
|
||||||
} = headlessCallBlock(setFn);
|
|
||||||
|
|
||||||
return <FunctionHeader
|
return <FunctionHeader
|
||||||
fn={fn.fn}
|
state={state.fn}
|
||||||
setFn={setFnFn}
|
setState={setFn}
|
||||||
onFnCancel={onFnFnCancel}
|
suggestionPriority={(fnSuggestion: ResolvedType) => computePriority(
|
||||||
input={fn.input}
|
fnSuggestion,
|
||||||
suggestionPriority={(fnSuggestion: SuggestionType) => computePriority(
|
evalEditorBlock(state.fn.input, env),
|
||||||
fnSuggestion[2],
|
|
||||||
evalEditorBlock(fn.input, env),
|
|
||||||
suggestionPriority,
|
suggestionPriority,
|
||||||
env,
|
env,
|
||||||
)}
|
)}
|
||||||
|
|
@ -138,53 +87,43 @@ function FunctionHeader({ fn, setFn, input, onFnCancel, suggestionPriority }) {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// end of recursion - draw function name
|
// end of recursion - draw function name
|
||||||
return <FunctionName fn={fn} setFn={setFn} onFnCancel={onFnCancel} suggestionPriority={suggestionPriority} input={input}/>;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function FunctionName({fn, setFn, onFnCancel, suggestionPriority, input}) {
|
|
||||||
const env = useContext(EnvContext);
|
|
||||||
return <span className="functionName">
|
return <span className="functionName">
|
||||||
𝑓𝑛
|
𝑓𝑛
|
||||||
<Editor
|
<Editor
|
||||||
state={fn}
|
state={state.fn}
|
||||||
setState={setFn}
|
setState={setFn}
|
||||||
onCancel={onFnCancel}
|
onCancel={onFnCancel}
|
||||||
suggestionPriority={
|
suggestionPriority={
|
||||||
(fnSuggestion: SuggestionType) => computePriority(
|
(fnSuggestion: ResolvedType) => computePriority(
|
||||||
fnSuggestion[2], // suggestions will be for function
|
fnSuggestion, // suggestions will be for function
|
||||||
evalEditorBlock(input, env), // input *may* be set
|
evalEditorBlock(state.input, env), // input *may* be set
|
||||||
suggestionPriority, // priority function we get from parent block
|
suggestionPriority, // priority function we get from parent block
|
||||||
env,
|
env,
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
</span>;
|
</span>;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function InputParams({ fn, setFn, input, setInput, onInputCancel, depth, errorDepth, suggestionPriority }) {
|
function InputParams({ state, setState, suggestionPriority, depth, errorDepth }) {
|
||||||
const env = useContext(EnvContext);
|
const env = useContext(EnvContext);
|
||||||
|
const globalContext = useContext(GlobalContext);
|
||||||
|
const {setFn, setInput, onInputCancel} = headlessCallBlock(setState);
|
||||||
let nestedParams;
|
let nestedParams;
|
||||||
if (fn.kind === "call") {
|
if (state.fn.kind === "call" && globalContext?.syntacticSugar) {
|
||||||
// Nest input of nested function
|
// Nest input of nested function
|
||||||
const {
|
|
||||||
setFn : setFnFn,
|
|
||||||
setInput : setFnInput,
|
|
||||||
} = headlessCallBlock(setFn);
|
|
||||||
nestedParams = <InputParams
|
nestedParams = <InputParams
|
||||||
fn={fn.fn}
|
state={state.fn}
|
||||||
setFn={setFnFn}
|
setState={setFn}
|
||||||
input={fn.input}
|
|
||||||
setInput={setFnInput}
|
|
||||||
onInputCancel={() => {/*todo*/}}
|
|
||||||
depth={depth+1}
|
|
||||||
errorDepth={errorDepth}
|
|
||||||
suggestionPriority={
|
suggestionPriority={
|
||||||
(inputSuggestion: SuggestionType) => computePriority(
|
(inputSuggestion: ResolvedType) => computePriority(
|
||||||
evalEditorBlock(fn.fn, env),
|
evalEditorBlock(state.fn.fn, env),
|
||||||
inputSuggestion[2],
|
inputSuggestion,
|
||||||
suggestionPriority,
|
suggestionPriority,
|
||||||
env,
|
env,
|
||||||
)}
|
)}
|
||||||
|
depth={depth+1}
|
||||||
|
errorDepth={errorDepth}
|
||||||
/>;
|
/>;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
@ -195,13 +134,13 @@ function InputParams({ fn, setFn, input, setInput, onInputCancel, depth, errorDe
|
||||||
{nestedParams}
|
{nestedParams}
|
||||||
{/* Our own input param */}
|
{/* Our own input param */}
|
||||||
<Editor
|
<Editor
|
||||||
state={input}
|
state={state.input}
|
||||||
setState={setInput}
|
setState={setInput}
|
||||||
onCancel={onInputCancel}
|
onCancel={onInputCancel}
|
||||||
suggestionPriority={
|
suggestionPriority={
|
||||||
(inputSuggestion: SuggestionType) => computePriority(
|
(inputSuggestion: ResolvedType) => computePriority(
|
||||||
evalEditorBlock(fn, env), // fn *may* be set
|
evalEditorBlock(state.fn, env), // fn *may* be set
|
||||||
inputSuggestion[2], // suggestions will be for input
|
inputSuggestion, // suggestions will be for input
|
||||||
suggestionPriority, // priority function we get from parent block
|
suggestionPriority, // priority function we get from parent block
|
||||||
env,
|
env,
|
||||||
)}
|
)}
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,11 @@ import { useContext, useEffect, useRef, useState } from "react";
|
||||||
|
|
||||||
import { getSymbol, getType, symbolFunction } from "dope2";
|
import { getSymbol, getType, symbolFunction } from "dope2";
|
||||||
|
|
||||||
import { CallBlock, CallBlockNoSugar, type CallBlockState } from "./CallBlock";
|
import { CallBlock, type CallBlockState } from "./CallBlock";
|
||||||
import { InputBlock, type InputBlockState, type SuggestionType } from "./InputBlock";
|
import { InputBlock, type InputBlockState, type SuggestionType } from "./InputBlock";
|
||||||
import { Type } from "./Type";
|
import { Type } from "./Type";
|
||||||
import { evalEditorBlock } from "./eval";
|
import { evalEditorBlock, type ResolvedType } from "./eval";
|
||||||
import { CommandContext } from "./CommandContext";
|
import { GlobalContext } from "./GlobalContext";
|
||||||
import "./Editor.css";
|
import "./Editor.css";
|
||||||
import { EnvContext } from "./EnvContext";
|
import { EnvContext } from "./EnvContext";
|
||||||
import { LambdaBlock, type LambdaBlockState } from "./LambdaBlock";
|
import { LambdaBlock, type LambdaBlockState } from "./LambdaBlock";
|
||||||
|
|
@ -25,10 +25,10 @@ export type SetStateFn<InType = EditorState, OutType = InType> = (state: InType)
|
||||||
export interface State2Props<InType, OutType = InType> {
|
export interface State2Props<InType, OutType = InType> {
|
||||||
state: InType;
|
state: InType;
|
||||||
setState: (callback: SetStateFn<InType, OutType>) => void;
|
setState: (callback: SetStateFn<InType, OutType>) => void;
|
||||||
|
suggestionPriority: (suggestion: ResolvedType) => number;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface EditorProps extends State2Props<EditorState> {
|
interface EditorProps extends State2Props<EditorState> {
|
||||||
suggestionPriority: (suggestion: SuggestionType) => number;
|
|
||||||
onCancel: () => void;
|
onCancel: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -69,7 +69,7 @@ export function Editor({state, setState, onCancel, suggestionPriority}: EditorPr
|
||||||
}
|
}
|
||||||
}, [needCommand]);
|
}, [needCommand]);
|
||||||
|
|
||||||
const globalContext = useContext(CommandContext);
|
const globalContext = useContext(GlobalContext);
|
||||||
const onCommand = (e: React.KeyboardEvent) => {
|
const onCommand = (e: React.KeyboardEvent) => {
|
||||||
const commands = ['e', 't', 'Enter', 'Backspace', 'ArrowLeft', 'ArrowRight', 'Tab', 'l', 'L', '=', '.', 'c', 'a'];
|
const commands = ['e', 't', 'Enter', 'Backspace', 'ArrowLeft', 'ArrowRight', 'Tab', 'l', 'L', '=', '.', 'c', 'a'];
|
||||||
if (!commands.includes(e.key)) {
|
if (!commands.includes(e.key)) {
|
||||||
|
|
@ -163,20 +163,11 @@ export function Editor({state, setState, onCancel, suggestionPriority}: EditorPr
|
||||||
onCancel={onCancel}
|
onCancel={onCancel}
|
||||||
/>;
|
/>;
|
||||||
case "call":
|
case "call":
|
||||||
if (globalContext?.syntacticSugar) {
|
|
||||||
return <CallBlock
|
return <CallBlock
|
||||||
state={state}
|
state={state}
|
||||||
setState={setState as (callback:(p:CallBlockState)=>EditorState)=>void}
|
setState={setState as (callback:(p:CallBlockState)=>EditorState)=>void}
|
||||||
suggestionPriority={suggestionPriority}
|
suggestionPriority={suggestionPriority}
|
||||||
/>;
|
/>;
|
||||||
}
|
|
||||||
else {
|
|
||||||
return <CallBlockNoSugar
|
|
||||||
state={state}
|
|
||||||
setState={setState as (callback:(p:CallBlockState)=>EditorState)=>void}
|
|
||||||
suggestionPriority={suggestionPriority}
|
|
||||||
/>;
|
|
||||||
}
|
|
||||||
case "let":
|
case "let":
|
||||||
return <LetInBlock
|
return <LetInBlock
|
||||||
state={state}
|
state={state}
|
||||||
|
|
|
||||||
|
|
@ -7,4 +7,4 @@ interface GlobalActions {
|
||||||
syntacticSugar: boolean;
|
syntacticSugar: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const CommandContext = createContext<GlobalActions|null>(null);
|
export const GlobalContext = createContext<GlobalActions|null>(null);
|
||||||
|
|
@ -3,7 +3,7 @@ import { memo, useContext, useEffect, useMemo, useRef, useState } from "react";
|
||||||
import { getType, prettyT, trie } from "dope2";
|
import { getType, prettyT, trie } from "dope2";
|
||||||
|
|
||||||
import { EnvContext } from "./EnvContext";
|
import { EnvContext } from "./EnvContext";
|
||||||
import type { Dynamic } from "./eval";
|
import type { Dynamic, ResolvedType } from "./eval";
|
||||||
import "./InputBlock.css";
|
import "./InputBlock.css";
|
||||||
import { Type } from "./Type";
|
import { Type } from "./Type";
|
||||||
import type { State2Props } from "./Editor";
|
import type { State2Props } from "./Editor";
|
||||||
|
|
@ -33,11 +33,10 @@ export type SuggestionType = ['literal'|'name', string, Dynamic];
|
||||||
export type PrioritizedSuggestionType = [number, ...SuggestionType];
|
export type PrioritizedSuggestionType = [number, ...SuggestionType];
|
||||||
|
|
||||||
interface InputBlockProps extends State2Props<InputBlockState> {
|
interface InputBlockProps extends State2Props<InputBlockState> {
|
||||||
suggestionPriority: (suggestion: SuggestionType) => number;
|
|
||||||
onCancel: () => void;
|
onCancel: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const computeSuggestions = (text, env, suggestionPriority: (s: SuggestionType) => number): PrioritizedSuggestionType[] => {
|
const computeSuggestions = (text, env, suggestionPriority: (s: ResolvedType) => number): PrioritizedSuggestionType[] => {
|
||||||
const literals = attemptParseLiteral(text, env);
|
const literals = attemptParseLiteral(text, env);
|
||||||
|
|
||||||
const ls: SuggestionType[] = [
|
const ls: SuggestionType[] = [
|
||||||
|
|
@ -46,11 +45,17 @@ const computeSuggestions = (text, env, suggestionPriority: (s: SuggestionType) =
|
||||||
|
|
||||||
// names
|
// names
|
||||||
... trie.suggest(env.name2dyn)(text)(Infinity)
|
... trie.suggest(env.name2dyn)(text)(Infinity)
|
||||||
.map(([name,type]) => ["name", name, {...type, substitutions: new Map()}]),
|
.map(([name,type]) => [
|
||||||
|
"name",
|
||||||
|
name, {
|
||||||
|
...type,
|
||||||
|
substitutions: type.substitutions || new Map(),
|
||||||
|
kind: type.kind || "value",
|
||||||
|
}]),
|
||||||
]
|
]
|
||||||
// return ls;
|
// return ls;
|
||||||
return ls
|
return ls
|
||||||
.map(suggestion => [suggestionPriority(suggestion), ...suggestion] as PrioritizedSuggestionType)
|
.map(suggestion => [suggestionPriority(suggestion[2]), ...suggestion] as PrioritizedSuggestionType)
|
||||||
.sort(([priorityA], [priorityB]) => priorityB - priorityA)
|
.sort(([priorityA], [priorityB]) => priorityB - priorityA)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,14 @@
|
||||||
import { useContext, useEffect, useRef } from "react";
|
import { useContext, useEffect, useRef } from "react";
|
||||||
|
|
||||||
|
import { growEnv } from "dope2";
|
||||||
|
|
||||||
import { Editor, type EditorState, type State2Props } from "./Editor";
|
import { Editor, type EditorState, type State2Props } from "./Editor";
|
||||||
import type { SuggestionType } from "./InputBlock";
|
|
||||||
import { EnvContext } from "./EnvContext";
|
import { EnvContext } from "./EnvContext";
|
||||||
import { growEnv, TYPE_VARS } from "dope2";
|
import { getUnusedTypeVar } from "./eval";
|
||||||
import { autoInputWidth } from "./util/dom_trickery";
|
import { autoInputWidth } from "./util/dom_trickery";
|
||||||
|
|
||||||
import "./LambdaBlock.css";
|
import "./LambdaBlock.css";
|
||||||
import { getUnusedTypeVar } from "./eval";
|
|
||||||
|
|
||||||
|
|
||||||
export interface LambdaBlockState {
|
export interface LambdaBlockState {
|
||||||
|
|
@ -19,9 +20,7 @@ export interface LambdaBlockState {
|
||||||
interface LambdaBlockProps<
|
interface LambdaBlockProps<
|
||||||
FnState=EditorState,
|
FnState=EditorState,
|
||||||
InputState=EditorState,
|
InputState=EditorState,
|
||||||
> extends State2Props<LambdaBlockState,EditorState> {
|
> extends State2Props<LambdaBlockState,EditorState> {}
|
||||||
suggestionPriority: (suggestion: SuggestionType) => number;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export function LambdaBlock({state, setState, suggestionPriority}: LambdaBlockProps) {
|
export function LambdaBlock({state, setState, suggestionPriority}: LambdaBlockProps) {
|
||||||
|
|
@ -78,9 +77,7 @@ export function LambdaBlock({state, setState, suggestionPriority}: LambdaBlockPr
|
||||||
state={state.expr}
|
state={state.expr}
|
||||||
setState={setExpr}
|
setState={setExpr}
|
||||||
onCancel={() => setState(state => state.expr)}
|
onCancel={() => setState(state => state.expr)}
|
||||||
suggestionPriority={(suggestion: SuggestionType) => {
|
suggestionPriority={suggestionPriority}
|
||||||
return suggestionPriority(suggestion);
|
|
||||||
}}
|
|
||||||
/>
|
/>
|
||||||
</EnvContext>
|
</EnvContext>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,13 @@
|
||||||
import { useContext, useEffect, useRef } from "react";
|
import { useContext, useEffect, useRef } from "react";
|
||||||
|
|
||||||
|
|
||||||
import { Editor, type EditorState } from "./Editor";
|
import { Editor, type EditorState } from "./Editor";
|
||||||
import { EnvContext } from "./EnvContext";
|
import { EnvContext } from "./EnvContext";
|
||||||
import { evalEditorBlock, makeInnerEnv, scoreResolved } from "./eval";
|
import { evalEditorBlock, makeInnerEnv, scoreResolved, type ResolvedType } from "./eval";
|
||||||
import { type State2Props } from "./Editor";
|
import { type State2Props } from "./Editor";
|
||||||
import { autoInputWidth } from "./util/dom_trickery";
|
import { autoInputWidth } from "./util/dom_trickery";
|
||||||
|
import { GlobalContext } from "./GlobalContext";
|
||||||
|
|
||||||
import "./LetInBlock.css";
|
import "./LetInBlock.css";
|
||||||
import type { SuggestionType } from "./InputBlock";
|
|
||||||
import { CommandContext } from "./CommandContext";
|
|
||||||
|
|
||||||
export interface LetInBlockState {
|
export interface LetInBlockState {
|
||||||
kind: "let";
|
kind: "let";
|
||||||
|
|
@ -18,9 +16,7 @@ export interface LetInBlockState {
|
||||||
inner: EditorState;
|
inner: EditorState;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface LetInBlockProps extends State2Props<LetInBlockState,EditorState> {
|
interface LetInBlockProps extends State2Props<LetInBlockState,EditorState> {}
|
||||||
suggestionPriority: (suggestion: SuggestionType) => number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function LetInBlock({state, setState, suggestionPriority}: LetInBlockProps) {
|
export function LetInBlock({state, setState, suggestionPriority}: LetInBlockProps) {
|
||||||
return <span className="letIn">
|
return <span className="letIn">
|
||||||
|
|
@ -43,7 +39,7 @@ export function LetInBlock({state, setState, suggestionPriority}: LetInBlockProp
|
||||||
|
|
||||||
function DeclColumns({state, setState, suggestionPriority}) {
|
function DeclColumns({state, setState, suggestionPriority}) {
|
||||||
const env = useContext(EnvContext);
|
const env = useContext(EnvContext);
|
||||||
const globalContext = useContext(CommandContext);
|
const globalContext = useContext(GlobalContext);
|
||||||
const {name, value, inner} = state;
|
const {name, value, inner} = state;
|
||||||
|
|
||||||
const setInner = callback => setState(state => ({...state, inner: callback(state.inner)}));
|
const setInner = callback => setState(state => ({...state, inner: callback(state.inner)}));
|
||||||
|
|
@ -52,8 +48,8 @@ function DeclColumns({state, setState, suggestionPriority}) {
|
||||||
setState(state => ({...state, name: e.target.value}));
|
setState(state => ({...state, name: e.target.value}));
|
||||||
}
|
}
|
||||||
|
|
||||||
const valueSuggestionPriority = (suggestion: SuggestionType) => {
|
const valueSuggestionPriority = (suggestion: ResolvedType) => {
|
||||||
const innerEnv = makeInnerEnv(env, name, suggestion[2]);
|
const innerEnv = makeInnerEnv(env, name, suggestion);
|
||||||
const resolved = evalEditorBlock(inner, innerEnv);
|
const resolved = evalEditorBlock(inner, innerEnv);
|
||||||
return scoreResolved(resolved, suggestionPriority);
|
return scoreResolved(resolved, suggestionPriority);
|
||||||
};
|
};
|
||||||
|
|
@ -103,7 +99,7 @@ function DeclColumns({state, setState, suggestionPriority}) {
|
||||||
|
|
||||||
function InnerMost({state, setState, suggestionPriority}) {
|
function InnerMost({state, setState, suggestionPriority}) {
|
||||||
const env = useContext(EnvContext);
|
const env = useContext(EnvContext);
|
||||||
const globalContext = useContext(CommandContext);
|
const globalContext = useContext(GlobalContext);
|
||||||
const setInner = callback => setState(state => ({...state, inner: callback(state.inner)}));
|
const setInner = callback => setState(state => ({...state, inner: callback(state.inner)}));
|
||||||
const valueResolved = evalEditorBlock(state.value, env);
|
const valueResolved = evalEditorBlock(state.value, env);
|
||||||
const innerEnv = makeInnerEnv(env, state.name, valueResolved);
|
const innerEnv = makeInnerEnv(env, state.name, valueResolved);
|
||||||
|
|
|
||||||
13
src/eval.ts
13
src/eval.ts
|
|
@ -66,7 +66,7 @@ export function evalInputBlock(text: string, value: InputValueType, env): Resolv
|
||||||
return {
|
return {
|
||||||
kind: found.kind || "value",
|
kind: found.kind || "value",
|
||||||
...found,
|
...found,
|
||||||
substitutions: new Map(),
|
substitutions: found.substitutions || new Map(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -170,6 +170,7 @@ export function evalCallBlock(fn: EditorState, input: EditorState, env): Resolve
|
||||||
export function evalLetInBlock(value: EditorState, name: string, inner: EditorState, env): ResolvedType {
|
export function evalLetInBlock(value: EditorState, name: string, inner: EditorState, env): ResolvedType {
|
||||||
const valueResolved = evalEditorBlock(value, env);
|
const valueResolved = evalEditorBlock(value, env);
|
||||||
// console.log('eval', name, '...', valueResolved.kind, valueResolved.e);
|
// console.log('eval', name, '...', valueResolved.kind, valueResolved.e);
|
||||||
|
// const innerEnv = growEnv(env)(name)(valueResolved);
|
||||||
const innerEnv = makeInnerEnv(env, name, valueResolved);
|
const innerEnv = makeInnerEnv(env, name, valueResolved);
|
||||||
return evalEditorBlock(inner, innerEnv);
|
return evalEditorBlock(inner, innerEnv);
|
||||||
}
|
}
|
||||||
|
|
@ -281,14 +282,8 @@ export function attemptParseLiteral(text: string, env): Dynamic[] {
|
||||||
.filter(resolved => (resolved.kind !== "unknown" && resolved.kind !== "error")) as unknown as Dynamic[];
|
.filter(resolved => (resolved.kind !== "unknown" && resolved.kind !== "error")) as unknown as Dynamic[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function scoreResolved(resolved: ResolvedType, outPriority: (s:SuggestionType) => number) {
|
export function scoreResolved(resolved: ResolvedType, outPriority: (s:ResolvedType) => number) {
|
||||||
const bias = outPriority(['literal', '<computed>',
|
const bias = outPriority(resolved);
|
||||||
{
|
|
||||||
// @ts-ignore
|
|
||||||
kind: "unknown",
|
|
||||||
t: resolved.t,
|
|
||||||
substitutions: new Map(),
|
|
||||||
}]);
|
|
||||||
|
|
||||||
if (resolved.kind === "value") {
|
if (resolved.kind === "value") {
|
||||||
return 1 + bias;
|
return 1 + bias;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue