Browse Source

[Refactoring] Script now corrects the formatting of inserted &

replaced nodes.
newNRvisualizers
Mike Krüger 13 years ago
parent
commit
fc72147b88
  1. 12
      ICSharpCode.NRefactory.CSharp/Formatter/AstFormattingVisitor.cs
  2. 1
      ICSharpCode.NRefactory.CSharp/Formatter/FormattingOptionsFactory.cs
  3. 15
      ICSharpCode.NRefactory.CSharp/Refactoring/Script.cs
  4. 6
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/AddCatchTypeTests.cs
  5. 4
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertAnonymousDelegateToLambdaTests.cs
  6. 6
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertLamdaToAnonymousDelegateTests.cs
  7. 21
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertSwitchToIfTests.cs
  8. 2
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/DeclareLocalVariableTests.cs
  9. 4
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ExtensionMethodInvocationToStaticMethodInvocationTests.cs
  10. 50
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/MoveToOuterScopeTests.cs
  11. 6
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/RemoveRedundantCatchTypeTests.cs
  12. 3
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/SplitDeclarationAndAssignmentTests.cs
  13. 6
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/AccessToModifiedClosureTests.cs
  14. 9
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/ConstantConditionIssueTests.cs
  15. 12
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/ExpressionIsAlwaysOfProvidedTypeIssueTests.cs
  16. 3
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/InspectionActionTestBase.cs
  17. 12
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/RedundantToStringTests.cs
  18. 10
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/ReferenceToStaticMemberViaDerivedTypeTests.cs
  19. 3
      ICSharpCode.NRefactory.Tests/FormattingTests/TextEditorTestAdapter.cs

12
ICSharpCode.NRefactory.CSharp/Formatter/AstFormattingVisitor.cs

