aboutsummaryrefslogtreecommitdiff
path: root/vim/.config/nvim/lua/lsp/init.lua
blob: 474f649c96658bfd16596ec7f1fd1b7070254f3d (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
local first_hover_code_i = 0
local first_hover_code_line = -1
local first_hover_code_col = -1
local first_hover_code_prev = ''

-- Returns the first code block from the markdown returned for the LSP request
-- textDocument/hover. Multiple lines are joined with a space.
function _G.lsp_first_hover_code()
	--[[ NB: there is a bug that causes statusline to be reloaded continuously;
	  see https://github.com/neovim/neovim/issues/14303. It is not good to do a
	  sync request every time. Instead, keep track of line & col and only
	  update when either has changed. This should be fixed in nvim 0.5, after
	  which the first_hover_code_* variables and this logic can be removed. ]]

	local line, col = unpack(vim.api.nvim_win_get_cursor(0))
	if line == first_hover_code_line and col == first_hover_code_col then
		return first_hover_code_prev
	end
	first_hover_code_line = line
	first_hover_code_col = col

	--[[ To check whether the bug has been fixed:
	  vim.api.nvim_echo({{'request '},{tostring(first_hover_code_i)},{tostring(line)}},false,{})
	  first_hover_code_i = first_hover_code_i + 1
	--]]

	if not vim.lsp.buf.server_ready() then return '' end

	local responses = vim.lsp.buf_request_sync(
		0,
		'textDocument/hover',
		vim.lsp.util.make_position_params(),
		1000
	)

	if not responses then return '' end

	local full_result = ''
	local markdown_started = false

	for _, response in ipairs(responses) do
		if response.result and response.result.contents.value then
			for line in response.result.contents.value:gmatch '[^\n]+' do
				if line:sub(0,3) == '```' then
					if markdown_started then
						first_hover_code_prev = full_result
						return full_result
					else
						markdown_started = true
					end
				else
					full_result = full_result .. ' ' .. line
				end
			end
		end
	end

	first_hover_code_prev = full_result
	return full_result
end

local function on_attach (client, bufnr)
	local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end
	local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end

	buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')

	local opts = {noremap=true, silent=true}

	buf_set_keymap('n', '[d', '<Cmd>lua vim.lsp.diagnostic.goto_prev()<CR>', opts)
	buf_set_keymap('n', ']d', '<Cmd>lua vim.lsp.diagnostic.goto_next()<CR>', opts)
	buf_set_keymap('n', 'K', '<Cmd>lua vim.lsp.buf.hover()<CR>', opts)
	buf_set_keymap('n', '<space>ca', '<Cmd>lua vim.lsp.buf.code_action()<CR>', opts)
	buf_set_keymap('n', '<space>f', '<Cmd>lua vim.lsp.buf.formatting()<CR>', opts)
	buf_set_keymap('n', '<space>t', '<Cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
	buf_set_keymap('n', '<space>d', '<Cmd>lua vim.lsp.diagnostic.show_line_diagnostics {show_header=false}<CR>', opts)
end

local default_config = {
	on_attach = on_attach
}

local servers = {
	hls = {},
	yamlls = {}
}

local nvim_lsp = require('lspconfig')
for server, config in pairs(servers) do
	nvim_lsp[server].setup(vim.tbl_deep_extend('force', default_config, config))
end