rokucommunity / brighterscript-formatter

A code formatter for BrighterScript (and BrightScript)
MIT License
15 stars 5 forks source link

continue for causes formatting to be not indented properly #63

Closed iBicha closed 1 year ago

iBicha commented 1 year ago

Before

function ArrayRemoveAt(array as object, index as integer) as integer
    result = []
    for i = 0 to array.Count() - 1
        if i = index
            ' continue for
        end if
        result.push(array[i])
    end for
    return result
end function

After

function ArrayRemoveAt(array as object, index as integer) as integer
    result = []
    for i = 0 to array.Count() - 1
        if i = index
            continue for
            end if
            result.push(array[i])
        end for
        return result
    end function
TwitchBronBron commented 1 year ago

Ah. Yeah, I think this is because it thinks the for is the start of a for loop. The formatter is not overly smart. :) Yeah, we need to fix this.

iBicha commented 1 year ago

Looking at it, it seems like a matter of adding an exception to "continue for" and "exit for" in https://github.com/rokucommunity/brighterscript-formatter/blob/master/src/formatters/IndentFormatter.ts if I understand the problem correctly

TwitchBronBron commented 1 year ago

Yeah, that looks like the right spot to fix it. Would you be interested in submitting a pull request to fix this?

iBicha commented 1 year ago

It might take me a while since I'm pretty new to brighterscript parser and tokens etc. I'm not sure if this should be hardcoded or added as a token to https://github.com/rokucommunity/brighterscript-formatter/blob/master/src/constants.ts I don't see a ContinueFor in CompositeKeywords so kinda unsure. And interestingly same is for ContinueWhile which not present but also reproduces the issue. Also I don't see a these two composite tokens in TokenKind from bs, so kinda not sure where the best way/place to fix - I'm too new to the system!

iBicha commented 1 year ago

@TwitchBronBron I opened a PR with a pretty naive approach

TwitchBronBron commented 1 year ago

@iBicha this should have been included in the latest vscode release. Can you confirm it's working for you?