mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2025-12-23 23:22:26 +01:00
Better option completion/validation.
This commit is contained in:
@@ -233,7 +233,7 @@ function Bookmarks() //{{{
|
||||
"Set the default search engine",
|
||||
"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)
|
||||
});
|
||||
|
||||
|
||||
@@ -162,7 +162,7 @@ function Buffer() //{{{
|
||||
["1", "Show the link in the status line"],
|
||||
["2", "Show the link in the command line"]
|
||||
],
|
||||
validator: function (value) value >= 0 && value <= 2
|
||||
validator: options.validateCompleter
|
||||
});
|
||||
|
||||
options.add(["usermode", "um"],
|
||||
|
||||
@@ -952,7 +952,9 @@ function Completion() //{{{
|
||||
_runCompleter: function _runCompleter(name, 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)
|
||||
liberator.threadYield(true, true);
|
||||
return context.allItems;
|
||||
|
||||
@@ -48,16 +48,8 @@ function AutoCommands() //{{{
|
||||
"List of autocommand event names which should be ignored",
|
||||
"stringlist", "",
|
||||
{
|
||||
completer: function (value) config.autocommands.concat([["all", "All events"]]),
|
||||
validator: function (value)
|
||||
{
|
||||
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);
|
||||
}
|
||||
completer: function () config.autocommands.concat([["all", "All events"]]),
|
||||
validator: options.validateCompleter
|
||||
});
|
||||
|
||||
options.add(["focuscontent", "fc"],
|
||||
|
||||
@@ -568,7 +568,7 @@ function Hints() //{{{
|
||||
{
|
||||
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"],
|
||||
|
||||
@@ -128,7 +128,7 @@ const liberator = (function () //{{{
|
||||
["b", "Bookmark bar"]
|
||||
].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"],
|
||||
|
||||
@@ -218,7 +218,7 @@ function Mail() //{{{
|
||||
|
||||
return value;
|
||||
},
|
||||
validator: function (value) /^(classic|wide|vertical|inherit)$/.test(value)
|
||||
validator: options.validateCompleter
|
||||
});
|
||||
|
||||
/*options.add(["threads"],
|
||||
|
||||
@@ -170,10 +170,10 @@ Option.prototype = {
|
||||
|
||||
hasName: function (name) this.names.indexOf(name) >= 0,
|
||||
|
||||
isValidValue: function (value)
|
||||
isValidValue: function (values)
|
||||
{
|
||||
if (this.validator)
|
||||
return this.validator(value);
|
||||
return this.validator(values);
|
||||
else
|
||||
return true;
|
||||
},
|
||||
@@ -277,10 +277,9 @@ Option.prototype = {
|
||||
|
||||
if (newValue == null)
|
||||
return "Operator " + operator + " not supported for option type " + this.type;
|
||||
newValue = this.joinValues(newValue);
|
||||
if (!this.isValidValue(newValue))
|
||||
return "E474: Invalid argument: " + values;
|
||||
this.set(newValue, scope);
|
||||
this.setValues(newValue, scope);
|
||||
}
|
||||
}; //}}}
|
||||
|
||||
@@ -744,9 +743,13 @@ function Options() //{{{
|
||||
if (!opt.value)
|
||||
completions = [[option.value, "Current value"], [option.defaultValue, "Default value"]].filter(function (f) f[0]);
|
||||
|
||||
context.title = ["Option Value"];
|
||||
if (completer)
|
||||
{
|
||||
completions = completions.concat(completer(filter));
|
||||
let res = completer(context);
|
||||
if (!res)
|
||||
return;
|
||||
completions = completions.concat(res);
|
||||
if (/list$/.test(option.type))
|
||||
{
|
||||
completions = completions.filter(function (val) opt.values.indexOf(val[0]) == -1);
|
||||
@@ -762,7 +765,6 @@ function Options() //{{{
|
||||
}
|
||||
}
|
||||
context.compare = function (a, b) 0;
|
||||
context.title = ["Option Value"];
|
||||
context.completions = completions;
|
||||
},
|
||||
literal: true,
|
||||
@@ -989,6 +991,15 @@ function Options() //{{{
|
||||
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,
|
||||
|
||||
getPref: function (name, forcedDefault)
|
||||
|
||||
@@ -151,7 +151,7 @@ function Tabs() //{{{
|
||||
["2", "Always show tab bar"]
|
||||
];
|
||||
},
|
||||
validator: function (value) value >= 0 && value <= 2
|
||||
validator: options.validateCompleter
|
||||
});
|
||||
|
||||
if (config.name == "Vimperator")
|
||||
@@ -169,12 +169,7 @@ function Tabs() //{{{
|
||||
["paste", "P and gP mappings"]
|
||||
];
|
||||
},
|
||||
validator: function (value)
|
||||
{
|
||||
return value.split(",").every(
|
||||
function (item) /^(homepage|quickmark|tabopen|paste|)$/.test(item)
|
||||
);
|
||||
}
|
||||
validator: options.validateCompleter
|
||||
});
|
||||
|
||||
options.add(["newtab"],
|
||||
@@ -192,12 +187,7 @@ function Tabs() //{{{
|
||||
["prefs", ":pref[erences]! or :prefs! command"]
|
||||
];
|
||||
},
|
||||
validator: function (value)
|
||||
{
|
||||
return value == "all" || value.split(",").every(
|
||||
function (item) /^(addons|downloads|help|javascript|prefs|)$/.test(item)
|
||||
);
|
||||
}
|
||||
validator: options.validateCompleter
|
||||
});
|
||||
|
||||
options.add(["popups", "pps"],
|
||||
@@ -227,7 +217,7 @@ function Tabs() //{{{
|
||||
["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]
|
||||
styles.addSheet("tab-binding", "chrome://*",
|
||||
|
||||
@@ -441,7 +441,7 @@ function CommandLine() //{{{
|
||||
"charlist", "sfl",
|
||||
{
|
||||
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"],
|
||||
@@ -475,28 +475,19 @@ function CommandLine() //{{{
|
||||
|
||||
return engines.map(function (engine) [engine.alias, engine.description]);
|
||||
},
|
||||
validator: function validator(value)
|
||||
{
|
||||
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");
|
||||
});
|
||||
}
|
||||
validator: options.validateCompleter
|
||||
});
|
||||
|
||||
options.add(["wildignore", "wig"],
|
||||
"List of file patterns to ignore when completing files",
|
||||
"stringlist", "",
|
||||
{
|
||||
validator: function validator(value)
|
||||
validator: function validator(values)
|
||||
{
|
||||
// TODO: allow for escaping the ","
|
||||
try
|
||||
{
|
||||
new RegExp("^(" + value.replace(",", "|", "g") + ")$");
|
||||
new RegExp("^(" + value.join("|") + ")$");
|
||||
return true;
|
||||
}
|
||||
catch (e)
|
||||
@@ -521,12 +512,7 @@ function CommandLine() //{{{
|
||||
["list:longest", "List all and complete common string"]
|
||||
];
|
||||
},
|
||||
validator: function validator(value)
|
||||
{
|
||||
let self = this;
|
||||
return value.split(",").every(function (opt)
|
||||
self.completer().some(function ([key]) key == opt))
|
||||
},
|
||||
validator: options.validateCompleter,
|
||||
checkHas: function (value, val)
|
||||
{
|
||||
let [first, second] = value.split(":", 2);
|
||||
@@ -545,10 +531,7 @@ function CommandLine() //{{{
|
||||
["sort", "Always sort the completion list"]
|
||||
];
|
||||
},
|
||||
validator: function validator(value)
|
||||
{
|
||||
return value.split(",").every(function (item) /^(sort|auto|)$/.test(item));
|
||||
}
|
||||
validator: options.validateCompleter
|
||||
});
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////}}}
|
||||
@@ -1571,7 +1554,7 @@ function StatusLine() //{{{
|
||||
["2", "Always display status line"]
|
||||
];
|
||||
},
|
||||
validator: function validator(value) value >= 0 && value <= 2
|
||||
validator: options.validateCompleter
|
||||
});
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////}}}
|
||||
|
||||
Reference in New Issue
Block a user