slevithan / xregexp

Extended JavaScript regular expressions
http://xregexp.com/
MIT License
3.29k stars 278 forks source link

allow set configable features for install/uninstall a plugin #239

Open bluelovers opened 6 years ago

bluelovers commented 6 years ago

current XRegExp can't check a plugin already install or not and only can addToken, but can't remove token

hope can have a configable for check plugin is installed and disable or uninstall

josephfrazier commented 6 years ago

Thanks for the report! Since you also opened #240 recently, I assume you're talking about custom plugins like your xregexp-plugin-hanzi-cjk.

You're right in that XRegExp doesn't have any mechanism to manage third-party plugins like this, but you should be able to do it in the plugin itself, by keeping the instance in a cache somewhere (it looks like you're already doing this here?).

I think you can take a similar approach for disabling plugins: Have the plugin export an "uninstall" function that takes XRegExp and removes it from the cache. Then, in your addToken callback, check isInstalled to determine whether the plugin should work or not.

Hope this helps!

slevithan commented 6 years ago

That's a clever workaround, @josephfrazier. Extending on that, multiple custom tokens can check whether the same feature (e.g. Ruby regex syntax) is installed or not, so they can be installed/uninstalled together.

I haven't had much signal on whether custom syntax tokens are useful to people, hence the very minimal controls around them. It would not be difficult to create a removeToken method--you'd just need to introduce a new way to reference tokens such as by allowing them to be given a name when first added. But IMO it might be bad practice to use custom tokens at all without also renaming XRegExp, since otherwise they introduce ambiguity about what syntax and flags are enabled across different projects.

A couple of related things I've considered in the past that would improve the management of custom syntax tokens and flags:

josephfrazier commented 6 years ago

I like this idea! It feels cleaner than continuing to keep a shared XRegExp instance even despite having resetTokens. Not exactly sure what the best way to go about it is, though...


EDIT: I also realized that for this part to work:

Then, in your addToken callback, check isInstalled to determine whether the plugin should work or not

we'll probably need to add XRegExp to the context that the token handler is called with.