-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsp.lua
235 lines (217 loc) · 9.35 KB
/
lsp.lua
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
return {
{ -- LSP Configuration & Plugins
"neovim/nvim-lspconfig",
event = { "BufReadPost", "BufNewFile", "BufWritePre" },
dependencies = {
-- Automatically install LSPs and related tools to stdpath for neovim
{ "williamboman/mason.nvim", config = true },
"williamboman/mason-lspconfig.nvim",
"WhoIsSethDaniel/mason-tool-installer.nvim",
-- Useful status updates for LSP.
-- NOTE: `opts = {}` is the same as calling `require('fidget').setup({})`
{ "j-hui/fidget.nvim", opts = {} },
},
config = function()
-- This function gets run when an LSP attaches to a particular buffer.
-- That is to say, every time a new file is opened that is associated with
-- an lsp (for example, opening `main.rs` is associated with `rust_analyzer`) this
-- function will be executed to configure the current buffer
vim.api.nvim_create_autocmd("LspAttach", {
group = vim.api.nvim_create_augroup("kickstart-lsp-attach", { clear = true }),
callback = function(event)
-- In this case, we create a function that lets us more easily define mappings specific
-- for LSP related items. It sets the mode, buffer and description for us each time.
local map = function(keys, func, desc)
vim.keymap.set("n", keys, func, { buffer = event.buf, desc = "LSP: " .. desc })
end
-- Jump to the definition of the word under your cursor.
-- This is where a variable was first declared, or where a function is defined, etc.
-- To jump back, press <C-T>.
map("gd", require("telescope.builtin").lsp_definitions, "[G]oto [D]efinition")
-- Find references for the word under your cursor.
map("gR", require("telescope.builtin").lsp_references, "[G]oto [R]eferences")
-- Jump to the implementation of the word under your cursor.
-- Useful when your language has ways of declaring types without an actual implementation.
map("gI", require("telescope.builtin").lsp_implementations, "[G]oto [I]mplementation")
-- Jump to the type of the word under your cursor.
-- Useful when you're not sure what type a variable is and you want to see
-- the definition of its *type*, not where it was *defined*.
map("<leader>D", require("telescope.builtin").lsp_type_definitions, "Type [D]efinition")
-- Fuzzy find all the symbols in your current document.
-- Symbols are things like variables, functions, types, etc.
map("<leader>ds", require("telescope.builtin").lsp_document_symbols, "[D]ocument [S]ymbols")
-- Fuzzy find all the symbols in your current workspace
-- Similar to document symbols, except searches over your whole project.
map("<leader>ws", require("telescope.builtin").lsp_dynamic_workspace_symbols, "[W]orkspace [S]ymbols")
-- Rename the variable under your cursor
-- Most Language Servers support renaming across files, etc.
map("<leader>rn", vim.lsp.buf.rename, "[R]e[n]ame")
-- Execute a code action, usually your cursor needs to be on top of an error
-- or a suggestion from your LSP for this to activate.
map("<leader>ca", vim.lsp.buf.code_action, "[C]ode [A]ction")
-- Opens a popup that displays documentation about the word under your cursor
-- See `:help K` for why this keymap
map("K", vim.lsp.buf.hover, "Hover Documentation")
-- WARN: This is not Goto Definition, this is Goto Declaration.
-- For example, in C this would take you to the header
map("gD", vim.lsp.buf.declaration, "[G]oto [D]eclaration")
local client = vim.lsp.get_client_by_id(event.data.client_id)
-- The following autocommand is used to enable inlay hints in your
-- code, if the language server you are using supports them
--
-- This may be unwanted, since they displace some of your code
if client and client.server_capabilities.inlayHintProvider and vim.lsp.inlay_hint then
map("<leader>uh", function()
print("Inlay Hints Enabled: " .. tostring(not vim.lsp.inlay_hint.is_enabled({})))
vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled({}))
end, "Toggle Inlay [H]ints")
end
end,
})
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = vim.tbl_deep_extend("force", capabilities, require("cmp_nvim_lsp").default_capabilities())
-- Enable the following language servers
-- Feel free to add/remove any LSPs that you want here. They will automatically be installed.
--
-- Add any additional override configuration in the following tables. Available keys are:
-- - cmd (table): Override the default command used to start the server
-- - filetypes (table): Override the default list of associated filetypes for the server
-- - capabilities (table): Override fields in capabilities. Can be used to disable certain LSP features.
-- - settings (table): Override the default settings passed when initializing the server.
-- For example, to see the options for `lua_ls`, you could go to: https://luals.github.io/wiki/settings/
local servers = {
-- clangd = {},
-- gopls = {},
-- pyright = {},
-- rust_analyzer = {},
-- ... etc. See `:help lspconfig-all` for a list of all the pre-configured LSPs
--
-- Some languages (like typescript) have entire language plugins that can be useful:
-- https://github.com/pmizio/typescript-tools.nvim
--
-- But for many setups, the LSP (`tsserver`) will work just fine
-- tsserver = {},
--
lua_ls = {
-- cmd = {...},
-- filetypes = {...},
-- capabilities = {},
settings = {
Lua = {
runtime = { version = "LuaJIT" },
completion = {
callSnippet = "Replace",
},
-- You can toggle below to ignore Lua_LS's noisy `missing-fields` warnings
diagnostics = {
disable = { "missing-fields" },
-- Get the language server to recognize the `vim` global
globals = { "vim", "nvim_bufferline" },
},
},
},
},
tsserver = {
settings = {
completions = {
completeFunctionCalls = true,
},
},
},
yamlls = {
filetypes = { "yaml", "yaml.docker-compose", "yml" },
settings = {
yaml = {
redhat = { telemetry = { enabled = false } },
keyOrdering = false,
},
},
},
emmet_ls = {
filetypes = {
"astro",
"css",
"eruby",
"handlebars",
"html",
"htmldjango",
"javascriptreact",
"less",
"pug",
"sass",
"scss",
"svelte",
"typescriptreact",
"vue",
},
},
eslint = {
settings = {
-- helps eslint find the eslintrc when it's placed in a subfolder instead of the cwd root
workingDirectories = { mode = "auto" },
},
},
}
-- Ensure the servers and tools above are installed
-- To check the current status of installed tools and/or manually install
-- other tools, you can run
-- :Mason
--
-- You can press `g?` for help in this menu
require("mason").setup()
-- You can add other tools here that you want Mason to install
-- for you, so that they are available from within Neovim.
local ensure_installed = vim.tbl_keys(servers or {})
vim.list_extend(ensure_installed, {
"astro-language-server",
"cbfmt",
"emmet-ls",
"eslint-lsp",
"glint",
"html-lsp",
"htmx-lsp",
"json-lsp",
"lemminx",
"lua-language-server",
"markdownlint",
"marksman",
"prettierd",
"shfmt",
"stylua",
"tailwindcss-language-server",
"typescript-language-server",
"xmlformatter",
"yaml-language-server",
})
require("mason-tool-installer").setup({ ensure_installed = ensure_installed })
require("mason-lspconfig").setup({
handlers = {
function(server_name)
local server = servers[server_name] or {}
server.capabilities = vim.tbl_deep_extend("force", {}, capabilities, server.capabilities or {})
require("lspconfig")[server_name].setup(server)
end,
},
})
end,
},
-- Incremental rename
{
"smjonas/inc-rename.nvim",
event = { "BufNewFile", "BufReadPost" },
dependencies = {
"folke/noice.nvim",
},
cmd = "IncRename",
config = function()
---@diagnostic disable-next-line: missing-parameter
require("inc_rename").setup()
require("noice").setup({
presets = { inc_rename = true },
})
vim.keymap.set("n", "<leader>cr", function()
return ":IncRename " .. vim.fn.expand("<cword>")
end, { expr = true, desc = "Incremental Rename" })
end,
},
}