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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
require('mason').setup()
local lspconfig = require 'lspconfig'
local null_ls = require 'null-ls'
local keymap = vim.keymap
local cmd = vim.cmd
local border = {
{ '🭽', 'FloatBorder' },
{ '▔', 'FloatBorder' },
{ '🭾', 'FloatBorder' },
{ '▕', 'FloatBorder' },
{ '🭿', 'FloatBorder' },
{ '▁', 'FloatBorder' },
{ '🭼', 'FloatBorder' },
{ '▏', 'FloatBorder' },
}
local signs = { Error = " ", Warn = "▲", Info = "", Hint = "⚑" }
for type, icon in pairs(signs) do
local hl = "DiagnosticSign" .. type
vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = hl })
end
vim.diagnostic.config({
underline = false,
signs = true,
virtual_text = false,
virtual_lines = { only_current_line = true },
float = {
show_header = true,
source = 'if_many',
--border = 'rounded',
border = border,
focusable = true,
},
update_in_insert = false, -- default to false
severity_sort = false, -- default to false
})
vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, {
underline = false,
virtual_text = false,
signs = true,
update_in_insert = false,
})
vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, { border = "rounded" })
vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, { border = "rounded" })
-- Use an on_attach function to only map the following keys after the language server attaches to the current buffer
local on_attach = function(client, bufnr)
-- Enable completion triggered by <c-x><c-o>
vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')
local map = function(mode, l, r, opts)
opts = opts or {}
opts.silent = true
opts.noremap = true
opts.buffer = bufnr
keymap.set(mode, l, r, opts)
end
-- Mappings
map("n", "K", "<Cmd>lua vim.lsp.buf.hover()<CR>")
--map("n", "gd", "<Cmd>lua vim.lsp.buf.definition()<CR>")
map("n", "gd", "<cmd>lua require('goto-preview').goto_preview_definition()<CR>")
--map("n", "gi", "<Cmd>lua vim.lsp.buf.implementation()<CR>")
map("n", "gi", "<cmd>lua require('goto-preview').goto_preview_implementation()<CR>")
--map("n", "gr", "<Cmd>lua vim.lsp.buf.references()<CR>")
map("n", "gr", "<cmd>lua require('goto-preview').goto_preview_references()<CR>")
map("n", "gD", "<Cmd>lua vim.lsp.buf.declaration()<CR>") -- most lsp servers don't implement textDocument/Declaration, so gD is useless for now.
map("n", "<leader>k", "<Cmd>lua vim.lsp.buf.signature_help()<CR>")
--map("n", "gt", "<Cmd>lua vim.lsp.buf.type_definition()<CR>")
map("n", "gt", "<cmd>lua require('goto-preview').goto_preview_type_definition()<CR>")
map("n", "gn", "<Cmd>lua vim.lsp.buf.rename()<CR>")
map("n", "ga", "<Cmd>lua vim.lsp.buf.code_action()<CR>")
map("n", "gf", "<Cmd>lua vim.lsp.buf.formatting()<CR>")
map("n", "go", "<Cmd>lua vim.diagnostic.open_float()<CR>")
map("n", "<leader>go", ":call utils#ToggleDiagnosticsOpenFloat()<CR> | :echom ('Toggle Diagnostics Float open/close...')<CR> | :sl! | echo ('')<CR>")
map("n", "[d", "<Cmd>lua vim.diagnostic.goto_prev()<CR>")
map("n", "]d", "<Cmd>lua vim.diagnostic.goto_next()<CR>")
map("n", "gs", "<Cmd>lua vim.lsp.buf.document_symbol()<CR>")
map("n", "gw", "<Cmd>lua vim.lsp.buf.workspace_symbol()<CR>")
map("n", "<leader>wa", "<Cmd>lua vim.lsp.buf.add_workspace_folder()<CR>")
map("n", "<leader>wr", "<Cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>")
map("n", "<leader>wl", function()
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end)
-- TODO: Use the nicer new API for autocommands
cmd 'augroup lsp_aucmds'
if client.server_capabilities.documentHighlightProvider then
cmd 'au CursorHold <buffer> lua vim.lsp.buf.document_highlight()'
cmd 'au CursorMoved <buffer> lua vim.lsp.buf.clear_references()'
end
cmd 'augroup END'
end
-- Toggle diagnostics visibility
vim.g.diagnostics_visible = true
function _G.toggle_diagnostics()
if vim.g.diagnostics_visible then
vim.g.diagnostics_visible = false
vim.diagnostic.disable()
else
vim.g.diagnostics_visible = true
vim.diagnostic.enable()
end
end
-- Open float for diagnostics automatically
vim.cmd([[
augroup OpenFloat
" autocmd CursorHold,CursorHoldI * lua vim.diagnostic.open_float(nil, {focusable = false,})
autocmd CursorHold * lua vim.diagnostic.open_float(nil, {focusable = false,})
augroup END
]])
-- Suppress error messages from lang servers
vim.lsp.set_log_level("debug")
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = require("cmp_nvim_lsp").default_capabilities()
capabilities.textDocument.completion.completionItem.snippetSupport = true
capabilities.offsetEncoding = { "utf-16" }
local function prefer_null_ls_fmt(client)
client.server_capabilities.documentHighlightProvider = false
client.server_capabilities.documentFormattingProvider = false
on_attach(client)
end
local servers = {
asm_lsp = {},
bashls = {},
clangd = {},
cssls = {
filetypes = { 'css', 'scss', 'less', 'sass' },
root_dir = lspconfig.util.root_pattern('package.json', '.git'),
},
-- ghcide = {},
html = {},
jsonls = { prefer_null_ls = true, cmd = { '--stdio' } },
intelephense = {},
julials = {
on_new_config = function(new_config, _)
local julia = vim.fn.expand '~/.julia/environments/nvim-lspconfig/bin/julia'
if lspconfig.util.path.is_file(julia) then
new_config.cmd[1] = julia
end
end,
settings = { julia = { format = { indent = 2 } } },
},
pyright = { settings = { python = { formatting = { provider = 'yapf' }, linting = { pytypeEnabled = true } } } },
rust_analyzer = {
settings = {
['rust-analyzer'] = {
cargo = { allFeatures = true },
checkOnSave = {
command = 'clippy',
extraArgs = { '--no-deps' },
},
},
},
},
lua_ls = ({
on_attach = on_attach,
capabilities = capabilities,
debounce_text_changes = 500,
settings = {
Lua = {
runtime = {
-- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
version = "LuaJIT",
},
diagnostics = {
-- Get the language server to recognize the `vim` global
globals = { "vim" },
},
workspace = {
-- Make the server aware of Neovim runtime files,
maxPreload = 2000,
preloadFileSize = 50000,
},
},
},
}),
tsserver = { capabilities = require("cmp_nvim_lsp").default_capabilities(vim.lsp.protocol.make_client_capabilities()),
on_attach = function(client)
client.server_capabilities.document_formatting = false
client.server_capabilities.document_range_formatting = false
end,
filetypes = {
'javascript',
'javascriptreact',
'javascript.jsx',
'typescript',
'typescriptreact',
'typescript.tsx'
}, },
vimls = {},
}
for server, config in pairs(servers) do
if config.prefer_null_ls then
if config.on_attach then
local old_on_attach = config.on_attach
config.on_attach = function(client, bufnr)
old_on_attach(client, bufnr)
prefer_null_ls_fmt(client)
end
else
config.on_attach = prefer_null_ls_fmt
end
elseif not config.on_attach then
config.on_attach = on_attach
end
lspconfig[server].setup(config)
end
-- null_ls setup
local builtins = null_ls.builtins
local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
null_ls.setup {
sources = {
builtins.diagnostics.chktex,
--null_ls.builtins.code_actions.eslint_d,
--null_ls.builtins.diagnostics.eslint_d,
--null_ls.builtins.formatting.eslint_d,
-- null_ls.builtins.diagnostics.cppcheck,
-- null_ls.builtins.diagnostics.proselint,
-- null_ls.builtins.diagnostics.pylint,
builtins.diagnostics.selene,
builtins.diagnostics.shellcheck,
builtins.diagnostics.teal,
-- null_ls.builtins.diagnostics.vale,
builtins.diagnostics.vint,
builtins.diagnostics.tidy,
builtins.diagnostics.php,
builtins.diagnostics.phpcs,
-- null_ls.builtins.diagnostics.write_good.with { filetypes = { 'markdown', 'tex' } },
builtins.formatting.clang_format,
-- null_ls.builtins.formatting.cmake_format,
builtins.formatting.isort,
builtins.formatting.htmlbeautifier,
-- null_ls.builtins.formatting.prettier,
builtins.formatting.prettier.with({
filetypes = { "javascript", "javascriptreact", "typescript", "typescriptreact", "json", "yaml", "markdown", "html",
"css", "scss", "less", "graphql", "vue", "svelte" },
extra_args = { "--single-quote", "--tab-width 4", "--print-width 200" },
}),
--null_ls.builtins.formatting.prettierd,
builtins.formatting.rustfmt,
builtins.formatting.shfmt,
builtins.formatting.stylua,
builtins.formatting.trim_whitespace,
builtins.formatting.yapf,
-- null_ls.builtins.formatting.black
builtins.code_actions.gitsigns,
-- null_ls.builtins.code_actions.refactoring.with { filetypes = { 'javascript', 'typescript', 'lua', 'python', 'c', 'cpp' } },
},
on_attach = function(client, bufnr)
if client.supports_method("textDocument/formatting") then
vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
vim.api.nvim_create_autocmd("BufWritePre", {
group = augroup,
buffer = bufnr,
callback = function()
vim.lsp.buf.format()
end
})
end
end,
}
|