14

Suppose I have a function like

Options[f] = {foo -> 1};
f[opt : OptionsPattern[{f,g}]] := h@g[Sequence @@ FilterRules[{opt}, Options[g]]]

I purposefully do not want to append Options[g] to Options[f] because I want the defaults to be inherited from g instead of setting them separately for f.

In this situation, is there a way to use SyntaxInformation with option highlighting for f? It seems a naïve attempt results in red colouring for anything that is not in Options[f] (as the documentation states). But I wanted to ask anyway, just in case I missed something and there's a way to get this working after all.

Example:

Options[g] = {bar -> 2}; (* in practice g could be a builtin *)
SyntaxInformation[f] = {"ArgumentsPattern" -> {OptionsPattern[{f, g}]}};

Mathematica graphics

So it appears that arguments of OptionsPattern are not supported within SyntaxInformation (they are ignored).

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • Or perhaps people consider it bad practice to want to inherit the default option values from g? If so, why? – Szabolcs Oct 20 '15 at 08:40
  • 1
    I use a different approach to inheriting default options here. I think this is not directly useful to you, but it may still interest you and your feedback would be appreciated. – Jacob Akkerboom Oct 20 '15 at 11:17

1 Answers1

14

SyntaxInformation accepts undocumented "OptionNames" property. Using it we can explicitly define list of option names that will not be colored red.

ClearAll[f]
Options[f] = {foo -> 1};
SyntaxInformation[f] = {
    "ArgumentsPattern" -> {OptionsPattern[]},
    "OptionNames" -> {"bar"}
};

f with bar

With simple helper function extracting relevant option names:

optionNames = ToString /@ Apply[Join, Options /@ {##}][[All, 1]] &;

ClearAll[f, g]
Options[f] = {foo -> 1};
Options[g] = {bar -> 2};
SyntaxInformation[f] = {
   "ArgumentsPattern" -> {OptionsPattern[]}, 
   "OptionNames" -> optionNames[f, g]
};

f with foo and bar

Tested in Mathematica versions: 8.0, 9.0, 10.0, 10.1, 10.2 and 10.3.

jkuczm
  • 15,078
  • 2
  • 53
  • 84
  • 1
    +1, great find! Did you find this by doing PrintDefinitions@System`Utilities`GetSystemSyntaxInformation? – Jacob Akkerboom Nov 03 '15 at 12:19
  • @JacobAkkerboom No, I was looking at SyntaxInformation@StyleBox and there it was. I never seen System`Utilities`GetSystemSyntaxInformation, thanks for showing it. – jkuczm Nov 03 '15 at 12:41