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

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

@ -24,49 +24,75 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
using System; using System;
using System.Collections.Generic;
namespace ICSharpCode.NRefactory.CSharp namespace ICSharpCode.NRefactory.CSharp
{ {
public enum IndentType {
Block,
Continuation,
Label
}
public class Indent public class Indent
{ {
readonly Stack<IndentType> indentStack = new Stack<IndentType> ();
readonly TextEditorOptions options; readonly TextEditorOptions options;
int curIndent;
public int Level { public Indent(TextEditorOptions options)
get; {
set; this.options = options;
} }
public int ExtraSpaces {
get; public void Push(IndentType type)
set; {
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; switch (indentType) {
ExtraSpaces = extraSpaces; 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 { void Update()
get { {
return (options.TabsToSpaces ? new string(' ', Level * options.TabSize) : new string ('\t', Level)) + new string (' ', ExtraSpaces); 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 { 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
set; 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> /// <summary>
/// Gets or sets the eol marker. /// Gets or sets the eol marker.
/// </summary> /// </summary>

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

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

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

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

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

@ -28,22 +28,24 @@ namespace ICSharpCode.NRefactory.CSharp.FormattingTests
return b.ToString(); 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 document = new StringBuilderDocument (input);
var visitor = new AstFormattingVisitor (policy, document); var options = new TextEditorOptions ();
visitor.EolMarker = "\n"; options.EolMarker = "\n";
var visitor = new AstFormattingVisitor (policy, document, options);
visitor.FormattingMode = mode;
var compilationUnit = new CSharpParser ().Parse (document, "test.cs"); var compilationUnit = new CSharpParser ().Parse (document, "test.cs");
compilationUnit.AcceptVisitor (visitor); compilationUnit.AcceptVisitor (visitor);
visitor.ApplyChanges(); visitor.ApplyChanges();
return document; 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); expectedOutput = NormalizeNewlines(expectedOutput);
IDocument doc = GetResult(policy, input); IDocument doc = GetResult(policy, input, mode);
if (expectedOutput != doc.Text) { if (expectedOutput != doc.Text) {
Console.WriteLine (doc.Text); Console.WriteLine (doc.Text);
} }
@ -56,11 +58,13 @@ namespace ICSharpCode.NRefactory.CSharp.FormattingTests
return input.Replace("\r\n", "\n"); 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); expectedOutput = NormalizeNewlines (expectedOutput);
var visitior = new AstFormattingVisitor (policy, document); var options = new TextEditorOptions ();
visitior.EolMarker = "\n"; options.EolMarker = "\n";
var visitior = new AstFormattingVisitor (policy, document, options);
visitior.FormattingMode = formattingMode;
var compilationUnit = new CSharpParser ().Parse (document, "test.cs"); var compilationUnit = new CSharpParser ().Parse (document, "test.cs");
compilationUnit.AcceptVisitor (visitior); compilationUnit.AcceptVisitor (visitior);
visitior.ApplyChanges(); visitior.ApplyChanges();

Loading…
Cancel
Save