g2384 / VHDLFormatter

VHDL formatter web online written in typescript
https://g2384.github.io/VHDLFormatter/
MIT License
52 stars 20 forks source link

If source lines are split in a nested beautify3() call, unrelated lines are split/dropped #43

Closed fenkes-ibm closed 3 years ago

fenkes-ibm commented 3 years ago

Report a bug

If code in a nested beautify3() call ends up splitting a code line in two, the fact that we now have more code lines to look at doesn't ripple back up the call stack, so the code two stack frames up has the wrong idea about where the nested block ends and where it's supposed to continue processing.

This results in lines at the end of the containing block being duplicated, and the output ending prematurely.

I don't see an easy way to fix this, my current idea would be to wrap the notion of "range of lines to consider" in a class that keeps track of its parent ranges and updates everyone's range if lines are added/removed. Instead of returning two line numbers the nested functions would hand sub-ranges down the call chain. Or maybe keep track of the end of the current range by counting from the end of the line array instead of from the beginning.

Input

test: entity work.testing
    generic map( onegeneric => 1,
                 twogeneric => 2
                 )
    port map(
        oneport => open,
        twoport => '1'
        );

testsignal <= '1';
testsignal2 <= '1';

Note how the intentionally ugly example above will cause the onegeneric part to be split out of the generic map line down in top->beautifySemicolonBlock->beautifyPortGenericBlock.

Expected Behavior

test : ENTITY work.testing
    GENERIC MAP(
        onegeneric => 1,
        twogeneric => 2
    )
    PORT MAP(
        oneport => OPEN,
        twoport => '1'
    );

testsignal <= '1';
testsignal2 <= '1';

Actual Behavior

test : ENTITY work.testing
    GENERIC MAP(
        onegeneric => 1,
        twogeneric => 2
    )
    PORT MAP(
        oneport => OPEN,
        twoport => '1'
    );
);

testsignal <= '1';

The top level didn't get the memo about the added line so it will continue processing at the line that the ); just shifted into so will duplicate the );. Also the loop across all lines will terminate early, so the last line of input is dropped.

fenkes-ibm commented 3 years ago

Aaand here's a fix for you :) Let me know if you'd like me to make any further changes.

g2384 commented 3 years ago

Thanks for the PR. It's been merged. This issue can now be closed?

fenkes-ibm commented 3 years ago

Yes, thanks for merging!