Browse Source

[Refactoring] Better use of the text editor options class in the

script node output (output formatter should maybe use the formatting
indent class).
newNRvisualizers
mike 14 years ago
parent
commit
bcb45ff77e
  1. 9
      ICSharpCode.NRefactory.CSharp/Refactoring/DocumentScript.cs
  2. 20
      ICSharpCode.NRefactory.CSharp/Refactoring/Script.cs

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

@ -39,14 +39,11 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -39,14 +39,11 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
}
readonly IDisposable undoGroup;
readonly TextEditorOptions options;
public DocumentScript(IDocument document, CSharpFormattingOptions formattingOptions, TextEditorOptions options) : base(formattingOptions)
public DocumentScript(IDocument document, CSharpFormattingOptions formattingOptions, TextEditorOptions options) : base(formattingOptions, options)
{
this.originalDocument = document.CreateDocumentSnapshot();
this.currentDocument = document;
this.options = options;
this.eolMarker = options.EolMarker;
Debug.Assert(currentDocument.Version.CompareAge(originalDocument.Version) == 0);
this.undoGroup = document.OpenUndoGroup();
}
@ -102,7 +99,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -102,7 +99,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
var segment = GetSegment(node);
var cu = CompilationUnit.Parse(currentDocument, "dummy.cs");
var formatter = new AstFormattingVisitor(FormattingOptions, currentDocument, options);
var formatter = new AstFormattingVisitor(FormattingOptions, currentDocument, Options);
cu.AcceptVisitor(formatter);
formatter.ApplyChanges(segment.Offset, segment.Length);
}

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

@ -67,15 +67,18 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -67,15 +67,18 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
}
}
protected string eolMarker = Environment.NewLine;
readonly CSharpFormattingOptions formattingOptions;
readonly TextEditorOptions options;
Dictionary<AstNode, ISegment> segmentsForInsertedNodes = new Dictionary<AstNode, ISegment>();
protected Script(CSharpFormattingOptions formattingOptions)
protected Script(CSharpFormattingOptions formattingOptions, TextEditorOptions options)
{
if (formattingOptions == null)
throw new ArgumentNullException("formattingOptions");
if (options == null)
throw new ArgumentNullException("options");
this.formattingOptions = formattingOptions;
this.options = options;
}
/// <summary>
@ -134,14 +137,18 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -134,14 +137,18 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
public CSharpFormattingOptions FormattingOptions {
get { return formattingOptions; }
}
public TextEditorOptions Options {
get { return options; }
}
public void InsertBefore(AstNode node, AstNode insertNode)
{
var startOffset = GetCurrentOffset(new TextLocation(node.StartLocation.Line, 1));
var output = OutputNode (GetIndentLevelAt (startOffset), insertNode);
string text = output.Text;
if (!(insertNode is Expression || insertNode is AstType))
text += eolMarker;
text += Options.EolMarker;
InsertText(startOffset, text);
output.RegisterTrackedSegments(this, startOffset);
}
@ -248,7 +255,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -248,7 +255,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var stringWriter = new StringWriter ();
var formatter = new SegmentTrackingOutputFormatter (stringWriter);
formatter.Indentation = indentLevel;
stringWriter.NewLine = eolMarker;
formatter.IndentationString = Options.TabsToSpaces ? new string (' ', Options.IndentSize) : "\t";
stringWriter.NewLine = Options.EolMarker;
if (startWithNewLine)
formatter.NewLine ();
var visitor = new CSharpOutputVisitor (formatter, formattingOptions);
@ -256,7 +264,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -256,7 +264,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
string text = stringWriter.ToString().TrimEnd();
if (node is FieldDeclaration)
text += eolMarker;
text += Options.EolMarker;
return new NodeOutput(text, formatter.NewSegments);
}

Loading…
Cancel
Save