mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2025-12-23 16:42:26 +01:00
Better option completion/validation.
This commit is contained in:
@@ -233,8 +233,8 @@ function Bookmarks() //{{{
|
|||||||
"Set the default search engine",
|
"Set the default search engine",
|
||||||
"string", "google",
|
"string", "google",
|
||||||
{
|
{
|
||||||
completer: function completer(filter) completion.runCompleter("search", filter, true),
|
completer: function completer(context) completion.search(context, true),
|
||||||
validator: function validator(value) completion.runCompleter("search", "", true).some(function ([s]) s == value)
|
validator: function validator(value) completion.runCompleter("search", "", true).some(function ([s]) s == value)
|
||||||
});
|
});
|
||||||
|
|
||||||
options.add(["preload"],
|
options.add(["preload"],
|
||||||
|
|||||||
@@ -162,7 +162,7 @@ function Buffer() //{{{
|
|||||||
["1", "Show the link in the status line"],
|
["1", "Show the link in the status line"],
|
||||||
["2", "Show the link in the command line"]
|
["2", "Show the link in the command line"]
|
||||||
],
|
],
|
||||||
validator: function (value) value >= 0 && value <= 2
|
validator: options.validateCompleter
|
||||||
});
|
});
|
||||||
|
|
||||||
options.add(["usermode", "um"],
|
options.add(["usermode", "um"],
|
||||||
|
|||||||
@@ -952,7 +952,9 @@ function Completion() //{{{
|
|||||||
_runCompleter: function _runCompleter(name, filter)
|
_runCompleter: function _runCompleter(name, filter)
|
||||||
{
|
{
|
||||||
let context = CompletionContext(filter);
|
let context = CompletionContext(filter);
|
||||||
context.fork.apply(context, ["run", 0, this, name].concat(Array.slice(arguments, 2)));
|
let res = context.fork.apply(context, ["run", 0, this, name].concat(Array.slice(arguments, 2)));
|
||||||
|
if (res) // FIXME
|
||||||
|
return { items: res.map(function (i) ({ item: i })) };
|
||||||
while (context.incomplete)
|
while (context.incomplete)
|
||||||
liberator.threadYield(true, true);
|
liberator.threadYield(true, true);
|
||||||
return context.allItems;
|
return context.allItems;
|
||||||
|
|||||||
@@ -48,16 +48,8 @@ function AutoCommands() //{{{
|
|||||||
"List of autocommand event names which should be ignored",
|
"List of autocommand event names which should be ignored",
|
||||||
"stringlist", "",
|
"stringlist", "",
|
||||||
{
|
{
|
||||||
completer: function (value) config.autocommands.concat([["all", "All events"]]),
|
completer: function () config.autocommands.concat([["all", "All events"]]),
|
||||||
validator: function (value)
|
validator: options.validateCompleter
|
||||||
{
|
|
||||||
let values = value.split(",");
|
|
||||||
let events = config.autocommands.map(function (event) event[0]);
|
|
||||||
|
|
||||||
events.push("all");
|
|
||||||
|
|
||||||
return values.every(function (event) events.indexOf(event) >= 0);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
options.add(["focuscontent", "fc"],
|
options.add(["focuscontent", "fc"],
|
||||||
|
|||||||
@@ -568,7 +568,7 @@ function Hints() //{{{
|
|||||||
{
|
{
|
||||||
return [[m, ""] for each (m in ["contains", "wordstartswith", "firstletters", "custom"])];
|
return [[m, ""] for each (m in ["contains", "wordstartswith", "firstletters", "custom"])];
|
||||||
},
|
},
|
||||||
validator: function (value) /^(contains|wordstartswith|firstletters|custom)$/.test(value)
|
validator: options.validateCompleter
|
||||||
});
|
});
|
||||||
|
|
||||||
options.add(["wordseparators", "wsp"],
|
options.add(["wordseparators", "wsp"],
|
||||||
|
|||||||
@@ -128,7 +128,7 @@ const liberator = (function () //{{{
|
|||||||
["b", "Bookmark bar"]
|
["b", "Bookmark bar"]
|
||||||
].concat(!liberator.has("tabs") ? [] : tabopts);
|
].concat(!liberator.has("tabs") ? [] : tabopts);
|
||||||
},
|
},
|
||||||
validator: function (value) Array.every(value, function (c) c in config.guioptions || tabopts.some(function (a) a[0] == c))
|
validator: options.validateCompleter
|
||||||
});
|
});
|
||||||
|
|
||||||
options.add(["helpfile", "hf"],
|
options.add(["helpfile", "hf"],
|
||||||
|
|||||||
@@ -218,7 +218,7 @@ function Mail() //{{{
|
|||||||
|
|
||||||
return value;
|
return value;
|
||||||
},
|
},
|
||||||
validator: function (value) /^(classic|wide|vertical|inherit)$/.test(value)
|
validator: options.validateCompleter
|
||||||
});
|
});
|
||||||
|
|
||||||
/*options.add(["threads"],
|
/*options.add(["threads"],
|
||||||
|
|||||||
@@ -170,10 +170,10 @@ Option.prototype = {
|
|||||||
|
|
||||||
hasName: function (name) this.names.indexOf(name) >= 0,
|
hasName: function (name) this.names.indexOf(name) >= 0,
|
||||||
|
|
||||||
isValidValue: function (value)
|
isValidValue: function (values)
|
||||||
{
|
{
|
||||||
if (this.validator)
|
if (this.validator)
|
||||||
return this.validator(value);
|
return this.validator(values);
|
||||||
else
|
else
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
@@ -277,10 +277,9 @@ Option.prototype = {
|
|||||||
|
|
||||||
if (newValue == null)
|
if (newValue == null)
|
||||||
return "Operator " + operator + " not supported for option type " + this.type;
|
return "Operator " + operator + " not supported for option type " + this.type;
|
||||||
newValue = this.joinValues(newValue);
|
|
||||||
if (!this.isValidValue(newValue))
|
if (!this.isValidValue(newValue))
|
||||||
return "E474: Invalid argument: " + values;
|
return "E474: Invalid argument: " + values;
|
||||||
this.set(newValue, scope);
|
this.setValues(newValue, scope);
|
||||||
}
|
}
|
||||||
}; //}}}
|
}; //}}}
|
||||||
|
|
||||||
@@ -744,9 +743,13 @@ function Options() //{{{
|
|||||||
if (!opt.value)
|
if (!opt.value)
|
||||||
completions = [[option.value, "Current value"], [option.defaultValue, "Default value"]].filter(function (f) f[0]);
|
completions = [[option.value, "Current value"], [option.defaultValue, "Default value"]].filter(function (f) f[0]);
|
||||||
|
|
||||||
|
context.title = ["Option Value"];
|
||||||
if (completer)
|
if (completer)
|
||||||
{
|
{
|
||||||
completions = completions.concat(completer(filter));
|
let res = completer(context);
|
||||||
|
if (!res)
|
||||||
|
return;
|
||||||
|
completions = completions.concat(res);
|
||||||
if (/list$/.test(option.type))
|
if (/list$/.test(option.type))
|
||||||
{
|
{
|
||||||
completions = completions.filter(function (val) opt.values.indexOf(val[0]) == -1);
|
completions = completions.filter(function (val) opt.values.indexOf(val[0]) == -1);
|
||||||
@@ -762,7 +765,6 @@ function Options() //{{{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
context.compare = function (a, b) 0;
|
context.compare = function (a, b) 0;
|
||||||
context.title = ["Option Value"];
|
|
||||||
context.completions = completions;
|
context.completions = completions;
|
||||||
},
|
},
|
||||||
literal: true,
|
literal: true,
|
||||||
@@ -989,6 +991,15 @@ function Options() //{{{
|
|||||||
return ret;
|
return ret;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// TODO: Run this by default?
|
||||||
|
validateCompleter: function (values)
|
||||||
|
{
|
||||||
|
let self = this;
|
||||||
|
let completions = completion.runCompleter(function (context) self.completer(context), "");
|
||||||
|
return Array.concat(values).every(
|
||||||
|
function (value) completions.some(function (item) item[0] == value));
|
||||||
|
},
|
||||||
|
|
||||||
get store() storage.options,
|
get store() storage.options,
|
||||||
|
|
||||||
getPref: function (name, forcedDefault)
|
getPref: function (name, forcedDefault)
|
||||||
|
|||||||
@@ -151,7 +151,7 @@ function Tabs() //{{{
|
|||||||
["2", "Always show tab bar"]
|
["2", "Always show tab bar"]
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
validator: function (value) value >= 0 && value <= 2
|
validator: options.validateCompleter
|
||||||
});
|
});
|
||||||
|
|
||||||
if (config.name == "Vimperator")
|
if (config.name == "Vimperator")
|
||||||
@@ -169,12 +169,7 @@ function Tabs() //{{{
|
|||||||
["paste", "P and gP mappings"]
|
["paste", "P and gP mappings"]
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
validator: function (value)
|
validator: options.validateCompleter
|
||||||
{
|
|
||||||
return value.split(",").every(
|
|
||||||
function (item) /^(homepage|quickmark|tabopen|paste|)$/.test(item)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
options.add(["newtab"],
|
options.add(["newtab"],
|
||||||
@@ -192,12 +187,7 @@ function Tabs() //{{{
|
|||||||
["prefs", ":pref[erences]! or :prefs! command"]
|
["prefs", ":pref[erences]! or :prefs! command"]
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
validator: function (value)
|
validator: options.validateCompleter
|
||||||
{
|
|
||||||
return value == "all" || value.split(",").every(
|
|
||||||
function (item) /^(addons|downloads|help|javascript|prefs|)$/.test(item)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
options.add(["popups", "pps"],
|
options.add(["popups", "pps"],
|
||||||
@@ -227,7 +217,7 @@ function Tabs() //{{{
|
|||||||
["4", "Open in the same tab unless it has a specific requested size"]
|
["4", "Open in the same tab unless it has a specific requested size"]
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
validator: function (value) value >= 0 && value <= 4
|
validator: options.validateCompleter
|
||||||
});
|
});
|
||||||
// TODO: Add option, or only apply when go~=[nN]
|
// TODO: Add option, or only apply when go~=[nN]
|
||||||
styles.addSheet("tab-binding", "chrome://*",
|
styles.addSheet("tab-binding", "chrome://*",
|
||||||
|
|||||||
@@ -441,7 +441,7 @@ function CommandLine() //{{{
|
|||||||
"charlist", "sfl",
|
"charlist", "sfl",
|
||||||
{
|
{
|
||||||
completer: function completer(filter) [k for each (k in completion.urlCompleters)],
|
completer: function completer(filter) [k for each (k in completion.urlCompleters)],
|
||||||
validator: function validator(value) Array.every(value, function (v) v in completion.urlCompleters)
|
validator: options.validateCompleter
|
||||||
});
|
});
|
||||||
|
|
||||||
options.add(["history", "hi"],
|
options.add(["history", "hi"],
|
||||||
@@ -475,28 +475,19 @@ function CommandLine() //{{{
|
|||||||
|
|
||||||
return engines.map(function (engine) [engine.alias, engine.description]);
|
return engines.map(function (engine) [engine.alias, engine.description]);
|
||||||
},
|
},
|
||||||
validator: function validator(value)
|
validator: options.validateCompleter
|
||||||
{
|
|
||||||
let ss = Components.classes["@mozilla.org/browser/search-service;1"]
|
|
||||||
.getService(Components.interfaces.nsIBrowserSearchService);
|
|
||||||
|
|
||||||
return value.split(",").every(function (alias) {
|
|
||||||
let engine = ss.getEngineByAlias(alias);
|
|
||||||
return engine && engine.supportsResponseType("application/x-suggestions+json");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
options.add(["wildignore", "wig"],
|
options.add(["wildignore", "wig"],
|
||||||
"List of file patterns to ignore when completing files",
|
"List of file patterns to ignore when completing files",
|
||||||
"stringlist", "",
|
"stringlist", "",
|
||||||
{
|
{
|
||||||
validator: function validator(value)
|
validator: function validator(values)
|
||||||
{
|
{
|
||||||
// TODO: allow for escaping the ","
|
// TODO: allow for escaping the ","
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
new RegExp("^(" + value.replace(",", "|", "g") + ")$");
|
new RegExp("^(" + value.join("|") + ")$");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (e)
|
catch (e)
|
||||||
@@ -521,12 +512,7 @@ function CommandLine() //{{{
|
|||||||
["list:longest", "List all and complete common string"]
|
["list:longest", "List all and complete common string"]
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
validator: function validator(value)
|
validator: options.validateCompleter,
|
||||||
{
|
|
||||||
let self = this;
|
|
||||||
return value.split(",").every(function (opt)
|
|
||||||
self.completer().some(function ([key]) key == opt))
|
|
||||||
},
|
|
||||||
checkHas: function (value, val)
|
checkHas: function (value, val)
|
||||||
{
|
{
|
||||||
let [first, second] = value.split(":", 2);
|
let [first, second] = value.split(":", 2);
|
||||||
@@ -545,10 +531,7 @@ function CommandLine() //{{{
|
|||||||
["sort", "Always sort the completion list"]
|
["sort", "Always sort the completion list"]
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
validator: function validator(value)
|
validator: options.validateCompleter
|
||||||
{
|
|
||||||
return value.split(",").every(function (item) /^(sort|auto|)$/.test(item));
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////}}}
|
/////////////////////////////////////////////////////////////////////////////}}}
|
||||||
@@ -1571,7 +1554,7 @@ function StatusLine() //{{{
|
|||||||
["2", "Always display status line"]
|
["2", "Always display status line"]
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
validator: function validator(value) value >= 0 && value <= 2
|
validator: options.validateCompleter
|
||||||
});
|
});
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////}}}
|
/////////////////////////////////////////////////////////////////////////////}}}
|
||||||
|
|||||||
Reference in New Issue
Block a user