@ -739,15 +739,15 @@ namespace ICSharpCode.NRefactory.CSharp @@ -739,15 +739,15 @@ namespace ICSharpCode.NRefactory.CSharp
}
var lastLoc = fieldDeclaration.StartLocation;
curIndent.Push(IndentType.Block);
foreach (var initializer in fieldDeclaration.Variables) {
if (lastLoc.Line != initializer.StartLocation.Line) {
curIndent.Push(IndentType.Block);
FixStatementIndentation(initializer.StartLocation);
curIndent.Pop ();
lastLoc = initializer.StartLocation;
}
initializer.AcceptVisitor(this);
}
curIndent.Pop ();
}
public override void VisitFixedFieldDeclaration(FixedFieldDeclaration fixedFieldDeclaration)
@ -1179,8 +1179,10 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1179,8 +1179,10 @@ namespace ICSharpCode.NRefactory.CSharp
nextStatementIndent = " ";
}
}
bool pushed = false;
if (policy.IndentBlocks && !(policy.AlignEmbeddedIfStatements && node is IfElseStatement && node.Parent is IfElseStatement || policy.AlignEmbeddedUsingStatements && node is UsingStatement && node.Parent is UsingStatement)) {
curIndent.Push(IndentType.Block);
pushed = true;
}
if (isBlock) {
VisitBlockWithoutFixingBraces((BlockStatement)node, false);
@ -1190,7 +1192,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1190,7 +1192,7 @@ namespace ICSharpCode.NRefactory.CSharp
}
node.AcceptVisitor(this);
}
if (policy.IndentBlocks && !(policy.AlignEmbeddedIfStatements && node is IfElseStatement && node.Parent is IfElseStatement || policy.AlignEmbeddedUsingStatements && node is UsingStatement && node.Parent is UsingStatement)) {
if (pushed) {
curIndent.Pop();
}
switch (braceForcement) {
@ -2105,6 +2107,10 @@ namespace ICSharpCode.NRefactory.CSharp @@ -2105,6 +2107,10 @@ namespace ICSharpCode.NRefactory.CSharp
void FixStatementIndentation(TextLocation location)
{
if (location.Line < 1 || location.Column < 1) {
Console.WriteLine("invalid location!");
return;
}
int offset = document.GetOffset(location);
if (offset <= 0) {
Console.WriteLine("possible wrong offset");

1
ICSharpCode.NRefactory.CSharp/Formatter/FormattingOptionsFactory.cs

@ -82,6 +82,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -82,6 +82,7 @@ namespace ICSharpCode.NRefactory.CSharp
StatementBraceStyle = BraceStyle.EndOfLine,
ElseNewLinePlacement = NewLinePlacement.SameLine,
ElseIfNewLinePlacement = NewLinePlacement.SameLine,
CatchNewLinePlacement = NewLinePlacement.SameLine,
FinallyNewLinePlacement = NewLinePlacement.SameLine,
WhileNewLinePlacement = NewLinePlacement.SameLine,

15
ICSharpCode.NRefactory.CSharp/Refactoring/Script.cs

@ -152,6 +152,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -152,6 +152,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
text += Options.EolMarker;
InsertText(startOffset, text);
output.RegisterTrackedSegments(this, startOffset);
CorrectFormatting (node, insertNode);
}
public void InsertAfter(AstNode node, AstNode insertNode)
@ -164,6 +165,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -164,6 +165,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var insertOffset = GetCurrentOffset(node.EndLocation);
InsertText(insertOffset, text);
output.RegisterTrackedSegments(this, insertOffset);
CorrectFormatting (node, insertNode);
}
public void AddTo(BlockStatement bodyStatement, AstNode insertNode)
@ -172,6 +174,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -172,6 +174,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var output = OutputNode(1 + GetIndentLevelAt(startOffset), insertNode, true);
InsertText(startOffset, output.Text);
output.RegisterTrackedSegments(this, startOffset);
CorrectFormatting (null, insertNode);
}
public virtual Task Link (params AstNode[] nodes)
@ -196,6 +199,18 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -196,6 +199,18 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
output.TrimStart ();
Replace (startOffset, segment.Length, output.Text);
output.RegisterTrackedSegments(this, startOffset);
CorrectFormatting (node, node);
}
void CorrectFormatting(AstNode node, AstNode newNode)
{
if (node is Identifier || node is IdentifierExpression || node is CSharpTokenNode || node is AstType)
return;
if (node == null || node.Parent is BlockStatement) {
FormatText(newNode);
} else {
FormatText((node.Parent != null && (node.Parent is Statement || node.Parent is Expression || node.Parent is VariableInitializer)) ? node.Parent : newNode);
}
}
public abstract void Remove (AstNode node, bool removeEmptyLine = true);

6
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/AddCatchTypeTests.cs

@ -50,8 +50,7 @@ class TestClass @@ -50,8 +50,7 @@ class TestClass
public void F()
{
try {
}
catch (System.Exception e) {
} catch (System.Exception e) {
}
}
}");
@ -107,8 +106,7 @@ class TestClass @@ -107,8 +106,7 @@ class TestClass
public void F()
{
try {
}
catch (Exception e) {
} catch (Exception e) {
}
}
}");

4
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertAnonymousDelegateToLambdaTests.cs

@ -49,7 +49,7 @@ class A @@ -49,7 +49,7 @@ class A
{
System.Action<int, int> action = (i1, i2) => {
System.Console.WriteLine (i1);
};
};
}
}");
}
@ -71,7 +71,7 @@ class A @@ -71,7 +71,7 @@ class A
{
var action = (int i1, int i2) => {
System.Console.WriteLine (i1);
};
};
}
}");
}

6
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertLamdaToAnonymousDelegateTests.cs

@ -48,7 +48,7 @@ class A @@ -48,7 +48,7 @@ class A
{
System.Action<int, int> = delegate (int i1, int i2) {
System.Console.WriteLine (i1);
};
};
}
}");
}
@ -70,7 +70,7 @@ class A @@ -70,7 +70,7 @@ class A
{
System.Action<int, int> = delegate (int i1, int i2) {
System.Console.WriteLine (i1);
};
};
}
}");
}
@ -92,7 +92,7 @@ class A @@ -92,7 +92,7 @@ class A
{
System.Action = delegate {
System.Console.WriteLine ();
};
};
}
}");
}

