summary refs log tree commit diff
path: root/.config/nvim/after/ftplugin/go.lua
blob: ad5c56e7e83bf6bfed5924ad3c79b7edc3a0880d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
vim.api.nvim_create_autocmd({ 'LspAttach' }, {
  pattern = '*.go',
  group = vim.api.nvim_create_augroup('lspattach.go', {}),
  callback = function(args)
    local client = vim.lsp.get_client_by_id(args.data.client_id)
    if not client:supports_method('workspace/executeCommand') then
      return
    end

    vim.keymap.set('n', '<leader>i', function()
      local p = vim
        .system({
          'sh',
          '-c',
          [[go mod graph | awk '{print $2}' | cut -d@ -f1 | sort -u]],
        })
        :wait()
      local opts = vim.split(p.stdout, '\n')
      vim.ui.select(opts, {}, function(choice)
        client:exec_cmd({
          command = 'gopls.add_import',
          arguments = {
            {
              ['ImportPath'] = choice,
              ['URI'] = vim.uri_from_bufnr(0),
            },
          },
        })
      end)
    end, { desc = 'gopls: add import' })
  end,
})