/// <summary> /// Formats the specified part of the document. /// </summary> public static void Format(IDocument document, int offset, int length, CSharpFormattingOptions options) { var syntaxTree = new CSharpParser().Parse(document); var fv = new AstFormattingVisitor(options, document); fv.FormattingRegion = new DomRegion(document.GetLocation(offset), document.GetLocation(offset + length)); syntaxTree.AcceptVisitor(fv); fv.ApplyChanges(offset, length); }
/*public static string ApplyChanges (string text, List<TextReplaceAction> changes) { changes.Sort ((x, y) => y.Offset.CompareTo (x.Offset)); StringBuilder b = new StringBuilder(text); foreach (var change in changes) { //Console.WriteLine ("---- apply:" + change); // Console.WriteLine (adapter.Text); if (change.Offset > b.Length) continue; b.Remove(change.Offset, change.RemovedChars); b.Insert(change.Offset, change.InsertedText); } // Console.WriteLine ("---result:"); // Console.WriteLine (adapter.Text); return b.ToString(); }*/ protected static IDocument GetResult (CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.OnTheFly) { input = NormalizeNewlines (input); var document = new StringBuilderDocument (input); var options = new TextEditorOptions (); options.EolMarker = "\n"; var visitor = new AstFormattingVisitor (policy, document, options); visitor.FormattingMode = mode; var compilationUnit = new CSharpParser ().Parse (document, "test.cs"); compilationUnit.AcceptVisitor (visitor); visitor.ApplyChanges(); return document; }
/*public static string ApplyChanges (string text, List<TextReplaceAction> changes) { changes.Sort ((x, y) => y.Offset.CompareTo (x.Offset)); StringBuilder b = new StringBuilder(text); foreach (var change in changes) { //Console.WriteLine ("---- apply:" + change); // Console.WriteLine (adapter.Text); if (change.Offset > b.Length) continue; b.Remove(change.Offset, change.RemovedChars); b.Insert(change.Offset, change.InsertedText); } // Console.WriteLine ("---result:"); // Console.WriteLine (adapter.Text); return b.ToString(); }*/ protected static IDocument GetResult(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive) { input = NormalizeNewlines(input); var document = new StringBuilderDocument(input); var options = new TextEditorOptions(); options.EolMarker = "\n"; options.WrapLineLength = 80; var visitor = new AstFormattingVisitor (policy, document, options); visitor.FormattingMode = mode; var syntaxTree = new CSharpParser ().Parse (document, "test.cs"); syntaxTree.AcceptVisitor (visitor); visitor.ApplyChanges(); return document; }
public CodeFormatResponse Format(CodeFormatRequest request) { var document = new StringBuilderDocument(request.Buffer); var options = new TextEditorOptions(); options.EolMarker = Environment.NewLine; options.WrapLineLength = 80; options.TabsToSpaces = request.ExpandTab; var policy = FormattingOptionsFactory.CreateAllman(); var visitor = new AstFormattingVisitor(policy, document, options); visitor.FormattingMode = FormattingMode.Intrusive; var syntaxTree = new CSharpParser().Parse(document, request.FileName); syntaxTree.AcceptVisitor(visitor); visitor.ApplyChanges(); return new CodeFormatResponse(document.Text); }
protected static void Continue (CSharpFormattingOptions policy, IDocument document, string expectedOutput, FormattingMode formattingMode = FormattingMode.OnTheFly) { expectedOutput = NormalizeNewlines (expectedOutput); var options = new TextEditorOptions (); options.EolMarker = "\n"; var visitior = new AstFormattingVisitor (policy, document, options); visitior.FormattingMode = formattingMode; var syntaxTree = new CSharpParser ().Parse (document, "test.cs"); syntaxTree.AcceptVisitor (visitior); visitior.ApplyChanges(); string newText = document.Text; if (expectedOutput != newText) { Console.WriteLine (newText); } Assert.AreEqual (expectedOutput, newText); }
public 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 parser = new CSharpParser (); var compilationUnit = parser.Parse (data); bool hadErrors = parser.HasErrors; 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.AstFormattingVisitor (policy.CreateOptions (), data.Document, textEditorOptions) { HadErrors = hadErrors }; compilationUnit.AcceptVisitor (formattingVisitor); try { formattingVisitor.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, ICSharpCode.NRefactory.Editor.AnchorMovementType.Default)); data.Dispose (); return result; }