Browse Source

[Refactoring] Format text can now take a node list to format / fixed a

formatting issue.
newNRvisualizers
Mike Krüger 13 years ago
parent
commit
8e1ed7e962
  1. 17
      ICSharpCode.NRefactory.CSharp/Formatter/AstFormattingVisitor.cs
  2. 2
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/IterateViaForeachAction.cs
  3. 17
      ICSharpCode.NRefactory.CSharp/Refactoring/DocumentScript.cs
  4. 16
      ICSharpCode.NRefactory.CSharp/Refactoring/Script.cs
  5. 11
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertSwitchToIfTests.cs
  6. 6
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/MoveToOuterScopeTests.cs
  7. 1
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/SplitDeclarationAndAssignmentTests.cs

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

@ -119,16 +119,20 @@ namespace ICSharpCode.NRefactory.CSharp
protected override void VisitChildren (AstNode node) protected override void VisitChildren (AstNode node)
{ {
if (!FormattingRegion.IsEmpty) {
if (node.EndLocation < FormattingRegion.Begin || node.StartLocation > FormattingRegion.End)
return;
}
AstNode next; AstNode next;
for (var child = node.FirstChild; child != null; child = next) { for (var child = node.FirstChild; child != null; child = next) {
// Store next to allow the loop to continue // Store next to allow the loop to continue
// if the visitor removes/replaces child. // if the visitor removes/replaces child.
next = child.NextSibling; next = child.NextSibling;
if (!FormattingRegion.IsEmpty) {
if (child.EndLocation < FormattingRegion.Begin) {
continue;
}
if (child.StartLocation > FormattingRegion.End) {
break;
}
}
child.AcceptVisitor (this); child.AcceptVisitor (this);
} }
} }
@ -404,7 +408,8 @@ namespace ICSharpCode.NRefactory.CSharp
void ForceSpace(int startOffset, int endOffset, bool forceSpace) void ForceSpace(int startOffset, int endOffset, bool forceSpace)
{ {
int lastNonWs = SearchLastNonWsChar(startOffset, endOffset); int lastNonWs = SearchLastNonWsChar(startOffset, endOffset);
AddChange(lastNonWs + 1, System.Math.Max(0, endOffset - lastNonWs - 1), forceSpace ? " " : ""); if (lastNonWs >= 0)
AddChange(lastNonWs + 1, System.Math.Max(0, endOffset - lastNonWs - 1), forceSpace ? " " : "");
} }
// void ForceSpacesAfter (AstNode n, bool forceSpaces) // void ForceSpacesAfter (AstNode n, bool forceSpaces)
// { // {

2
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/IterateViaForeachAction.cs

