public DFormattingVisitor(DFormattingOptions policy, IDocumentAdapter document, DModule ast, ITextEditorOptions options = null) { if (policy == null) { throw new ArgumentNullException("policy"); } if (document == null) { throw new ArgumentNullException("document"); } this.ast = ast; this.policy = policy; this.document = document; this.options = options ?? TextEditorOptions.Default; curIndent = new FormattingIndentStack(this.options); }
public static string FormatCode(string code, DModule ast = null, IDocumentAdapter document = null, DFormattingOptions options = null, ITextEditorOptions textStyle = null) { options = options ?? DFormattingOptions.CreateDStandard(); textStyle = textStyle ?? TextEditorOptions.Default; ast = ast ?? DParser.ParseString(code) as DModule; var formattingVisitor = new DFormattingVisitor(options, document ?? new TextDocument{ Text = code }, ast, textStyle); formattingVisitor.WalkThroughAst(); var sb = new StringBuilder(code); formattingVisitor.ApplyChanges((int start, int length, string insertedText) => { sb.Remove(start,length); sb.Insert(start,insertedText); }); return sb.ToString(); }
public static string FormatCode(string code, DModule ast = null, IDocumentAdapter document = null, DFormattingOptions options = null, ITextEditorOptions textStyle = null) { options = options ?? DFormattingOptions.CreateDStandard(); textStyle = textStyle ?? TextEditorOptions.Default; ast = ast ?? DParser.ParseString(code) as DModule; var formattingVisitor = new DFormattingVisitor(options, document ?? new TextDocument { Text = code }, ast, textStyle); formattingVisitor.WalkThroughAst(); var sb = new StringBuilder(code); formattingVisitor.ApplyChanges((int start, int length, string insertedText) => { sb.Remove(start, length); sb.Insert(start, insertedText); }); return(sb.ToString()); }
static void formattingTests() { var policy = new DFormattingOptions(); policy.TypeBlockBraces = BraceStyle.NextLine; policy.MultiVariableDeclPlacement = NewLinePlacement.SameLine; var code = @" class A { this() { } private: int* privPtr = 2134; public { int pubInt; } //SomeDoc void main () in{} out(v){} body{ int a = 123; a++ ; } @safe int[] d = 34; int a=12,b=23,c; void foo(string[] args) {} }"; Console.WriteLine("## Formatting ##"); var ast = D_Parser.Parser.DParser.ParseString(code) as DModule; var sw = new Stopwatch(); sw.Start(); code = Formatter.FormatCode(code, ast, null, policy); sw.Stop(); Console.WriteLine(code); Console.WriteLine("Took {0}ms", sw.Elapsed.TotalMilliseconds); }
public static void Fmt(string code, string targetCode, DFormattingOptions policy) { var formatOutput = Formatter.FormatCode(code, null, new TextDocument{Text = code},policy, TextEditorOptions.Default); Assert.AreEqual(targetCode, formatOutput.Trim()); }
public void FormatDocument(DFormattingOptions policy = null) { policy = policy ?? DFormattingOptions.CreateDStandard(); var formatter = new DFormattingVisitor(policy, new DocAdapter(dEditor.Editor.Document), dEditor.SyntaxTree); formatter.WalkThroughAst(); dEditor.Editor.Document.UndoStack.StartUndoGroup(); try { formatter.ApplyChanges(dEditor.Editor.Document.Replace); } catch(Exception ex) { ErrorLogger.Log(ex, ErrorType.Error, ErrorOrigin.Parser); } dEditor.Editor.Document.UndoStack.EndUndoGroup(); }