summaryrefslogtreecommitdiffstats
path: root/.config/nvim/plugin/50-lsp.lua
blob: 3b3fd4f37ad2ff9a388b88d7430bac4692d88fc8 (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
--
-- LSP client configurations
--

local function inspect_lsp()
	local client = vim.lsp.get_clients()[1]
	if client == nil then
		vim.notify("No LSP client loaded")
		return
	end
	vim.notify(vim.inspect(client.server_capabilities), vim.log.levels.INFO)
end

vim.pack.add({ "https://github.com/neovim/nvim-lspconfig" })

vim.api.nvim_create_user_command("LspInspect", inspect_lsp, { desc = "Inspect LSP client" })

-- Widen hover / signature-help popups so long signatures don't wrap as much.
-- Both go through the single built-in `vim.lsp.util.open_floating_preview`,
-- so we wrap that one function instead of every call site:
--   1. Save the original into a local (the only remaining reference to it).
--   2. Overwrite the table field with our function; Neovim resolves the call
--      through that field, so it now reaches ours transparently.
--   3. Inject `max_width` only when the caller didn't set one (the 120-col
--      Lua/Markdown line-length convention), then forward every argument
--      unchanged to the original and return its results.
-- The float is still clamped to the available screen space, so a signature
-- longer than the popup can fit will still wrap (just less often).
local open_floating_preview = vim.lsp.util.open_floating_preview
---@diagnostic disable-next-line: duplicate-set-field -- intentional monkey-patch
function vim.lsp.util.open_floating_preview(contents, syntax, opts, ...)
	opts = opts or {}
	opts.max_width = opts.max_width or 120
	return open_floating_preview(contents, syntax, opts, ...)
end

-- Enable LSP server capabilities if available when attaching, see `:help lsp-attach`
vim.api.nvim_create_autocmd("LspAttach", {
	desc = "Enable LSP capabilities",
	group = vim.g.dotfiles.augroup,
	callback = function(ev)
		local client = assert(vim.lsp.get_client_by_id(ev.data.client_id))
		if client:supports_method("textDocument/completion") then
			vim.lsp.completion.enable(true, client.id, ev.buf, { autotrigger = true })
		else
			vim.notify("Client " .. client.name .. " does not support completion", vim.log.levels.WARN)
		end
		if client:supports_method("textDocument/inlayHint") then
			vim.lsp.inlay_hint.enable(true, { bufnr = 0 })
		else
			vim.notify("Client " .. client.name .. " does not support inlay hints", vim.log.levels.WARN)
		end
	end,
})

vim.api.nvim_create_user_command("LspHintToggle", function()
	vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled({ bufnr = 0 }), { bufnr = 0 })
end, { desc = "Toggle LSP inlay hints" })

------------------------------------------------------------------------------------------------------------------------
-- Lua
------------------------------------------------------------------------------------------------------------------------

vim.lsp.config("lua_ls", {
	on_init = function(client)
		-- Use `.luarc.json` or `.luarc.jsonc` if available, then exit
		-- See `:help vim.lsp.ClientConfig` for `workspace_folders`
		if client.workspace_folders then
			local path = client.workspace_folders[1].name
			if
				path ~= vim.fn.stdpath("config")
				and (vim.uv.fs_stat(path .. "/.luarc.json") or vim.uv.fs_stat(path .. "/.luarc.jsonc"))
			then
				return
			end
		end
		-- See Lua language server configuration settings at https://luals.github.io/wiki/settings
		client.config.settings.Lua = vim.tbl_deep_extend("force", client.config.settings.Lua, {
			runtime = {
				version = "Lua 5.1",
				path = { "?.lua", "?/init.lua" }, -- See `:h lua-module-load`
			},
			workspace = {
				checkThirdParty = false,
				library = vim.api.nvim_get_runtime_file("", true), -- Load all folders in 'runtimepath'
			},
		})
	end,
})
vim.lsp.enable("lua_ls")

------------------------------------------------------------------------------------------------------------------------
-- Bash
------------------------------------------------------------------------------------------------------------------------

vim.lsp.enable("bashls")

------------------------------------------------------------------------------------------------------------------------
-- Markdown
------------------------------------------------------------------------------------------------------------------------

vim.lsp.config("marksman", { filetypes = { "markdown" } })
vim.lsp.enable("marksman")

------------------------------------------------------------------------------------------------------------------------
-- Vimscript
------------------------------------------------------------------------------------------------------------------------

vim.lsp.enable("vimls")