Merge pull request #2169 from mozbugbox/sort-completion-filter

Sort completion result based on Fuse.js score
This commit is contained in:
Oliver Blanthorn 2020-02-18 15:11:31 +00:00 committed by GitHub
commit 14a9630f94
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 8 deletions

View file

@ -155,6 +155,7 @@ export abstract class CompletionSourceFuse extends CompletionSource {
fuse = undefined
protected lastExstr: string
protected sortScoredOptions = false
protected optionContainer = html`<table class="optionContainer"></table>`
@ -257,9 +258,13 @@ export abstract class CompletionSourceFuse extends CompletionSource {
setStateFromScore(scoredOpts: ScoredOption[], autoselect = false) {
const matches = scoredOpts.map(res => res.index)
const hidden_options = []
for (const [index, option] of enumerate(this.options)) {
if (matches.includes(index)) option.state = "normal"
else option.state = "hidden"
else {
option.state = "hidden"
hidden_options.push(option)
}
}
// ideally, this would not deselect anything unless it fell off the list of matches
@ -268,6 +273,12 @@ export abstract class CompletionSourceFuse extends CompletionSource {
} else {
this.deselect()
}
// sort this.options by score
if (this.sortScoredOptions) {
const sorted_options = matches.map(index => this.options[index])
this.options = sorted_options.concat(hidden_options)
}
}
/** Call to replace the current display */
@ -286,6 +297,7 @@ export abstract class CompletionSourceFuse extends CompletionSource {
if (option.state !== "hidden")
this.optionContainer.appendChild(option.html)
}
this.next(0)
/* console.log('updateDisplay', this.optionContainer, newContainer) */

View file

@ -8,6 +8,7 @@ import * as config from "@src/lib/config"
class BufferCompletionOption extends Completions.CompletionOptionHTML
implements Completions.CompletionOptionFuse {
public fuseKeys = []
public tabIndex: number
constructor(
public value: string,
@ -16,6 +17,8 @@ class BufferCompletionOption extends Completions.CompletionOptionHTML
container: browser.contextualIdentities.ContextualIdentity,
) {
super()
this.tabIndex = tab.index
// Two character tab properties prefix
let pre = ""
if (tab.active) pre += "%"
@ -66,6 +69,7 @@ export class BufferCompletionSource extends Completions.CompletionSourceFuse {
"Tabs",
)
this.sortScoredOptions = true
this.updateOptions()
this._parent.appendChild(this.node)
}
@ -97,13 +101,8 @@ export class BufferCompletionSource extends Completions.CompletionSourceFuse {
let index = Number(args[0]) - 1
if (Math.abs(index) < options.length) {
index = index.mod(options.length)
return [
{
index,
option: options[index],
score: 0,
},
]
// options order might change by scored sorting
return this.nthTabscoredOptions(index, options)
}
} else if (args[0] === "#") {
for (const [index, option] of enumerate(options)) {
@ -124,6 +123,22 @@ export class BufferCompletionSource extends Completions.CompletionSourceFuse {
return super.scoredOptions(query, options)
}
/** Return the scoredOption[] result for the nth tab */
private nthTabscoredOptions(
n: number,
options: BufferCompletionOption[]
): Completions.ScoredOption[] {
for (const [index, option] of enumerate(options)) {
if (option.tabIndex === n) {
return [{
index,
option,
score: 0,
}, ]
}
}
}
private async fillOptions() {
const tabs: browser.tabs.Tab[] = await browserBg.tabs.query({
currentWindow: true,