-
Notifications
You must be signed in to change notification settings - Fork 8
add aliases search array and display best matches for aliases to user #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 13 commits
2af43ff
72f1342
73b0f15
44db0e3
2f74cc3
41d89f8
97325d4
e1e094b
4310337
a59c4d2
a903b8b
e843b3f
86cfc0a
7f0cb61
8f49db3
ea1745f
8a3304e
6ea2fbb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,13 +17,56 @@ | |
export let inputData = []; | ||
export let hotkeysGlobal; | ||
export let placeholderText; | ||
export let displayHints; | ||
export let debugOutput; | ||
export let orderedCommands; | ||
// re: space '(' alphanumeric_word_char | ||
// "0 or more word_char space/tab and -" ')' | ||
// end of line | ||
// Note: this should be the unicode equivalent of the Latin regexp: | ||
// / \(\w[\s\w-]*\)$/ | ||
let hintRegexp = / \([ \u0000-\u0019\u0021-\uFFFF_-]+\)$/u; | ||
const optionsFuse = { | ||
isCaseSensitive: false, | ||
shouldSort: true, | ||
keys: ["name", "description"] | ||
keys: ["name", "description", {name: "aliases", weight: 2}], | ||
includeScore: true, | ||
includeMatches: true, | ||
}; | ||
if ( orderedCommands ) { | ||
optionsFuse.sortFn = function (a,b) { | ||
/* Sort results in two groups. If scores of either item are | ||
below 0.009 sort by score. This prevents two items with | ||
weights of 0.003 and 2.2E-16 from being bucketed and sorted | ||
by order in the command array. If the value is low enough to | ||
be a really good match, don't use the implicit order in the | ||
commands array to override it. | ||
If both scores are larger than 0.009 and the difference | ||
between them is < 0.05, bucket them together and sort the | ||
items in the bucket by their original index location in the | ||
commands array. In this case fuse.js isn't that certain about | ||
the match and we order based on the position in the command | ||
array that we have chosen to be most likely. | ||
Note 0.009 and 0.05 are magic numbers that worked in testing, YMMV. | ||
*/ | ||
return ((a.score > 0.009 && b.score > 0.009) && // if scores > minimum | ||
Math.abs (a.score - b.score) < 0.05 ? // bucket them | ||
(a.idx < b.idx ? -1 : 1) : // sort by order in commands array | ||
(a.score == b.score ? // else if scores equal | ||
(a.idx < b.idx ? -1 : 1) : // sort by index order | ||
a.score < b.score ? -1 : 1)) // else sort lowest score first | ||
} | ||
rouilj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if (debugOutput) console.log('Using commands weighted sort'); | ||
} else { | ||
if (debugOutput) console.log("Using fuse.js's score for sorting"); | ||
} | ||
let showModal = false; | ||
let searchField; | ||
let loadingChildren = false; | ||
|
@@ -106,15 +149,107 @@ | |
} | ||
} | ||
function removeHints(items) { | ||
if (! displayHints ) return; | ||
items.map( (i) => { if ( i.hinted ) { | ||
i.name = i.name.replace(hintRegexp, ''); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this removes the hint if they have used it? Maybe a comment above here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It just resets the command palette names to remove hints. It doesn't matter if they are This way pulling up the palette doesn't display the hints from the last invocation before the user even types anything. |
||
i.hinted = false | ||
}}) | ||
} | ||
/** append best aliases match to command object's name */ | ||
function hintMatch(search_result) { | ||
/** | ||
* Accepts an array of 2 element arrays. These are the start/stop | ||
* that matched the search term for the current match. A metric | ||
* is calculated from these. Larger values indicate better matches. | ||
* | ||
* @param {array} indexList - list of 2 element lists | ||
* | ||
* For each index_list use the [start, end] range to calculate a | ||
* score. [0,*] indicates first char of term matched. It counts | ||
* for an additional 0.5 points. Each multi character match [6,7] | ||
* (2 chars) counts for 2.5 points/char. All numbers are magic | ||
* weighting factors that seem to work. Formula and number may | ||
* change. | ||
*/ | ||
let match_metric = (indices) => ( indices.map( | ||
range => range[0] == 0 ? | ||
((range[1] - range[0]) | ||
* 2.5) + 1.5 : | ||
((range[1] - range[0]) * 2.5) + 1 | ||
).reduce((sum, val) => sum+val)) | ||
const e = search_result.matches.filter( | ||
i => i.key === "aliases").sort((a,b) => { | ||
let a_mm = match_metric(a.indices) | ||
let b_mm = match_metric(b.indices) | ||
// a higher match_metric is assigned to a term that is a | ||
// better match for the search. | ||
// Sort by higher match_metric. If match_metrics are equal, | ||
// sort the one with the lower index in the aliases array | ||
// first. (Put the best choice aliases first.) | ||
// 1 - sort b before a; -1 sort a before b. | ||
// note: a.refIndex can never equal b.refIndex | ||
return a_mm == b_mm ? | ||
(a.refIndex < b.refIndex ? -1 : 1) : | ||
( a_mm > b_mm? -1 : 1) | ||
}) | ||
let item = search_result.item | ||
let hinted = !!item.hinted | ||
if ( e.length ) { | ||
/* add hints */ | ||
const hint = ` (${e[0].value})` | ||
if (! hinted) { | ||
item.name += hint | ||
} else { | ||
item.name = item.name.replace(hintRegexp, hint) | ||
} | ||
item.hinted = true | ||
} else { | ||
if (item.hinted) { | ||
/* remove previous hints */ | ||
item.name = item.name.replace(hintRegexp, '') | ||
item.hinted = false | ||
} | ||
} | ||
if (debugOutput) { | ||
console.group("CommandPal " + item.name); | ||
console.debug('score', search_result.score) | ||
console.debug('index', search_result.refIndex) | ||
console.debug('weight', item.weight) | ||
console.debug('hints', e.length) | ||
console.table(search_result.matches.filter( (i) => { | ||
if (i.key === "aliases") { | ||
i.metric = match_metric(i.indices); | ||
return true; | ||
} | ||
return false; | ||
})) | ||
console.groupEnd("CommandPal " + item.name); | ||
} | ||
return item | ||
} | ||
function onTextChange(e) { | ||
const text = e.detail; | ||
dispatch("textChanged", text); | ||
selectedIndex = 0; | ||
const processResult = displayHints ? hintMatch: (i) => i.item | ||
if (!text) { | ||
itemsFiltered = items; | ||
removeHints(itemsFiltered); | ||
} else { | ||
const fuseResult = fuse.search(text); | ||
itemsFiltered = fuseResult.map(i => i.item); | ||
if (debugOutput && displayHints) console.groupCollapsed( | ||
"CommandPal search: " + text) | ||
itemsFiltered = fuseResult.map(processResult); | ||
if (debugOutput && displayHints) console.groupEnd( | ||
"CommandPal search: " + text) | ||
} | ||
} | ||
|
@@ -125,6 +260,7 @@ | |
} | ||
dispatch("closed"); | ||
selectedIndex = 0; | ||
removeHints(inputData); | ||
setItems(inputData); | ||
showModal = false; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thankyou, I'm coming around to this option 👍