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. 24
      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. 11
      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
if (hadLineEnd) { if (hadLineEnd) {
// second line end before getting to a token // second line end before getting to a token
// -> here was a blank line // -> here was a blank line
specialTracker.AddEndOfLine(new Location(Line, Col)); specialTracker.AddEndOfLine(new Location(Col, Line));
} }
HandleLineEnd((char)nextChar); HandleLineEnd((char)nextChar);
hadLineEnd = true; hadLineEnd = true;

24
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) protected void WriteInPreviousLine(string txt, bool forceWriteInPreviousBlock)
{ {
if (txt.Length == 0) return;
bool lastCharacterWasNewLine = LastCharacterIsNewLine; bool lastCharacterWasNewLine = LastCharacterIsNewLine;
if (lastCharacterWasNewLine) { if (lastCharacterWasNewLine) {
if (forceWriteInPreviousBlock == false) { if (forceWriteInPreviousBlock == false) {
Indent(); if (txt != Environment.NewLine) Indent();
text.AppendLine(txt); text.Append(txt);
lineBeforeLastStart = lastLineStart; lineBeforeLastStart = lastLineStart;
lastLineStart = text.Length; lastLineStart = text.Length;
return; return;
@ -130,10 +137,12 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
} }
string lastLine = text.ToString(lastLineStart, text.Length - lastLineStart); string lastLine = text.ToString(lastLineStart, text.Length - lastLineStart);
text.Remove(lastLineStart, text.Length - lastLineStart); text.Remove(lastLineStart, text.Length - lastLineStart);
if (txt != Environment.NewLine) {
if (forceWriteInPreviousBlock) ++indentationLevel; if (forceWriteInPreviousBlock) ++indentationLevel;
Indent(); Indent();
if (forceWriteInPreviousBlock) --indentationLevel; if (forceWriteInPreviousBlock) --indentationLevel;
text.AppendLine(txt); }
text.Append(txt);
lineBeforeLastStart = lastLineStart; lineBeforeLastStart = lastLineStart;
lastLineStart = text.Length; lastLineStart = text.Length;
text.Append(lastLine); text.Append(lastLine);
@ -156,9 +165,14 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
public virtual void PrintPreProcessingDirective(PreProcessingDirective directive, bool forceWriteInPreviousBlock) public virtual void PrintPreProcessingDirective(PreProcessingDirective directive, bool forceWriteInPreviousBlock)
{ {
if (string.IsNullOrEmpty(directive.Arg)) if (string.IsNullOrEmpty(directive.Arg))
WriteInPreviousLine(directive.Cmd, forceWriteInPreviousBlock); WriteLineInPreviousLine(directive.Cmd, forceWriteInPreviousBlock);
else 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); public abstract void PrintToken(int token);

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

@ -118,19 +118,19 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
{ {
switch (comment.CommentType) { switch (comment.CommentType) {
case CommentType.Block: case CommentType.Block:
if (forceWriteInPreviousBlock) { // if (forceWriteInPreviousBlock) {
WriteInPreviousLine("/*" + comment.CommentText + "*/", forceWriteInPreviousBlock); WriteInPreviousLine("/*" + comment.CommentText + "*/", forceWriteInPreviousBlock);
} else { // } else {
PrintText("/*"); // PrintText("/*");
PrintText(comment.CommentText); // PrintText(comment.CommentText);
PrintText("*/"); // PrintText("*/");
} // }
break; break;
case CommentType.Documentation: case CommentType.Documentation:
WriteInPreviousLine("///" + comment.CommentText, forceWriteInPreviousBlock); WriteLineInPreviousLine("///" + comment.CommentText, forceWriteInPreviousBlock);
break; break;
default: default:
WriteInPreviousLine("//" + comment.CommentText, forceWriteInPreviousBlock); WriteLineInPreviousLine("//" + comment.CommentText, forceWriteInPreviousBlock);
break; break;
} }
} }

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

@ -54,5 +54,6 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
void Indent(); void Indent();
void PrintComment(Comment comment, bool forceWriteInPreviousBlock); void PrintComment(Comment comment, bool forceWriteInPreviousBlock);
void PrintPreProcessingDirective(PreProcessingDirective directive, bool forceWriteInPreviousBlock); void PrintPreProcessingDirective(PreProcessingDirective directive, bool forceWriteInPreviousBlock);
void PrintBlankLine(bool forceWriteInPreviousBlock);
} }
} }

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

@ -31,7 +31,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
public object Visit(BlankLine special, object data) public object Visit(BlankLine special, object data)
{ {
formatter.NewLine(); formatter.PrintBlankLine(ForceWriteInPreviousLine);
return data; return data;
} }
@ -93,15 +93,10 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
/// <summary> /// <summary>
/// Writes all specials up to the specified location. /// Writes all specials up to the specified location.
/// </summary> /// </summary>
public void AcceptPoint(Location a) public void AcceptPoint(Location loc)
{ {
while (available) { while (available && enumerator.Current.StartPosition <= loc) {
Location b = enumerator.Current.StartPosition;
if (b.Y < a.Y || (b.Y == a.Y && b.X <= a.X)) {
WriteCurrent(); WriteCurrent();
} else {
break;
}
} }
} }

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

@ -46,13 +46,13 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
{ {
switch (comment.CommentType) { switch (comment.CommentType) {
case CommentType.Block: case CommentType.Block:
WriteInPreviousLine("'" + comment.CommentText.Replace("\n", "\n'"), forceWriteInPreviousLine); WriteLineInPreviousLine("'" + comment.CommentText.Replace("\n", "\n'"), forceWriteInPreviousLine);
break; break;
case CommentType.Documentation: case CommentType.Documentation:
WriteInPreviousLine("'''" + comment.CommentText, forceWriteInPreviousLine); WriteLineInPreviousLine("'''" + comment.CommentText, forceWriteInPreviousLine);
break; break;
default: default:
WriteInPreviousLine("'" + comment.CommentText, forceWriteInPreviousLine); WriteLineInPreviousLine("'" + comment.CommentText, forceWriteInPreviousLine);
break; break;
} }
} }

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

@ -47,5 +47,61 @@ namespace ICSharpCode.NRefactory.Parser
return x <= 0 && y <= 0; 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
[Test] [Test]
public void BlockComment() public void BlockComment()
{ {
System.Diagnostics.Debugger.Break();
TestProgram("/* before class */\n" + TestProgram("/* before class */\n" +
"class A\n" + "class A\n" +
"{\n" + "{\n" +
@ -82,6 +81,23 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
"/* after class */"); "/* 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] [Test]
public void PreProcessing() public void PreProcessing()
{ {

Loading…
Cancel
Save