suggestions work again, improve error reporting

This commit is contained in:
Joeri Exelmans 2025-05-24 09:42:26 +02:00
parent 9050581a10
commit 69175c8cb1
12 changed files with 259 additions and 282 deletions

View file

@ -3,16 +3,14 @@ import { memo, useContext, useEffect, useMemo, useRef, useState } from "react";
import { trie } from "dope2";
import { EnvContext } from "../../context/EnvContext";
// import type { Environment, ResolvedType } from "./eval";
import "./InputBlock.css";
import { Type } from "../other/Type";
import type { ExprBlockState, State2Props } from "./ExprBlock";
// import { attemptParseLiteral } from "./eval";
import { Input } from "../other/Input";
import { CallContext } from "../../context/CallContext";
import { getActions } from "../app/actions";
import { GlobalContext } from "../../context/GlobalContext";
import { inferTypeInput } from "../../eval/infer_type";
import { inferType, inferTypeInput, type Environment, type Type } from "../../eval/infer_type";
import { Type as TypeBlock } from "../other/Type";
interface Literal {
kind: "literal";
@ -21,47 +19,60 @@ interface Literal {
interface Name {
kind: "name";
}
interface Text {
kind: "text";
interface Gibberish {
kind: "gibberish";
}
export type InputValueType = Literal | Name | Text;
export type InputValueType = Literal | Name | Gibberish;
export interface InputBlockState {
kind: "input";
text: string;
value: InputValueType;
focus: boolean
focus?: boolean;
}
// export type SuggestionType = ["literal"|"name", string, ResolvedType];
// export type PrioritizedSuggestionType = [number, ...SuggestionType];
export type PrioritizedSuggestionType = [number, Type, InputBlockState];
export interface InputBlockProps extends State2Props<InputBlockState,ExprBlockState> {
onCancel: () => void;
}
// const computeSuggestions = (
// text: string,
// env: Environment,
// score: InputBlockProps['score'],
// ): PrioritizedSuggestionType[] => {
// const literals = attemptParseLiteral(text, env);
// const ls: SuggestionType[] = [
// // literals
// ... literals.map((resolved) => ["literal", text, resolved]),
const attemptLiterals = [
["Double", text => (text !== '') && !Number.isNaN(Number(text))],
["Int" , text => /^-?[0-9]+$/.test(text)]
] as [string, (text: string) => boolean][];
// // names
// ... trie.suggest(env.names)(text)(Infinity)
// .map(([name, resolved]) => ["name", name, resolved]),
// ]
// // return []; // <-- uncomment to disable suggestions (useful for debugging)
// return ls
// .map((suggestion: SuggestionType) =>
// [score(suggestion[2]), ...suggestion] as PrioritizedSuggestionType)
// .sort(([priorityA], [priorityB]) => priorityB - priorityA)
// }
const computeSuggestions = (
text: string,
env: Environment,
score: InputBlockProps['score'],
): PrioritizedSuggestionType[] => {
const ls = [
...attemptLiterals
.filter(([_, test]) => test(text))
.map(([typename]) => ({
kind: "input",
text,
value: {
kind: "literal",
type: typename,
},
})),
...trie.suggest(env.names)(text)(Infinity)
.map(([name]) => ({
kind: "input",
text: name,
value: {
kind: "name",
},
})),
];
// return []; // <-- uncomment to disable suggestions (useful for debugging)
return ls.map((state) => [score(state), inferType(state, env).type, state] as PrioritizedSuggestionType)
.sort(([a],[b]) => b-a);
}
export function InputBlock({ state, setState, /*score,*/ onCancel }: InputBlockProps) {
export function InputBlock({ state, setState, score, onCancel }: InputBlockProps) {
const {text, focus} = state;
const globalContext = useContext(GlobalContext);
const env = useContext(EnvContext);
@ -70,8 +81,7 @@ export function InputBlock({ state, setState, /*score,*/ onCancel }: InputBlockP
const [i, setI] = useState(0); // selected suggestion idx
const singleSuggestion = trie.growPrefix(env.names)(text);
// const suggestions = useMemo(() => computeSuggestions(text, env, score), [text, score, env]);
const suggestions = useMemo(() => [], []);
const suggestions = useMemo(() => computeSuggestions(text, env, score), [text, score, env]);
useEffect(() => {
@ -96,21 +106,8 @@ export function InputBlock({ state, setState, /*score,*/ onCancel }: InputBlockP
}
const onSelectSuggestion = () => {
// const [_priority, kind, name, dynamic] = suggestions[i];
// if (kind === "literal") {
// setState(state => ({
// ...state,
// text: name,
// value: {kind, type: prettyT(getType(dynamic))},
// }));
// }
// else {
// setState(state => ({
// ...state,
// text: name,
// value: {kind},
// }))
// }
const [_priority, _type, inputState] = suggestions[i];
setState(_ => inputState);
};
const extraHandlers = {
@ -154,7 +151,7 @@ export function InputBlock({ state, setState, /*score,*/ onCancel }: InputBlockP
i={i} setI={setI} />
</span>
</Input>
::<Type type={typeInfo.type} />
::<TypeBlock type={typeInfo.type} />
</>
}
@ -176,10 +173,10 @@ interface SuggestionProps {
j: number;
onSelect: any;
highlighted: boolean;
// suggestion: PrioritizedSuggestionType;
suggestion: PrioritizedSuggestionType;
}
function Suggestion({ setI, j, onSelect, highlighted, /*suggestion: [priority, kind, text, resolved]*/ }: SuggestionProps) {
function Suggestion({ setI, j, onSelect, highlighted, suggestion: [priority, type, {text, value: {kind} }] }: SuggestionProps) {
const onMouseEnter = j => () => {
setI(j);
};
@ -192,7 +189,7 @@ function Suggestion({ setI, j, onSelect, highlighted, /*suggestion: [priority, k
className={(highlighted ? " selected" : "")}
onMouseEnter={onMouseEnter(j)}
onMouseDown={onMouseDown(j)}>
{/* ({priority}) ({kind}) {text} :: <Type type={resolved.t} /> */}
({priority}) ({kind}) {text} :: <TypeBlock type={type} />
</div>
}