Browse Source

Updated indent engine.

newNRvisualizers
Mike Krüger 14 years ago
parent
commit
b21e85f00e
  1. 120
      ICSharpCode.NRefactory.CSharp/Formatter/AstFormattingVisitor.cs
  2. 62
      ICSharpCode.NRefactory.CSharp/Formatter/Indent.cs
  3. 9
      ICSharpCode.NRefactory.CSharp/Formatter/TextEditorOptions.cs
  4. 4
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs
  5. 32
      ICSharpCode.NRefactory.Tests/FormattingTests/TestBlankLineFormatting.cs
  6. 24
      ICSharpCode.NRefactory.Tests/FormattingTests/TextEditorTestAdapter.cs

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

@ -34,6 +34,11 @@ using ICSharpCode.NRefactory.CSharp.Refactoring; @@ -34,6 +34,11 @@ using ICSharpCode.NRefactory.CSharp.Refactoring;
namespace ICSharpCode.NRefactory.CSharp
{
public enum FormattingMode {
OnTheFly,
Intrusive
}
public class AstFormattingVisitor : DepthFirstAstVisitor
{
sealed class TextReplaceAction
@ -82,22 +87,8 @@ namespace ICSharpCode.NRefactory.CSharp @@ -82,22 +87,8 @@ namespace ICSharpCode.NRefactory.CSharp
List<TextReplaceAction> changes = new List<TextReplaceAction> ();
Indent curIndent;
readonly TextEditorOptions options;
public int IndentLevel {
get {
return curIndent.Level;
}
set {
curIndent.Level = value;
}
}
public int CurrentSpaceIndents {
get;
set;
}
public bool CorrectBlankLines {
public FormattingMode FormattingMode {
get;
set;
}
@ -119,7 +110,6 @@ namespace ICSharpCode.NRefactory.CSharp @@ -119,7 +110,6 @@ namespace ICSharpCode.NRefactory.CSharp
this.document = document;
this.options = options ?? TextEditorOptions.Default;
curIndent = new Indent(this.options);
CorrectBlankLines = true;
}
/// <summary>
@ -195,9 +185,8 @@ namespace ICSharpCode.NRefactory.CSharp @@ -195,9 +185,8 @@ namespace ICSharpCode.NRefactory.CSharp
public void EnsureBlankLinesAfter(AstNode node, int blankLines)
{
if (!CorrectBlankLines) {
if (FormattingMode != FormattingMode.Intrusive)
return;
}
var loc = node.EndLocation;
int line = loc.Line;
do {
@ -226,9 +215,8 @@ namespace ICSharpCode.NRefactory.CSharp @@ -226,9 +215,8 @@ namespace ICSharpCode.NRefactory.CSharp
public void EnsureBlankLinesBefore(AstNode node, int blankLines)
{
if (!CorrectBlankLines) {
if (FormattingMode != FormattingMode.Intrusive)
return;
}
var loc = node.StartLocation;
int line = loc.Line;
do {
@ -272,11 +260,11 @@ namespace ICSharpCode.NRefactory.CSharp @@ -272,11 +260,11 @@ namespace ICSharpCode.NRefactory.CSharp
FixIndentationForceNewLine(namespaceDeclaration.StartLocation);
EnforceBraceStyle(policy.NamespaceBraceStyle, namespaceDeclaration.LBraceToken, namespaceDeclaration.RBraceToken);
if (policy.IndentNamespaceBody) {
IndentLevel++;
curIndent.Push(IndentType.Block);
}
base.VisitNamespaceDeclaration(namespaceDeclaration);
if (policy.IndentNamespaceBody) {
IndentLevel--;
curIndent.Pop ();
}
FixIndentation(namespaceDeclaration.RBraceToken.StartLocation);
}
@ -304,17 +292,17 @@ namespace ICSharpCode.NRefactory.CSharp @@ -304,17 +292,17 @@ namespace ICSharpCode.NRefactory.CSharp
indentBody = policy.IndentEnumBody;
break;
default:
throw new InvalidOperationException ("unsupported class type : " + typeDeclaration.ClassType);
throw new InvalidOperationException("unsupported class type : " + typeDeclaration.ClassType);
}
EnforceBraceStyle(braceStyle, typeDeclaration.LBraceToken, typeDeclaration.RBraceToken);
if (indentBody) {
IndentLevel++;
curIndent.Push(IndentType.Block);
}
base.VisitTypeDeclaration(typeDeclaration);
if (indentBody) {
IndentLevel--;
curIndent.Pop ();
}
if (typeDeclaration.NextSibling is TypeDeclaration || typeDeclaration.NextSibling is DelegateDeclaration) {
@ -497,7 +485,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -497,7 +485,7 @@ namespace ICSharpCode.NRefactory.CSharp
break;
}
if (policy.IndentPropertyBody) {
IndentLevel++;
curIndent.Push(IndentType.Block);
}
///System.Console.WriteLine ("one line: " + oneLine);
if (!propertyDeclaration.Getter.IsNull) {
@ -557,7 +545,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -557,7 +545,7 @@ namespace ICSharpCode.NRefactory.CSharp
}
if (policy.IndentPropertyBody) {
IndentLevel--;
curIndent.Pop ();
}
if (IsMember(propertyDeclaration.NextSibling)) {
EnsureBlankLinesAfter(propertyDeclaration, policy.BlankLinesBetweenMembers);
@ -576,7 +564,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -576,7 +564,7 @@ namespace ICSharpCode.NRefactory.CSharp
FormatAttributedNode(indexerDeclaration);
EnforceBraceStyle(policy.PropertyBraceStyle, indexerDeclaration.LBraceToken, indexerDeclaration.RBraceToken);
if (policy.IndentPropertyBody) {
IndentLevel++;
curIndent.Push(IndentType.Block);
}
if (!indexerDeclaration.Getter.IsNull) {
@ -603,7 +591,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -603,7 +591,7 @@ namespace ICSharpCode.NRefactory.CSharp
}
}
if (policy.IndentPropertyBody) {
IndentLevel--;
curIndent.Pop ();
}
if (IsMember(indexerDeclaration.NextSibling)) {
EnsureBlankLinesAfter(indexerDeclaration, policy.BlankLinesBetweenMembers);
@ -620,7 +608,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -620,7 +608,7 @@ namespace ICSharpCode.NRefactory.CSharp
FormatAttributedNode(eventDeclaration);
EnforceBraceStyle(policy.EventBraceStyle, eventDeclaration.LBraceToken, eventDeclaration.RBraceToken);
if (policy.IndentEventBody) {
IndentLevel++;
curIndent.Push(IndentType.Block);
}
if (!eventDeclaration.AddAccessor.IsNull) {
@ -649,7 +637,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -649,7 +637,7 @@ namespace ICSharpCode.NRefactory.CSharp
}
if (policy.IndentEventBody) {
IndentLevel--;
curIndent.Pop ();
}
if (eventDeclaration.NextSibling is EventDeclaration && IsSimpleEvent(eventDeclaration) && IsSimpleEvent(eventDeclaration.NextSibling)) {
@ -669,7 +657,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -669,7 +657,7 @@ namespace ICSharpCode.NRefactory.CSharp
}
var lastLoc = eventDeclaration.StartLocation;
IndentLevel++;
curIndent.Push(IndentType.Block);
foreach (var initializer in eventDeclaration.Variables) {
if (lastLoc.Line != initializer.StartLocation.Line) {
FixStatementIndentation(initializer.StartLocation);
@ -677,7 +665,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -677,7 +665,7 @@ namespace ICSharpCode.NRefactory.CSharp
}
initializer.AcceptVisitor(this);
}
IndentLevel--;
curIndent.Pop ();
}
public override void VisitAccessor(Accessor accessor)
@ -698,7 +686,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -698,7 +686,7 @@ namespace ICSharpCode.NRefactory.CSharp
}
var lastLoc = fieldDeclaration.StartLocation;
IndentLevel++;
curIndent.Push(IndentType.Block);
foreach (var initializer in fieldDeclaration.Variables) {
if (lastLoc.Line != initializer.StartLocation.Line) {
FixStatementIndentation(initializer.StartLocation);
@ -706,7 +694,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -706,7 +694,7 @@ namespace ICSharpCode.NRefactory.CSharp
}
initializer.AcceptVisitor(this);
}
IndentLevel--;
curIndent.Pop ();
}
public override void VisitFixedFieldDeclaration(FixedFieldDeclaration fixedFieldDeclaration)
@ -720,7 +708,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -720,7 +708,7 @@ namespace ICSharpCode.NRefactory.CSharp
}
var lastLoc = fixedFieldDeclaration.StartLocation;
IndentLevel++;
curIndent.Push(IndentType.Block);
foreach (var initializer in fixedFieldDeclaration.Variables) {
if (lastLoc.Line != initializer.StartLocation.Line) {
FixStatementIndentation(initializer.StartLocation);
@ -728,7 +716,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -728,7 +716,7 @@ namespace ICSharpCode.NRefactory.CSharp
}
initializer.AcceptVisitor(this);
}
IndentLevel--;
curIndent.Pop ();
}
public override void VisitEnumMemberDeclaration(EnumMemberDeclaration enumMemberDeclaration)
@ -876,7 +864,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -876,7 +864,7 @@ namespace ICSharpCode.NRefactory.CSharp
void VisitBlockWithoutFixingBraces(BlockStatement blockStatement, bool indent)
{
if (indent) {
IndentLevel++;
curIndent.Push(IndentType.Block);
}
foreach (var child in blockStatement.Children) {
if (child.Role == Roles.LBrace || child.Role == Roles.RBrace) {
@ -886,7 +874,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -886,7 +874,7 @@ namespace ICSharpCode.NRefactory.CSharp
child.AcceptVisitor(this);
}
if (indent) {
IndentLevel--;
curIndent.Pop ();
}
}
@ -949,7 +937,6 @@ namespace ICSharpCode.NRefactory.CSharp @@ -949,7 +937,6 @@ namespace ICSharpCode.NRefactory.CSharp
if (node == null) {
return;
}
int originalLevel = curIndent.Level;
bool isBlock = node is BlockStatement;
TextReplaceAction beginBraceAction = null;
TextReplaceAction endBraceAction = null;
@ -976,7 +963,9 @@ namespace ICSharpCode.NRefactory.CSharp @@ -976,7 +963,9 @@ namespace ICSharpCode.NRefactory.CSharp
break;
case BraceStyle.NextLineShifted2:
case BraceStyle.NextLineShifted:
startBrace = this.options.EolMarker + curIndent.IndentString + curIndent.SingleIndent + "{";
curIndent.Push(IndentType.Block);
startBrace = this.options.EolMarker + curIndent.IndentString + "{";
curIndent.Pop();
break;
}
beginBraceAction = AddChange(start, 0, startBrace);
@ -1012,17 +1001,15 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1012,17 +1001,15 @@ namespace ICSharpCode.NRefactory.CSharp
}
}
if (braceStyle == BraceStyle.NextLineShifted2) {
curIndent.Level++;
curIndent.Push(IndentType.Block);
}
} else {
if (allowInLine && token.StartLocation.Line == node.EndLocation.Line) {
nextStatementIndent = " ";
}
}
if (policy.IndentBlocks &&
!(policy.AlignEmbeddedIfStatements && node is IfElseStatement && node.Parent is IfElseStatement ||
policy.AlignEmbeddedUsingStatements && node is UsingStatement && node.Parent is UsingStatement)) {
curIndent.Level++;
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);
}
if (isBlock) {
VisitBlockWithoutFixingBraces((BlockStatement)node, false);
@ -1032,7 +1019,9 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1032,7 +1019,9 @@ namespace ICSharpCode.NRefactory.CSharp
}
node.AcceptVisitor(this);
}
curIndent.Level = originalLevel;
if (policy.IndentBlocks && !(policy.AlignEmbeddedIfStatements && node is IfElseStatement && node.Parent is IfElseStatement || policy.AlignEmbeddedUsingStatements && node is UsingStatement && node.Parent is UsingStatement)) {
curIndent.Pop();
}
switch (braceForcement) {
case BraceForcement.DoNotChange:
break;
@ -1059,8 +1048,11 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1059,8 +1048,11 @@ namespace ICSharpCode.NRefactory.CSharp
case BraceStyle.BannerStyle:
case BraceStyle.NextLineShifted2:
case BraceStyle.NextLineShifted:
startBrace = this.options.EolMarker + curIndent.IndentString + curIndent.SingleIndent + "}";
curIndent.Push(IndentType.Block);
startBrace = this.options.EolMarker + curIndent.IndentString + "}";
curIndent.Pop ();
break;
}
if (startBrace != null) {
endBraceAction = AddChange(offset, 0, startBrace);
@ -1112,7 +1104,9 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1112,7 +1104,9 @@ namespace ICSharpCode.NRefactory.CSharp
} else {
startIndent = " ";
}
endIndent = IsLineIsEmptyUpToEol(rbraceOffset) ? curIndent.IndentString + curIndent.SingleIndent : this.options.EolMarker + curIndent.IndentString + curIndent.SingleIndent;
curIndent.Push(IndentType.Block);
endIndent = IsLineIsEmptyUpToEol(rbraceOffset) ? curIndent.IndentString : this.options.EolMarker + curIndent.IndentString;
curIndent.Pop();
break;
case BraceStyle.EndOfLine:
prevNode = lbrace.GetPrevNode();
@ -1137,8 +1131,10 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1137,8 +1131,10 @@ namespace ICSharpCode.NRefactory.CSharp
break;
case BraceStyle.NextLineShifted2:
case BraceStyle.NextLineShifted:
startIndent = this.options.EolMarker + curIndent.IndentString + curIndent.SingleIndent;
endIndent = IsLineIsEmptyUpToEol(rbraceOffset) ? curIndent.IndentString + curIndent.SingleIndent : this.options.EolMarker + curIndent.IndentString + curIndent.SingleIndent;
curIndent.Push(IndentType.Block);
startIndent = this.options.EolMarker + curIndent.IndentString;
endIndent = IsLineIsEmptyUpToEol(rbraceOffset) ? curIndent.IndentString : this.options.EolMarker + curIndent.IndentString;
curIndent.Pop ();
break;
}
@ -1307,7 +1303,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1307,7 +1303,7 @@ namespace ICSharpCode.NRefactory.CSharp
public override void VisitSwitchSection(SwitchSection switchSection)
{
if (policy.IndentSwitchBody) {
curIndent.Level++;
curIndent.Push(IndentType.Block);
}
foreach (CaseLabel label in switchSection.CaseLabels) {
@ -1315,26 +1311,26 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1315,26 +1311,26 @@ namespace ICSharpCode.NRefactory.CSharp
label.AcceptVisitor(this);
}
if (policy.IndentCaseBody) {
curIndent.Level++;
curIndent.Push(IndentType.Block);
}
foreach (var stmt in switchSection.Statements) {
if (stmt is BreakStatement && !policy.IndentBreakStatements && policy.IndentCaseBody) {
curIndent.Level--;
curIndent.Pop();
FixStatementIndentation(stmt.StartLocation);
stmt.AcceptVisitor(this);
curIndent.Level++;
curIndent.Push(IndentType.Block);
continue;
}
FixStatementIndentation(stmt.StartLocation);
stmt.AcceptVisitor(this);
}
if (policy.IndentCaseBody) {
curIndent.Level--;
curIndent.Pop ();
}
if (policy.IndentSwitchBody) {
curIndent.Level--;
curIndent.Pop ();
}
}
@ -1410,7 +1406,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1410,7 +1406,7 @@ namespace ICSharpCode.NRefactory.CSharp
foreach (var initializer in variableDeclarationStatement.Variables) {
var indent = !(initializer.Initializer is AnonymousMethodExpression);
if (indent) {
IndentLevel++;
curIndent.Push(IndentType.Block);
}
if (lastLoc.Line != initializer.StartLocation.Line) {
FixStatementIndentation(initializer.StartLocation);
@ -1418,7 +1414,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1418,7 +1414,7 @@ namespace ICSharpCode.NRefactory.CSharp
}
initializer.AcceptVisitor(this);
if (indent) {
IndentLevel--;
curIndent.Pop ();
}
}
@ -1535,13 +1531,13 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1535,13 +1531,13 @@ namespace ICSharpCode.NRefactory.CSharp
base.VisitBinaryOperatorExpression(binaryOperatorExpression);
// Handle line breaks in binary opeartor expression.
if (binaryOperatorExpression.Left.EndLocation.Line != binaryOperatorExpression.Right.StartLocation.Line) {
IndentLevel++;
curIndent.Push(IndentType.Block);
if (binaryOperatorExpression.OperatorToken.StartLocation.Line == binaryOperatorExpression.Right.StartLocation.Line) {
FixStatementIndentation(binaryOperatorExpression.OperatorToken.StartLocation);
} else {
FixStatementIndentation(binaryOperatorExpression.Right.StartLocation);
}
IndentLevel--;
curIndent.Pop ();
}
}

