Surrounding Code With Regions???

Hey again,

I tend to ask many question as it seems but it's all from love :)

i want to know if there is any way to surround a group of method /
properties / members / etc...
with regions?

if so, i would be more than happy to know!

thank you very much,
Guy.


12 comments

The only way is to create #region/#endregion directives and insert them into
PSI-tree


--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Ga" <t4m.productions@gmail.com> wrote in message
news:f9voac$7ul$1@is.intellij.net...

Hey again,

>

I tend to ask many question as it seems but it's all from love :)

>

i want to know if there is any way to surround a group of method /
properties / members / etc...
with regions?

>

if so, i would be more than happy to know!

>

thank you very much,
Guy.



0

Are you trying to duplicate the Ctrl-Alt-J "Surround With" functionality?

"Ga" <t4m.productions@gmail.com> wrote in message
news:f9voac$7ul$1@is.intellij.net...

Hey again,

>

I tend to ask many question as it seems but it's all from love :)

>

i want to know if there is any way to surround a group of method /
properties / members / etc...
with regions?

>

if so, i would be more than happy to know!

>

thank you very much,
Guy.


0

Can you show me how please?


"Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> wrote in
message news:fa10up$es3$1@is.intellij.net...

The only way is to create #region/#endregion directives and insert them
into PSI-tree

>
>

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Ga" <t4m.productions@gmail.com> wrote in message
news:f9voac$7ul$1@is.intellij.net...

>> Hey again,
>>
>> I tend to ask many question as it seems but it's all from love :)
>>
>> i want to know if there is any way to surround a group of method /
>> properties / members / etc...
>> with regions?
>>
>> if so, i would be more than happy to know!
>>
>> thank you very much,
>> Guy.
>>
>



0

no,

i'm trying to create a plugin which will take all the class:
- members / methods / properties / delegates /...
and wrap each group into its region, and reorganize
them into a defined order... cool isn't it ?

So I would be happy if you could give me an example :)



"Paul Bradshaw" <pbradshaw@advsol.com> wrote in message
news:fa1p5r$q0o$1@is.intellij.net...

Are you trying to duplicate the Ctrl-Alt-J "Surround With" functionality?

>

"Ga" <t4m.productions@gmail.com> wrote in message
news:f9voac$7ul$1@is.intellij.net...

>> Hey again,
>>
>> I tend to ask many question as it seems but it's all from love :)
>>
>> i want to know if there is any way to surround a group of method /
>> properties / members / etc...
>> with regions?
>>
>> if so, i would be more than happy to know!
>>
>> thank you very much,
>> Guy.
>>



0

public void EnRegion(IList declarations, string regionName) { ITreeNode firstNode = declarations[0].ToTreeNode(); if (firstNode is IMultipleDeclarationMemberNode) { MultipleDeclarationUtil.SplitDeclarationBefore((IMultipleDeclarationMemberNode)firstNode); firstNode = ((IMultipleDeclarationMemberNode)firstNode).MultipleDeclaration; } ITreeNode lastNode = declarations[declarations.Count - 1].ToTreeNode(); if (lastNode is IMultipleDeclarationMemberNode) { MultipleDeclarationUtil.SplitDeclarationAfter((IMultipleDeclarationMemberNode)lastNode); lastNode = ((IMultipleDeclarationMemberNode)lastNode).MultipleDeclaration; } // Add new region IStartRegionNode startRegion; IEndRegionNode endRegion; CreateRegionDirectives(String.IsNullOrEmpty(regionName) ? "..." : regionName, out startRegion, out endRegion); using (WriteLockCookie.Create()) { if (startRegion != null) startRegion = ModificationUtil.AddChildBefore(firstNode, startRegion); if (endRegion != null) endRegion = ModificationUtil.AddChildAfter(lastNode, endRegion); } } private void CreateRegionDirectives(string regionText, out IStartRegionNode startRegion, out IEndRegionNode endRegion) { CSharpElementFactory factory = CSharpElementFactory.GetInstance(myDeclaration.GetManager().Solution); ICSharpFile file = factory.CreateFile("#region " + regionText + "\n#endregion"); ITreeNode node = file.FindTokenAt(0) as ITreeNode; startRegion = null; endRegion = null; while (node != null) { startRegion = node as IStartRegionNode; if (startRegion != null) break; node = node.Parent; } if (startRegion != null) endRegion = startRegion.EndRegion; } -- Eugene Pasynkov Developer JetBrains, Inc http://www.jetbrains.com "Develop with pleasure!" "Ga" ]]> wrote in message
news:fa22qc$fc9$1@is.intellij.net...

Can you show me how please?

>
>

"Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> wrote in
message news:fa10up$es3$1@is.intellij.net...

>> The only way is to create #region/#endregion directives and insert them
>> into PSI-tree
>>
>>
>> --
>> Eugene Pasynkov
>> Developer
>> JetBrains, Inc
>> http://www.jetbrains.com
>> "Develop with pleasure!"
>> "Ga" <t4m.productions@gmail.com> wrote in message
>> news:f9voac$7ul$1@is.intellij.net...
>>> Hey again,
>>>
>>> I tend to ask many question as it seems but it's all from love :)
>>>
>>> i want to know if there is any way to surround a group of method /
>>> properties / members / etc...
>>> with regions?
>>>
>>> if so, i would be more than happy to know!
>>>
>>> thank you very much,
>>> Guy.
>>>
>>
>>
>



0

Hello,

i'm trying to create a plugin which will take all the class:
- members / methods / properties / delegates /...
and wrap each group into its region, and reorganize
them into a defined order... cool isn't it ?
So I would be happy if you could give me an example :)


Looks like the R#'s “Reorder Type Members” functionality.


Serge Baltic
JetBrains, Inc — http://www.jetbrains.com
“Develop with pleasure!”


