diff options
author | Mike Krüger <mkrueger@xamarin.com> | 2011-11-08 19:23:45 +0400 |
---|---|---|
committer | Mike Krüger <mkrueger@xamarin.com> | 2011-11-08 19:23:45 +0400 |
commit | ec035e02db5d9b6e9a2c2275d3145b44264dc546 (patch) | |
tree | b012e0a8d871e9e3e02b38d6789f46bf41c2e653 /main/src/addins/CSharpBinding | |
parent | 86162078a5c47dfaab029f4cb52beb3ee9315f73 (diff) |
[TextEditor] Fixed a formatting issue caused by line ending
conversions.
Diffstat (limited to 'main/src/addins/CSharpBinding')
-rw-r--r-- | main/src/addins/CSharpBinding/MonoDevelop.CSharp.Formatting/CSharpFormatter.cs | 30 | ||||
-rw-r--r-- | main/src/addins/CSharpBinding/MonoDevelop.CSharp.Formatting/CSharpTextEditorIndentation.cs | 12 |
2 files changed, 11 insertions, 31 deletions
diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Formatting/CSharpFormatter.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Formatting/CSharpFormatter.cs index 75c4bb218a..c15761ba97 100644 --- a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Formatting/CSharpFormatter.cs +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Formatting/CSharpFormatter.cs @@ -162,15 +162,13 @@ namespace MonoDevelop.CSharp.Formatting if (textPolicy != null) { data.Options.TabsToSpaces = textPolicy.TabsToSpaces; data.Options.TabSize = textPolicy.TabWidth; - data.Options.DefaultEolMarker = textPolicy.GetEolMarker (); } - data.Options.OverrideDocumentEolMarker = true; data.Text = input; -// System.Console.WriteLine ("-----"); -// System.Console.WriteLine (data.Text.Replace (" ", ".").Replace ("\t", "->")); -// System.Console.WriteLine ("-----"); - + // System.Console.WriteLine ("-----"); + // System.Console.WriteLine (data.Text.Replace (" ", ".").Replace ("\t", "->")); + // System.Console.WriteLine ("-----"); + var parser = new CSharpParser (); var compilationUnit = parser.Parse (data); bool hadErrors = parser.HasErrors; @@ -181,18 +179,18 @@ namespace MonoDevelop.CSharp.Formatting return input.Substring (startOffset, Math.Max (0, Math.Min (endOffset, input.Length) - startOffset)); } var adapter = new TextEditorDataAdapter (data); - var formattingVisitor = new ICSharpCode.NRefactory.CSharp.AstFormattingVisitor (policy.CreateOptions (), adapter, new FormattingActionFactory (data)) { + var factory = new FormattingActionFactory (data); + var formattingVisitor = new ICSharpCode.NRefactory.CSharp.AstFormattingVisitor (policy.CreateOptions (), adapter, factory) { HadErrors = hadErrors }; compilationUnit.AcceptVisitor (formattingVisitor, null); - var changes = new List<ICSharpCode.NRefactory.CSharp.Refactoring.Action> (); - changes.AddRange (formattingVisitor.Changes. Where (c => (startOffset <= c.Offset && c.Offset < endOffset))); - + var endPositionChange = factory.CreateTextReplaceAction (endOffset, 0, null); + changes.Add (endPositionChange); MDRefactoringContext.MdScript.RunActions (changes, null); // check if the formatter has produced errors @@ -200,21 +198,13 @@ namespace MonoDevelop.CSharp.Formatting parser.Parse (data); if (parser.HasErrors) { LoggingService.LogError ("C# formatter produced source code errors. See console for output."); - Console.WriteLine (data.Text); return input.Substring (startOffset, Math.Max (0, Math.Min (endOffset, input.Length) - startOffset)); } - - int end = endOffset; - foreach (TextReplaceAction c in changes) { - end -= c.RemovedChars; - if (c.InsertedText != null) - end += c.InsertedText.Length; - } - /* System.Console.WriteLine ("-----"); +/* System.Console.WriteLine ("-----"); System.Console.WriteLine (data.Text.Replace (" ", "^").Replace ("\t", "->")); System.Console.WriteLine ("-----");*/ - string result = data.GetTextBetween (startOffset, Math.Min (data.Length, end)); + string result = data.GetTextBetween (startOffset, Math.Min (data.Length, endPositionChange.Offset)); data.Dispose (); return result; } diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Formatting/CSharpTextEditorIndentation.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Formatting/CSharpTextEditorIndentation.cs index d623aafb48..f911f5cdb1 100644 --- a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Formatting/CSharpTextEditorIndentation.cs +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Formatting/CSharpTextEditorIndentation.cs @@ -94,7 +94,6 @@ namespace MonoDevelop.CSharp.Formatting if (textEditorData != null) { textEditorData.VirtualSpaceManager = new IndentVirtualSpaceManager (textEditorData, new DocumentStateTracker<CSharpIndentEngine> (new CSharpIndentEngine (policy), textEditorData)); textEditorData.Caret.AllowCaretBehindLineEnd = true; - textEditorData.Paste += TextEditorDataPaste; } InitTracker (); @@ -120,14 +119,7 @@ namespace MonoDevelop.CSharp.Formatting // textEditorData.Document.TextReplaced += TextCut; } } - - void TextEditorDataPaste (int insertionOffset, string text) - { - // if (string.IsNullOrEmpty (text) || text.Length < 2) - // return; - // RunFormatterAt (insertionOffset); - } - + class IndentVirtualSpaceManager : Mono.TextEditor.TextEditorData.IVirtualSpaceManager { Mono.TextEditor.TextEditorData data; @@ -514,7 +506,6 @@ namespace MonoDevelop.CSharp.Formatting void RunFormatter () { if (PropertyService.Get ("OnTheFlyFormatting", false) && textEditorData != null && !(textEditorData.CurrentMode is TextLinkEditMode)) { - textEditorData.Paste -= TextEditorDataPaste; // textEditorData.Document.TextReplaced -= TextCut; ProjectDom dom = ProjectDomService.GetProjectDom (Document.Project); if (dom == null) @@ -525,7 +516,6 @@ namespace MonoDevelop.CSharp.Formatting OnTheFlyFormatter.Format (Document, dom, location, lastCharInserted == '\n'); // textEditorData.Document.TextReplaced += TextCut; - textEditorData.Paste += TextEditorDataPaste; } } |