when sorting suggestions, not only function and input type are matched, but also output type (=input of surrounding function)
This commit is contained in:
parent
d3138e6617
commit
ea8c015eff
5 changed files with 132 additions and 55 deletions
|
|
@ -30,13 +30,14 @@ export interface InputBlockState {
|
|||
}
|
||||
|
||||
export type SuggestionType = ['literal'|'name', string, Dynamic];
|
||||
export type PrioritizedSuggestionType = [number, ...SuggestionType];
|
||||
|
||||
interface InputBlockProps extends State2Props<InputBlockState> {
|
||||
filter: (suggestion: SuggestionType) => boolean;
|
||||
suggestionPriority: (suggestion: SuggestionType) => number;
|
||||
onCancel: () => void;
|
||||
}
|
||||
|
||||
const computeSuggestions = (text, env, filter): SuggestionType[] => {
|
||||
const computeSuggestions = (text, env, suggestionPriority: (s: SuggestionType) => number): PrioritizedSuggestionType[] => {
|
||||
const asDouble = parseDouble(text);
|
||||
const asInt = parseInt(text);
|
||||
|
||||
|
|
@ -46,14 +47,12 @@ const computeSuggestions = (text, env, filter): SuggestionType[] => {
|
|||
... trie.suggest(env.name2dyn)(text)(Infinity).map(([name,type]) => ["name", name, type]),
|
||||
]
|
||||
// return ls;
|
||||
return [
|
||||
...ls.filter(filter), // ones that match filter come first
|
||||
...ls.filter(s => !filter(s)),
|
||||
]
|
||||
// .slice(0,30);
|
||||
return ls
|
||||
.map(suggestion => [suggestionPriority(suggestion), ...suggestion] as PrioritizedSuggestionType)
|
||||
.sort(([priorityA], [priorityB]) => priorityB - priorityA)
|
||||
}
|
||||
|
||||
export function InputBlock({ state, setState, filter, onCancel }: InputBlockProps) {
|
||||
export function InputBlock({ state, setState, suggestionPriority, onCancel }: InputBlockProps) {
|
||||
const {text, focus} = state;
|
||||
const env = useContext(EnvContext);
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
|
|
@ -61,7 +60,7 @@ export function InputBlock({ state, setState, filter, onCancel }: InputBlockProp
|
|||
const [haveFocus, setHaveFocus] = useState(false); // whether to render suggestions or not
|
||||
|
||||
const singleSuggestion = trie.growPrefix(env.name2dyn)(text);
|
||||
const suggestions = useMemo(() => computeSuggestions(text, env, filter), [text]);
|
||||
const suggestions = useMemo(() => computeSuggestions(text, env, suggestionPriority), [text]);
|
||||
|
||||
useEffect(() => autoInputWidth(inputRef, text), [inputRef, text]);
|
||||
|
||||
|
|
@ -146,7 +145,7 @@ export function InputBlock({ state, setState, filter, onCancel }: InputBlockProp
|
|||
onTextChange(e.target.value);
|
||||
};
|
||||
|
||||
const onSelectSuggestion = ([kind, name, dynamic]: SuggestionType) => {
|
||||
const onSelectSuggestion = ([priority, kind, name, dynamic]: PrioritizedSuggestionType) => {
|
||||
if (kind === "literal") {
|
||||
setState(state => ({
|
||||
...state,
|
||||
|
|
@ -200,13 +199,13 @@ function Suggestions({ suggestions, onSelect, i, setI }) {
|
|||
};
|
||||
return <>{(suggestions.length > 0) &&
|
||||
<div className={"suggestions"}>
|
||||
{suggestions.map(([kind, name, dynamic], j) =>
|
||||
{suggestions.map(([priority, kind, name, dynamic], j) =>
|
||||
<div
|
||||
key={`${j}_${name}`}
|
||||
className={(i === j ? " selected" : "")}
|
||||
onMouseEnter={onMouseEnter(j)}
|
||||
onMouseDown={onMouseDown(j)}>
|
||||
({kind}) {name} :: <Type type={getType(dynamic)} />
|
||||
({priority}) ({kind}) {name} :: <Type type={getType(dynamic)} />
|
||||
</div>)}
|
||||
</div>
|
||||
}</>;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue