segmentio / golines

A golang formatter that fixes long lines
MIT License
937 stars 60 forks source link

It doesn't work for "if with a statement" #139

Open y7ls8i opened 4 months ago

y7ls8i commented 4 months ago
if err := veryLongFunctionWithManyLongArguments(veryLongArgument1, veryLongArgument2, veryLongArgument3, veryLongArgument4, veryLongArgument5); err != nil {
    // ... ... ...
}

should have been broken down into:

if err := veryLongFunctionWithManyLongArguments(
    veryLongArgument1,
    veryLongArgument2,
    veryLongArgument3,
    veryLongArgument4,
    veryLongArgument5,
); err != nil {
    // ... ... ...
}

but it doesn't.

shuaiyy commented 3 months ago

the same case

if err = n.db.Unscoped().Model(&models.User{}).Where("id = ?", id).First(&item).Error; err == nil {
}

but below is worked:

err = n.db.Unscoped().Model(&models.NotebookEnv{}).Where("id = ?", id).First(&item).Error
if err == nil {
}
matthewhughes934 commented 1 month ago

The IfStmt.Init node isn't considered for shortening in the block: https://github.com/segmentio/golines/blob/13c3f0d546260ae1a722ad39e761392f7c52b653/shortener.go#L460, but even if it was the shortening Decoration is on the IfStmt node and not the Init node so even if we did s.formatStmt(st.Init) in that first block linked the hasAnnotation call in formatStmt will return false" https://github.com/segmentio/golines/blob/13c3f0d546260ae1a722ad39e761392f7c52b653/shortener.go#L424

I suspect a fix would require adding something like the force parameter like formatExpr has: https://github.com/segmentio/golines/blob/13c3f0d546260ae1a722ad39e761392f7c52b653/shortener.go#L485