0

everythings goes out fine except that the modification util does not
actually change the file and adds the region...
when debugging i see all that the debugging data is good (in terms of where
to create the region).
what might be the problem ???
other than that... thank you very much for your patience to write that code
:)
Guy.

//ModificationUtil doesn't change anything :(
startRegion = ModificationUtil.AddChildBefore(firstNode, startRegion);


"Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> wrote in
message news:fa46mh$4r0$1@is.intellij.net...

public void EnRegion(IList<IDeclaration> declarations, string regionName)

>

{

>

ITreeNode firstNode = declarations[0].ToTreeNode();

>

if (firstNode is IMultipleDeclarationMemberNode)

>

{

>

MultipleDeclarationUtil.SplitDeclarationBefore((IMultipleDeclarationMemberNode)firstNode);

>

firstNode =
((IMultipleDeclarationMemberNode)firstNode).MultipleDeclaration;

>

}

>

ITreeNode lastNode = declarations[declarations.Count - 1].ToTreeNode();

>

if (lastNode is IMultipleDeclarationMemberNode)

>

{

>

MultipleDeclarationUtil.SplitDeclarationAfter((IMultipleDeclarationMemberNode)lastNode);

>

lastNode = ((IMultipleDeclarationMemberNode)lastNode).MultipleDeclaration;

>

}

>

// Add new region

>

IStartRegionNode startRegion;

>

IEndRegionNode endRegion;

>

CreateRegionDirectives(String.IsNullOrEmpty(regionName) ? "..." :
regionName, out startRegion, out endRegion);

>

using (WriteLockCookie.Create())

>

{

>

if (startRegion != null) startRegion =
ModificationUtil.AddChildBefore(firstNode, startRegion);

>

if (endRegion != null) endRegion =
ModificationUtil.AddChildAfter(lastNode, endRegion);

>

}

>

}

>

private void CreateRegionDirectives(string regionText, out
IStartRegionNode startRegion, out IEndRegionNode endRegion)

>

{

>

CSharpElementFactory factory =
CSharpElementFactory.GetInstance(myDeclaration.GetManager().Solution);

>

ICSharpFile file = factory.CreateFile("#region " + regionText +
"\n#endregion");

>

ITreeNode node = file.FindTokenAt(0) as ITreeNode;

>

startRegion = null;

>

endRegion = null;

>

while (node != null)

>

{

>

startRegion = node as IStartRegionNode;

>

if (startRegion != null)

>

break;

>

node = node.Parent;

>

}

>

if (startRegion != null)

>

endRegion = startRegion.EndRegion;

>

}

>
>

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Ga" <t4m.productions@gmail.com> wrote in message
news:fa22qc$fc9$1@is.intellij.net...

>> Can you show me how please?
>>
>>
>> "Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> wrote in
>> message news:fa10up$es3$1@is.intellij.net...
>>> The only way is to create #region/#endregion directives and insert them
>>> into PSI-tree
>>>
>>>
>>> --
>>> Eugene Pasynkov
>>> Developer
>>> JetBrains, Inc
>>> http://www.jetbrains.com
>>> "Develop with pleasure!"
>>> "Ga" <t4m.productions@gmail.com> wrote in message
>>> news:f9voac$7ul$1@is.intellij.net...
>>>> Hey again,
>>>>
>>>> I tend to ask many question as it seems but it's all from love :)
>>>>
>>>> i want to know if there is any way to surround a group of method /
>>>> properties / members / etc...
>>>> with regions?
>>>>
>>>> if so, i would be more than happy to know!
>>>>
>>>> thank you very much,
>>>> Guy.
>>>>
>>>
>>>
>>
>>
>



0

Hey... :)

"Serge Baltic" <baltic@intellij.net> wrote in message
news:dc0986bf6bece8c9af16b69bbe84@news.intellij.net...

Hello,

>
>> i'm trying to create a plugin which will take all the class:
>> - members / methods / properties / delegates /...
>> and wrap each group into its region, and reorganize
>> them into a defined order... cool isn't it ?
>> So I would be happy if you could give me an example :)
>

Looks like the R#'s ?Reorder Type Members? functionality.

>

?
Serge Baltic
JetBrains, Inc ? http://www.jetbrains.com
?Develop with pleasure!?

>



0

Please could you minimize your code and send it to me?
it will be much easier for me to check what's going wrong looking into the
code, then trying to guess it from your words :)

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Ga" <t4m.productions@gmail.com> wrote in message
news:fa4iea$umk$1@is.intellij.net...

everythings goes out fine except that the modification util does not
actually change the file and adds the region...
when debugging i see all that the debugging data is good (in terms of
where to create the region).
what might be the problem ???
other than that... thank you very much for your patience to write that
code :)
Guy.

>

//ModificationUtil doesn't change anything :(
startRegion = ModificationUtil.AddChildBefore(firstNode, startRegion);

>
>

"Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> wrote in
message news:fa46mh$4r0$1@is.intellij.net...

