Skip to content

gofmt adds empty changes to undo tree  #5

@muffin4

Description

@muffin4

Description

Every time gofmt#apply is invoked, another change is added to the undo tree. This is perfectly fine when gofmt actually changes the buffer, but it's unnecessary and inconvenient if the buffer doesn't actually change.

The empty change in the undo tree makes it more effort to navigate the undo tree (for example with u and CTRL-R) and it marks the buffer as modified. This can force users to save the buffer unnecessarily, if the buffer was previously unmodified. This can happen when the function gets invoked in a way other than with the autocmd, like with a mapping.

Suggestion

gofmt#apply could check whether gofmt -l lists its standard input as differing from its formatting. Based on that gofmt#apply could terminate without making any changes to the buffer.

from gofmt --help:

  -l    list files whose formatting differs from gofmt's

The -l option is also supported by goimports.

It might be a good idea to also add an option to enable or disable this behavior, for example if g:gofmt_exe doesn't support the -l flag or if it's undesired for some other reason.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions