-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlsp.lua
173 lines (156 loc) · 5.47 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
--- @class ergou.util.lsp
local M = {}
local Util = require('ergou.util')
function M.get_clients(opts)
local ret = {} ---@type vim.lsp.Client[]
if vim.lsp.get_clients then
ret = vim.lsp.get_clients(opts)
else
---@diagnostic disable-next-line: deprecated
ret = vim.lsp.get_active_clients(opts)
if opts and opts.method then
---@param client vim.lsp.Client
ret = vim.tbl_filter(function(client)
return client.supports_method(opts.method, { bufnr = opts.bufnr })
end, ret)
end
end
return opts and opts.filter and vim.tbl_filter(opts.filter, ret) or ret
end
---@param from string
---@param to string
---@from https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/util/lsp.lua
function M.on_rename(from, to)
local clients = M.get_clients()
for _, client in ipairs(clients) do
if client.supports_method('workspace/willRenameFiles') then
---@diagnostic disable-next-line: invisible
local resp = client.request_sync('workspace/willRenameFiles', {
files = {
{
oldUri = vim.uri_from_fname(from),
newUri = vim.uri_from_fname(to),
},
},
}, 1000, 0)
if resp and resp.result ~= nil then
vim.lsp.util.apply_workspace_edit(resp.result, client.offset_encoding)
end
end
end
end
function M.lsp_autocmd()
vim.api.nvim_create_autocmd('LspAttach', {
group = vim.api.nvim_create_augroup('lsp-attach', { clear = true }),
callback = function(event)
local bufnr = event.buf
local client = vim.lsp.get_client_by_id(event.data.client_id)
if client then
local client_name = client.name
local file_type = vim.bo[bufnr].filetype
if
not (file_type == 'vue' and client_name == 'tsserver')
and client.server_capabilities['documentSymbolProvider']
then
require('nvim-navic').attach(client, bufnr)
end
end
local nmap = function(keys, func, desc)
vim.keymap.set('n', keys, func, { buffer = bufnr, desc = 'LSP: ' .. desc })
end
nmap('<leader>rn', vim.lsp.buf.rename, 'Rename')
nmap('<leader>ca', vim.lsp.buf.code_action, 'Code Action')
nmap('gd', function()
require('telescope.builtin').lsp_definitions({ reuse_win = true })
end, 'Goto Definition')
nmap('gr', require('telescope.builtin').lsp_references, 'Goto References')
nmap('gI', function()
require('telescope.builtin').lsp_implementations({ reuse_win = true })
end, 'Goto Implementation')
nmap('gy', function()
require('telescope.builtin').lsp_type_definitions({ reuse_win = true })
end, 'Goto Type')
nmap('K', vim.lsp.buf.hover, 'Hover Documentation')
nmap('<leader>k', vim.lsp.buf.signature_help, 'Signature Documentation')
-- Lesser used LSP functionality
nmap('gD', vim.lsp.buf.declaration, 'Goto Declaration')
-- Create a command `:Format` local to the LSP buffer
vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_)
vim.lsp.buf.format()
end, { desc = 'Format current buffer with LSP' })
end,
})
end
M.get_servers = function()
local mason_registry = require('mason-registry')
--- @type table<string, lspconfig.Config>
local servers = {
-- clangd = {},
-- gopls = {},
-- pyright = {},
rust_analyzer = {},
tsserver = {
init_options = {
plugins = {},
},
filetypes = { 'typescript', 'javascript', 'javascriptreact', 'typescriptreact', 'vue' },
},
html = { filetypes = { 'html', 'twig', 'hbs' } },
eslint = {
filetypes = {
'typescript',
'javascript',
'javascriptreact',
'typescriptreact',
'vue',
'json',
'jsonc',
'markdown',
},
},
volar = {},
intelephense = {},
marksman = {},
lua_ls = {
settings = {
Lua = {
workspace = { checkThirdParty = false },
telemetry = { enable = false },
window = {
progressBar = false,
},
},
},
},
bashls = { filetypes = { 'sh', 'bash', 'zsh' } },
typos_lsp = {},
tailwindcss = {},
unocss = {},
theme_check = {},
prismals = {},
}
local has_volar, volar = pcall(mason_registry.get_package, 'vue-language-server')
-- If server `volar` and `tsserver` exists, add `@vue/typescript-plugin` to `tsserver`
if servers.volar ~= nil and servers.tsserver ~= nil and has_volar then
local tsserver = servers.tsserver or {} -- Ensure tsserver is initialized
tsserver.init_options = tsserver.init_options or {} -- Ensure init_options is initialized
tsserver.init_options.plugins = tsserver.init_options.plugins or {} -- Ensure plugins is initialized
-- Even for now can use
local vue_ts_plugin_path = volar:get_install_path()
.. '/node_modules/@vue/language-server/node_modules/@vue/typescript-plugin'
-- after volar 2.0.7
-- local vue_ts_plugin_path = mason_registry.get_package('vue-language-server'):get_install_path() .. '/typescript-plugin'
local vue_plugin = {
name = '@vue/typescript-plugin',
-- Maybe a function to get the location of the plugin is better?
-- e.g. pnpm fallback to nvm fallback to default node path
location = vue_ts_plugin_path,
languages = { 'vue' },
}
-- Append the plugin to the `tsserver` server
vim.list_extend(tsserver.init_options.plugins, { vue_plugin })
servers.tsserver = tsserver
end
return servers
end
return M