>> public void EnRegion(IList declarations, string regionName) >> >> { >> >> ITreeNode firstNode = declarations[0].ToTreeNode(); >> >> if (firstNode is IMultipleDeclarationMemberNode) >> >> { >> >> MultipleDeclarationUtil.SplitDeclarationBefore((IMultipleDeclarationMemberNode)firstNode); >> >> firstNode = >> ((IMultipleDeclarationMemberNode)firstNode).MultipleDeclaration; >> >> } >> >> ITreeNode lastNode = declarations[declarations.Count - 1].ToTreeNode(); >> >> if (lastNode is IMultipleDeclarationMemberNode) >> >> { >> >> MultipleDeclarationUtil.SplitDeclarationAfter((IMultipleDeclarationMemberNode)lastNode); >> >> lastNode = >> ((IMultipleDeclarationMemberNode)lastNode).MultipleDeclaration; >> >> } >> >> // Add new region >> >> IStartRegionNode startRegion; >> >> IEndRegionNode endRegion; >> >> CreateRegionDirectives(String.IsNullOrEmpty(regionName) ? "..." : >> regionName, out startRegion, out endRegion); >> >> using (WriteLockCookie.Create()) >> >> { >> >> if (startRegion != null) startRegion = >> ModificationUtil.AddChildBefore(firstNode, startRegion); >> >> if (endRegion != null) endRegion = >> ModificationUtil.AddChildAfter(lastNode, endRegion); >> >> } >> >> } >> >> private void CreateRegionDirectives(string regionText, out >> IStartRegionNode startRegion, out IEndRegionNode endRegion) >> >> { >> >> CSharpElementFactory factory = >> CSharpElementFactory.GetInstance(myDeclaration.GetManager().Solution); >> >> ICSharpFile file = factory.CreateFile("#region " + regionText + >> "\n#endregion"); >> >> ITreeNode node = file.FindTokenAt(0) as ITreeNode; >> >> startRegion = null; >> >> endRegion = null; >> >> while (node != null) >> >> { >> >> startRegion = node as IStartRegionNode; >> >> if (startRegion != null) >> >> break; >> >> node = node.Parent; >> >> } >> >> if (startRegion != null) >> >> endRegion = startRegion.EndRegion; >> >> } >> >> >> -- >> Eugene Pasynkov >> Developer >> JetBrains, Inc >> http://www.jetbrains.com >> "Develop with pleasure!" >> "Ga" wrote in message >> news:fa22qc$fc9$1@is.intellij.net... >>> Can you show me how please? >>> >>> >>> "Eugene Pasynkov (JetBrains)" wrote in >>> message news:fa10up$es3$1@is.intellij.net... >>>> The only way is to create #region/#endregion directives and insert them >>>> into PSI-tree >>>> >>>> >>>> -- >>>> Eugene Pasynkov >>>> Developer >>>> JetBrains, Inc >>>> http://www.jetbrains.com >>>> "Develop with pleasure!" >>>> "Ga" wrote in message >>>> news:f9voac$7ul$1@is.intellij.net... >>>>> Hey again, >>>>> >>>>>]]> I tend to ask many question as it seems but it's all from love :)
>>>>>
>>>>> i want to know if there is any way to surround a group of method /
>>>>> properties / members / etc...
>>>>> with regions?
>>>>>
>>>>> if so, i would be more than happy to know!
>>>>>
>>>>> thank you very much,
>>>>> Guy.
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>



0

Here is my source code... Just the relavent parts...
Thanks!!!

===== Cotext Action Class =====

/// /// in case the plugin could be applied, and the user selected /// to apply it, this method is invocated by resharper core. /// /// /// public void Execute(ISolution solution, ITextControl textControl) { PsiManager.GetInstance(m_solution).UpdateCaches(); m_tokenAccessor = new TokenAccessor(solution, textControl); //default sanity check made by the api dev team. if (m_solution != solution || m_textControl != textControl) { throw new InvalidOperationException(); } //check that we can modify the current file. Shell.Instance.AssertReadAccessAllowed(); //creating a command cookie is ment for supporting undo later //by visual studio. this is why inside the command cookie //definition we have a transaction, in order to 'tell' visual studio //to reverse all the changes that we perform now as a single action using (CommandCookie.Create("GenerateClassSingleton")) { // Any changes to PSI should be under a transaction PsiManager.GetInstance(m_solution). DoTransaction( delegate { //make the actual change to the class ExecuteMakeClassSingleton(TokenAccessor.ClassDeclaration.DeclaredName); }); } } private List declarations; ///

/// receives a class name and turns it into a singleton by adding 3 declarations: /// private constructor, a class member, and an Instance property. /// /// private void ExecuteMakeClassSingleton(string className) { //the formats string ctor = @" /// /// private constructor in order to prevent creating class instances /// private $0(){} "; string instance = @" /// /// the singleton instance created using the private constructor /// private static readonly $0 $1 = new $0(); "; string property = @" /// /// returns the singleton instance /// public static $0 Instance { get { return $1; } } "; //generate the three declarations IConstructorDeclaration ctorDeclaration = Factory.CreateTypeMemberDeclaration(ctor, className) as IConstructorDeclaration; IClassMemberDeclaration instanceDeclaration = Factory.CreateTypeMemberDeclaration(instance, className, SINGLETON_MEMBER_NAME) as IClassMemberDeclaration; IPropertyDeclaration propDeclaration = Factory.CreateTypeMemberDeclaration(property, className, SINGLETON_MEMBER_NAME) as IPropertyDeclaration; //adding the declarations to the current class IClassDeclaration currentClass = TokenAccessor.ClassDeclaration; currentClass.AddClassMemberDeclarationAfter(propDeclaration, null); currentClass.AddClassMemberDeclarationAfter(instanceDeclaration, null); if (IsClassHasEmptyCtor == false) currentClass.AddClassMemberDeclarationAfter(ctorDeclaration, null); //surrounding with region List declarations = new List(); declarations.Add(ctorDeclaration); declarations.Add(instanceDeclaration); declarations.Add(propDeclaration); RegionsUtil.EnRegion(m_solution, declarations, "Singleton"); } ===== RegionsUtil.EnRegion===== public static void EnRegion(ISolution solution, IList declarations, string regionName) { ITreeNode firstNode = declarations[0].ToTreeNode(); if (firstNode is IMultipleDeclarationMemberNode) { MultipleDeclarationUtil.SplitDeclarationBefore((IMultipleDeclarationMemberNode) firstNode); firstNode = ((IMultipleDeclarationMemberNode) firstNode).MultipleDeclaration; } ITreeNode lastNode = declarations[declarations.Count - 1].ToTreeNode(); if (lastNode is IMultipleDeclarationMemberNode) { MultipleDeclarationUtil.SplitDeclarationAfter((IMultipleDeclarationMemberNode) lastNode); lastNode = ((IMultipleDeclarationMemberNode) lastNode).MultipleDeclaration; } // Add new region IStartRegionNode startRegion; IEndRegionNode endRegion; CreateRegionDirectives(solution, String.IsNullOrEmpty(regionName) ? "..." : regionName, out startRegion, out endRegion); using (WriteLockCookie.Create()) { if (startRegion != null) startRegion = ModificationUtil.AddChildBefore(firstNode, startRegion); if (endRegion != null) endRegion = ModificationUtil.AddChildAfter(lastNode, endRegion); } } private static void CreateRegionDirectives(ISolution solution, string regionText, out IStartRegionNode startRegion, out IEndRegionNode endRegion) { CSharpElementFactory factory = CSharpElementFactory.GetInstance(solution); ICSharpFile file = factory.CreateFile("#region " + regionText + "\n#endregion"); ITreeNode node = file.FindTokenAt(0) as ITreeNode; startRegion = null; endRegion = null; while (node != null) { startRegion = node as IStartRegionNode; if (startRegion != null) break; node = node.Parent; } if (startRegion != null) endRegion = startRegion.EndRegion; } "Eugene Pasynkov (JetBrains)" ]]> wrote in
message news:fabt1l$5do$1@is.intellij.net...

Please could you minimize your code and send it to me?
it will be much easier for me to check what's going wrong looking into the
code, then trying to guess it from your words :)

