summaryrefslogtreecommitdiff
path: root/dot_config/nvim/lua/plugins/nvim-lspconfig.lua
blob: af45a4961e99fbe261dce0f99c671b340820d79b (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
---@diagnostic disable: undefined-global

local lspc = require 'lspconfig'
local lspu = require 'lspconfig.util'

local function plist(...)
	local p = lspu.root_pattern(...)
	return function(f) return p(f) end
end

-- mappings
local wk = require 'which-key'
---@diagnostic disable-next-line: unused-local
local on_attach = function(c, b)
	local lsp = vim.lsp
	local lbf = lsp.buf
	vim.api.nvim_buf_set_option(b, 'omnifunc', 'v:lua.vim.lsp.omnifunc')
	wk.register({
		name = '+lsp',
		D = {lbf.declaration, 'goto declaration'},
		d = {lbf.definition, 'goto definition'},
		k = {lbf.hover, 'hover'},
		K = {lbf.signature_help, 'signature help'},
		i = {lbf.implementation, 'goto implementation'},
		t = {lbf.type_definition, 'goto type definition'},
		r = {lbf.rename, 'rename'},
		c = {lbf.code_action, 'code action'},
		R = {lbf.references, 'list references'},
		w = {
			name = '+workspace',
			a = {lbf.add_workspace_folder, 'add folder'},
			r = {lbf.remove_workspace_folder, 'remove folder'},
			l = {
				function() print(vim.inspect(lbf.list_workspace_folders())) end,
				'list folders'
			},
		},
	}, {
	prefix = '<localleader><localleader>',
		buffer = b,
	})
	wk.register({
		['<leader>F'] = {
			function() vim.lsp.buf.format {async = true} end,
			'run format',
		},
	}, { buffer = b })
end

-- configs
local common = { on_attach = on_attach }
local deno = vim.deepcopy(common)
local tss  = vim.deepcopy(common)
deno.root_dir = plist('deno.json', 'deno.jsonc')
tss.root_dir  = plist('tsconfig.json', 'package.json')

-- servers
local function enable(name, opts)
	opts = opts or common
	local s = lspc[name]
	local c = opts.cmd or s.document_config.default_config.cmd
	if vim.fn.executable(c[1]) ~= 0 then
		s.setup(opts)
	end
end

enable('denols', deno)
enable 'gopls'
enable 'ltex'
enable 'sumneko_lua'
enable('tsserver', tss)