summaryrefslogtreecommitdiff
path: root/stowables-dotlocal/share/nvim/site/pack/manual/start/nvim-lspconfig-v0.1.6/lua/lspconfig/configs.lua
blob: 1d9d9a62796dbc9d84d4e608a40cdeaf997e5c41 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
local util = require 'lspconfig.util'
local api, validate, lsp, uv, fn = vim.api, vim.validate, vim.lsp, vim.loop, vim.fn
local tbl_deep_extend = vim.tbl_deep_extend

local configs = {}

function configs.__newindex(t, config_name, config_def)
  validate {
    name = { config_name, 's' },
    default_config = { config_def.default_config, 't' },
    on_new_config = { config_def.on_new_config, 'f', true },
    on_attach = { config_def.on_attach, 'f', true },
    commands = { config_def.commands, 't', true },
  }
  if config_def.commands then
    for k, v in pairs(config_def.commands) do
      validate {
        ['command.name'] = { k, 's' },
        ['command.fn'] = { v[1], 'f' },
      }
    end
  else
    config_def.commands = {}
  end

  local M = {}

  local default_config = tbl_deep_extend('keep', config_def.default_config, util.default_config)

  -- Force this part.
  default_config.name = config_name

  function M.setup(user_config)
    local lsp_group = api.nvim_create_augroup('lspconfig', { clear = false })

    validate {
      cmd = {
        user_config.cmd,
        { 'f', 't' },
        true,
      },
      root_dir = { user_config.root_dir, 'f', true },
      filetypes = { user_config.filetype, 't', true },
      on_new_config = { user_config.on_new_config, 'f', true },
      on_attach = { user_config.on_attach, 'f', true },
      commands = { user_config.commands, 't', true },
    }
    if user_config.commands then
      for k, v in pairs(user_config.commands) do
        validate {
          ['command.name'] = { k, 's' },
          ['command.fn'] = { v[1], 'f' },
        }
      end
    end

    local config = tbl_deep_extend('keep', user_config, default_config)

    if util.on_setup then
      pcall(util.on_setup, config, user_config)
    end

    if config.autostart == true then
      local event
      local pattern
      if config.filetypes then
        event = 'FileType'
        pattern = table.concat(config.filetypes, ',')
      else
        event = 'BufReadPost'
        pattern = '*'
      end
      api.nvim_create_autocmd(event, {
        pattern = pattern,
        callback = function(opt)
          M.manager.try_add(opt.buf)
        end,
        group = lsp_group,
        desc = string.format(
          'Checks whether server %s should start a new instance or attach to an existing one.',
          config.name
        ),
      })
    end

    local get_root_dir = config.root_dir

    function M.launch()
      local root_dir
      if get_root_dir then
        local bufnr = api.nvim_get_current_buf()
        local bufname = api.nvim_buf_get_name(bufnr)
        if #bufname == 0 and not config.single_file_support then
          return
        elseif #bufname ~= 0 then
          if not util.bufname_valid(bufname) then
            return
          end
          root_dir = get_root_dir(util.path.sanitize(bufname), bufnr)
        end
      end

      if root_dir then
        api.nvim_create_autocmd('BufReadPost', {
          pattern = fn.fnameescape(root_dir) .. '/*',
          callback = function()
            M.manager.try_add_wrapper()
          end,
          group = lsp_group,
          desc = string.format(
            'Checks whether server %s should attach to a newly opened buffer inside workspace %q.',
            config.name,
            root_dir
          ),
        })
        for _, bufnr in ipairs(api.nvim_list_bufs()) do
          local bufname = api.nvim_buf_get_name(bufnr)
          if util.bufname_valid(bufname) then
            local buf_dir = util.path.sanitize(bufname)
            if buf_dir:sub(1, root_dir:len()) == root_dir then
              M.manager.try_add_wrapper(bufnr)
            end
          end
        end
      elseif config.single_file_support then
        -- This allows on_new_config to use the parent directory of the file
        -- Effectively this is the root from lspconfig's perspective, as we use
        -- this to attach additional files in the same parent folder to the same server.
        -- We just no longer send rootDirectory or workspaceFolders during initialization.
        local bufname = api.nvim_buf_get_name(0)
        if #bufname ~= 0 and not util.bufname_valid(bufname) then
          return
        end
        local pseudo_root = #bufname == 0 and uv.cwd() or util.path.dirname(util.path.sanitize(bufname))
        M.manager.add(pseudo_root, true, api.nvim_get_current_buf())
      end
    end

    -- Used by :LspInfo
    M.get_root_dir = get_root_dir
    M.filetypes = config.filetypes
    M.handlers = config.handlers
    M.cmd = config.cmd
    M.autostart = config.autostart

    -- In the case of a reload, close existing things.
    local reload = false
    if M.manager then
      for _, client in ipairs(M.manager.clients()) do
        client.stop(true)
      end
      reload = true
      M.manager = nil
    end

    local make_config = function(root_dir)
      local new_config = tbl_deep_extend('keep', vim.empty_dict(), config)
      new_config.capabilities = tbl_deep_extend('keep', new_config.capabilities, {
        workspace = {
          configuration = true,
        },
      })

      if config_def.on_new_config then
        pcall(config_def.on_new_config, new_config, root_dir)
      end
      if config.on_new_config then
        pcall(config.on_new_config, new_config, root_dir)
      end

      new_config.on_init = util.add_hook_after(new_config.on_init, function(client, result)
        -- Handle offset encoding by default
        if result.offsetEncoding then
          client.offset_encoding = result.offsetEncoding
        end

        -- Send `settings` to server via workspace/didChangeConfiguration
        function client.workspace_did_change_configuration(settings)
          if not settings then
            return
          end
          if vim.tbl_isempty(settings) then
            settings = { [vim.type_idx] = vim.types.dictionary }
          end
          return client.notify('workspace/didChangeConfiguration', {
            settings = settings,
          })
        end
        if not vim.tbl_isempty(new_config.settings) then
          client.workspace_did_change_configuration(new_config.settings)
        end
      end)

      -- Save the old _on_attach so that we can reference it via the BufEnter.
      new_config._on_attach = new_config.on_attach
      new_config.on_attach = vim.schedule_wrap(function(client, bufnr)
        if bufnr == api.nvim_get_current_buf() then
          M._setup_buffer(client.id, bufnr)
        else
          if api.nvim_buf_is_valid(bufnr) then
            api.nvim_create_autocmd('BufEnter', {
              callback = function()
                M._setup_buffer(client.id, bufnr)
              end,
              group = lsp_group,
              buffer = bufnr,
              once = true,
              desc = 'Reattaches the server with the updated configurations if changed.',
            })
          end
        end
      end)

      new_config.root_dir = root_dir
      new_config.workspace_folders = {
        {
          uri = vim.uri_from_fname(root_dir),
          name = string.format('%s', root_dir),
        },
      }
      return new_config
    end

    local manager = util.server_per_root_dir_manager(function(root_dir)
      return make_config(root_dir)
    end)

    -- Try to attach the buffer `bufnr` to a client using this config, creating
    -- a new client if one doesn't already exist for `bufnr`.
    function manager.try_add(bufnr)
      bufnr = bufnr or api.nvim_get_current_buf()

      if api.nvim_buf_get_option(bufnr, 'buftype') == 'nofile' then
        return
      end

      local root_dir

      local bufname = api.nvim_buf_get_name(bufnr)
      if #bufname == 0 and not config.single_file_support then
        return
      elseif #bufname ~= 0 then
        if not util.bufname_valid(bufname) then
          return
        end
      end
      local buf_path = util.path.sanitize(bufname)

      if get_root_dir then
        root_dir = get_root_dir(buf_path, bufnr)
      end

      if root_dir then
        manager.add(root_dir, false, bufnr)
      elseif config.single_file_support then
        local pseudo_root = #bufname == 0 and uv.cwd() or util.path.dirname(buf_path)
        manager.add(pseudo_root, true, bufnr)
      end
    end

    -- Check that the buffer `bufnr` has a valid filetype according to
    -- `config.filetypes`, then do `manager.try_add(bufnr)`.
    function manager.try_add_wrapper(bufnr)
      bufnr = bufnr or api.nvim_get_current_buf()
      local buf_filetype = api.nvim_buf_get_option(bufnr, 'filetype')
      if config.filetypes then
        for _, filetype in ipairs(config.filetypes) do
          if buf_filetype == filetype then
            manager.try_add(bufnr)
            return
          end
        end
        -- `config.filetypes = nil` means all filetypes are valid.
      else
        manager.try_add(bufnr)
      end
    end

    M.manager = manager
    M.make_config = make_config
    if reload and config.autostart ~= false then
      for _, bufnr in ipairs(api.nvim_list_bufs()) do
        manager.try_add_wrapper(bufnr)
      end
    end
  end

  function M._setup_buffer(client_id, bufnr)
    local client = lsp.get_client_by_id(client_id)
    if not client then
      return
    end
    if client.config._on_attach then
      client.config._on_attach(client, bufnr)
    end
    if client.config.commands and not vim.tbl_isempty(client.config.commands) then
      M.commands = vim.tbl_deep_extend('force', M.commands, client.config.commands)
    end
    if not M.commands_created and not vim.tbl_isempty(M.commands) then
      util.create_module_commands(config_name, M.commands)
    end
  end

  M.commands = config_def.commands
  M.name = config_name
  M.document_config = config_def

  rawset(t, config_name, M)

  return M
end

return setmetatable({}, configs)