62
ICSharpCode.NRefactory.CSharp/Formatter/Indent.cs

@ -24,49 +24,75 @@ @@ -24,49 +24,75 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
namespace ICSharpCode.NRefactory.CSharp
{
public enum IndentType {
Block,
Continuation,
Label
}
public class Indent
{
readonly Stack<IndentType> indentStack = new Stack<IndentType> ();
readonly TextEditorOptions options;
int curIndent;
public int Level {
get;
set;
public Indent(TextEditorOptions options)
{
this.options = options;
}
public int ExtraSpaces {
get;
set;
public void Push(IndentType type)
{
indentStack.Push(type);
curIndent += GetIndent(type);
Update();
}
public Indent(TextEditorOptions options)
public void Pop()
{
this.options = options;
curIndent -= GetIndent(indentStack.Pop());
Update();
}
public Indent(TextEditorOptions options, int level, int extraSpaces)
int GetIndent(IndentType indentType)
{
Level = level;
ExtraSpaces = extraSpaces;
switch (indentType) {
case IndentType.Block:
return options.IndentSize;
case IndentType.Continuation:
return options.ContinuationIndent;
case IndentType.Label:
return options.LabelIndent;
default:
throw new ArgumentOutOfRangeException();
}
}
public string IndentString {
get {
return (options.TabsToSpaces ? new string(' ', Level * options.TabSize) : new string ('\t', Level)) + new string (' ', ExtraSpaces);
void Update()
{
if (options.TabsToSpaces) {
indentString = new string(' ', curIndent);
return;
}
indentString = new string('\t', curIndent / options.TabSize) + new string(' ', curIndent % options.TabSize);
}
public string SingleIndent {
string indentString;
public string IndentString {
get {
return options.TabsToSpaces ? new string(' ', options.TabSize) : "\t";
return indentString;
}
}
public override string ToString ()
public override string ToString()
{
return string.Format ("[Indent: Level={0}, ExtraSpaces={1}]", Level, ExtraSpaces);
return string.Format("[Indent: curIndent={0}]", curIndent);
}
}
}

9
ICSharpCode.NRefactory.CSharp/Formatter/TextEditorOptions.cs

@ -69,6 +69,15 @@ namespace ICSharpCode.NRefactory.CSharp @@ -69,6 +69,15 @@ namespace ICSharpCode.NRefactory.CSharp
set;
}
/// <summary>
/// Gets or sets the label indent. A label indent is the indent that will be put before an label.
/// (Note: it may be negative -IndentSize would cause that labels are unindented)
/// </summary>
public int LabelIndent {
get;
set;
}
/// <summary>
/// Gets or sets the eol marker.
/// </summary>

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

@ -84,10 +84,9 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -84,10 +84,9 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
sealed class TestScript : DocumentScript
{
readonly TestRefactoringContext context;
public TestScript(TestRefactoringContext context) : base(context.doc, new CSharpFormattingOptions())
public TestScript(TestRefactoringContext context) : base(context.doc, new CSharpFormattingOptions(), new TextEditorOptions ())
{
this.context = context;
this.eolMarker = context.EolMarker;
}
public override void Link (params AstNode[] nodes)
@ -181,7 +180,6 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -181,7 +180,6 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
}
#region Text stuff
public override string EolMarker { get { return Environment.NewLine; } }
public override bool IsSomethingSelected { get { return selectionStart > 0; } }

32
ICSharpCode.NRefactory.Tests/FormattingTests/TestBlankLineFormatting.cs

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
//
//
// TastBlankLineFormatting.cs
//
// Author:
@ -51,7 +51,7 @@ using System.Text; @@ -51,7 +51,7 @@ using System.Text;
namespace Test
{
}");
}", FormattingMode.Intrusive);
policy.BlankLinesAfterUsings = 0;
Continue (policy, adapter,
@ -59,7 +59,7 @@ namespace Test @@ -59,7 +59,7 @@ namespace Test
using System.Text;
namespace Test
{
}");
}", FormattingMode.Intrusive);
}
[Test()]
@ -80,7 +80,7 @@ using System; @@ -80,7 +80,7 @@ using System;
using System.Text;
namespace Test
{
}");
}", FormattingMode.Intrusive);
policy.BlankLinesBeforeUsings = 0;
Continue (policy, adapter,
@ -88,7 +88,7 @@ namespace Test @@ -88,7 +88,7 @@ namespace Test
using System.Text;
namespace Test
{
}");
}", FormattingMode.Intrusive);
}
[Test()]
@ -110,7 +110,7 @@ namespace Test @@ -110,7 +110,7 @@ namespace Test
class Test
{
}
}");
}", FormattingMode.Intrusive);
policy.BlankLinesBeforeFirstDeclaration = 0;
Continue (policy, adapter,
@ -119,7 +119,7 @@ namespace Test @@ -119,7 +119,7 @@ namespace Test
class Test
{
}
}");
}", FormattingMode.Intrusive);
}
[Test()]
@ -153,7 +153,7 @@ namespace Test @@ -153,7 +153,7 @@ namespace Test
class Test3
{
}
}");
}", FormattingMode.Intrusive);
policy.BlankLinesBetweenTypes = 0;
Continue (policy, adapter, @"namespace Test
@ -167,7 +167,7 @@ namespace Test @@ -167,7 +167,7 @@ namespace Test
class Test3
{
}
}");
}", FormattingMode.Intrusive);
}
[Test()]
@ -189,7 +189,7 @@ namespace Test @@ -189,7 +189,7 @@ namespace Test
int b;
int c;
}");
}", FormattingMode.Intrusive);
policy.BlankLinesBetweenFields = 0;
Continue (policy, adapter, @"class Test
@ -197,7 +197,7 @@ namespace Test @@ -197,7 +197,7 @@ namespace Test
int a;
int b;
int c;
}");
}", FormattingMode.Intrusive);
}
[Test()]
@ -219,7 +219,7 @@ namespace Test @@ -219,7 +219,7 @@ namespace Test
public event EventHandler b;
public event EventHandler c;
}");
}", FormattingMode.Intrusive);
policy.BlankLinesBetweenEventFields = 0;
Continue (policy, adapter,
@ -228,7 +228,7 @@ namespace Test @@ -228,7 +228,7 @@ namespace Test
public event EventHandler a;
public event EventHandler b;
public event EventHandler c;
}");
}", FormattingMode.Intrusive);
}
[Test()]
@ -237,7 +237,7 @@ namespace Test @@ -237,7 +237,7 @@ namespace Test
CSharpFormattingOptions policy = new CSharpFormattingOptions ();
policy.BlankLinesBetweenMembers = 1;
var adapter = Test (policy,@"class Test
var adapter = Test (policy, @"class Test
{
void AMethod ()
{
@ -261,7 +261,7 @@ namespace Test @@ -261,7 +261,7 @@ namespace Test
void CMethod ()
{
}
}");
}", FormattingMode.Intrusive);
policy.BlankLinesBetweenMembers = 0;
Continue (policy, adapter, @"class Test
@ -275,7 +275,7 @@ namespace Test @@ -275,7 +275,7 @@ namespace Test
void CMethod ()
{
}
}");
}", FormattingMode.Intrusive);
}

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

