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. 21
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertSwitchToIfTests.cs
  6. 6
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/RemoveRedundantCatchTypeTests.cs
  7. 3
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/SplitDeclarationAndAssignmentTests.cs
  8. 9
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/ConstantConditionIssueTests.cs
  9. 12
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/ExpressionIsAlwaysOfProvidedTypeIssueTests.cs
  10. 3
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/InspectionActionTestBase.cs
  11. 3
      ICSharpCode.NRefactory.Tests/FormattingTests/TextEditorTestAdapter.cs

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

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

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

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

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

@ -152,6 +152,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
text += Options.EolMarker; text += Options.EolMarker;
InsertText(startOffset, text); InsertText(startOffset, text);
output.RegisterTrackedSegments(this, startOffset); output.RegisterTrackedSegments(this, startOffset);
CorrectFormatting (node, insertNode);
} }
public void InsertAfter(AstNode node, AstNode insertNode) public void InsertAfter(AstNode node, AstNode insertNode)
@ -164,6 +165,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var insertOffset = GetCurrentOffset(node.EndLocation); var insertOffset = GetCurrentOffset(node.EndLocation);
InsertText(insertOffset, text); InsertText(insertOffset, text);
output.RegisterTrackedSegments(this, insertOffset); output.RegisterTrackedSegments(this, insertOffset);
CorrectFormatting (node, insertNode);
} }
public void AddTo(BlockStatement bodyStatement, AstNode insertNode) public void AddTo(BlockStatement bodyStatement, AstNode insertNode)
@ -172,6 +174,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var output = OutputNode(1 + GetIndentLevelAt(startOffset), insertNode, true); var output = OutputNode(1 + GetIndentLevelAt(startOffset), insertNode, true);
InsertText(startOffset, output.Text); InsertText(startOffset, output.Text);
output.RegisterTrackedSegments(this, startOffset); output.RegisterTrackedSegments(this, startOffset);
CorrectFormatting (null, insertNode);
} }
public virtual Task Link (params AstNode[] nodes) public virtual Task Link (params AstNode[] nodes)
@ -196,6 +199,18 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
output.TrimStart (); output.TrimStart ();
Replace (startOffset, segment.Length, output.Text); Replace (startOffset, segment.Length, output.Text);
output.RegisterTrackedSegments(this, startOffset); 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); public abstract void Remove (AstNode node, bool removeEmptyLine = true);

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

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

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

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

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

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

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

@ -100,7 +100,8 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
" void Test ()" + Environment.NewLine + " void Test ()" + Environment.NewLine +
" {" + Environment.NewLine + " {" + Environment.NewLine +
" int i;" + 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 + " }" + Environment.NewLine +
"}", result); "}", result);
} }

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

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

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

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

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

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

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

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

Loading…
Cancel
Save