Parameters in IMacroDefinition, IMacroImplementation (Live Template Macro) [ReSharper 8+]

I need some help with a macro implemenation in ReSharper 8 (not 7, only 8)

Goal: I want to template code commentary seperator blocks that look like this. The second line is a repeated character (the char is actually U2500, not Minus) and shall have the same length as the comment above.

// ----------------------

so I'm creating a life template ...

// $comment$
// $commentline$

... and now $commentline$ shall call a macro with 2 parameters; first one is the character ('-', '=', '~') to repeat, second one is the reference to $comment$.

That was the easy part. Now, how do I define parameters in the macro implementation? If there's no way to label or describe or something these parameters in Template Explorer (just having 2 "silent" fields stacked to each other) then I got this part working. But how do I access the value of these parameters in string EvaluateQuickResult(IHotspotContext context)? There's no more IList<> in the method's signature, so I assume I have to extract it from the 7 gazillion properties and fields in "context".

string paramCharacter = ...;
string paramComment = ...;

return new string(paramCharacter[0], paramComment.Length);

Now, the bogus question. Is there a way to keep or modify $comment$ UPPERCASED while typing? Only interested if it's simple and can be realized in less than 10 code lines (without adding another bunch of classes).


Comment actions Permalink

This page provides details on how to implement a macro extension:

To add parameters, implement the IMacroDefinition.Parameters property and return an array of ParameterInfo, one for each parameter. The first parameter would have ParameterType.String, and the second ParameterType.VariableReference. You'll be given these values in the IMacroDefinition.GetPlaceholder method, and you can simply call parameter.GetValue(lifetime, session) - the lifetime parameter can come from constructor injection of the definition, e.g. MyDefinition(Lifetime myLifetime).

The implementation gets the parameters as an optional constructor parameter, of type MacroParameterValueCollection. This is essentially a list of IMacroParameterValueNew interfaces (got to love the "New" bit :)) that you can enumerate and call GetValue(), in order to get the value as a string. Stash the collection into a field, and call GetValue in your IMacroImplementation methods.

Comment actions Permalink

Excellent, got it working.

Thank you very much.


Please sign in to leave a comment.