@ -28,22 +28,24 @@ namespace ICSharpCode.NRefactory.CSharp.FormattingTests @@ -28,22 +28,24 @@ namespace ICSharpCode.NRefactory.CSharp.FormattingTests
return b.ToString();
}*/
protected static IDocument GetResult (CSharpFormattingOptions policy, string input)
protected static IDocument GetResult (CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.OnTheFly)
{
input = NormalizeNewlines(input);
input = NormalizeNewlines (input);
var document = new StringBuilderDocument (input);
var visitor = new AstFormattingVisitor (policy, document);
visitor.EolMarker = "\n";
var options = new TextEditorOptions ();
options.EolMarker = "\n";
var visitor = new AstFormattingVisitor (policy, document, options);
visitor.FormattingMode = mode;
var compilationUnit = new CSharpParser ().Parse (document, "test.cs");
compilationUnit.AcceptVisitor (visitor);
visitor.ApplyChanges();
return document;
}
protected static IDocument Test (CSharpFormattingOptions policy, string input, string expectedOutput)
protected static IDocument Test (CSharpFormattingOptions policy, string input, string expectedOutput, FormattingMode mode = FormattingMode.OnTheFly)
{
expectedOutput = NormalizeNewlines(expectedOutput);
IDocument doc = GetResult(policy, input);
IDocument doc = GetResult(policy, input, mode);
if (expectedOutput != doc.Text) {
Console.WriteLine (doc.Text);
}
@ -56,11 +58,13 @@ namespace ICSharpCode.NRefactory.CSharp.FormattingTests @@ -56,11 +58,13 @@ namespace ICSharpCode.NRefactory.CSharp.FormattingTests
return input.Replace("\r\n", "\n");
}
protected static void Continue (CSharpFormattingOptions policy, IDocument document, string expectedOutput)
protected static void Continue (CSharpFormattingOptions policy, IDocument document, string expectedOutput, FormattingMode formattingMode = FormattingMode.OnTheFly)
{
expectedOutput = NormalizeNewlines(expectedOutput);
var visitior = new AstFormattingVisitor (policy, document);
visitior.EolMarker = "\n";
expectedOutput = NormalizeNewlines (expectedOutput);
var options = new TextEditorOptions ();
options.EolMarker = "\n";
var visitior = new AstFormattingVisitor (policy, document, options);
visitior.FormattingMode = formattingMode;
var compilationUnit = new CSharpParser ().Parse (document, "test.cs");
compilationUnit.AcceptVisitor (visitior);
visitior.ApplyChanges();

Loading…
Cancel
Save