Skip to content

Commit 10d1e88

Browse files
committed
Add activateOnTypingDelay option
FEATURE: The new `activateOnTypingDelay` option allows control over the debounce time before the completions are queried when the user types. Closes #20
1 parent 0b6faf9 commit 10d1e88

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

src/config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ export interface CompletionConfig {
77
/// When enabled (defaults to true), autocompletion will start
88
/// whenever the user types something that can be completed.
99
activateOnTyping?: boolean
10+
/// The amount of time to wait for further typing before querying
11+
/// completion sources via
12+
/// [`activateOnTyping`](#autocomplete.autocompletion^config.activateOnTyping).
13+
/// Defaults to 100, which should be fine unless your completion
14+
/// source is very slow and/or doesn't use `validFor`.
15+
activateOnTypingDelay?: number
1016
/// By default, when completion opens, the first option is selected
1117
/// and can be confirmed with
1218
/// [`acceptCompletion`](#autocomplete.acceptCompletion). When this
@@ -82,6 +88,7 @@ export const completionConfig = Facet.define<CompletionConfig, Required<Completi
8288
combine(configs) {
8389
return combineConfig<Required<CompletionConfig>>(configs, {
8490
activateOnTyping: true,
91+
activateOnTypingDelay: 100,
8592
selectOnOpen: true,
8693
override: null,
8794
closeOnBlur: true,

src/view.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export const completionPlugin = ViewPlugin.fromClass(class implements PluginValu
7272
debounceUpdate = -1
7373
running: RunningQuery[] = []
7474
debounceAccept = -1
75+
pendingStart = false
7576
composing = CompositionState.None
7677

7778
constructor(readonly view: EditorView) {
@@ -102,8 +103,10 @@ export const completionPlugin = ViewPlugin.fromClass(class implements PluginValu
102103
}
103104

104105
if (this.debounceUpdate > -1) clearTimeout(this.debounceUpdate)
106+
if (update.transactions.some(tr => tr.effects.some(e => e.is(startCompletionEffect)))) this.pendingStart = true
107+
let delay = this.pendingStart ? 50 : update.state.facet(completionConfig).activateOnTypingDelay
105108
this.debounceUpdate = cState.active.some(a => a.state == State.Pending && !this.running.some(q => q.active.source == a.source))
106-
? setTimeout(() => this.startUpdate(), 50) : -1
109+
? setTimeout(() => this.startUpdate(), delay) : -1
107110

108111
if (this.composing != CompositionState.None) for (let tr of update.transactions) {
109112
if (getUserEvent(tr) == "input")
@@ -115,6 +118,7 @@ export const completionPlugin = ViewPlugin.fromClass(class implements PluginValu
115118

116119
startUpdate() {
117120
this.debounceUpdate = -1
121+
this.pendingStart = false
118122
let {state} = this.view, cState = state.field(completionState)
119123
for (let active of cState.active) {
120124
if (active.state == State.Pending && !this.running.some(r => r.active.source == active.source))

test/webtest-autocomplete.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ class Runner {
3737
state: EditorState.create({
3838
doc: spec.doc,
3939
selection,
40-
extensions: [autocompletion({override: spec.sources, interactionDelay: 0, updateSyncTime: 40}), EditorState.allowMultipleSelections.of(true)]
40+
extensions: [
41+
autocompletion({override: spec.sources, interactionDelay: 0, updateSyncTime: 40, activateOnTypingDelay: 10}),
42+
EditorState.allowMultipleSelections.of(true)
43+
]
4144
}),
4245
parent: document.querySelector("#workspace")! as HTMLElement,
4346
dispatchTransactions: trs => {

0 commit comments

Comments
 (0)