greatly simplify and improve type inferencing - suggestions broken

This commit is contained in:
Joeri Exelmans 2025-05-23 19:48:24 +02:00
parent c3f7cea310
commit c5c5def598
12 changed files with 1022 additions and 694 deletions

View file

@ -3,15 +3,16 @@ import { memo, useContext, useEffect, useMemo, useRef, useState } from "react";
import { getType, prettyT, trie } from "dope2";
import { EnvContext } from "./EnvContext";
import type { Environment, ResolvedType } from "./eval";
// import type { Environment, ResolvedType } from "./eval";
import "./InputBlock.css";
import { Type } from "./Type";
import type { ExprBlockState, State2Props } from "./ExprBlock";
import { attemptParseLiteral } from "./eval";
// import { attemptParseLiteral } from "./eval";
import { Input } from "./Input";
import { CallContext } from "./CallContext";
import { getActions } from "./actions";
import { GlobalContext } from "./GlobalContext";
import { inferTypeInput } from "./infer_type";
interface Literal {
kind: "literal";
@ -32,35 +33,35 @@ export interface InputBlockState {
focus: boolean
}
export type SuggestionType = ["literal"|"name", string, ResolvedType];
export type PrioritizedSuggestionType = [number, ...SuggestionType];
// export type SuggestionType = ["literal"|"name", string, ResolvedType];
// export type PrioritizedSuggestionType = [number, ...SuggestionType];
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 computeSuggestions = (
// text: string,
// env: Environment,
// score: InputBlockProps['score'],
// ): PrioritizedSuggestionType[] => {
// const literals = attemptParseLiteral(text, env);
// const ls: SuggestionType[] = [
// // literals
// ... literals.map((resolved) => ["literal", text, resolved]),
// 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)
}
// // 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)
// }
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);
@ -69,7 +70,8 @@ export function InputBlock({ state, setState, score, onCancel }: InputBlockProps
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(() => computeSuggestions(text, env, score), [text, score, env]);
const suggestions = useMemo(() => [], []);
useEffect(() => {
@ -94,21 +96,21 @@ export function InputBlock({ state, setState, score, onCancel }: InputBlockProps
}
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, 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 extraHandlers = {
@ -134,7 +136,9 @@ export function InputBlock({ state, setState, score, onCancel }: InputBlockProps
},
};
return <Input
const typeInfo = inferTypeInput(state, env);
return <><Input
placeholder="<name or literal>"
onCancel={onCancel}
onEnter={onSelectSuggestion}
@ -150,6 +154,8 @@ export function InputBlock({ state, setState, score, onCancel }: InputBlockProps
i={i} setI={setI} />
</span>
</Input>
::<Type type={typeInfo.type} />
</>
}
function Suggestions({ suggestions, onSelect, i, setI }) {
@ -170,10 +176,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, kind, text, resolved]*/ }: SuggestionProps) {
const onMouseEnter = j => () => {
setI(j);
};
@ -186,7 +192,7 @@ function Suggestion({ setI, j, onSelect, highlighted, suggestion: [priority, kin
className={(highlighted ? " selected" : "")}
onMouseEnter={onMouseEnter(j)}
onMouseDown={onMouseDown(j)}>
({priority}) ({kind}) {text} :: <Type type={resolved.t} />
{/* ({priority}) ({kind}) {text} :: <Type type={resolved.t} /> */}
</div>
}