>

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Ga" <t4m.productions@gmail.com> wrote in message
news:fa4iea$umk$1@is.intellij.net...

>> everythings goes out fine except that the modification util does not
>> actually change the file and adds the region...
>> when debugging i see all that the debugging data is good (in terms of
>> where to create the region).
>> what might be the problem ???
>> other than that... thank you very much for your patience to write that
>> code :)
>> Guy.
>>
>> //ModificationUtil doesn't change anything :(
>> startRegion = ModificationUtil.AddChildBefore(firstNode, startRegion);
>>
>>
>> "Eugene Pasynkov (JetBrains)" wrote in >> message news:fa46mh$4r0$1@is.intellij.net... >>> public void EnRegion(IList declarations, string >>> regionName) >>> >>> { >>> >>> ITreeNode firstNode = declarations[0].ToTreeNode(); >>> >>> if (firstNode is IMultipleDeclarationMemberNode) >>> >>> { >>> >>> MultipleDeclarationUtil.SplitDeclarationBefore((IMultipleDeclarationMemberNode)firstNode); >>> >>> firstNode = >>> ((IMultipleDeclarationMemberNode)firstNode).MultipleDeclaration; >>> >>> } >>> >>> ITreeNode lastNode = declarations[declarations.Count - 1].ToTreeNode(); >>> >>> if (lastNode is IMultipleDeclarationMemberNode) >>> >>> { >>> >>> MultipleDeclarationUtil.SplitDeclarationAfter((IMultipleDeclarationMemberNode)lastNode); >>> >>> lastNode = >>> ((IMultipleDeclarationMemberNode)lastNode).MultipleDeclaration; >>> >>> } >>> >>> // Add new region >>> >>> IStartRegionNode startRegion; >>> >>> IEndRegionNode endRegion; >>> >>> CreateRegionDirectives(String.IsNullOrEmpty(regionName) ? "..." : >>> regionName, out startRegion, out endRegion); >>> >>> using (WriteLockCookie.Create()) >>> >>> { >>> >>> if (startRegion != null) startRegion = >>> ModificationUtil.AddChildBefore(firstNode, startRegion); >>> >>> if (endRegion != null) endRegion = >>> ModificationUtil.AddChildAfter(lastNode, endRegion); >>> >>> } >>> >>> } >>> >>> private void CreateRegionDirectives(string regionText, out >>> IStartRegionNode startRegion, out IEndRegionNode endRegion) >>> >>> { >>> >>> CSharpElementFactory factory = >>> CSharpElementFactory.GetInstance(myDeclaration.GetManager().Solution); >>> >>> ICSharpFile file = factory.CreateFile("#region " + regionText + >>> "\n#endregion"); >>> >>> ITreeNode node = file.FindTokenAt(0) as ITreeNode; >>> >>> startRegion = null; >>> >>> endRegion = null; >>> >>> while (node != null) >>> >>> { >>> >>> startRegion = node as IStartRegionNode; >>> >>> if (startRegion != null) >>> >>> break; >>> >>> node = node.Parent; >>> >>> } >>> >>> if (startRegion != null) >>> >>> endRegion = startRegion.EndRegion; >>> >>> } >>> >>> >>> -- >>> Eugene Pasynkov >>> Developer >>> JetBrains, Inc >>> http://www.jetbrains.com >>> "Develop with pleasure!" >>> "Ga" wrote in message >>> news:fa22qc$fc9$1@is.intellij.net... >>>> Can you show me how please? >>>> >>>> >>>> "Eugene Pasynkov (JetBrains)" wrote in >>>> message news:fa10up$es3$1@is.intellij.net... >>>>> The only way is to create #region/#endregion directives and insert >>>>> them into PSI-tree >>>>> >>>>> >>>>> -- >>>>> Eugene Pasynkov >>>>> Developer >>>>> JetBrains, Inc >>>>> http://www.jetbrains.com >>>>> "Develop with pleasure!" >>>>> "Ga" wrote in message >>>>> news:f9voac$7ul$1@is.intellij.net... >>>>>> Hey again, >>>>>> >>>>>>]]> I tend to ask many question as it seems but it's all from love :)
>>>>>>
>>>>>> i want to know if there is any way to surround a group of method /
>>>>>> properties / members / etc...
>>>>>> with regions?
>>>>>>
>>>>>> if so, i would be more than happy to know!
>>>>>>
>>>>>> thank you very much,
>>>>>> Guy.
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>



0

The problem is in these lines:

currentClass.AddClassMemberDeclarationAfter(propDeclaration, null);

currentClass.AddClassMemberDeclarationAfter(instanceDeclaration, null);



Adding members to class copies the given declarations, and returns the new
instances of AST sub-tree.

So you have to modify these lines:


propDeclaration =
currentClass.AddClassMemberDeclarationAfter(propDeclaration, null);

instanceDeclaration =
currentClass.AddClassMemberDeclarationAfter(instanceDeclaration, null);


--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Ga" <t4m.productions@gmail.com> wrote in message
news:famjf0$rb9$1@is.intellij.net...

Here is my source code... Just the relavent parts...
Thanks!!!

>

===== Cotext Action Class =====

>

/// <summary>
/// in case the plugin could be applied, and the user selected
/// to apply it, this method is invocated by resharper core.
/// </summary>
/// <param name="solution"></param>
/// <param name="textControl"></param>
public void Execute(ISolution solution, ITextControl textControl)
{
PsiManager.GetInstance(m_solution).UpdateCaches();
m_tokenAccessor = new TokenAccessor(solution, textControl);
//default sanity check made by the api dev team.
if (m_solution != solution || m_textControl != textControl)
{
throw new InvalidOperationException();
}
//check that we can modify the current file.
Shell.Instance.AssertReadAccessAllowed();

>

//creating a command cookie is ment for supporting undo later
//by visual studio. this is why inside the command cookie
//definition we have a transaction, in order to 'tell' visual studio
//to reverse all the changes that we perform now as a single action
using (CommandCookie.Create("GenerateClassSingleton"))
{
// Any changes to PSI should be under a transaction
PsiManager.GetInstance(m_solution).
DoTransaction(
delegate
{
//make the actual change to the class
ExecuteMakeClassSingleton(TokenAccessor.ClassDeclaration.DeclaredName);
});
}
}
private List<IDeclaration> declarations;
/// <summary>
/// receives a class name and turns it into a singleton by adding 3
declarations:
/// private constructor, a class member, and an Instance property.
/// </summary>
/// <param name="className"></param>
private void ExecuteMakeClassSingleton(string className)
{
//the formats
string ctor =
@"
/// <summary>
/// private constructor in order to prevent creating class instances
/// </summary>
private $0(){}
";
string instance =
@"
/// <summary>
/// the singleton instance created using the private constructor
/// </summary>
private static readonly $0 $1 = new $0();
";

>

string property =
@"
/// <summary>
/// returns the singleton instance
/// </summary>
public static $0 Instance { get { return $1; } }
";
//generate the three declarations
IConstructorDeclaration ctorDeclaration =
Factory.CreateTypeMemberDeclaration(ctor, className) as
IConstructorDeclaration;
IClassMemberDeclaration instanceDeclaration =
Factory.CreateTypeMemberDeclaration(instance, className,
SINGLETON_MEMBER_NAME) as IClassMemberDeclaration;
IPropertyDeclaration propDeclaration =
Factory.CreateTypeMemberDeclaration(property, className,
SINGLETON_MEMBER_NAME) as IPropertyDeclaration;
//adding the declarations to the current class
IClassDeclaration currentClass = TokenAccessor.ClassDeclaration;
currentClass.AddClassMemberDeclarationAfter(propDeclaration, null);
currentClass.AddClassMemberDeclarationAfter(instanceDeclaration, null);
if (IsClassHasEmptyCtor == false)
currentClass.AddClassMemberDeclarationAfter(ctorDeclaration, null);
//surrounding with region
List<IDeclaration> declarations = new List<IDeclaration>();
declarations.Add(ctorDeclaration);
declarations.Add(instanceDeclaration);
declarations.Add(propDeclaration);
RegionsUtil.EnRegion(m_solution, declarations, "Singleton");
}

>
>

===== RegionsUtil.EnRegion=====

>

public static void EnRegion(ISolution solution, IList<IDeclaration>
declarations, string regionName)
{
ITreeNode firstNode = declarations[0].ToTreeNode();
if (firstNode is IMultipleDeclarationMemberNode)
{
MultipleDeclarationUtil.SplitDeclarationBefore((IMultipleDeclarationMemberNode)
firstNode);
firstNode = ((IMultipleDeclarationMemberNode)
firstNode).MultipleDeclaration;
}
ITreeNode lastNode = declarations[declarations.Count - 1].ToTreeNode();
if (lastNode is IMultipleDeclarationMemberNode)
{
MultipleDeclarationUtil.SplitDeclarationAfter((IMultipleDeclarationMemberNode)
lastNode);
lastNode = ((IMultipleDeclarationMemberNode)
lastNode).MultipleDeclaration;
}
// Add new region
IStartRegionNode startRegion;
IEndRegionNode endRegion;
CreateRegionDirectives(solution, String.IsNullOrEmpty(regionName)
? "..."
:
regionName, out startRegion, out endRegion);
using (WriteLockCookie.Create())
{
if (startRegion != null)
startRegion = ModificationUtil.AddChildBefore(firstNode, startRegion);
if (endRegion != null)
endRegion = ModificationUtil.AddChildAfter(lastNode, endRegion);
}
}
private static void CreateRegionDirectives(ISolution solution, string
regionText, out IStartRegionNode startRegion, out IEndRegionNode
endRegion)
{
CSharpElementFactory factory = CSharpElementFactory.GetInstance(solution);
ICSharpFile file = factory.CreateFile("#region " + regionText +
"\n#endregion");
ITreeNode node = file.FindTokenAt(0) as ITreeNode;
startRegion = null;
endRegion = null;
while (node != null)
{
startRegion = node as IStartRegionNode;
if (startRegion != null)
break;
node = node.Parent;
}
if (startRegion != null)
endRegion = startRegion.EndRegion;
}

>
>
>
>
>
>

"Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> wrote in
message news:fabt1l$5do$1@is.intellij.net...

>> Please could you minimize your code and send it to me?
>> it will be much easier for me to check what's going wrong looking into
>> the code, then trying to guess it from your words :)
>>
>> --
>> Eugene Pasynkov
>> Developer
>> JetBrains, Inc
>> http://www.jetbrains.com
>> "Develop with pleasure!"
>> "Ga" <t4m.productions@gmail.com> wrote in message
>> news:fa4iea$umk$1@is.intellij.net...
>>> everythings goes out fine except that the modification util does not
>>> actually change the file and adds the region...
>>> when debugging i see all that the debugging data is good (in terms of
>>> where to create the region).
>>> what might be the problem ???
>>> other than that... thank you very much for your patience to write that
>>> code :)
>>> Guy.
>>>
>>> //ModificationUtil doesn't change anything :(
>>> startRegion = ModificationUtil.AddChildBefore(firstNode, startRegion);
>>>
>>>
>>> "Eugene Pasynkov (JetBrains)" wrote in >>> message news:fa46mh$4r0$1@is.intellij.net... >>>> public void EnRegion(IList declarations, string >>>> regionName) >>>> >>>> { >>>> >>>> ITreeNode firstNode = declarations[0].ToTreeNode(); >>>> >>>> if (firstNode is IMultipleDeclarationMemberNode) >>>> >>>> { >>>> >>>> MultipleDeclarationUtil.SplitDeclarationBefore((IMultipleDeclarationMemberNode)firstNode); >>>> >>>> firstNode = >>>> ((IMultipleDeclarationMemberNode)firstNode).MultipleDeclaration; >>>> >>>> } >>>> >>>> ITreeNode lastNode = declarations[declarations.Count - 1].ToTreeNode(); >>>> >>>> if (lastNode is IMultipleDeclarationMemberNode) >>>> >>>> { >>>> >>>> MultipleDeclarationUtil.SplitDeclarationAfter((IMultipleDeclarationMemberNode)lastNode); >>>> >>>> lastNode = >>>> ((IMultipleDeclarationMemberNode)lastNode).MultipleDeclaration; >>>> >>>> } >>>> >>>> // Add new region >>>> >>>> IStartRegionNode startRegion; >>>> >>>> IEndRegionNode endRegion; >>>> >>>> CreateRegionDirectives(String.IsNullOrEmpty(regionName) ? "..." : >>>> regionName, out startRegion, out endRegion); >>>> >>>> using (WriteLockCookie.Create()) >>>> >>>> { >>>> >>>> if (startRegion != null) startRegion = >>>> ModificationUtil.AddChildBefore(firstNode, startRegion); >>>> >>>> if (endRegion != null) endRegion = >>>> ModificationUtil.AddChildAfter(lastNode, endRegion); >>>> >>>> } >>>> >>>> } >>>> >>>> private void CreateRegionDirectives(string regionText, out >>>> IStartRegionNode startRegion, out IEndRegionNode endRegion) >>>> >>>> { >>>> >>>> CSharpElementFactory factory = >>>> CSharpElementFactory.GetInstance(myDeclaration.GetManager().Solution); >>>> >>>> ICSharpFile file = factory.CreateFile("#region " + regionText + >>>> "\n#endregion"); >>>> >>>> ITreeNode node = file.FindTokenAt(0) as ITreeNode; >>>> >>>> startRegion = null; >>>> >>>> endRegion = null; >>>> >>>> while (node != null) >>>> >>>> { >>>> >>>> startRegion = node as IStartRegionNode; >>>> >>>> if (startRegion != null) >>>> >>>> break; >>>> >>>> node = node.Parent; >>>> >>>> } >>>> >>>> if (startRegion != null) >>>> >>>> endRegion = startRegion.EndRegion; >>>> >>>> } >>>> >>>> >>>> -- >>>> Eugene Pasynkov >>>> Developer >>>> JetBrains, Inc >>>> http://www.jetbrains.com >>>> "Develop with pleasure!" >>>> "Ga" wrote in message >>>> news:fa22qc$fc9$1@is.intellij.net... >>>>> Can you show me how please? >>>>> >>>>> >>>>> "Eugene Pasynkov (JetBrains)" wrote in >>>>> message news:fa10up$es3$1@is.intellij.net... >>>>>> The only way is to create #region/#endregion directives and insert >>>>>> them into PSI-tree >>>>>> >>>>>> >>>>>> -- >>>>>> Eugene Pasynkov >>>>>> Developer >>>>>> JetBrains, Inc >>>>>> http://www.jetbrains.com >>>>>> "Develop with pleasure!" >>>>>> "Ga" wrote in message >>>>>> news:f9voac$7ul$1@is.intellij.net... >>>>>>> Hey again, >>>>>>> >>>>>>>]]> I tend to ask many question as it seems but it's all from love :)
>>>>>>>
>>>>>>> i want to know if there is any way to surround a group of method /
>>>>>>> properties / members / etc...
>>>>>>> with regions?
>>>>>>>
>>>>>>> if so, i would be more than happy to know!
>>>>>>>
>>>>>>> thank you very much,
>>>>>>> Guy.
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>



0

Hey just wanted to say,

you are amazing,
this forum is amazing,

soon i will publish the plugins i created thanks to you,
thanks,
guy.


"Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> wrote in
message news:fau2h9$c6p$1@is.intellij.net...

The problem is in these lines:

>

currentClass.AddClassMemberDeclarationAfter(propDeclaration, null);

>

currentClass.AddClassMemberDeclarationAfter(instanceDeclaration, null);

>
>
>

Adding members to class copies the given declarations, and returns the new
instances of AST sub-tree.

>

So you have to modify these lines:

>
>

propDeclaration =
currentClass.AddClassMemberDeclarationAfter(propDeclaration, null);

>

instanceDeclaration =
currentClass.AddClassMemberDeclarationAfter(instanceDeclaration, null);

>
>

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Ga" <t4m.productions@gmail.com> wrote in message
news:famjf0$rb9$1@is.intellij.net...

>> Here is my source code... Just the relavent parts...
>> Thanks!!!
>>
>> ===== Cotext Action Class =====
>>
>> /// >> /// in case the plugin could be applied, and the user selected >> /// to apply it, this method is invocated by resharper core. >> /// >> /// >> /// >> public void Execute(ISolution solution, ITextControl textControl) >> { >> PsiManager.GetInstance(m_solution).UpdateCaches(); >> m_tokenAccessor = new TokenAccessor(solution, textControl); >> //default sanity check made by the api dev team. >> if (m_solution != solution || m_textControl != textControl) >> { >> throw new InvalidOperationException(); >> } >> //check that we can modify the current file. >> Shell.Instance.AssertReadAccessAllowed(); >> >> //creating a command cookie is ment for supporting undo later >> //by visual studio. this is why inside the command cookie >> //definition we have a transaction, in order to 'tell' visual studio >> //to reverse all the changes that we perform now as a single action >> using (CommandCookie.Create("GenerateClassSingleton")) >> { >> // Any changes to PSI should be under a transaction >> PsiManager.GetInstance(m_solution). >> DoTransaction( >> delegate >> { >> //make the actual change to the class >> ExecuteMakeClassSingleton(TokenAccessor.ClassDeclaration.DeclaredName); >> }); >> } >> } >> private List declarations; >> ///

>> /// receives a class name and turns it into a singleton by adding 3 >> declarations: >> /// private constructor, a class member, and an Instance property. >> /// >> /// >> private void ExecuteMakeClassSingleton(string className) >> { >> //the formats >> string ctor = >> @" >> /// >> /// private constructor in order to prevent creating class instances >> /// >> private $0(){} >> "; >> string instance = >> @" >> /// >> /// the singleton instance created using the private constructor >> /// >> private static readonly $0 $1 = new $0(); >> "; >> >> string property = >> @" >> /// >> /// returns the singleton instance >> /// >> public static $0 Instance { get { return $1; } } >> "; >> //generate the three declarations >> IConstructorDeclaration ctorDeclaration = >> Factory.CreateTypeMemberDeclaration(ctor, className) as >> IConstructorDeclaration; >> IClassMemberDeclaration instanceDeclaration = >> Factory.CreateTypeMemberDeclaration(instance, className, >> SINGLETON_MEMBER_NAME) as IClassMemberDeclaration; >> IPropertyDeclaration propDeclaration = >> Factory.CreateTypeMemberDeclaration(property, className, >> SINGLETON_MEMBER_NAME) as IPropertyDeclaration; >> //adding the declarations to the current class >> IClassDeclaration currentClass = TokenAccessor.ClassDeclaration; >> currentClass.AddClassMemberDeclarationAfter(propDeclaration, null); >> currentClass.AddClassMemberDeclarationAfter(instanceDeclaration, null); >> if (IsClassHasEmptyCtor == false) >> currentClass.AddClassMemberDeclarationAfter(ctorDeclaration, null); >> //surrounding with region >> List declarations = new List(); >> declarations.Add(ctorDeclaration); >> declarations.Add(instanceDeclaration); >> declarations.Add(propDeclaration); >> RegionsUtil.EnRegion(m_solution, declarations, "Singleton"); >> } >> >> >> ===== RegionsUtil.EnRegion===== >> >> public static void EnRegion(ISolution solution, IList >> declarations, string regionName) >> { >> ITreeNode firstNode = declarations[0].ToTreeNode(); >> if (firstNode is IMultipleDeclarationMemberNode) >> { >> MultipleDeclarationUtil.SplitDeclarationBefore((IMultipleDeclarationMemberNode) >> firstNode); >> firstNode = ((IMultipleDeclarationMemberNode) >> firstNode).MultipleDeclaration; >> } >> ITreeNode lastNode = declarations[declarations.Count - 1].ToTreeNode(); >> if (lastNode is IMultipleDeclarationMemberNode) >> { >> MultipleDeclarationUtil.SplitDeclarationAfter((IMultipleDeclarationMemberNode) >> lastNode); >> lastNode = ((IMultipleDeclarationMemberNode) >> lastNode).MultipleDeclaration; >> } >> // Add new region >> IStartRegionNode startRegion; >> IEndRegionNode endRegion; >> CreateRegionDirectives(solution, String.IsNullOrEmpty(regionName) >> ? "..." >> : >> regionName, out startRegion, out endRegion); >> using (WriteLockCookie.Create()) >> { >> if (startRegion != null) >> startRegion = ModificationUtil.AddChildBefore(firstNode, startRegion); >> if (endRegion != null) >> endRegion = ModificationUtil.AddChildAfter(lastNode, endRegion); >> } >> } >> private static void CreateRegionDirectives(ISolution solution, string >> regionText, out IStartRegionNode startRegion, out IEndRegionNode >> endRegion) >> { >> CSharpElementFactory factory = >> CSharpElementFactory.GetInstance(solution); >> ICSharpFile file = factory.CreateFile("#region " + regionText + >> "\n#endregion"); >> ITreeNode node = file.FindTokenAt(0) as ITreeNode; >> startRegion = null; >> endRegion = null; >> while (node != null) >> { >> startRegion = node as IStartRegionNode; >> if (startRegion != null) >> break; >> node = node.Parent; >> } >> if (startRegion != null) >> endRegion = startRegion.EndRegion; >> } >> >> >> >> >> >> >> "Eugene Pasynkov (JetBrains)" wrote in >> message news:fabt1l$5do$1@is.intellij.net... >>> Please could you minimize your code and send it to me? >>> it will be much easier for me to check what's going wrong looking into >>>]]> the code, then trying to guess it from your words :)
>>>
>>> --
>>> Eugene Pasynkov
>>> Developer
>>> JetBrains, Inc
>>> http://www.jetbrains.com
>>> "Develop with pleasure!"
>>> "Ga" <t4m.productions@gmail.com> wrote in message
>>> news:fa4iea$umk$1@is.intellij.net...
>>>> everythings goes out fine except that the modification util does not
>>>> actually change the file and adds the region...
>>>> when debugging i see all that the debugging data is good (in terms of
>>>> where to create the region).
>>>> what might be the problem ???
>>>> other than that... thank you very much for your patience to write that
>>>> code :)
>>>> Guy.
>>>>
>>>> //ModificationUtil doesn't change anything :(
>>>> startRegion = ModificationUtil.AddChildBefore(firstNode, startRegion);
>>>>
>>>>
>>>> "Eugene Pasynkov (JetBrains)" wrote in >>>> message news:fa46mh$4r0$1@is.intellij.net... >>>>> public void EnRegion(IList declarations, string >>>>> regionName) >>>>> >>>>> { >>>>> >>>>> ITreeNode firstNode = declarations[0].ToTreeNode(); >>>>> >>>>> if (firstNode is IMultipleDeclarationMemberNode) >>>>> >>>>> { >>>>> >>>>> MultipleDeclarationUtil.SplitDeclarationBefore((IMultipleDeclarationMemberNode)firstNode); >>>>> >>>>> firstNode = >>>>> ((IMultipleDeclarationMemberNode)firstNode).MultipleDeclaration; >>>>> >>>>> } >>>>> >>>>> ITreeNode lastNode = declarations[declarations.Count - >>>>> 1].ToTreeNode(); >>>>> >>>>> if (lastNode is IMultipleDeclarationMemberNode) >>>>> >>>>> { >>>>> >>>>> MultipleDeclarationUtil.SplitDeclarationAfter((IMultipleDeclarationMemberNode)lastNode); >>>>> >>>>> lastNode = >>>>> ((IMultipleDeclarationMemberNode)lastNode).MultipleDeclaration; >>>>> >>>>> } >>>>> >>>>> // Add new region >>>>> >>>>> IStartRegionNode startRegion; >>>>> >>>>> IEndRegionNode endRegion; >>>>> >>>>> CreateRegionDirectives(String.IsNullOrEmpty(regionName) ? "..." : >>>>> regionName, out startRegion, out endRegion); >>>>> >>>>> using (WriteLockCookie.Create()) >>>>> >>>>> { >>>>> >>>>> if (startRegion != null) startRegion = >>>>> ModificationUtil.AddChildBefore(firstNode, startRegion); >>>>> >>>>> if (endRegion != null) endRegion = >>>>> ModificationUtil.AddChildAfter(lastNode, endRegion); >>>>> >>>>> } >>>>> >>>>> } >>>>> >>>>> private void CreateRegionDirectives(string regionText, out >>>>> IStartRegionNode startRegion, out IEndRegionNode endRegion) >>>>> >>>>> { >>>>> >>>>> CSharpElementFactory factory = >>>>> CSharpElementFactory.GetInstance(myDeclaration.GetManager().Solution); >>>>> >>>>> ICSharpFile file = factory.CreateFile("#region " + regionText + >>>>> "\n#endregion"); >>>>> >>>>> ITreeNode node = file.FindTokenAt(0) as ITreeNode; >>>>> >>>>> startRegion = null; >>>>> >>>>> endRegion = null; >>>>> >>>>> while (node != null) >>>>> >>>>> { >>>>> >>>>> startRegion = node as IStartRegionNode; >>>>> >>>>> if (startRegion != null) >>>>> >>>>> break; >>>>> >>>>> node = node.Parent; >>>>> >>>>> } >>>>> >>>>> if (startRegion != null) >>>>> >>>>> endRegion = startRegion.EndRegion; >>>>> >>>>> } >>>>> >>>>> >>>>> -- >>>>> Eugene Pasynkov >>>>> Developer >>>>> JetBrains, Inc >>>>> http://www.jetbrains.com >>>>> "Develop with pleasure!" >>>>> "Ga" wrote in message >>>>> news:fa22qc$fc9$1@is.intellij.net... >>>>>> Can you show me how please? >>>>>> >>>>>> >>>>>> "Eugene Pasynkov (JetBrains)" wrote >>>>>> in message news:fa10up$es3$1@is.intellij.net... >>>>>>> The only way is to create #region/#endregion directives and insert >>>>>>> them into PSI-tree >>>>>>> >>>>>>> >>>>>>> -- >>>>>>> Eugene Pasynkov >>>>>>> Developer >>>>>>> JetBrains, Inc >>>>>>> http://www.jetbrains.com >>>>>>> "Develop with pleasure!" >>>>>>> "Ga" wrote in message >>>>>>> news:f9voac$7ul$1@is.intellij.net... >>>>>>>> Hey again, >>>>>>>> >>>>>>>>]]> I tend to ask many question as it seems but it's all from love :)
>>>>>>>>
>>>>>>>> i want to know if there is any way to surround a group of method /
>>>>>>>> properties / members / etc...
>>>>>>>> with regions?
>>>>>>>>
>>>>>>>> if so, i would be more than happy to know!
>>>>>>>>
>>>>>>>> thank you very much,
>>>>>>>> Guy.
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>



0

Please sign in to leave a comment.