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. 7
      ICSharpCode.NRefactory.CSharp/Refactoring/DocumentScript.cs
  2. 18
      ICSharpCode.NRefactory.CSharp/Refactoring/Script.cs

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

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

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

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

Loading…
Cancel
Save