diff options
author | Lluis Sanchez Gual <lluis@xamarin.com> | 2015-04-20 19:07:51 +0300 |
---|---|---|
committer | Lluis Sanchez Gual <lluis@xamarin.com> | 2015-04-20 19:07:51 +0300 |
commit | 95408b1a41b31b645f3a8c3323ab7d3ca4bff26b (patch) | |
tree | f8f137cddcb9a40499d303be95722fd2c6081bce /main/src/addins/CSharpBinding/MonoDevelop.CSharp.Formatting/CSharpFormatter.cs | |
parent | b70e4e04b4e41b8ec28d7e4b220b401ecf467e7a (diff) | |
parent | de043d330a8bf5855d4d983c804cd36727cb2406 (diff) |
Merge remote-tracking branch 'origin/roslyn' into new-project-model
Diffstat (limited to 'main/src/addins/CSharpBinding/MonoDevelop.CSharp.Formatting/CSharpFormatter.cs')
-rw-r--r-- | main/src/addins/CSharpBinding/MonoDevelop.CSharp.Formatting/CSharpFormatter.cs | 92 |
1 files changed, 25 insertions, 67 deletions
diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Formatting/CSharpFormatter.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Formatting/CSharpFormatter.cs index fd3d5506ed..65120bbf3b 100644 --- a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Formatting/CSharpFormatter.cs +++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Formatting/CSharpFormatter.cs @@ -27,15 +27,20 @@ using System; using System.Collections.Generic; -using Mono.TextEditor; using MonoDevelop.CSharp.Formatting; using MonoDevelop.Ide.Gui.Content; using MonoDevelop.Projects.Policies; using System.Linq; using MonoDevelop.Ide.CodeFormatting; -using ICSharpCode.NRefactory.CSharp; using MonoDevelop.Core; using MonoDevelop.CSharp.Refactoring; +using MonoDevelop.Ide.Editor; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.Text; +using Microsoft.CodeAnalysis.Formatting; +using MonoDevelop.Ide.TypeSystem; +using ICSharpCode.NRefactory6.CSharp; +using MonoDevelop.Ide; namespace MonoDevelop.CSharp.Formatting { @@ -48,100 +53,53 @@ namespace MonoDevelop.CSharp.Formatting public override bool SupportsCorrectingIndent { get { return true; } } public override void CorrectIndenting (PolicyContainer policyParent, IEnumerable<string> mimeTypeChain, - TextEditorData data, int line) + TextEditor data, int line) { - DocumentLine lineSegment = data.Document.GetLine (line); + var lineSegment = data.GetLine (line); if (lineSegment == null) return; try { var policy = policyParent.Get<CSharpFormattingPolicy> (mimeTypeChain); - var tracker = new CSharpIndentEngine (data.Document, data.CreateNRefactoryTextEditorOptions (), policy.CreateOptions ()); + var textpolicy = policyParent.Get<TextStylePolicy> (mimeTypeChain); + var tracker = new CSharpIndentEngine (policy.CreateOptions (textpolicy)); - tracker.Update (lineSegment.Offset); + tracker.Update (IdeApp.Workbench.ActiveDocument.Editor, lineSegment.Offset); for (int i = lineSegment.Offset; i < lineSegment.Offset + lineSegment.Length; i++) { - tracker.Push (data.Document.GetCharAt (i)); + tracker.Push (data.GetCharAt (i)); } - string curIndent = lineSegment.GetIndentation (data.Document); + string curIndent = lineSegment.GetIndentation (data); int nlwsp = curIndent.Length; - if (!tracker.LineBeganInsideMultiLineComment || (nlwsp < lineSegment.LengthIncludingDelimiter && data.Document.GetCharAt (lineSegment.Offset + nlwsp) == '*')) { + if (!tracker.LineBeganInsideMultiLineComment || (nlwsp < lineSegment.LengthIncludingDelimiter && data.GetCharAt (lineSegment.Offset + nlwsp) == '*')) { // Possibly replace the indent string newIndent = tracker.ThisLineIndent; if (newIndent != curIndent) - data.Replace (lineSegment.Offset, nlwsp, newIndent); + data.ReplaceText (lineSegment.Offset, nlwsp, newIndent); } } catch (Exception e) { LoggingService.LogError ("Error while indenting", e); } } - public override void OnTheFlyFormat (MonoDevelop.Ide.Gui.Document doc, int startOffset, int endOffset) + public override void OnTheFlyFormat (TextEditor editor, DocumentContext context, int startOffset, int endOffset) { - OnTheFlyFormatter.Format (doc, startOffset, endOffset); + OnTheFlyFormatter.Format (editor, context, startOffset, endOffset); } public static string FormatText (CSharpFormattingPolicy policy, TextStylePolicy textPolicy, string mimeType, string input, int startOffset, int endOffset) { - var data = new TextEditorData (); - data.Document.SuppressHighlightUpdate = true; - data.Document.MimeType = mimeType; - data.Document.FileName = "toformat.cs"; - if (textPolicy != null) { - data.Options.TabsToSpaces = textPolicy.TabsToSpaces; - data.Options.TabSize = textPolicy.TabWidth; - data.Options.IndentationSize = textPolicy.IndentWidth; - data.Options.IndentStyle = textPolicy.RemoveTrailingWhitespace ? IndentStyle.Virtual : IndentStyle.Smart; - } - data.Text = input; - - // System.Console.WriteLine ("-----"); - // System.Console.WriteLine (data.Text.Replace (" ", ".").Replace ("\t", "->")); - // System.Console.WriteLine ("-----"); + var inputTree = CSharpSyntaxTree.ParseText (SourceText.From (input)); - var parser = new CSharpParser (); - var compilationUnit = parser.Parse (data); - bool hadErrors = parser.HasErrors; + var root = inputTree.GetRoot (); + var doc = Formatter.Format (root, new TextSpan (startOffset, endOffset - startOffset), TypeSystemService.Workspace, policy.CreateOptions (textPolicy)); + var result = doc.ToFullString (); + if (textPolicy.GetEolMarker () != "\r\n") + result = result.Replace ("\r", ""); - if (hadErrors) { - // foreach (var e in parser.ErrorReportPrinter.Errors) - // Console.WriteLine (e.Message); - return input.Substring (startOffset, Math.Max (0, Math.Min (endOffset, input.Length) - startOffset)); - } - - var originalVersion = data.Document.Version; - - var textEditorOptions = data.CreateNRefactoryTextEditorOptions (); - var formattingVisitor = new ICSharpCode.NRefactory.CSharp.CSharpFormatter ( - policy.CreateOptions (), - textEditorOptions - ) { - FormattingMode = FormattingMode.Intrusive - }; - - var changes = formattingVisitor.AnalyzeFormatting (data.Document, compilationUnit); - try { - changes.ApplyChanges (startOffset, endOffset - startOffset); - } catch (Exception e) { - LoggingService.LogError ("Error in code formatter", e); - return input.Substring (startOffset, Math.Max (0, Math.Min (endOffset, input.Length) - startOffset)); - } - - // check if the formatter has produced errors - parser = new CSharpParser (); - parser.Parse (data); - if (parser.HasErrors) { - LoggingService.LogError ("C# formatter produced source code errors. See console for output."); - return input.Substring (startOffset, Math.Max (0, Math.Min (endOffset, input.Length) - startOffset)); - } - - var currentVersion = data.Document.Version; - - string result = data.GetTextBetween (startOffset, originalVersion.MoveOffsetTo (currentVersion, endOffset)); - data.Dispose (); - return result; + return result.Substring (startOffset, endOffset + result.Length - input.Length - startOffset); } public override string FormatText (PolicyContainer policyParent, IEnumerable<string> mimeTypeChain, string input, int startOffset, int endOffset) |