wsl icon indicating copy to clipboard operation
wsl copied to clipboard

Group `DeclStmt` instead of splitting with empty lines

Open bombsimon opened this issue 1 year ago • 0 comments

Currently the fixer does not do any rewrite other than adding or removing empty lines. Usually this is the only thing we can do but there are cases where we can do more complex rewrites.

The original idea with declarations should never be cuddled was to convert like this:

FromTo
var foo = 1
var notFoo = 2
var b = 3
var aBitLonger = errors.New("an error")
var (
    foo        = 1
    notFoo     = 2
    b          = 3
    aBitLonger = errors.New("an error")
)

Mostly because it aligns the variables which when a lot of them are stacked (can for someone like me) improve readability.

This was implemented fairly easy in the original PR but after digging deeper with edge cases around comments it became very complex to figure out where comments would end up. Working with comments in general is very annoying due to the fact that they're not a part of the ast. I tried to use dst which helps a lot but even dst can behave different depending on the context in regards to which node a comment gets tied to.

I want to keep tracking this because it would be a nice feature and I think a decent compromise would be to merge all DeclStmt that has no comment or is a single line with a comment after the node since that would probably cover a majority of the cases. Something like this:

FromTo
var a = 1
var b = 2 // This is fine
var c = 3

var (
    d = 4
)
var (
    e = 5 // This should be fine
    f = 6
)

// Comment above
var g = 7
var h = 8
// Comment after

// Comment between with spaces

var i = 9
// Between no spaces
var j = 10
var (
    a = 1
    b = 2 // This is fine
    c = 3
)

var (
    d = 4
    e = 5 // This should be fine
    f = 6
)

// Comment above
var g = 7
var h = 8
// Comment after

// Comment between with spaces

var i = 9
// Between no spaces
var j = 10

bombsimon avatar Mar 15 '23 21:03 bombsimon