echasnovski / mini.nvim

Library of 40+ independent Lua modules improving overall Neovim (version 0.8 and higher) experience with minimal effort
MIT License
4.96k stars 184 forks source link

mini.splitjoin - Golang requires an additional `,` on the last argument #1117

Closed svartkanin closed 1 month ago

svartkanin commented 1 month ago

Contributing guidelines

Module(s)

mini.splitjoin

Description

In Golang when a split of arguments is donw it will correctly put each argument on a new line, e.g.

func test(
    a int,
    b int,
    c int
) {

But Golang requires the last argument to be suffixed with a , to compile, so the correct split should result in

func test(
    a int,
    b int,
    c int,
) {

Neovim version

NVIM v0.10.0

Steps to reproduce

  1. mkdir test && cd test && go mod init test.com
  2. touch main.go
  3. 
    package main

func test(a int, b int, c int) { println("test") }

func main() { test(1, 2, 3) }


4. Put cursor inside `test(...)` params and execute the mini.splitjoin 

### Expected behavior

Formatting should be 

func test( a int, b int, c int, ) {


`c int,` needs the trailing `,` at the end for it to compile

### Actual behavior

func test( a int, b int, c int ) {

echasnovski commented 1 month ago

This (not adding trailing comma) is the default behavior. Customizing it for specific languages is left to users. The dedicated customization approach is via hooks with built-in gen_hooks table containing several most common cases. Handling of trailing comma included.

This is use case can be resolved by creating '~/.config/nvim/after/ftplugin/go.lua' with the following content:

if _G.MiniSplitjoin ~= nil then
  local gen_hook = MiniSplitjoin.gen_hook
  local parens = { brackets = { '%b()' } }

  -- Add trailing comma when splitting inside parenthesis
  local add_comma_parens = gen_hook.add_trailing_separator(parens)

  -- Delete trailing comma when joining inside parenthesis
  local del_comma_parens = gen_hook.del_trailing_separator(parens)

  vim.b.minisplitjoin_config = {
    split = { hooks_post = { add_comma_parens } },
    join = { hooks_post = { del_comma_parens } },
  }
end
svartkanin commented 1 month ago

Aahh perfect thanks for the quick reply