Is it possible to add new Live Template Macro Types via a r# plugin?
You can add a live template macro in the following way by defining a class like: using JetBrains.ReSharper.LiveTemplates;
[Macro("myMacroName", Short Description="Does something usefull with {#0:type}", LongDescription="...")] public class MyBrandNewLiveTemplateMacro : IMacro { #region IMacro implementation
public ParameterInfo[] Parameters { get { return new ParameterInfo[] {new ParameterInfo (ParameterType.Type)}; } }
...
#endregion }
However, you cannot add ParameterInfo's different from the existing ones.
To create a template with a field using your macro you need code like:
//if you do not need to persist your template you can use ITemplateExpression implementations different from MacroCallExpression MacroCallExpression expression = new MacroCallExpression(new MyBrandNewLiveTemplateMacro());
// you can also use VariableMacroParameter(templateFieldName) if the value passed is defined by the value of another field of the template expression.AddParameter(new ConstantMacroParameter("SomeTypeName"));
TemplateField templateField = new TemplateField("",
expression, -1);
// if you create a user template it is sensible to persist it somewhere
under user templates
ITemplateFolder folder = TemplateManager.Instance.LiveTemplatesStorageGroup.UserStorage;
// create a new template
myTemplate = TemplateManager.Instance.CreateTemplate("]]>", new TemplateField[] , TemplateKind.LiveTemplate, folder); // set some settings myTemplate.Shortcut = "alphaNumericShortcut"; myTemplate.Description = "Some understandable description"; myTemplate.Context = new FileNameTemplateContext("*.cs");// you can also use other ITemplateContext implementations or define your own ITemplateContext factory. // make template presistent folder.AddTemplate(defaultTemplate);
I tried it and failed to make it work. I've restarted VS right after defining new Live Template which had one variable assigned to custom macro, located in plugin (in fact, that was AddLiveTemplatesMacro sample). Immediately after defining this template it works as expected. After restart, my custom macro cannot be found, and Resharper throws an exception.
Configuration file is like this:
]]> And the class is defined this way [Macro("MyCurrentFilePath", ShortDescription = "Current file path", LongDescription="Evaluates current file path" )] public class MyCurrentFilePath : IMacro { ... the rest of file is left unchaged ... }
You can add a live template macro in the following way by defining a class
like:
using JetBrains.ReSharper.LiveTemplates;
[Macro("myMacroName", Short Description="Does something usefull with {#0:type}",
LongDescription="...")]
public class MyBrandNewLiveTemplateMacro : IMacro
{
#region IMacro implementation
public ParameterInfo[] Parameters
{
get { return new ParameterInfo[] {new ParameterInfo (ParameterType.Type)};
}
}
...
#endregion
}
However, you cannot add ParameterInfo's different from the existing ones.
To create a template with a field using your macro you need code like:
//if you do not need to persist your template you can use ITemplateExpression
implementations different from MacroCallExpression
MacroCallExpression expression = new MacroCallExpression(new MyBrandNewLiveTemplateMacro());
// you can also use VariableMacroParameter(templateFieldName) if
the value passed is defined by the value of another field of the template
expression.AddParameter(new ConstantMacroParameter("SomeTypeName"));
TemplateField templateField = new TemplateField("", expression, -1); // if you create a user template it is sensible to persist it somewhere under user templates ITemplateFolder folder = TemplateManager.Instance.LiveTemplatesStorageGroup.UserStorage; // create a new template myTemplate = TemplateManager.Instance.CreateTemplate("]]>",
new TemplateField[] , TemplateKind.LiveTemplate, folder);
// set some settings
myTemplate.Shortcut = "alphaNumericShortcut";
myTemplate.Description = "Some understandable description";
myTemplate.Context = new FileNameTemplateContext("*.cs");// you
can also use other ITemplateContext implementations or define your own ITemplateContext
factory.
// make template presistent
folder.AddTemplate(defaultTemplate);
Thanks,
Andrey Simanovsky
ok, thx, ill try this :)
There is a plugin sample which does exactly this. Have you looked into it?
Valentin Kipiatkov
Chief Scientist, Vice President of Product Development
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
I tried it and failed to make it work.
I've restarted VS right after defining new Live Template which had one variable assigned to custom macro, located in plugin (in fact, that was AddLiveTemplatesMacro sample).
Immediately after defining this template it works as expected. After restart, my custom macro cannot be found, and Resharper throws an exception.
Configuration file is like this:
]]>
And the class is defined this way
[Macro("MyCurrentFilePath",
ShortDescription = "Current file path",
LongDescription="Evaluates current file path" )]
public class MyCurrentFilePath : IMacro
{
... the rest of file is left unchaged ...
}
I have posted the exception details to JIRA tracker. See here: http://jetbrains.net/jira/browse/RSRP-28091
As far as we've investigated, there is a problem in ReSharper core which
will prevent user macro's from loading. We are working on fixing this issue.
--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Carsten Jendro" <carsten.jendro@gmx.net> wrote in message
news:32974933.1143116851385.JavaMail.itn@is.intellij.net...