segmentio / golines

A golang formatter that fixes long lines
MIT License
871 stars 54 forks source link

golines format code that does not need to be shortened #123

Open amandeepgautam opened 4 months ago

amandeepgautam commented 4 months ago

I have the following line of code:

       Create(unique_id *UniqueId, name string) (*graphProto.CollectionProto, error)

which is formatted as:

       Create(
               unique_id *UniqueId,
               name string,
       ) (*graphProto.CollectionProto, error)

The initial one is less than 80 lines. It did not need any formatting.

telemachus commented 4 months ago

Can you provide a minimal example file for reproduction, please?

amandeepgautam commented 4 months ago

@telemachus I am running into a different problem now while trying to get a minimal example working:

╰─ cat /tmp/minimal.go
type A interface {
    // This is a small example and it is not being formatted with single line comment.
    Create(unique_id *UniqueId, name string) (*apackagenm.CollectionProto, error)
}

I run the command ~/.vim-go/golines -w -m 80 /tmp/minimal.go and get a segfault.

╰─ ~/.vim-go/golines -w -m 80 /tmp/minimal.go
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x6f34c6]

goroutine 1 [running]:
go/token.(*File).Base(...)
    /usr/local/go/src/go/token/position.go:114
github.com/dave/dst/decorator.(*fileDecorator).fragment.func1(0xc000112f00)
    /home/cohesity/go/pkg/mod/github.com/dave/dst@v0.27.3/decorator/decorator-fragment.go:128 +0x4a6
github.com/dave/dst/decorator.(*fileDecorator).fragment(0xc00003ba48, {0x856f58, 0xc000112f00})
    /home/cohesity/go/pkg/mod/github.com/dave/dst@v0.27.3/decorator/decorator-fragment.go:170 +0xc3
github.com/dave/dst/decorator.(*Decorator).DecorateNode(0xc00014fa40, {0x856f58, 0xc000112f00})
    /home/cohesity/go/pkg/mod/github.com/dave/dst@v0.27.3/decorator/decorator.go:131 +0x11c
github.com/dave/dst/decorator.(*Decorator).DecorateFile(0xc000040740?, 0x0?)
    /home/cohesity/go/pkg/mod/github.com/dave/dst@v0.27.3/decorator/decorator.go:109 +0x1d
github.com/dave/dst/decorator.(*Decorator).ParseFile(0xc00014fa40, {0x0?, 0x7f08123b7108?}, {0x75b980?, 0xc000012480?}, 0xc000012480?)
    /home/cohesity/go/pkg/mod/github.com/dave/dst@v0.27.3/decorator/decorator.go:81 +0x5f
github.com/dave/dst/decorator.(*Decorator).Parse(...)
    /home/cohesity/go/pkg/mod/github.com/dave/dst@v0.27.3/decorator/decorator.go:67
github.com/dave/dst/decorator.Parse({0x75b980, 0xc000012480})
    /home/cohesity/go/pkg/mod/github.com/dave/dst@v0.27.3/decorator/helpers.go:17 +0x165
main.(*Shortener).Shorten(0xc00014f9d0, {0xc000170000?, 0xb8?, 0x200?})
    /home/cohesity/go/pkg/mod/github.com/segmentio/golines@v0.12.2/shortener.go:134 +0x34d
main.processFile(0xc00014f9d0, {0x7ffe09c0874d, 0xf})
    /home/cohesity/go/pkg/mod/github.com/segmentio/golines@v0.12.2/main.go:222 +0x16c
main.run()
    /home/cohesity/go/pkg/mod/github.com/segmentio/golines@v0.12.2/main.go:191 +0x29f
main.main()
    /home/cohesity/go/pkg/mod/github.com/segmentio/golines@v0.12.2/main.go:112 +0x14a

Because of the above segfault, I am not certain how the above example will be formatted, but that is what I have in my file which was formatted incorrectly.

amandeepgautam commented 3 months ago

@telemachus Please find the example:

╰─ cat /tmp/minimal.go
package main

type A interface {
    // This is a small example with 79 char but is formatted when limit is 80 chars.
    Create(uniqueIdWithPaddingToRe int, nameWithPaddingToRe string) (*int, error)
}

func main() {}

Running golines:

╰─ ~/.vim-go/golines -m 80 --shorten-comments /tmp/minimal.go
package main

type A interface {
    // This is a small example with 79 char but is formatted when limit is 80
    // chars.
    Create(
        uniqueIdWithPaddingToRe int,
        nameWithPaddingToRe string,
    ) (*int, error)
}

func main() {}