@@ -92,11 +92,11 @@ class CompletionDialog {
9292 conf : Required < CompletionConfig > ,
9393 didSetActive : boolean
9494 ) : CompletionDialog | null {
95- if ( prev && ! didSetActive && active . some ( s => s . state == State . Pending ) )
95+ if ( prev && ! didSetActive && active . some ( s => s . isPending ) )
9696 return prev . setDisabled ( )
9797 let options = sortOptions ( active , state )
9898 if ( ! options . length )
99- return prev && active . some ( a => a . state == State . Pending ) ? prev . setDisabled ( ) : null
99+ return prev && active . some ( a => a . isPending ) ? prev . setDisabled ( ) : null
100100 let selected = state . facet ( completionConfig ) . selectOnOpen ? 0 : - 1
101101 if ( prev && prev . selected != selected && prev . selected != - 1 ) {
102102 let selectedValue = prev . options [ prev . selected ] . completion
@@ -147,10 +147,10 @@ export class CompletionState {
147147 if ( tr . selection || active . some ( a => a . hasResult ( ) && tr . changes . touchesRange ( a . from , a . to ) ) ||
148148 ! sameResults ( active , this . active ) || didSet )
149149 open = CompletionDialog . build ( active , state , this . id , open , conf , didSet )
150- else if ( open && open . disabled && ! active . some ( a => a . state == State . Pending ) )
150+ else if ( open && open . disabled && ! active . some ( a => a . isPending ) )
151151 open = null
152152
153- if ( ! open && active . every ( a => a . state != State . Pending ) && active . some ( a => a . hasResult ( ) ) )
153+ if ( ! open && active . every ( a => ! a . isPending ) && active . some ( a => a . hasResult ( ) ) )
154154 active = active . map ( a => a . hasResult ( ) ? new ActiveSource ( a . source , State . Inactive ) : a )
155155 for ( let effect of tr . effects ) if ( effect . is ( setSelectedEffect ) ) open = open && open . setSelected ( effect . value , this . id )
156156
@@ -165,8 +165,8 @@ export class CompletionState {
165165function sameResults ( a : readonly ActiveSource [ ] , b : readonly ActiveSource [ ] ) {
166166 if ( a == b ) return true
167167 for ( let iA = 0 , iB = 0 ; ; ) {
168- while ( iA < a . length && ! a [ iA ] . hasResult ) iA ++
169- while ( iB < b . length && ! b [ iB ] . hasResult ) iB ++
168+ while ( iA < a . length && ! a [ iA ] . hasResult ( ) ) iA ++
169+ while ( iB < b . length && ! b [ iB ] . hasResult ( ) ) iB ++
170170 let endA = iA == a . length , endB = iB == b . length
171171 if ( endA || endB ) return endA == endB
172172 if ( ( a [ iA ++ ] as ActiveResult ) . result != ( b [ iB ++ ] as ActiveResult ) . result ) return false
@@ -191,7 +191,7 @@ function makeAttrs(id: string, selected: number) {
191191
192192const none : readonly any [ ] = [ ]
193193
194- export const enum State { Inactive = 0 , Pending = 1 , Result = 2 }
194+ export const enum State { Inactive = 0 , Pending = 1 , Result = 3 }
195195
196196export const enum UpdateType {
197197 None = 0 ,
@@ -219,10 +219,12 @@ export function getUpdateType(tr: Transaction, conf: Required<CompletionConfig>)
219219export class ActiveSource {
220220 constructor ( readonly source : CompletionSource ,
221221 readonly state : State ,
222- readonly explicitPos : number = - 1 ) { }
222+ readonly explicit : boolean = false ) { }
223223
224224 hasResult ( ) : this is ActiveResult { return false }
225225
226+ get isPending ( ) { return this . state == State . Pending }
227+
226228 update ( tr : Transaction , conf : Required < CompletionConfig > ) : ActiveSource {
227229 let type = getUpdateType ( tr , conf ) , value : ActiveSource = this
228230 if ( ( type & UpdateType . Reset ) || ( type & UpdateType . ResetIfTouching ) && this . touches ( tr ) )
@@ -233,7 +235,7 @@ export class ActiveSource {
233235
234236 for ( let effect of tr . effects ) {
235237 if ( effect . is ( startCompletionEffect ) )
236- value = new ActiveSource ( value . source , State . Pending , effect . value ? cur ( tr . state ) : - 1 )
238+ value = new ActiveSource ( value . source , State . Pending , effect . value )
237239 else if ( effect . is ( closeCompletionEffect ) )
238240 value = new ActiveSource ( value . source , State . Inactive )
239241 else if ( effect . is ( setActiveEffect ) )
@@ -244,9 +246,7 @@ export class ActiveSource {
244246
245247 updateFor ( tr : Transaction , type : UpdateType ) : ActiveSource { return this . map ( tr . changes ) }
246248
247- map ( changes : ChangeDesc ) {
248- return changes . empty || this . explicitPos < 0 ? this : new ActiveSource ( this . source , this . state , changes . mapPos ( this . explicitPos ) )
249- }
249+ map ( changes : ChangeDesc ) : ActiveSource { return this }
250250
251251 touches ( tr : Transaction ) {
252252 return tr . changes . touchesRange ( cur ( tr . state ) )
@@ -255,11 +255,12 @@ export class ActiveSource {
255255
256256export class ActiveResult extends ActiveSource {
257257 constructor ( source : CompletionSource ,
258- explicitPos : number ,
258+ explicit : boolean ,
259+ readonly limit : number ,
259260 readonly result : CompletionResult ,
260261 readonly from : number ,
261262 readonly to : number ) {
262- super ( source , State . Result , explicitPos )
263+ super ( source , State . Result , explicit )
263264 }
264265
265266 hasResult ( ) : this is ActiveResult { return true }
@@ -270,24 +271,23 @@ export class ActiveResult extends ActiveSource {
270271 if ( result ! . map && ! tr . changes . empty ) result = result ! . map ( result ! , tr . changes )
271272 let from = tr . changes . mapPos ( this . from ) , to = tr . changes . mapPos ( this . to , 1 )
272273 let pos = cur ( tr . state )
273- if ( ( this . explicitPos < 0 ? pos <= from : pos < this . from ) ||
274- pos > to || ! result ||
275- ( type & UpdateType . Backspacing ) && cur ( tr . startState ) == this . from )
274+ if ( pos > to || ! result ||
275+ ( type & UpdateType . Backspacing ) && ( cur ( tr . startState ) == this . from || pos < this . limit ) )
276276 return new ActiveSource ( this . source , type & UpdateType . Activate ? State . Pending : State . Inactive )
277- let explicitPos = this . explicitPos < 0 ? - 1 : tr . changes . mapPos ( this . explicitPos )
277+ let limit = tr . changes . mapPos ( this . limit )
278278 if ( checkValid ( result . validFor , tr . state , from , to ) )
279- return new ActiveResult ( this . source , explicitPos , result , from , to )
279+ return new ActiveResult ( this . source , this . explicit , limit , result , from , to )
280280 if ( result . update &&
281- ( result = result . update ( result , from , to , new CompletionContext ( tr . state , pos , explicitPos >= 0 ) ) ) )
282- return new ActiveResult ( this . source , explicitPos , result , result . from , result . to ?? cur ( tr . state ) )
283- return new ActiveSource ( this . source , State . Pending , explicitPos )
281+ ( result = result . update ( result , from , to , new CompletionContext ( tr . state , pos , false ) ) ) )
282+ return new ActiveResult ( this . source , this . explicit , limit , result , result . from , result . to ?? cur ( tr . state ) )
283+ return new ActiveSource ( this . source , State . Pending , this . explicit )
284284 }
285285
286286 map ( mapping : ChangeDesc ) {
287287 if ( mapping . empty ) return this
288288 let result = this . result . map ? this . result . map ( this . result , mapping ) : this . result
289289 if ( ! result ) return new ActiveSource ( this . source , State . Inactive )
290- return new ActiveResult ( this . source , this . explicitPos < 0 ? - 1 : mapping . mapPos ( this . explicitPos ) , this . result ,
290+ return new ActiveResult ( this . source , this . explicit , mapping . mapPos ( this . limit ) , this . result ,
291291 mapping . mapPos ( this . from ) , mapping . mapPos ( this . to , 1 ) )
292292 }
293293
0 commit comments