thomasrussellmurphy / istyle-verilog-formatter

Open source implementation of a Verilog formatter
GNU General Public License v2.0
173 stars 44 forks source link

`--brackets=attach` breaks the code #25

Open MikeWalrus opened 2 years ago

MikeWalrus commented 2 years ago

original file:

module a(
        input b
    );
    always @(*) begin      
        if (b)                            
        begin
        end
        else
        begin
        end
    end
endmodule

after formatting using -a

module a(
        input b
    );
    always @(*) begin
        if (b)begin
        end
        elsebegin
        end
    end
    endmodule

expected

module a(
        input b
    );
    always @(*) begin
        if (b) begin
        end
        else begin
        end
    end
endmodule

This might have something to do with #20 and the member function ASFormatter::appendSpacePad(). When there is no character to peek (i.e.: at the end of the line), appendSpacePad refuses to append a space, which causes the content of the next line to "attach" to the current line without a space in between.

To fix this, the implementation of the newer AStyle might be helpful. They have two separate methods to append a space.

/**
 * append a space to the current formattedline, UNLESS the
 * last character is already a white-space character.
 */
void ASFormatter::appendSpacePad()
{
    int len = formattedLine.length();
    if (len > 0 && !isWhiteSpace(formattedLine[len - 1]))
    {
        formattedLine.append(1, ' ');
        spacePadNum++;
        if (maxCodeLength != string::npos)
        {
            // These compares reduce the frequency of function calls.
            if (isOkToSplitFormattedLine())
                updateFormattedLineSplitPoints(' ');
            if (formattedLine.length() > maxCodeLength)
                testForTimeToSplitFormattedLine();
        }
    }
}

/**
 * append a space to the current formattedline, UNLESS the
 * next character is already a white-space character.
 */
void ASFormatter::appendSpaceAfter()
{
    int len = currentLine.length();
    if (charNum + 1 < len && !isWhiteSpace(currentLine[charNum + 1]))
    {
        formattedLine.append(1, ' ');
        spacePadNum++;
        if (maxCodeLength != string::npos)
        {
            // These compares reduce the frequency of function calls.
            if (isOkToSplitFormattedLine())
                updateFormattedLineSplitPoints(' ');
            if (formattedLine.length() > maxCodeLength)
                testForTimeToSplitFormattedLine();
        }
    }
}
thomasrussellmurphy commented 2 years ago

Pull requests welcome. I have no good understanding of how old aStyle was converted to support Verilog by the original devs. If you have familiarity with these functions from the distant parent project, please see how they substitute into this project.