Skip to main content
added 598 characters in body
Source Link
Pimgd
  • 22.5k
  • 5
  • 68
  • 144

You can even move this section

            for (; i < code.Length; i++)
            {
                line = code[i];
                var commentLength = line.Length - index;
                commentBuilder.Append(line.Substring(index, commentLength).TrimStart());
                if(!line.EndsWith("_"))
                {
                   break;
                }
            }

to a new function:

private int appendComment(code, lineNumber, commentBuilder, commentColumn)

But it takes... 4 arguments. So that's maybe not ideal.

You can even move this section

            for (; i < code.Length; i++)
            {
                line = code[i];
                var commentLength = line.Length - index;
                commentBuilder.Append(line.Substring(index, commentLength).TrimStart());
                if(!line.EndsWith("_"))
                {
                   break;
                }
            }

to a new function:

private int appendComment(code, lineNumber, commentBuilder, commentColumn)

But it takes... 4 arguments. So that's maybe not ideal.

rephrasing to be much nicer
Source Link
Pimgd
  • 22.5k
  • 5
  • 68
  • 144

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.

Duplication. Fix:

Additionally...

continuing here is checked three times?

Eww, if (a || b) { if(!a) structures are messy. I wonder if there's something we can do about that?

Invert it, maybe?

Now, it will make a new selection instead, which supposedly ends at code.Length + 1. So past EOF.

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:

commentBuilder.Append(line.Substring(index, commentLength).TrimStart()); is duplicated.

So lets remove it.

Additionally, there's some duplication here...

continuing is checked three times between changes. That's a bit of a waste.

Hmm, 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.

added 48 characters in body
Source Link
Pimgd
  • 22.5k
  • 5
  • 68
  • 144
    for (var i = 0; i < code.Length; i++)
    {
        var line = code[i];                
        var index = 0;

        if (line.HasComment(out index))
        {
            startLine = i;
            startColumn = index;
            
            //multiline comment forloop...
            for (; i < code.Length; i++)
            {
                line = code[i];
                var commentLength = line.Length - index;
                commentBuilder.Append(line.Substring(index, commentLength).TrimStart());
                if(!line.EndsWith("_"))
                {
                   break;
                }
            }
            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;
        }
    }
    for (var i = 0; i < code.Length; i++)
    {
        var line = code[i];                
        var index = 0;

        if (line.HasComment(out index))
        {
            startLine = i;
            startColumn = index;
            
            for (; i < code.Length; i++)
            {
                line = code[i];
                var commentLength = line.Length - index;
                commentBuilder.Append(line.Substring(index, commentLength).TrimStart());
                if(!line.EndsWith("_"))
                {
                   break;
                }
            }
            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;
        }
    }
    for (var i = 0; i < code.Length; i++)
    {
        var line = code[i];                
        var index = 0;

        if (line.HasComment(out index))
        {
            startLine = i;
            startColumn = index;
            
            //multiline comment forloop...
            for (; i < code.Length; i++)
            {
                line = code[i];
                var commentLength = line.Length - index;
                commentBuilder.Append(line.Substring(index, commentLength).TrimStart());
                if(!line.EndsWith("_"))
                {
                   break;
                }
            }
            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;
        }
    }
woops, forgot a parenthesis
Source Link
Pimgd
  • 22.5k
  • 5
  • 68
  • 144
Loading
there we go
Source Link
Pimgd
  • 22.5k
  • 5
  • 68
  • 144
Loading
grrr
Source Link
Pimgd
  • 22.5k
  • 5
  • 68
  • 144
Loading
added 3033 characters in body
Source Link
Pimgd
  • 22.5k
  • 5
  • 68
  • 144
Loading
Source Link
Pimgd
  • 22.5k
  • 5
  • 68
  • 144
Loading