summary refs log tree commit diff
path: root/.vim/lua/plugins/_lsp.lua
blob: bf1cc6deffe1402addefbfe252311692797c3f50 (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
-- lsp config

local lsp      = require('lspconfig')
local util     = require('lspconfig.util')
local u        = require('utils')
local nnoremap = require('astronauta.keymap').nnoremap

local M = {}
local my_attach = function(client)
  -- attach completion
  require 'completion'.on_attach(client)

  -- lsp mappings
  nnoremap { 'K', function() vim.lsp.buf.hover() end }
  nnoremap { '<C-]>', function() vim.lsp.buf.definition() end }
  nnoremap { 'gd', function() vim.lsp.buf.definition() end }
  nnoremap { 'grr',   function() vim.lsp.buf.rename() end }
  nnoremap { '<c-h>', function() vim.lsp.buf.signature_help() end }
  nnoremap { ']d', function() vim.lsp.diagnostic.goto_next() end }
  nnoremap { '[d', function() vim.lsp.diagnostic.goto_prev() end }

  nnoremap { '<leader>f', function() vim.lsp.buf.formatting() end }

  -- nnoremap { 'g0', function() vim.lsp.buf.document_symbol() end }
  nnoremap { 'gR', function() vim.lsp.buf.references() end }
  nnoremap { 'gW', function() vim.lsp.buf.workspace_symbol() end }
  nnoremap { 'gD', function() vim.lsp.buf.declaration() end }
  nnoremap { 'gT', function() vim.lsp.buf.type_definition() end }
  nnoremap { 'gI', function() vim.lsp.buf.implementation() end }

  -- nnoremap <silent> ;s <cmd> lua vim.lsp.buf.signature_help()<CR>
  nnoremap { '<leader>d', function() vim.lsp.diagnostic.show_line_diagnostics() end }

  u.augroup2 {
    lsp_user = {
      {'BufWritePre', '*', 'lua vim.lsp.buf.formatting_sync(nil, 1000)'},
      {'BufWritePre', '*.go', 'lua vim.lsp.buf.code_action({ source = { organizeImports = true } })'},
      {'CursorHold', '*', 'lua vim.lsp.diagnostic.show_line_diagnostics()'},
    }
  }
end

-- handler overwrites {{{
-- configure builtin diagnositcs
vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(
  vim.lsp.diagnostic.on_publish_diagnostics, {
    virtual_text = false,
    signs = true,
    underline = true,
    update_in_insert = true,
  }
)

-- overwrite codeAction handler to not ask for confirmation if only a single
-- action is given
vim.lsp.handlers["textDocument/codeAction"] = function(_, _, actions)
  if actions == nil or vim.tbl_isempty(actions) then
    -- skip printing 'no code actions available'
    return
  end

  local action_cosen
  if #actions == 1 then
    action_chosen = actions[1]
  else
    local option_strings = {"Code Actions:"}
    for i, action in ipairs(actions) do
      local title = action.title:gsub('\r\n', '\\r\\n')
      title = title:gsub('\n', '\\n')
      table.insert(option_strings, string.format("%d. %s", i, title))
    end

    local choice = vim.fn.inputlist(option_strings)
    if choice < 1 or choice > #actions then
      return
    end
    action_chosen = actions[choice]
  end

  if action_chosen.edit or type(action_chosen.command) == "table" then
    if action_chosen.edit then
      vim.lsp.util.apply_workspace_edit(action_chosen.edit)
    end
    if type(action_chosen.command) == "table" then
      vim.lsp.buf.execute_command(action_chosen.command)
    end
  else
    vim.lsp.buf.execute_command(action_chosen)
  end
end
-- }}}

-- configure signs {{{
vim.fn.sign_define("LspDiagnosticsSignError", {text="⚡", numhl="Error"})
vim.fn.sign_define("LspDiagnosticsSignWarning", {text="⯈", numhl="Warning"})
vim.fn.sign_define("LspDiagnosticsSignInformation", {text="≈"})
vim.fn.sign_define("LspDiagnosticsSignHint", {text="🞶"})
-- }}}

-- lsp.ccls.setup{
--  on_attach = my_attach,
-- }

lsp.gopls.setup{
  on_attach = my_attach,
  -- root_dir = util.root_pattern('go.mod', '.git'),
  settings = {
    gopls = {
      -- analyses = {
      --  composites = false
      -- },
      codelenses = {
        test = true,
        tidy = true,
      },
      gofumpt = true,
    }
  }
}

lsp.sumneko_lua.setup{
  on_attach = my_attach;
  cmd = { 'lua-language-server' };
  settings = {
    Lua = {
      runtime = {
        version = 'LuaJIT',
      },
      diagnostics = {
        globals = {'vim'},
      },
      workspace = {
        library = {
          [vim.fn.expand('$VIMRUNTIME/lua')] = true,
          [vim.fn.expand('$VIMRUNTIME/lua/vim/lsp')] = true,
        }
      }
    }
  }
}

lsp.rust_analyzer.setup{
  on_attach = my_attach,
  cmd = { 'env', 'CARGO_TARGET_DIR=/tmp', 'rust-analyzer' },
}

lsp.efm.setup{
  on_attach = my_attach,
  root_dir = util.root_pattern('.git', '.editorconfig'),
  init_options = { documentFormatting = true },
  filetypes = { "sh" },
  settings = {
    -- rootMarkers = {".git/"},
    languages = {
      sh = {
        {
          lintCommand = 'shellcheck -f gcc -x',
          lintSource = 'shellcheck',
          lintFormats = {
            '%f:%l:%c: %trror: %m',
            '%f:%l:%c: %tarning: %m',
            '%f:%l:%c: %tote: %m',
          },
          formatCommand = 'shfmt -ci -s -bn',
          formatStdin = true,
        }
      }
    }
  }
}

lsp.zls.setup{
  on_attach = my_attach
}

return M