Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(go): add gofumpt formatter with conform/none-ls #1683

Merged
merged 3 commits into from
Oct 13, 2023

Conversation

delatorrejuanchi
Copy link
Contributor

@delatorrejuanchi delatorrejuanchi commented Oct 12, 2023

After #1535 and #1549, some formatters (i.e.: gofumpt) were removed from the conform.nvim and none-ls.nvim configurations in extras.lang.go because they were already already handled by the LSP gopls.

With the release of LazyVim 10.0.0, only one formatter can be active at a time so gopls is no longer taking care of running gofumpt when conform or none-ls are active.

This PR adds it back to the conform.nvim and none-ls.nvim configurations so people using any of the two can use gofumpt. This PR also makes sure packages are only installed when strictly necessary: for example, delve is only needed when nvim-dap is available and gomodifytags and impl are not supported by conform since they are code actions, so they are only installed when using none-ls.

delatorrejuanchi added a commit to delatorrejuanchi/dotfiles that referenced this pull request Oct 12, 2023
This workaround can be removed once LazyVim/LazyVim#1683 is merged
@delatorrejuanchi
Copy link
Contributor Author

Also, this is my first time contributing to the project. Please do let me know if in the future you'd like me to first create an issue or similar.

Thanks ❤️

@willnorris
Copy link
Contributor

I'm still catching up myself on the new interaction between conform, none-ls, and the primary/secondary formatters. But would this now enforce the use of gofumpt if none-ls is enabled? That's certainly doesn't seem desirable, since gofumpt is far from standard in the Go community.

@delatorrejuanchi
Copy link
Contributor Author

delatorrejuanchi commented Oct 12, 2023

But would this now enforce the use of gofumpt if none-ls is enabled?

With this PR, gofumpt would be enabled if none-ls or conform are installed. Before 10.0.0 this was the default, since it was managed by gopls.

Of course, if someone does not wish to use gofumpt they could always disable it (possibly in favor of the less restrictive gofmt?)

@willnorris
Copy link
Contributor

I would argue that having gofumpt enabled by default pre-10.0.0 was a mistake. If someone wants to use it, they could always add it, could they not?

@delatorrejuanchi
Copy link
Contributor Author

I would argue that having gofumpt enabled by default pre-10.0.0 was a mistake. If someone wants to use it, they could always add it, could they not?

That's a good argument, I guess it ends up as a matter of preference. I'm happy with closing this if other people agree.

Out of curiosity, do you regularly use a different formatter yourself?

@willnorris
Copy link
Contributor

Yeah, it's always tricky to pick the "right" set of defaults for a project like this. It definitely often comes down to individual preference. The fact that gopls supports gofumpt but has it disabled by default, seems like reasonable behavior to follow.

I just the default goimports behavior.

@folke
Copy link
Collaborator

folke commented Oct 13, 2023

If I understand correctly, the ideal case is golsp being the primary formatter and goimports being a secundary?

If so I can change it like that.

@Jomik
Copy link
Contributor

Jomik commented Oct 13, 2023

@willnorris

...since gofumpt is far from standard in the Go community.

I sort of disagree with this. gopls ships gofumpt, it really does become a standard.
I am aware that it is opt-in, but still, I would argue that it is better to enable it here in LazyVim, as most people would be satisfied by it. It is compatible with gofmt.
Of course it may make sense to add docs on how to use gofmt instead of gofumpt.

@folke It does seem to work for me, but yes gopls should be primary, and goimports secondary. I am wondering if I missed something though 😄
I do have times where goimports times out, and is unable to actually fix my imports, but it is rarely. I also rarely have to manually modify my imports due to that.
My setup should be default: https://github.com/Jomik/dotfiles/blob/e71bdf109896ccb12a336c3212199c4326fe56b6/.config/nvim/lua/plugins/lang/go.lua

@amaanq
Copy link
Contributor

amaanq commented Oct 13, 2023

@willnorris

...since gofumpt is far from standard in the Go community.

I sort of disagree with this. gopls ships gofumpt, it really does become a standard.
I am aware that it is opt-in, but still, I would argue that it is better to enable it here in LazyVim, as most people would be satisfied by it. It is compatible with gofmt.
Of course it may make sense to add docs on how to use gofmt instead of gofumpt.

@folke It does seem to work for me, but yes gopls should be primary, and goimports secondary. I am wondering if I missed something though 😄
I do have times where goimports times out, and is unable to actually fix my imports, but it is rarely. I also rarely have to manually modify my imports due to that.
My setup should be default: https://github.com/Jomik/dotfiles/blob/e71bdf109896ccb12a336c3212199c4326fe56b6/.config/nvim/lua/plugins/lang/go.lua

Yeah it would be nice to have gofumpt as the default, gofumpt got (unnecessarily imo) removed when someone updated the spec a while back

@Jomik
Copy link
Contributor

Jomik commented Oct 13, 2023

@amaanq I don't think gofumpt is removed, it is just handled by gopls, see

@amaanq
Copy link
Contributor

amaanq commented Oct 13, 2023

@amaanq I don't think gofumpt is removed, it is just handled by gopls, see

ooh yeah, forgot about that 😅, I just noticed it being removed here but then saw why - 964dd6c

@folke
Copy link
Collaborator

folke commented Oct 13, 2023

  • Initially we had gofumpt and goimports. That means that gopls was not used for formatting.
  • Both got removed in favor of gopls
  • apparently removing goimports was in error, so that was moved back
  • because of that change, neither gofumpt nor gopls were being used for formatting. (not in 10.0 and also not before that)

So I agree that it's probably best to merge this PR and get back to how it was before.

@folke folke merged commit 385c99d into LazyVim:main Oct 13, 2023
@delatorrejuanchi delatorrejuanchi deleted the extras-lang-go branch October 13, 2023 13:02
@AnhQuanTrl
Copy link

Just want to give my thought on this but imo the current way of handling formatters is less flexible than the old one. The old way allowed us to disable null-ls in favour of lsp in some languages. Go is a prime example of such languages. With the current way, we have no mechanism to do so since the priority field is hard coded in the LazyVim config.

@folke
Copy link
Collaborator

folke commented Oct 14, 2023

That's not true. It's pretty much the same as with null-ls. You can change the conform formatters_by_ft for go and set it to an empty list so that the lsp will be used instead. Thats' the same as it was before

@fredrikaverpil
Copy link
Contributor

In case it helps anyone, here's my conform setup for go where I can remove/extend/replace per file type: https://github.com/fredrikaverpil/dotfiles/blob/3d58a40bf2a9cc13546cf9e1aaaef93750c47c5e/nvim-lazyvim/lua/plugins/lsp.lua#L163-L189

@AnhQuanTrl
Copy link

Thank you sir @folke. This is awesome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants