mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-15 21:53:36 +01:00
36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
import { closeCompletion, startCompletion } from '@codemirror/autocomplete';
|
|
import { EditorView } from 'codemirror';
|
|
import { debounce } from '../../../lib/debounce';
|
|
|
|
/*
|
|
* Debounce autocomplete until user stops typing for `millis` milliseconds.
|
|
*/
|
|
export function debouncedAutocompletionDisplay({ millis }: { millis: number }) {
|
|
// TODO: Figure out how to show completion without setting context.explicit = true
|
|
const debouncedStartCompletion = debounce(function (view: EditorView) {
|
|
startCompletion(view);
|
|
}, millis);
|
|
|
|
return EditorView.updateListener.of(({ view, docChanged }) => {
|
|
// const completions = currentCompletions(view.state);
|
|
// const status = completionStatus(view.state);
|
|
|
|
if (!view.hasFocus) {
|
|
debouncedStartCompletion.cancel();
|
|
closeCompletion(view);
|
|
return;
|
|
}
|
|
|
|
if (view.state.doc.length === 0) {
|
|
debouncedStartCompletion.cancel();
|
|
closeCompletion(view);
|
|
return;
|
|
}
|
|
|
|
// If the document hasn't changed, we don't need to do anything
|
|
if (docChanged) {
|
|
debouncedStartCompletion(view);
|
|
}
|
|
});
|
|
}
|