Let's tackle this piece of code, shall we?
public IEnumerable<CommentNode> ParseComments(VBComponent component)
{
var code = component.CodeModule.Code();
var qualifiedName = new QualifiedModuleName(component.Collection.Parent.Name, component.Name);
var commentBuilder = new StringBuilder();
var continuing = false;
var startLine = 0;
var startColumn = 0;
for (var i = 0; i < code.Length; i++)
{
var line = code[i];
var index = 0;
if (continuing || line.HasComment(out index))
{
startLine = continuing ? startLine : i;
startColumn = continuing ? startColumn : index;
var commentLength = line.Length - index;
continuing = line.EndsWith("_");
if (!continuing)
{
commentBuilder.Append(line.Substring(index, commentLength).TrimStart());
var selection = new Selection(startLine + 1, startColumn + 1, i + 1, line.Length);
var result = new CommentNode(commentBuilder.ToString(), new QualifiedSelection(qualifiedName, selection));
commentBuilder.Clear();
yield return result;
}
else
{
// ignore line continuations in comment text:
commentBuilder.Append(line.Substring(index, commentLength).TrimStart());
}
}
}
}
Alright, first thing I see is some duplication:
DuplicationcommentBuilder.Append(line.Substring(index, commentLength).TrimStart()); is duplicated. Fix:
So lets remove it.
Additionally, there's some duplication here...
continuing herecontinuing is checked three times? between changes. That's a bit of a waste.
EwwHmm, we're still checking it twice... and if (a || b) { if(!a) structures are messy. I wonder if there's something we can do about that?
Invert it, maybe? (if A OR B is true then NOT A implies B)
Now, it will make a new selection instead, which supposedly ends at code.Length + 1. So past EOF. This can be a feature ("Your comment goes past EOF!") or something that will break some asserts.