Browse Source

reduced comment insertion time from O(m log n) to O (m).

newNRvisualizers
Mike Krüger 15 years ago
parent
commit
e8153e0a69
  1. 70
      ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs

70
ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs

@ -2869,40 +2869,56 @@ namespace ICSharpCode.NRefactory.CSharp
CompilerArguments = args; CompilerArguments = args;
} }
public static void InsertComment (AstNode node, Comment comment) static AstNode GetOuterLeft (AstNode node)
{ {
if (node.EndLocation < comment.StartLocation) { var outerLeft = node;
node.AddChild (comment, AstNode.Roles.Comment); while (outerLeft.FirstChild != null)
return; outerLeft = outerLeft.FirstChild;
} return outerLeft;
}
foreach (var child in node.Children) {
if (child.StartLocation < comment.StartLocation && comment.StartLocation < child.EndLocation) { static AstNode NextLeaf (AstNode node)
InsertComment (child, comment); {
return; if (node == null)
} return null;
if (comment.StartLocation < child.StartLocation) { var next = node.NextSibling;
node.InsertChildBefore (child, comment, AstNode.Roles.Comment); if (next == null)
return; return node.Parent;
} return GetOuterLeft(next);
}
node.AddChild (comment, AstNode.Roles.Comment);
} }
static void InsertComments (CompilerCompilationUnit top, ConversionVisitor conversionVisitor) static void InsertComments (CompilerCompilationUnit top, ConversionVisitor conversionVisitor)
{ {
var leaf = GetOuterLeft(conversionVisitor.Unit);
foreach (var special in top.SpecialsBag.Specials) { foreach (var special in top.SpecialsBag.Specials) {
var comment = special as SpecialsBag.Comment; var comment = special as SpecialsBag.Comment;
if (comment == null)
if (comment != null) { continue;
var type = (CommentType)comment.CommentType;
var start = new AstLocation (comment.Line, comment.Col); var type = (CommentType)comment.CommentType;
var end = new AstLocation (comment.EndLine, comment.EndCol); var start = new AstLocation (comment.Line, comment.Col);
var domComment = new Comment (type, start, end); var end = new AstLocation (comment.EndLine, comment.EndCol);
domComment.StartsLine = comment.StartsLine; var domComment = new Comment (type, start, end);
domComment.Content = comment.Content; domComment.StartsLine = comment.StartsLine;
InsertComment (conversionVisitor.Unit, domComment); domComment.Content = comment.Content;
while (true) {
var nextLeaf = NextLeaf(leaf);
// instert comment at the end
if (nextLeaf == null) {
leaf.Parent.AddChild(domComment, AstNode.Roles.Comment);
leaf = domComment;
break;
}
// comment is between 2 nodes
if (leaf.EndLocation <= domComment.StartLocation && domComment.StartLocation <= nextLeaf.StartLocation) {
leaf.Parent.InsertChildAfter(leaf, domComment, AstNode.Roles.Comment);
leaf = domComment;
break;
}
leaf = nextLeaf;
} }
} }
} }

Loading…
Cancel
Save