Make text.* excmds work in the command line on the newtab page again

This commit is contained in:
glacambre 2019-04-23 19:13:46 +02:00
parent aa3129c132
commit eda244b75a
No known key found for this signature in database
GPG key ID: B9625DB1767553AC
4 changed files with 50 additions and 21 deletions

View file

@ -9,6 +9,8 @@ import * as perf from "@src/perf"
import { listenForCounters } from "@src/perf"
import * as messaging from "@src/lib/messaging"
import * as excmds_background from "@src/.excmds_background.generated"
import { CmdlineCmds } from "@src/background/commandline_cmds"
import { EditorCmds } from "@src/background/editor"
import * as convert from "@src/lib/convert"
import * as config from "@src/lib/config"
import * as dom from "@src/lib/dom"
@ -45,7 +47,11 @@ import * as extension_info from "@src/lib/extension_info"
// running from this entry point, which is to say, everything in the
// background script, will use the excmds that we give to the module
// here.
controller.setExCmds(excmds_background)
controller.setExCmds({
"": excmds_background,
"ex": CmdlineCmds,
"text": EditorCmds
})
messaging.addListener("excmd_background", messaging.attributeCaller(excmds_background))
messaging.addListener("controller_background", messaging.attributeCaller(controller))

View file

@ -30,7 +30,13 @@ import {
// here.
import * as controller from "@src/lib/controller"
import * as excmds_content from "@src/.excmds_content.generated"
controller.setExCmds(excmds_content)
import { CmdlineCmds } from "@src/content/commandline_cmds"
import { EditorCmds } from "@src/content/editor"
controller.setExCmds({
"": excmds_content,
"ex": CmdlineCmds,
"text": EditorCmds
})
messaging.addListener("excmd_content", messaging.attributeCaller(excmds_content))
messaging.addListener("controller_content", messaging.attributeCaller(controller))

View file

@ -0,0 +1,15 @@
import { getCommandlineFns } from "@src/lib/commandline_cmds"
import { messageOwnTab } from "@src/lib/messaging"
const functions = getCommandlineFns({} as any)
type ft = typeof functions
type ArgumentsType<T> = T extends (...args: infer U) => any ? U: never;
export const CmdlineCmds = new Proxy (functions as any, {
get(target, property) {
if (target[property]) {
return (...args) => messageOwnTab("commandline_cmd", property as string, args)
}
return target[property]
}
}) as { [k in keyof ft]: (...args: ArgumentsType<ft[k]>) => Promise<ReturnType<ft[k]>> }

View file

@ -1,7 +1,5 @@
/** Ex Mode (AKA cmd mode) */
import { CmdlineCmds } from "@src/background/commandline_cmds"
import { EditorCmds } from "@src/background/editor"
import * as convert from "@src/lib/convert"
import * as aliases from "@src/lib/aliases"
import * as Logging from "@src/lib/logging"
@ -48,34 +46,38 @@ function convertArgs(params, argv) {
// TODO: Quoting arguments
// TODO: Pipe to separate commands
// TODO: Abbreviated commands
export function parser(exstr: string, excmds: any): any[] {
export function parser(exstr: string, all_excmds: any): any[] {
// Expand aliases
const expandedExstr = aliases.expandExstr(exstr)
const [func, ...args] = expandedExstr.trim().split(/\s+/)
if (excmds.cmd_params.has(func)) {
// Try to find which namespace (ex, text, ...) the command is in
const dotIndex = func.indexOf(".")
const namespce = func.substring(0, dotIndex)
const funcName = func.substring(dotIndex + 1)
const excmds = all_excmds[namespce]
if (excmds === undefined) {
throw new Error(`Unknwown namespace: ${namespce}.`);
}
// Convert arguments
let converted_args
if (excmds.cmd_params !== undefined && excmds.cmd_params.has(func)) {
try {
return [
excmds[func],
convertArgs(excmds.cmd_params.get(func), args),
]
converted_args = convertArgs(excmds.cmd_params.get(func), args)
} catch (e) {
logger.error("Error executing or parsing:", exstr, e)
throw e
}
} else {
const match = func.match("^\([^.]+\)\.\(.*\)")
if (match !== undefined) {
const [namespce, name] = match.slice(1)
const funcs = ({
"text": EditorCmds,
"ex": CmdlineCmds,
})[namespce]
if (funcs !== undefined) {
return [funcs[name] , args]
}
}
converted_args = args
}
if (excmds[funcName] === undefined) {
logger.error("Not an excmd:", exstr)
throw `Not an excmd: ${func}`
}
return [excmds[funcName], converted_args]
}