How to create live template macro with parameter?

Hi,
I'm trying to create a macro based on value of another variable. Unfortunatelly the SDK Samples and documentation are very short. Phrases like "Most ReSharper LTs typically put an "a" there." don't help much. There is a change in R# 8 in splitting IMacro to two different classes, but I haven't found how to interact between them.

Thanks

6 comments
Comment actions Permalink

Hi Jan. I've just updated the docs, brought them up to date with the changes in R#8, and expanded some of the details.

http://confluence.jetbrains.com/display/NETCOM/4.04+Live+Template+Macros+%28R8%29

Let me know what you think - does it give you the information you need?

Thanks
Matt

0
Comment actions Permalink

Hi Matt,
I was able to create the macro easily with the new doc. Especially the injected constructor parameters I was not able to track with dotPeek before. The only minor issue I have with the Short description - the placeholder, eg. {0:name} doesn't work for my VariableReference parameter.

Thank you very much,
Jan

0
Comment actions Permalink

What do you mean by doesn't work? You also need to return a parameter from your IMacroDefinition instance - the short description just allows you to name it so that other macros can get the value by name, although this is very rare.

0
Comment actions Permalink

When you are defining new template, some macros are using entered parameter value in their description, eg. "Display date in specified FORMAT". So I supposed that placeholder {0:whatever} will be replaced by value of the 0th parameter. I haven't tested with Text paramters, but for variable parameters it doesn't work.

As Parameters property in my macro definition I return return new[] {new ParameterInfo(ParameterType.VariableReference)}; I haven't found a way to name created parameter (which is done by ShortDescription).

Thanks in advance,
Jan

0
Comment actions Permalink

Nothing gets replaced. The placeholder in the short description is used to get a name for the parameter at that particular index. It's also used to format the description in the list box in the template editor - any parameters in the {0:whatever} format are shown in bold (in this case, the word "whatever" would be shown in bold). That's all the short description is used for.

The way to actually get parameters working is to return an array of ParameterInfo objects from your IMacroDefinition. The template editor will use this to display UI in the template editor. The user defining the template needs to fill those values in, and they will be saved with the template. When your macro implementation is created, the values are passed to your constructor. The variable parameter will show a drop down in the template editor, listing other variables from the template (e.g. if you have "$className$" in your template text, "className" will appear as one of the options in the drop down for your parameter. If you don't give a parameter a value in the template editor, you'll get null passed in to your implementation.

As it happens, the parameter name from the description isn't really used anywhere useful. It's given to the template editor UI, so I presume at one point it was intended to be used as a label for the field in the UI, but it's not currently used. However, it's useful to know why some values are bold, and to bold the parts of the description of the macro that can be parmaterised. I'll update the docs to show that it's less useful than it first appears.

Hope this helps clear up any confusion around this - if you still can't get the variable parameter work, let me know, and we'll see what we can do to get it working.

0
Comment actions Permalink

Thank you for clarification.

0

Please sign in to leave a comment.