r/neovim • u/Geo0W lua • 1d ago
Need Help Language-aware code alignment in Neovim?
Hi everyone! I have a use case that I'd like to automate somehow, and I'd like to know if anyone knows how to achieve this.
I'm looking for a Neovim solution that can do the following:
type User struct {
ID string `json:"id,omitempty"`
Status UserStatus `json:"status"`
CreatedAt time.Time `json:"created_at"`
Password UserPassword `json:"-"`
Admin bool `json:"admin"`
}
// ... into this:
type User struct {
ID string `json:"id,omitempty"`
Status UserStatus `json:"status"`
CreatedAt time.Time `json:"created_at"`
Password UserPassword `json:"-"`
Admin bool `json:"admin"`
}Basically, I'm looking for automatic, language-aware alignment, similar to how you can comment or uncomment a block of code in Neovim using visual selection + gc.
I'm not looking for something specific to Go or struct fields, but rather a general-purpose solution.
14
Upvotes
13
u/TheNoeTrevino :wq 23h ago
i know you specifically asked for general purpose solution but `gofmt` would work here if i am not mistaken.
general purpose would probs be mini.align
12
u/stCarolas 1d ago
In old vim there was 'easy-align' plugin - https://github.com/junegunn/vim-easy-align.
But I assume nowadays you can find modern lua replacement like 'mini.align'
7
u/Gangsir 23h ago
I use mini.align for this. https://github.com/nvim-mini/mini.align
6
u/gplusplus314 22h ago
The best solution would be to install formatters for all languages. If you want it to be language aware, that’s already what you’re asking for. You may as well use the canonical solutions for each language, such as gofmt.
2
u/kilkil 19h ago
my general-purpose solution to this is to download the appropriate LSP formatter (in this case either gofmt or just the golang language server — don't recall if the language server comes with formatting built in), and use it via the plugin confirm.nvim.
didn't know about mini.align, TIL. not sure how much of a usecase I have for it personally, as I tend to prefer autoformatting my files most of the time
2
u/linhusp3 18h ago
I think ideally the lsp formatter should be the one handle this, like gofmt
2
u/Anton-Demkin 14h ago
Manual alignment tools are not really meant for this. Use proper automatic formatter to format code language aware. I use `stevearc/conform.nvim` for this- it applies formatting via LSP or installed tools (like gofmt or ruff)
1
u/AutoModerator 1d ago
Please remember to update the post flair to
Need Help|Solvedwhen you got the answer you were looking for.I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/Necessary-Plate1925 10h ago
Not language-aware (don't know what that means anyways), but I wrote a simple 80lines lua function which aligns based on a vim pattern, in your case this would work like <cmd>AlignChar `<cr>
https://pastebin.com/sn0iEgFJ
-1
u/Asleep_Literature_70 set noexpandtab 23h ago
I like a formatter?
Start with lazyvim and go from there.