Fix excmd completion not working with nested aliases

This commit is contained in:
glacambre 2018-08-18 07:15:14 +02:00
parent e6639ceb85
commit e19b3ce49d
No known key found for this signature in database
GPG key ID: B9625DB1767553AC
2 changed files with 41 additions and 9 deletions

View file

@ -34,3 +34,42 @@ export function expandExstr(
prevExpansions,
)
}
/**
* Get all aliases for all commands.
*
* @param aliases An object mapping aliases to commands
* @return commands An object mapping commands to an array of aliases
*/
export function getCmdAliasMapping(
aliases = config.get("exaliases"),
): { [str: string]: string[] } {
let commands = {}
// aliases look like this: {alias: command} but what we really need is this: {command: [alias1, alias2...]}
// This is what this loop builds
for (let alias in aliases) {
let cmd = aliases[alias].trim()
if (!commands[cmd]) commands[cmd] = []
commands[cmd].push(alias.trim())
}
// Some aliases might be aliases to other aliases (e.g. colors -> colourscheme -> set theme) so we need to recursively resolve them
function getAliases(cmd: string, prevExpansions: string[]): string[] {
if (!commands[cmd]) return []
if (prevExpansions.includes(cmd))
throw `Infinite loop detected while expanding aliases. Stack: ${prevExpansions}.`
return commands[cmd]
.reduce(
(result, alias) =>
result.concat(
getAliases(alias, prevExpansions.concat(cmd)),
),
[],
)
.concat(commands[cmd])
}
let result = {}
for (let cmd in commands) {
result[cmd] = getAliases(cmd, [])
}
return result
}

View file

@ -14,6 +14,7 @@ import * as Fuse from "fuse.js"
import { enumerate } from "./itertools"
import { toNumber } from "./convert"
import * as config from "./config"
import * as aliases from "./aliases"
export const DEFAULT_FAVICON = browser.extension.getURL(
"static/defaultFavicon.svg",
@ -37,15 +38,7 @@ export abstract class CompletionSource {
protected prefixes: string[] = []
constructor(prefixes) {
let exaliases = config.get("exaliases")
let commands = {}
// exaliases look like this: {alias: command} but what we really need is this: {command: [alias1, alias2...]}
// This is what this loop builds
for (let alias in exaliases) {
let cmd = exaliases[alias]
if (!commands[cmd]) commands[cmd] = []
commands[cmd].push(alias)
}
let commands = aliases.getCmdAliasMapping()
// Now, for each prefix given as argument, add it to the completionsource's prefix list and also add any alias it has
prefixes.map(p => p.trim()).forEach(p => {