parsers/exmode.ts: replace macro-generated info with ts-generated info

I tested nearly all ex commands and this didn't seem to break anything.
This commit is contained in:
glacambre 2019-05-31 15:13:28 +02:00
parent 03953315a8
commit 9ef99b0ab4
No known key found for this signature in database
GPG key ID: B9625DB1767553AC
3 changed files with 26 additions and 40 deletions

View file

@ -14,11 +14,7 @@ export class NumberType implements Type {
} }
public convert(argument) { public convert(argument) {
let n = parseInt(argument, 10) const n = parseFloat(argument)
if (!Number.isNaN(n)) {
return n
}
n = parseFloat(argument)
if (!Number.isNaN(n)) { if (!Number.isNaN(n)) {
return n return n
} }

View file

@ -1368,7 +1368,7 @@ export async function tutor(newtab?: string) {
* Display Tridactyl's contributors in order of commits in a user-friendly fashion * Display Tridactyl's contributors in order of commits in a user-friendly fashion
*/ */
//#background //#background
export async function credits(excmd?: string) { export async function credits() {
const creditspage = browser.runtime.getURL("static/authors.html") const creditspage = browser.runtime.getURL("static/authors.html")
tabopen(creditspage) tabopen(creditspage)
} }

View file

@ -1,43 +1,23 @@
/** Ex Mode (AKA cmd mode) */ /** Ex Mode (AKA cmd mode) */
import { FunctionType } from "../../compiler/types/AllTypes"
import { everything as metadata } from "@src/.metadata.generated"
import * as convert from "@src/lib/convert" import * as convert from "@src/lib/convert"
import * as aliases from "@src/lib/aliases" import * as aliases from "@src/lib/aliases"
import * as Logging from "@src/lib/logging" import * as Logging from "@src/lib/logging"
import { enumerate, izip } from "@src/lib/itertools"
const logger = new Logging.Logger("exmode") const logger = new Logging.Logger("exmode")
/* Converts numbers, boolean, string[]. function convertArgs(types, argv) {
string[] eats all remaining arguments, so it should only be used as a
type of the last arg.
TODO: quoted strings
TODO: shell style options
TODO: counts
*/
function convertArgs(params, argv) {
const conversions = {
number: convert.toNumber,
boolean: convert.toBoolean,
string: s => s,
ModeName: s => s,
}
const typedArgs = [] const typedArgs = []
let type for (let itypes = 0, iargv = 0; itypes < types.length && iargv < argv.length; ++itypes && ++iargv) {
let arg const curType = types[itypes]
let i const curArg = argv[iargv]
for ([type, [i, arg]] of izip(params.values(), enumerate(argv))) { // Special casing arrays because that's why the previous arg conversion code did
if (type in conversions) { if (curType.isDotDotDot || curType.kind === "array") {
typedArgs.push(conversions[type](arg)) return typedArgs.concat(curType.convert(argv.slice(iargv)))
} else if (type.includes("|") || ["'", '"'].includes(type[0])) { }
// Do your own conversions! typedArgs.push(curType.convert(curArg))
typedArgs.push(arg)
} else if (type === "string[]") {
// Eat all remaining arguments
return [...typedArgs, ...argv.slice(i)]
} else throw new TypeError(`Unknown type: ${type}`)
} }
return typedArgs return typedArgs
} }
@ -61,11 +41,21 @@ export function parser(exstr: string, all_excmds: any): any[] {
throw new Error(`Unknwown namespace: ${namespce}.`); throw new Error(`Unknwown namespace: ${namespce}.`);
} }
// Convert arguments // Convert arguments, but only for ex commands
let converted_args let converted_args
if (excmds.cmd_params !== undefined && excmds.cmd_params.has(func)) { if (namespce == "") {
let types
try { try {
converted_args = convertArgs(excmds.cmd_params.get(func), args) types = (metadata
.getFile("src/excmds.ts")
.getFunction(funcName)
.type as FunctionType)
.args
} catch (e) {
throw `Could not find type information for excmd ${funcName}`
}
try {
converted_args = convertArgs(types, args)
} catch (e) { } catch (e) {
logger.error("Error executing or parsing:", exstr, e) logger.error("Error executing or parsing:", exstr, e)
throw e throw e