如何在Resharper模板中大写整个变量?

时间:2013-10-07 22:32:58

标签: visual-studio-2012 resharper-7.1

我有一种情况,我希望变量大写为文档,例如
(例如,trivalized)

///AT+$COMMAND$

void At$COMMAND$()
{
}

所以我希望模板的用户输入类似“Blah”的东西,并在方法名称中使用,但文档部分变为“BLAH”。

例如

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

我可以这样做吗?我在宏中看到我可以将第一个字母大写,但我希望整个单词大写。是否可以创建自定义宏?

1 个答案:

答案 0 :(得分:0)

他们刚刚更新了文档以满足Resharper 8中宏的更改。您可以在http://confluence.jetbrains.com/display/NETCOM/4.04+Live+Template+Macros+%28R8%29

进行检查

使用新文档很容易,我的实现就在这里:

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();
        }
    }
}