Skip to content

Commit

Permalink
Make updateSyncTime apply to synchronously updates completion sources
Browse files Browse the repository at this point in the history
FIX: Don't immediately show synchronously updated completions when there are some sources
that still need to return.

See https://issues.chromium.org/issues/366491590
  • Loading branch information
marijnh committed Oct 28, 2024
1 parent 908cf18 commit 5e97bd6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
21 changes: 13 additions & 8 deletions src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,14 @@ class CompletionDialog {
state: EditorState,
id: string,
prev: CompletionDialog | null,
conf: Required<CompletionConfig>
conf: Required<CompletionConfig>,
didSetActive: boolean
): CompletionDialog | null {
if (prev && !didSetActive && active.some(s => s.state == State.Pending))
return prev.setDisabled()
let options = sortOptions(active, state)
if (!options.length) {
return prev && active.some(a => a.state == State.Pending) ?
new CompletionDialog(prev.options, prev.attrs, prev.tooltip, prev.timestamp, prev.selected, true) : null
}
if (!options.length)
return prev && active.some(a => a.state == State.Pending) ? prev.setDisabled() : null
let selected = state.facet(completionConfig).selectOnOpen ? 0 : -1
if (prev && prev.selected != selected && prev.selected != -1) {
let selectedValue = prev.options[prev.selected].completion
Expand All @@ -115,6 +116,10 @@ class CompletionDialog {
return new CompletionDialog(this.options, this.attrs, {...this.tooltip, pos: changes.mapPos(this.tooltip.pos)},
this.timestamp, this.selected, this.disabled)
}

setDisabled() {
return new CompletionDialog(this.options, this.attrs, this.tooltip, this.timestamp, this.selected, true)
}
}

export class CompletionState {
Expand All @@ -137,11 +142,11 @@ export class CompletionState {
})
if (active.length == this.active.length && active.every((a, i) => a == this.active[i])) active = this.active

let open = this.open
let open = this.open, didSet = tr.effects.some(e => e.is(setActiveEffect))
if (open && tr.docChanged) open = open.map(tr.changes)
if (tr.selection || active.some(a => a.hasResult() && tr.changes.touchesRange(a.from, a.to)) ||
!sameResults(active, this.active))
open = CompletionDialog.build(active, state, this.id, open, conf)
!sameResults(active, this.active) || didSet)
open = CompletionDialog.build(active, state, this.id, open, conf, didSet)
else if (open && open.disabled && !active.some(a => a.state == State.Pending))
open = null

Expand Down
10 changes: 7 additions & 3 deletions src/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ export const completionPlugin = ViewPlugin.fromClass(class implements PluginValu
if (active.state == State.Pending && !this.running.some(r => r.active.source == active.source))
this.startQuery(active)
}
if (this.running.length && cState.open && cState.open.disabled)
this.debounceAccept = setTimeout(() => this.accept(),
this.view.state.facet(completionConfig).updateSyncTime)
}

startQuery(active: ActiveSource) {
Expand Down Expand Up @@ -159,7 +162,7 @@ export const completionPlugin = ViewPlugin.fromClass(class implements PluginValu
this.debounceAccept = -1

let updated: ActiveSource[] = []
let conf = this.view.state.facet(completionConfig)
let conf = this.view.state.facet(completionConfig), cState = this.view.state.field(completionState)
for (let i = 0; i < this.running.length; i++) {
let query = this.running[i]
if (query.done === undefined) continue
Expand All @@ -178,7 +181,7 @@ export const completionPlugin = ViewPlugin.fromClass(class implements PluginValu
}
}

let current = this.view.state.field(completionState).active.find(a => a.source == query.active.source)
let current = cState.active.find(a => a.source == query.active.source)
if (current && current.state == State.Pending) {
if (query.done == null) {
// Explicitly failed. Should clear the pending status if it
Expand All @@ -193,7 +196,8 @@ export const completionPlugin = ViewPlugin.fromClass(class implements PluginValu
}
}

if (updated.length) this.view.dispatch({effects: setActiveEffect.of(updated)})
if (updated.length || cState.open && cState.open.disabled)
this.view.dispatch({effects: setActiveEffect.of(updated)})
}
}, {
eventHandlers: {
Expand Down

0 comments on commit 5e97bd6

Please sign in to comment.