Browse Source

NRefactory now preserves blank lines.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@1585 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 19 years ago
parent
commit
d4abe4a752
  1. 2
      src/Libraries/NRefactory/Project/Src/Lexer/CSharp/Lexer.cs
  2. 30
      src/Libraries/NRefactory/Project/Src/Output/AbstractOutputFormatter.cs
  3. 16
      src/Libraries/NRefactory/Project/Src/Output/CSharp/OutputFormatter.cs
  4. 1
      src/Libraries/NRefactory/Project/Src/Output/IOutputASTVisitor.cs
  5. 13
      src/Libraries/NRefactory/Project/Src/Output/SpecialNodesInserter.cs
  6. 6
      src/Libraries/NRefactory/Project/Src/Output/VBNet/VBNetOutputFormatter.cs
  7. 56
      src/Libraries/NRefactory/Project/Src/Parser/Location.cs
  8. 18
      src/Libraries/NRefactory/Test/Output/SpecialOutputVisitor.cs

2
src/Libraries/NRefactory/Project/Src/Lexer/CSharp/Lexer.cs

@ -48,7 +48,7 @@ namespace ICSharpCode.NRefactory.Parser.CSharp @@ -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;

30
src/Libraries/NRefactory/Project/Src/Output/AbstractOutputFormatter.cs

@ -115,13 +115,20 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -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 @@ -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 @@ -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);

16
src/Libraries/NRefactory/Project/Src/Output/CSharp/OutputFormatter.cs

@ -118,19 +118,19 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -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;
}
}

1
src/Libraries/NRefactory/Project/Src/Output/IOutputASTVisitor.cs

@ -54,5 +54,6 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -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);
}
}

13
src/Libraries/NRefactory/Project/Src/Output/SpecialNodesInserter.cs

@ -31,7 +31,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -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 @@ -93,15 +93,10 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
/// <summary>
/// Writes all specials up to the specified location.
/// </summary>
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();
}
}

6
src/Libraries/NRefactory/Project/Src/Output/VBNet/VBNetOutputFormatter.cs

@ -46,13 +46,13 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -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;
}
}

56
src/Libraries/NRefactory/Project/Src/Parser/Location.cs

@ -47,5 +47,61 @@ namespace ICSharpCode.NRefactory.Parser @@ -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);
}
}
}

18
src/Libraries/NRefactory/Test/Output/SpecialOutputVisitor.cs

@ -73,7 +73,6 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter @@ -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 @@ -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()
{

Loading…
Cancel
Save