21
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertSwitchToIfTests.cs

@ -61,11 +61,9 @@ class TestClass @@ -61,11 +61,9 @@ class TestClass
{
if (a == 0) {
return 0;
} else
if (a == 1 || a == 2) {
} else if (a == 1 || a == 2) {
return 1;
} else
if (a == 3 || a == 4 || a == 5) {
} else if (a == 3 || a == 4 || a == 5) {
return 1;
} else {
return 2;
@ -101,11 +99,9 @@ class TestClass @@ -101,11 +99,9 @@ class TestClass
{
if (a == 0) {
return 0;
} else
if (a == 1 || a == 2) {
} else if (a == 1 || a == 2) {
return 1;
} else
if (a == 3 || a == 4 || a == 5) {
} else if (a == 3 || a == 4 || a == 5) {
return 1;
}
}
@ -142,10 +138,8 @@ class TestClass @@ -142,10 +138,8 @@ class TestClass
{
if (a == 0) {
int b = 1;
} else
if (a == 1 || a == 2) {
} else
if (a == 3 || a == 4 || a == 5) {
} else if (a == 1 || a == 2) {
} else if (a == 3 || a == 4 || a == 5) {
} else {
}
}
@ -176,8 +170,7 @@ class TestClass @@ -176,8 +170,7 @@ class TestClass
{
if (a == 0) {
return 0;
} else
if (a == (1 == 1 ? 1 : 2)) {
} else if (a == (1 == 1 ? 1 : 2)) {
return 1;
} else {
return 2;

2
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/DeclareLocalVariableTests.cs

@ -206,7 +206,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -206,7 +206,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
void DoStuff()
{
System.Func<int> getInt = GetInt;
if (getInt() == 0) {
if (getInt () == 0) {
}
}

4
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ExtensionMethodInvocationToStaticMethodInvocationTests.cs

@ -82,7 +82,7 @@ class C @@ -82,7 +82,7 @@ class C
void F()
{
A a = new A();
if(a.$Ext (1))
if (a.$Ext (1))
return;
}
}", @"
@ -99,7 +99,7 @@ class C @@ -99,7 +99,7 @@ class C
void F()
{
A a = new A();
if(B.Ext (a, 1))
if (B.Ext (a, 1))
return;
}
}");

50
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/MoveToOuterScopeTests.cs

@ -54,13 +54,13 @@ class A @@ -54,13 +54,13 @@ class A
public void SimpleCase()
{
TestStatements(@"
while (true) {
while (true) {
int $i = 2;
}
}
", @"
int i = 2;
while (true) {
}
int i = 2;
while (true) {
}
");
}
@ -81,14 +81,14 @@ class A @@ -81,14 +81,14 @@ class A
public void MovesOnlyTheCurrentVariableInitialization()
{
TestStatements(@"
while (true) {
while (true) {
int $i = 2, j = 3;
}
}
", @"
int i = 2;
while (true) {
int i = 2;
while (true) {
int j = 3;
}
}
");
}
@ -96,13 +96,13 @@ while (true) { @@ -96,13 +96,13 @@ while (true) {
public void MovesAllInitializersWhenOnType()
{
TestStatements(@"
while (true) {
while (true) {
i$nt i = 2, j = 3;
}
}
", @"
int i = 2, j = 3;
while (true) {
}
int i = 2, j = 3;
while (true) {
}
");
}
@ -110,16 +110,16 @@ while (true) { @@ -110,16 +110,16 @@ while (true) {
public void OnlyMovesDeclarationWhenInitializerDependsOnOtherStatements()
{
TestStatements(@"
while (true) {
while (true) {
int i = 2;
int j$ = i;
}
}
", @"
int j;
while (true) {
int j;
while (true) {
int i = 2;
j = i;
}
}
");
}
@ -127,13 +127,13 @@ while (true) { @@ -127,13 +127,13 @@ while (true) {
public void HandlesLambdaDelegate()
{
TestStatements(@"
var action = new Action<int>(i => {
var action = new Action<int>(i => {
int j$ = 2;
});
});
", @"
int j = 2;
var action = new Action<int>(i => {
});
int j = 2;
var action = new Action<int>(i => {
});
");
}
}

6
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/RemoveRedundantCatchTypeTests.cs

@ -52,8 +52,7 @@ class TestClass @@ -52,8 +52,7 @@ class TestClass
public void F()
{
try {
}
catch {
} catch {
}
}
}");
@ -79,8 +78,7 @@ class TestClass @@ -79,8 +78,7 @@ class TestClass
public void F()
{
try {
}
catch {
} catch {
System.Console.WriteLine (""Hi"");
}
}

3
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/SplitDeclarationAndAssignmentTests.cs

@ -100,7 +100,8 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -100,7 +100,8 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" int i;" + Environment.NewLine +
" for (i = 1; i < 10; i++) {}" + Environment.NewLine +
" for (i = 1; i < 10; i++) {" + Environment.NewLine +
" }" + Environment.NewLine +
" }" + Environment.NewLine +
"}", result);
}

6
ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/AccessToModifiedClosureTests.cs

@ -403,7 +403,7 @@ class TestClass @@ -403,7 +403,7 @@ class TestClass
foreach (var i in a) {
var f = new System.Func<int, int> (x => {
var f2 = new System.Func<int, int> (y => y - i);
return f2(x) + i;
return f2 (x) + i;
});
}
}
@ -417,7 +417,7 @@ class TestClass @@ -417,7 +417,7 @@ class TestClass
var i1 = i;
var f = new System.Func<int, int> (x => {
var f2 = new System.Func<int, int> (y => y - i1);
return f2(x) + i1;
return f2 (x) + i1;
});
}
}
@ -432,7 +432,7 @@ class TestClass @@ -432,7 +432,7 @@ class TestClass
var f = new System.Func<int, int> (x => {
var i1 = i;
var f2 = new System.Func<int, int> (y => y - i1);
return f2(x) + i;
return f2 (x) + i;
});
}
}

9
ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/ConstantConditionIssueTests.cs

@ -114,7 +114,8 @@ class TestClass @@ -114,7 +114,8 @@ class TestClass
{
void TestMethod ()
{
for (int i = 0; true; i++) ;
for (int i = 0; true; i++)
;
}
}";
Test<ConstantConditionIssue> (input, 1, output);
@ -128,7 +129,8 @@ class TestClass @@ -128,7 +129,8 @@ class TestClass
{
void TestMethod ()
{
while (1 > 0) ;
while (1 > 0)
;
}
}";
var output = @"
@ -136,7 +138,8 @@ class TestClass @@ -136,7 +138,8 @@ class TestClass
{
void TestMethod ()
{
while (true) ;
while (true)
;
}
}";
Test<ConstantConditionIssue> (input, 1, output);

12
ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/ExpressionIsAlwaysOfProvidedTypeIssueTests.cs

@ -39,7 +39,8 @@ class TestClass @@ -39,7 +39,8 @@ class TestClass
{
void TestMethod (" + variableType + @" x)
{
if (x is " + providedType + @") ;
if (x is " + providedType + @")
;
}
}";
var output = @"
@ -47,7 +48,8 @@ class TestClass @@ -47,7 +48,8 @@ class TestClass
{
void TestMethod (" + variableType + @" x)
{
if (x != null) ;
if (x != null)
;
}
}";
Test<ExpressionIsAlwaysOfProvidedTypeIssue> (input, 1, output);
@ -73,7 +75,8 @@ class TestClass @@ -73,7 +75,8 @@ class TestClass
{
void TestMethod<T> (T x) where T : TestClass
{
if (x is TestClass) ;
if (x is TestClass)
;
}
}";
var output = @"
@ -81,7 +84,8 @@ class TestClass @@ -81,7 +84,8 @@ class TestClass
{
void TestMethod<T> (T x) where T : TestClass
{
if (x != null) ;
if (x != null)
;
}
}";
Test<ExpressionIsAlwaysOfProvidedTypeIssue> (input, 1, output);

3
ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/InspectionActionTestBase.cs

@ -59,6 +59,9 @@ namespace ICSharpCode.NRefactory.CSharp.CodeIssues @@ -59,6 +59,9 @@ namespace ICSharpCode.NRefactory.CSharp.CodeIssues
}
bool pass = expectedOutput == ctx.Text;
if (!pass) {
Console.WriteLine ("expected:");
Console.WriteLine (expectedOutput);
Console.WriteLine ("got:");
Console.WriteLine (ctx.Text);
}
Assert.AreEqual (expectedOutput, ctx.Text);

12
ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/RedundantToStringTests.cs

@ -140,7 +140,7 @@ class Foo @@ -140,7 +140,7 @@ class Foo
{
void Bar (int i)
{
string s = string.Format(""{0}"", i);
string s = string.Format (""{0}"", i);
}
}");
}
@ -167,7 +167,7 @@ class Foo @@ -167,7 +167,7 @@ class Foo
void Bar (int i)
{
string format = ""{0}"";
string s = string.Format(format, i);
string s = string.Format (format, i);
}
}");
}
@ -196,7 +196,7 @@ class Foo @@ -196,7 +196,7 @@ class Foo
{
void Bar (int i)
{
string s = FakeFormat(""{0} {1}"", i.ToString(), i);
string s = FakeFormat (""{0} {1}"", i.ToString (), i);
}
void FakeFormat(string format, string arg0, object arg1)
@ -229,7 +229,7 @@ class Foo @@ -229,7 +229,7 @@ class Foo
{
void Bar (int i)
{
string s = FakeFormat(""{0} {1}"", i, i);
string s = FakeFormat (""{0} {1}"", i, i);
}
void FakeFormat(string format, params object[] args)
@ -261,8 +261,8 @@ class Foo @@ -261,8 +261,8 @@ class Foo
void Bar (int i)
{
var w = new System.IO.StringWriter();
w.Write(i);
w.WriteLine(i);
w.Write (i);
w.WriteLine (i);
}
}");
}

10
ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/ReferenceToStaticMemberViaDerivedTypeTests.cs

@ -46,7 +46,7 @@ class C @@ -46,7 +46,7 @@ class C
{
void Main()
{
B.F();
B.F ();
}
}";
TestRefactoringContext context;
@ -64,7 +64,7 @@ class C @@ -64,7 +64,7 @@ class C
{
void Main()
{
A.F();
A.F ();
}
}"
);
@ -181,7 +181,7 @@ class D @@ -181,7 +181,7 @@ class D
{
void Main()
{
A.B.F();
A.B.F ();
}
}"
);
@ -205,7 +205,7 @@ namespace Second @@ -205,7 +205,7 @@ namespace Second
{
void Main()
{
B.F();
B.F ();
}
}
}";
@ -229,7 +229,7 @@ namespace Second @@ -229,7 +229,7 @@ namespace Second
{
void Main()
{
First.A.F();
First.A.F ();
}
}
}"

3
ICSharpCode.NRefactory.Tests/FormattingTests/TextEditorTestAdapter.cs

@ -48,6 +48,9 @@ namespace ICSharpCode.NRefactory.CSharp.FormattingTests @@ -48,6 +48,9 @@ namespace ICSharpCode.NRefactory.CSharp.FormattingTests
expectedOutput = NormalizeNewlines(expectedOutput);
IDocument doc = GetResult(policy, input, mode);
if (expectedOutput != doc.Text) {
Console.WriteLine ("expected:");
Console.WriteLine (expectedOutput);
Console.WriteLine ("got:");
Console.WriteLine (doc.Text);
}
Assert.AreEqual (expectedOutput, doc.Text);

Loading…
Cancel
Save