Permalink
Browse files

Merge pull request #36 from awatertree/master

Formatter issues with if/else statements and // comments
  • Loading branch information...
2 parents 0179c76 + 658cb4b commit cdd492e4d94947e6d4f1cb9d7afa2e769016f59f @mkrueger mkrueger committed Apr 24, 2012
View
@@ -1,2 +1,3 @@
/lib/*.dll
-/ICSharpCode.NRefactory.Tests/PartCover/*
+/ICSharpCode.NRefactory.Tests/PartCover/*
+_ReSharper*/*
@@ -2710,73 +2710,85 @@ public override object Visit (NewAnonymousType newAnonymousType)
}
return result;
}
-
- ArrayInitializerExpression ConvertCollectionOrObjectInitializers (CollectionOrObjectInitializers minit)
- {
- if (minit == null)
- return null;
- var init = new ArrayInitializerExpression ();
- AddConvertCollectionOrObjectInitializers (init, minit);
- return init;
- }
-
- void AddConvertCollectionOrObjectInitializers (Expression init, CollectionOrObjectInitializers minit)
- {
- var initLoc = LocationsBag.GetLocations (minit);
- var commaLoc = LocationsBag.GetLocations (minit.Initializers);
- int curComma = 0;
- if (initLoc != null)
- init.AddChild (new CSharpTokenNode (Convert (initLoc [0])), Roles.LBrace);
- foreach (var expr in minit.Initializers) {
- var collectionInit = expr as CollectionElementInitializer;
- if (collectionInit != null) {
- var parent = new ArrayInitializerExpression ();
-
- parent.AddChild (new CSharpTokenNode (Convert (expr.Location)), Roles.LBrace);
- for (int i = 0; i < collectionInit.Arguments.Count; i++) {
- var arg = collectionInit.Arguments [i] as CollectionElementInitializer.ElementInitializerArgument;
- if (arg == null)
- continue;
- parent.AddChild ((ICSharpCode.NRefactory.CSharp.Expression)arg.Expr.Accept (this), Roles.Expression);
- }
-
- var braceLocs = LocationsBag.GetLocations (expr);
- if (braceLocs != null)
- parent.AddChild (new CSharpTokenNode (Convert (braceLocs [0])), Roles.RBrace);
-
- init.AddChild (parent, Roles.Expression);
- } else {
- var eleInit = expr as ElementInitializer;
- if (eleInit != null) {
- var nexpr = new NamedExpression ();
- nexpr.AddChild (Identifier.Create (eleInit.Name, Convert (eleInit.Location)), Roles.Identifier);
- var assignLoc = LocationsBag.GetLocations (eleInit);
- if (assignLoc != null)
- nexpr.AddChild (new CSharpTokenNode (Convert (assignLoc [0])), Roles.Assign);
- if (eleInit.Source != null) {
- if (eleInit.Source is CollectionOrObjectInitializers) {
- var arrInit = new ArrayInitializerExpression ();
- AddConvertCollectionOrObjectInitializers (arrInit, eleInit.Source as CollectionOrObjectInitializers);
- nexpr.AddChild (arrInit, Roles.Expression);
- } else {
- nexpr.AddChild ((Expression)eleInit.Source.Accept (this), Roles.Expression);
- }
- }
-
- init.AddChild (nexpr, Roles.Expression);
- }
- }
- if (commaLoc != null && curComma < commaLoc.Count)
- init.AddChild (new CSharpTokenNode (Convert (commaLoc [curComma++])), Roles.Comma);
-
- }
- if (initLoc != null) {
- if (initLoc.Count == 3) // optional comma
- init.AddChild (new CSharpTokenNode (Convert (initLoc [1])), Roles.Comma);
- init.AddChild (new CSharpTokenNode (Convert (initLoc [initLoc.Count - 1])), Roles.RBrace);
- }
- }
+ ArrayInitializerExpression ConvertCollectionOrObjectInitializers(CollectionOrObjectInitializers minit)
+ {
+ if (minit == null)
+ return null;
+ var init = new ArrayInitializerExpression();
+ AddConvertCollectionOrObjectInitializers(init, minit);
+ return init;
+ }
+
+ void AddConvertCollectionOrObjectInitializers(Expression init, CollectionOrObjectInitializers minit)
+ {
+ var initLoc = LocationsBag.GetLocations(minit);
+ var commaLoc = LocationsBag.GetLocations(minit.Initializers);
+ int curComma = 0;
+ if (initLoc != null)
+ init.AddChild(new CSharpTokenNode(Convert(initLoc[0])), Roles.LBrace);
+ foreach (var expr in minit.Initializers)
+ {
+ var collectionInit = expr as CollectionElementInitializer;
+ if (collectionInit != null)
+ {
+ var parent = new ArrayInitializerExpression();
+
+ var braceLocs = LocationsBag.GetLocations(expr);
+ if (braceLocs != null)
+ parent.AddChild(new CSharpTokenNode(Convert(braceLocs[0])), Roles.LBrace);
+
+ for (int i = 0; i < collectionInit.Arguments.Count; i++)
+ {
+ var arg = collectionInit.Arguments[i] as CollectionElementInitializer.ElementInitializerArgument;
+ if (arg == null)
+ continue;
+ parent.AddChild((ICSharpCode.NRefactory.CSharp.Expression)arg.Expr.Accept(this), Roles.Expression);
+ }
+
+ if (braceLocs != null && braceLocs.Count > 1)
+ parent.AddChild(new CSharpTokenNode(Convert(braceLocs[1])), Roles.RBrace);
+
+ init.AddChild(parent, Roles.Expression);
+ }
+ else
+ {
+ var eleInit = expr as ElementInitializer;
+ if (eleInit != null)
+ {
+ var nexpr = new NamedExpression();
+ nexpr.AddChild(Identifier.Create(eleInit.Name, Convert(eleInit.Location)), Roles.Identifier);
+ var assignLoc = LocationsBag.GetLocations(eleInit);
+ if (assignLoc != null)
+ nexpr.AddChild(new CSharpTokenNode(Convert(assignLoc[0])), Roles.Assign);
+ if (eleInit.Source != null)
+ {
+ if (eleInit.Source is CollectionOrObjectInitializers)
+ {
+ var arrInit = new ArrayInitializerExpression();
+ AddConvertCollectionOrObjectInitializers(arrInit, eleInit.Source as CollectionOrObjectInitializers);
+ nexpr.AddChild(arrInit, Roles.Expression);
+ }
+ else
+ {
+ nexpr.AddChild((Expression)eleInit.Source.Accept(this), Roles.Expression);
+ }
+ }
+
+ init.AddChild(nexpr, Roles.Expression);
+ }
+ }
+ if (commaLoc != null && curComma < commaLoc.Count)
+ init.AddChild(new CSharpTokenNode(Convert(commaLoc[curComma++])), Roles.Comma);
+
+ }
+ if (initLoc != null)
+ {
+ if (initLoc.Count == 3) // optional comma
+ init.AddChild(new CSharpTokenNode(Convert(initLoc[1])), Roles.Comma);
+ init.AddChild(new CSharpTokenNode(Convert(initLoc[initLoc.Count - 1])), Roles.RBrace);
+ }
+ }
public override object Visit (NewInitialize newInitializeExpression)
{
@@ -58,7 +58,9 @@
<Compile Include="..\ICSharpCode.NRefactory\Properties\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
- <Compile Include="CSDemo.cs" />
+ <Compile Include="CSDemo.cs">
+ <SubType>UserControl</SubType>
+ </Compile>
<Compile Include="CSDemo.Designer.cs">
<DependentUpon>CSDemo.cs</DependentUpon>
</Compile>
@@ -70,7 +72,9 @@
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
- <Compile Include="VBDemo.cs" />
+ <Compile Include="VBDemo.cs">
+ <SubType>UserControl</SubType>
+ </Compile>
<Compile Include="VBDemo.Designer.cs">
<DependentUpon>VBDemo.cs</DependentUpon>
</Compile>
@@ -227,7 +227,84 @@ public object GetValue ()
}
}");
}
-
+
+ /// <summary>
+ /// Bug GH35 - Formatter issues with if/else statements and // comments
+ /// </summary>
+ [Ignore]
+ public void TestBugGH35()
+ {
+ var policy = new CSharpFormattingOptions();
+ policy.ConstructorBraceStyle = BraceStyle.EndOfLine;
+
+ Test(policy, @"public class A : B
+{
+ public void Test()
+ {
+ // Comment before
+ if (conditionA) {
+ DoSomething();
+ }
+ // Comment before else ends up incorporating it
+ else if (conditionB) {
+ DoSomethingElse();
+ }
+ }
+}",
+@"public class A : B
+{
+ public void Test()
+ {
+ // Comment before
+ if (conditionA) {
+ DoSomething();
+ }
+ // Comment before else ends up incorporating it
+ else if (conditionB) {
+ DoSomethingElse();
+ }
+ }
+}");
+ }
+
+ /// <summary>
+ /// Bug GH35a - Formatter issues with if/else statements and // comments else variant
+ /// </summary>
+ [Ignore]
+ public void TestBugGH35a()
+ {
+ var policy = new CSharpFormattingOptions();
+ policy.ConstructorBraceStyle = BraceStyle.EndOfLine;
+
+ Test(policy, @"public class A : B
+{
+ public void Test()
+ {
+ // Comment before
+ if (conditionA) {
+ DoSomething();
+ }
+ // Comment before else ends up incorporating it
+ else (conditionB) {
+ DoSomethingElse();
+ }
+ }
+}",
+@"public class A : B
+{
+ public void Test()
+ {
+ // Comment before
+ if (conditionA) {
+ DoSomething();
+ }
+ // Comment before else ends up incorporating it
+ else (conditionB) {
+ DoSomethingElse();
+ }
+ }
+}");
+ }
}
}
View
@@ -0,0 +1,11 @@
+<Configuration>
+ <SnapshotDialog>
+ <InitialDirectory>C:\work\NRefactory</InitialDirectory>
+ </SnapshotDialog>
+ <CoverageFilters>
+ <IncludeFilters>
+ <Filter ModuleMask="*" ClassMask="*" FunctionMask="*" />
+ </IncludeFilters>
+ <ExcludeFilters />
+ </CoverageFilters>
+</Configuration>
Oops, something went wrong.

0 comments on commit cdd492e

Please sign in to comment.