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. 15
      ICSharpCode.NRefactory.CSharp/Formatter/AstFormattingVisitor.cs
  2. 2
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/IterateViaForeachAction.cs
  3. 9
      ICSharpCode.NRefactory.CSharp/Refactoring/DocumentScript.cs
  4. 14
      ICSharpCode.NRefactory.CSharp/Refactoring/Script.cs
  5. 11
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertSwitchToIfTests.cs
  6. 1
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/SplitDeclarationAndAssignmentTests.cs

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

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

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

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

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

@ -17,8 +17,10 @@ @@ -17,8 +17,10 @@
// DEALINGS IN THE SOFTWARE.
using System;
using System.Linq;
using System.Diagnostics;
using ICSharpCode.NRefactory.Editor;
using System.Collections.Generic;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
@ -95,15 +97,18 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -95,15 +97,18 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
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");
foreach (var node in nodes.OrderByDescending (n => n.StartLocation)) {
var segment = GetSegment(node);
var formatter = new AstFormattingVisitor(FormattingOptions, currentDocument, Options);
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)
{

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

@ -202,20 +202,27 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -202,20 +202,27 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
CorrectFormatting (node, node);
}
List<AstNode> nodesToFormat = new List<AstNode> ();
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);
nodesToFormat.Add (newNode);
} 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 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)
{
@ -380,6 +387,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -380,6 +387,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
public virtual void Dispose()
{
FormatText (nodesToFormat);
}
public enum NewTypeContext {

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

@ -138,9 +138,14 @@ class TestClass @@ -138,9 +138,14 @@ class TestClass
{
if (a == 0) {
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 {
}
}
}");

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

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

Loading…
Cancel
Save