summary refs log tree commit diff
path: root/.vim/lua/lsp.lua
blob: 059579ac993fc1d922a824c45b36bbdb4005d122 (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
local lsp        = require('lspconfig')
local nnoremap   = require('astronauta.keymap').nnoremap
local spinner_lsp= require('spinner.lsp')

local my_attach = function(client, bufnr)
  spinner_lsp.on_attach(client, bufnr)

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

  nnoremap { '<leader>f' , vim.lsp.buf.formatting }
  -- nnoremap { 'g0', vim.lsp.buf.document_symbol }
  nnoremap { 'gR'        , vim.lsp.buf.references }
  nnoremap { 'gW'        , vim.lsp.buf.workspace_symbol }
  nnoremap { 'gD'        , vim.lsp.buf.declaration }
  nnoremap { 'gT'        , vim.lsp.buf.type_definition }
  nnoremap { 'gI'        , vim.lsp.buf.implementation }

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

  augroup {
    lsp_user = {
      {'BufWritePre' , '*'    ,  'lua vim.lsp.buf.formatting_seq_sync(nil, 1000)'},
      {'BufWritePre' , '*.go' ,  'lua vim.lsp.buf.code_action({ only = {"source.organizeImports"} })'},
      {'CursorHold'  , '*'    ,  'lua vim.lsp.diagnostic.show_line_diagnostics()'},
    }
  }
end

local setup = function()
  local caps = vim.lsp.protocol.make_client_capabilities()
  -- enable lsp-based snippets
  caps.textDocument.completion.completionItem.snippetSupport = true
  caps.textDocument.completion.completionItem.resolveSupport = {
    properties = {
      'documentation',
      'detail',
      'additionalTextEdits',
    }
  }

  spinner_lsp.setup()
  spinner_lsp.init_capabilities(caps)

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

  -- 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
  -- }}}

  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,
  --  capabilities = caps,
  -- }

  lsp.gopls.setup {
    on_attach = my_attach,
    capabilities = caps,
    settings = {
      gopls = {
        -- analyses = {
        --  composites = false
        -- },
        codelenses = {
          test = true,
          tidy = true,
        },
        gofumpt = true,
      }
    }
  }

  lsp.sumneko_lua.setup {
    on_attach = my_attach,
    capabilities = caps,
    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,
    capabilities = caps,
    cmd = { 'env', 'CARGO_TARGET_DIR=/tmp', 'rust-analyzer' },
  }

  lsp.zls.setup {
    on_attach = my_attach,
    capabilities = caps,
  }

end

return {
  my_attach = my_attach,
  setup = setup
}