Keith Nicholas
Keith Nicholas

Reputation: 44298

How to uppercase entire variable in Resharper Template?

I have a situation where I want the variable to be capitalized for documentation eg
(trivalized for example)

///AT+$COMMAND$

void At$COMMAND$()
{
}

So I want the user of the template to type in something like "Blah" and that gets used in the method name, but the documentation part gets changed to "BLAH".

eg

///AT+BLAH
void AtBlah()
{
}

Can I do this? I see in the macros I can capitalize the first letter, but I'd like the whole word capitalized. Is it possible to create custom macros?

Upvotes: 0

Views: 985

Answers (1)

Jan Zahradník
Jan Zahradník

Reputation: 2497

They just updated documentation to meet changes in macros in Resharper 8. You can check it at http://confluence.jetbrains.com/display/NETCOM/4.04+Live+Template+Macros+%28R8%29

With the new docs it is quite easy, my implementation goes here:

using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using JetBrains.DocumentModel;
using JetBrains.ReSharper.Feature.Services.LiveTemplates.Macros;
using JetBrains.ReSharper.Feature.Services.LiveTemplates.Hotspots;

namespace ReSharperPlugin
{
    [MacroDefinition("LiveTemplatesMacro.CapitalizeVariable", // macro name should be unique among all other macros, it's recommended to prefix it with your plugin name to achieve that
    ShortDescription = "Capitalizes variable {0:list}", // description of the macro to be shown in the list of macros
    LongDescription = "Capitalize full name of variable" // long description of the macro to be shown in the area below the list
    )]
    public class CapitalizeVariableMacro : IMacroDefinition
    {
        public string GetPlaceholder(IDocument document, IEnumerable<IMacroParameterValue> parameters)
        {
            return "A";
        }

        public ParameterInfo[] Parameters
        {
            get { return new[] {new ParameterInfo(ParameterType.VariableReference)}; }
        }
    }

    [MacroImplementation(Definition = typeof(CapitalizeVariableMacro))]
    public class CapitalizeVariableMacroImpl : SimpleMacroImplementation
    {
        private readonly IMacroParameterValueNew _parameter;

        public CapitalizeVariableMacroImpl([Optional] MacroParameterValueCollection parameters)
        {
            _parameter = parameters.OptionalFirstOrDefault();
        }

        public override string EvaluateQuickResult(IHotspotContext context)
        {
            return _parameter == null ? null : _parameter.GetValue().ToUpperInvariant();
        }
    }
}

Upvotes: 0

Related Questions