@ -70,7 +70,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var blockStatement = new BlockStatement(); var blockStatement = new BlockStatement();
blockStatement.Statements.Add(iterator); blockStatement.Statements.Add(iterator);
script.Replace(usingStatement.EmbeddedStatement, blockStatement); script.Replace(usingStatement.EmbeddedStatement, blockStatement);
script.FormatText(blockStatement); script.FormatText((AstNode)blockStatement);
} else if (usingStatement.EmbeddedStatement is BlockStatement) { } else if (usingStatement.EmbeddedStatement is BlockStatement) {
var anchorNode = usingStatement.EmbeddedStatement.FirstChild; var anchorNode = usingStatement.EmbeddedStatement.FirstChild;
script.InsertAfter(anchorNode, iterator); script.InsertAfter(anchorNode, iterator);

17
ICSharpCode.NRefactory.CSharp/Refactoring/DocumentScript.cs

@ -17,8 +17,10 @@
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
using System; using System;
using System.Linq;
using System.Diagnostics; using System.Diagnostics;
using ICSharpCode.NRefactory.Editor; using ICSharpCode.NRefactory.Editor;
using System.Collections.Generic;
namespace ICSharpCode.NRefactory.CSharp.Refactoring namespace ICSharpCode.NRefactory.CSharp.Refactoring
{ {
@ -95,14 +97,17 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
return originalDocument.Version.MoveOffsetTo(currentDocument.Version, originalDocumentOffset); return originalDocument.Version.MoveOffsetTo(currentDocument.Version, originalDocumentOffset);
} }
public override void FormatText(AstNode node) public override void FormatText(IEnumerable<AstNode> nodes)
{ {
var segment = GetSegment(node);
var syntaxTree = SyntaxTree.Parse(currentDocument, "dummy.cs"); var syntaxTree = SyntaxTree.Parse(currentDocument, "dummy.cs");
var formatter = new AstFormattingVisitor(FormattingOptions, currentDocument, Options); foreach (var node in nodes.OrderByDescending (n => n.StartLocation)) {
formatter.FormattingRegion = new ICSharpCode.NRefactory.TypeSystem.DomRegion (node.StartLocation, node.EndLocation); var segment = GetSegment(node);
syntaxTree.AcceptVisitor(formatter); var formatter = new AstFormattingVisitor(FormattingOptions, currentDocument, Options);
formatter.ApplyChanges(segment.Offset, segment.Length);
formatter.FormattingRegion = new ICSharpCode.NRefactory.TypeSystem.DomRegion (node.StartLocation, node.EndLocation);
syntaxTree.AcceptVisitor(formatter);
formatter.ApplyChanges(segment.Offset, segment.Length);
}
} }
protected override int GetIndentLevelAt(int offset) protected override int GetIndentLevelAt(int offset)

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

@ -202,21 +202,28 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
CorrectFormatting (node, node); CorrectFormatting (node, node);
} }
List<AstNode> nodesToFormat = new List<AstNode> ();
void CorrectFormatting(AstNode node, AstNode newNode) void CorrectFormatting(AstNode node, AstNode newNode)
{ {
if (node is Identifier || node is IdentifierExpression || node is CSharpTokenNode || node is AstType) if (node is Identifier || node is IdentifierExpression || node is CSharpTokenNode || node is AstType)
return; return;
if (node == null || node.Parent is BlockStatement) { if (node == null || node.Parent is BlockStatement) {
FormatText(newNode); nodesToFormat.Add (newNode);
} else { } else {
FormatText((node.Parent != null && (node.Parent is Statement || node.Parent is Expression || node.Parent is VariableInitializer)) ? node.Parent : newNode); nodesToFormat.Add ((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);
public abstract void FormatText (AstNode node); public abstract void FormatText (IEnumerable<AstNode> nodes);
public void FormatText (params AstNode[] node)
{
FormatText ((IEnumerable<AstNode>)node);
}
public virtual void Select (AstNode node) public virtual void Select (AstNode node)
{ {
// default implementation: do nothing // default implementation: do nothing
@ -380,6 +387,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
public virtual void Dispose() public virtual void Dispose()
{ {
FormatText (nodesToFormat);
} }
public enum NewTypeContext { public enum NewTypeContext {

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

@ -138,10 +138,15 @@ class TestClass
{ {
if (a == 0) { if (a == 0) {
int b = 1; int b = 1;
} else if (a == 1 || a == 2) {
} else if (a == 3 || a == 4 || a == 5) {
} else {
} }
else
if (a == 1 || a == 2) {
}
else
if (a == 3 || a == 4 || a == 5) {
}
else {
}
} }
}"); }");
} }

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

@ -88,7 +88,7 @@ class A
int i = 2; int i = 2;
while (true) { while (true) {
int j = 3; int j = 3;
} }
"); ");
} }
@ -116,10 +116,10 @@ class A
} }
", @" ", @"
int j; int j;
while (true) { while (true) {
int i = 2; int i = 2;
j = i; j = i;
} }
"); ");
} }

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

@ -101,6 +101,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
" {" + 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 +
" }" + Environment.NewLine + " }" + Environment.NewLine +
"}", result); "}", result);

Loading…
Cancel
Save