From d4abe4a75283a91d5dd9652e531ca24535129b95 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sun, 16 Jul 2006 13:25:04 +0000 Subject: [PATCH] NRefactory now preserves blank lines. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@1585 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Project/Src/Lexer/CSharp/Lexer.cs | 2 +- .../Src/Output/AbstractOutputFormatter.cs | 30 +++++++--- .../Src/Output/CSharp/OutputFormatter.cs | 16 +++--- .../Project/Src/Output/IOutputASTVisitor.cs | 1 + .../Src/Output/SpecialNodesInserter.cs | 13 ++--- .../Src/Output/VBNet/VBNetOutputFormatter.cs | 6 +- .../NRefactory/Project/Src/Parser/Location.cs | 56 +++++++++++++++++++ .../Test/Output/SpecialOutputVisitor.cs | 18 +++++- 8 files changed, 112 insertions(+), 30 deletions(-) diff --git a/src/Libraries/NRefactory/Project/Src/Lexer/CSharp/Lexer.cs b/src/Libraries/NRefactory/Project/Src/Lexer/CSharp/Lexer.cs index 5eee057784..988a37d4d2 100644 --- a/src/Libraries/NRefactory/Project/Src/Lexer/CSharp/Lexer.cs +++ b/src/Libraries/NRefactory/Project/Src/Lexer/CSharp/Lexer.cs @@ -48,7 +48,7 @@ namespace ICSharpCode.NRefactory.Parser.CSharp if (hadLineEnd) { // second line end before getting to a token // -> here was a blank line - specialTracker.AddEndOfLine(new Location(Line, Col)); + specialTracker.AddEndOfLine(new Location(Col, Line)); } HandleLineEnd((char)nextChar); hadLineEnd = true; diff --git a/src/Libraries/NRefactory/Project/Src/Output/AbstractOutputFormatter.cs b/src/Libraries/NRefactory/Project/Src/Output/AbstractOutputFormatter.cs index c03a64f6c6..abacf58b19 100644 --- a/src/Libraries/NRefactory/Project/Src/Output/AbstractOutputFormatter.cs +++ b/src/Libraries/NRefactory/Project/Src/Output/AbstractOutputFormatter.cs @@ -115,13 +115,20 @@ namespace ICSharpCode.NRefactory.PrettyPrinter { } + protected void WriteLineInPreviousLine(string txt, bool forceWriteInPreviousBlock) + { + WriteInPreviousLine(txt + Environment.NewLine, forceWriteInPreviousBlock); + } + protected void WriteInPreviousLine(string txt, bool forceWriteInPreviousBlock) { + if (txt.Length == 0) return; + bool lastCharacterWasNewLine = LastCharacterIsNewLine; if (lastCharacterWasNewLine) { if (forceWriteInPreviousBlock == false) { - Indent(); - text.AppendLine(txt); + if (txt != Environment.NewLine) Indent(); + text.Append(txt); lineBeforeLastStart = lastLineStart; lastLineStart = text.Length; return; @@ -130,10 +137,12 @@ namespace ICSharpCode.NRefactory.PrettyPrinter } string lastLine = text.ToString(lastLineStart, text.Length - lastLineStart); text.Remove(lastLineStart, text.Length - lastLineStart); - if (forceWriteInPreviousBlock) ++indentationLevel; - Indent(); - if (forceWriteInPreviousBlock) --indentationLevel; - text.AppendLine(txt); + if (txt != Environment.NewLine) { + if (forceWriteInPreviousBlock) ++indentationLevel; + Indent(); + if (forceWriteInPreviousBlock) --indentationLevel; + } + text.Append(txt); lineBeforeLastStart = lastLineStart; lastLineStart = text.Length; text.Append(lastLine); @@ -156,9 +165,14 @@ namespace ICSharpCode.NRefactory.PrettyPrinter public virtual void PrintPreProcessingDirective(PreProcessingDirective directive, bool forceWriteInPreviousBlock) { if (string.IsNullOrEmpty(directive.Arg)) - WriteInPreviousLine(directive.Cmd, forceWriteInPreviousBlock); + WriteLineInPreviousLine(directive.Cmd, forceWriteInPreviousBlock); else - WriteInPreviousLine(directive.Cmd + " " + directive.Arg, forceWriteInPreviousBlock); + WriteLineInPreviousLine(directive.Cmd + " " + directive.Arg, forceWriteInPreviousBlock); + } + + public void PrintBlankLine(bool forceWriteInPreviousBlock) + { + WriteInPreviousLine(Environment.NewLine, forceWriteInPreviousBlock); } public abstract void PrintToken(int token); diff --git a/src/Libraries/NRefactory/Project/Src/Output/CSharp/OutputFormatter.cs b/src/Libraries/NRefactory/Project/Src/Output/CSharp/OutputFormatter.cs index 1837cc9c51..6db3ac68ba 100644 --- a/src/Libraries/NRefactory/Project/Src/Output/CSharp/OutputFormatter.cs +++ b/src/Libraries/NRefactory/Project/Src/Output/CSharp/OutputFormatter.cs @@ -118,19 +118,19 @@ namespace ICSharpCode.NRefactory.PrettyPrinter { switch (comment.CommentType) { case CommentType.Block: - if (forceWriteInPreviousBlock) { +// if (forceWriteInPreviousBlock) { WriteInPreviousLine("/*" + comment.CommentText + "*/", forceWriteInPreviousBlock); - } else { - PrintText("/*"); - PrintText(comment.CommentText); - PrintText("*/"); - } +// } else { +// PrintText("/*"); +// PrintText(comment.CommentText); +// PrintText("*/"); +// } break; case CommentType.Documentation: - WriteInPreviousLine("///" + comment.CommentText, forceWriteInPreviousBlock); + WriteLineInPreviousLine("///" + comment.CommentText, forceWriteInPreviousBlock); break; default: - WriteInPreviousLine("//" + comment.CommentText, forceWriteInPreviousBlock); + WriteLineInPreviousLine("//" + comment.CommentText, forceWriteInPreviousBlock); break; } } diff --git a/src/Libraries/NRefactory/Project/Src/Output/IOutputASTVisitor.cs b/src/Libraries/NRefactory/Project/Src/Output/IOutputASTVisitor.cs index 7bf454969e..449cbb3d65 100644 --- a/src/Libraries/NRefactory/Project/Src/Output/IOutputASTVisitor.cs +++ b/src/Libraries/NRefactory/Project/Src/Output/IOutputASTVisitor.cs @@ -54,5 +54,6 @@ namespace ICSharpCode.NRefactory.PrettyPrinter void Indent(); void PrintComment(Comment comment, bool forceWriteInPreviousBlock); void PrintPreProcessingDirective(PreProcessingDirective directive, bool forceWriteInPreviousBlock); + void PrintBlankLine(bool forceWriteInPreviousBlock); } } diff --git a/src/Libraries/NRefactory/Project/Src/Output/SpecialNodesInserter.cs b/src/Libraries/NRefactory/Project/Src/Output/SpecialNodesInserter.cs index 9fc5d105ba..8621adc39c 100644 --- a/src/Libraries/NRefactory/Project/Src/Output/SpecialNodesInserter.cs +++ b/src/Libraries/NRefactory/Project/Src/Output/SpecialNodesInserter.cs @@ -31,7 +31,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter public object Visit(BlankLine special, object data) { - formatter.NewLine(); + formatter.PrintBlankLine(ForceWriteInPreviousLine); return data; } @@ -93,15 +93,10 @@ namespace ICSharpCode.NRefactory.PrettyPrinter /// /// Writes all specials up to the specified location. /// - public void AcceptPoint(Location a) + public void AcceptPoint(Location loc) { - while (available) { - Location b = enumerator.Current.StartPosition; - if (b.Y < a.Y || (b.Y == a.Y && b.X <= a.X)) { - WriteCurrent(); - } else { - break; - } + while (available && enumerator.Current.StartPosition <= loc) { + WriteCurrent(); } } diff --git a/src/Libraries/NRefactory/Project/Src/Output/VBNet/VBNetOutputFormatter.cs b/src/Libraries/NRefactory/Project/Src/Output/VBNet/VBNetOutputFormatter.cs index 3a5087cbe2..d1c4f4adb3 100644 --- a/src/Libraries/NRefactory/Project/Src/Output/VBNet/VBNetOutputFormatter.cs +++ b/src/Libraries/NRefactory/Project/Src/Output/VBNet/VBNetOutputFormatter.cs @@ -46,13 +46,13 @@ namespace ICSharpCode.NRefactory.PrettyPrinter { switch (comment.CommentType) { case CommentType.Block: - WriteInPreviousLine("'" + comment.CommentText.Replace("\n", "\n'"), forceWriteInPreviousLine); + WriteLineInPreviousLine("'" + comment.CommentText.Replace("\n", "\n'"), forceWriteInPreviousLine); break; case CommentType.Documentation: - WriteInPreviousLine("'''" + comment.CommentText, forceWriteInPreviousLine); + WriteLineInPreviousLine("'''" + comment.CommentText, forceWriteInPreviousLine); break; default: - WriteInPreviousLine("'" + comment.CommentText, forceWriteInPreviousLine); + WriteLineInPreviousLine("'" + comment.CommentText, forceWriteInPreviousLine); break; } } diff --git a/src/Libraries/NRefactory/Project/Src/Parser/Location.cs b/src/Libraries/NRefactory/Project/Src/Parser/Location.cs index 80e890299b..788f7eb281 100644 --- a/src/Libraries/NRefactory/Project/Src/Parser/Location.cs +++ b/src/Libraries/NRefactory/Project/Src/Parser/Location.cs @@ -47,5 +47,61 @@ namespace ICSharpCode.NRefactory.Parser return x <= 0 && y <= 0; } } + + public override string ToString() + { + return string.Format("(Line {1}, Col {0})", this.x, this.y); + } + + public override int GetHashCode() + { + return unchecked (87 * x.GetHashCode() ^ y.GetHashCode()); + } + + public override bool Equals(object obj) + { + if (!(obj is Location)) return false; + return (Location)obj == this; + } + + public static bool operator ==(Location a, Location b) + { + return a.x == b.x && a.y == b.y; + } + + public static bool operator !=(Location a, Location b) + { + return a.x != b.x || a.y != b.y; + } + + public static bool operator <(Location a, Location b) + { + if (a.y < b.y) + return true; + else if (a.y == b.y) + return a.x < b.x; + else + return false; + } + + public static bool operator >(Location a, Location b) + { + if (a.y > b.y) + return true; + else if (a.y == b.y) + return a.x > b.x; + else + return false; + } + + public static bool operator <=(Location a, Location b) + { + return !(a > b); + } + + public static bool operator >=(Location a, Location b) + { + return !(a < b); + } } } diff --git a/src/Libraries/NRefactory/Test/Output/SpecialOutputVisitor.cs b/src/Libraries/NRefactory/Test/Output/SpecialOutputVisitor.cs index 3c0c1609f6..78ea8d9efc 100644 --- a/src/Libraries/NRefactory/Test/Output/SpecialOutputVisitor.cs +++ b/src/Libraries/NRefactory/Test/Output/SpecialOutputVisitor.cs @@ -73,7 +73,6 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter [Test] public void BlockComment() { - System.Diagnostics.Debugger.Break(); TestProgram("/* before class */\n" + "class A\n" + "{\n" + @@ -82,6 +81,23 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter "/* after class */"); } + [Test] + public void ComplexCommentMix() + { + TestProgram("/* before class */\n" + + "// line comment before\n" + + "/* block comment before */\n" + + "class A\n" + + "{\n" + + "\t/* in class */\n" + + "\t// in class 2" + + "\t/* in class 3 */\n" + + "}\n" + + "/* after class */\n" + + "// after class 2\n" + + "/* after class 3*/"); + } + [Test] public void PreProcessing() {