customization - How to prevent front end from suggesting Symbol option names (when actually String), and from adding them to Global` context
Help!
I am writing a package with a function which takes lots and lots of different options which are String
s. When used, the front end autocompletion is suggesting them in Symbol
form. If the resulting code is run, it is added to the General`
context. Observe:
Define the Options
of MyFunc
, and its SyntaxInformation
Options[MyFunc] = {"MyOpt" -> True};
SyntaxInformation[MyFunc] = {"ArgumentsPattern" -> {_, OptionsPattern[]}};
Then, start typing MyFunc[3.2, My
, and you will see MyOpt
as a suggestion:
If you run the finished statement MyFunc[3.2, MyOpt->False]
, the program works as if it had been a String
(totally unacceptable), and the option symbol MyOpt
is added to the Global`
context (also totally unacceptable):
This leads to a proliferation of these symbols in the user's front end, which is annoying. How can I prevent this?
Answer
You can use undocumented "OptionNames"
property of SyntaxInformation
to customize option names suggestions (and coloring of invalid option names).
MyFunc // ClearAll
MyFunc // Options = {"MyOpt" -> True};
MyFunc // SyntaxInformation = {
"ArgumentsPattern" -> {_, OptionsPattern[]},
"OptionNames" -> {"\"MyOpt\""}
};
After typing MyFunc[3.2, My
I get (in version 11.0):
Comments
Post a Comment