254 lines
9.7 KiB
Lua
254 lines
9.7 KiB
Lua
local tool = require("util.tool")
|
|
local mason = tool.load_module("mason")
|
|
if mason == nil then
|
|
return
|
|
end
|
|
|
|
local ghproxy = tool.ghproxy
|
|
local has_cmd = tool.has_cmd
|
|
|
|
local installer = tool.load_module("mason-lspconfig")
|
|
if installer == nil then
|
|
return
|
|
end
|
|
|
|
local lspconfig = tool.load_module("lspconfig")
|
|
if lspconfig == nil then
|
|
return
|
|
end
|
|
|
|
local opts = { noremap=true, silent=true }
|
|
vim.keymap.set('n', '<space>e', vim.diagnostic.open_float, opts)
|
|
vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, opts)
|
|
vim.keymap.set('n', ']d', vim.diagnostic.goto_next, opts)
|
|
vim.keymap.set('n', '<space>q', vim.diagnostic.setloclist, opts)
|
|
|
|
-- 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')
|
|
--
|
|
-- -- Mappings.
|
|
-- -- See `:help vim.lsp.*` for documentation on any of the below functions
|
|
-- local bufopts = { noremap=true, silent=true, buffer=bufnr }
|
|
-- vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, bufopts)
|
|
-- vim.keymap.set('n', 'gd', vim.lsp.buf.definition, bufopts)
|
|
-- vim.keymap.set('n', 'K', vim.lsp.buf.hover, bufopts)
|
|
-- vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, bufopts)
|
|
-- vim.keymap.set('n', '<C-k>', vim.lsp.buf.signature_help, bufopts)
|
|
-- vim.keymap.set('n', '<space>wa', vim.lsp.buf.add_workspace_folder, bufopts)
|
|
-- vim.keymap.set('n', '<space>wr', vim.lsp.buf.remove_workspace_folder, bufopts)
|
|
-- vim.keymap.set('n', '<space>wl', function()
|
|
-- print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
|
|
-- end, bufopts)
|
|
-- vim.keymap.set('n', '<space>D', vim.lsp.buf.type_definition, bufopts)
|
|
-- vim.keymap.set('n', '<space>rn', vim.lsp.buf.rename, bufopts)
|
|
-- vim.keymap.set('n', '<space>ca', vim.lsp.buf.code_action, bufopts)
|
|
-- vim.keymap.set('n', 'gr', vim.lsp.buf.references, bufopts)
|
|
-- vim.keymap.set('n', '<space>f', vim.lsp.buf.formatting, bufopts)
|
|
-- end
|
|
local on_attach = function(_, bufnr)
|
|
-- Deprecated api
|
|
-- vim.api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc")
|
|
vim.api.nvim_set_option_value("omnifunc", "v:lua.vim.lsp.omnifunc", { buf = bufnr })
|
|
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "gD", "<cmd>lua vim.lsp.buf.declaration()<CR>", opts)
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "gd", "<cmd>lua vim.lsp.buf.definition()<CR>", opts)
|
|
-- vim.api.nvim_buf_set_keymap(bufnr, "n", "K", "<cmd>lua vim.lsp.buf.hover()<CR>", opts)
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts)
|
|
-- vim.api.nvim_buf_set_keymap(bufnr, 'n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts)
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>wa", "<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>", opts)
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>wr", "<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>", opts)
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>wl", "<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>", opts)
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>D", "<cmd>lua vim.lsp.buf.type_definition()<CR>", opts)
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>rn", "<cmd>lua vim.lsp.buf.rename()<CR>", opts)
|
|
-- vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>', opts)
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>ca", "<cmd>lua vim.lsp.buf.code_action()<CR>", opts)
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>e", "<cmd>lua vim.diagnostic.open_float()<CR>", opts)
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "[d", "<cmd>lua vim.diagnostic.goto_prev()<CR>", opts)
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "]d", "<cmd>lua vim.diagnostic.goto_next()<CR>", opts)
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>q", "<cmd>lua vim.diagnostic.setloclist()<CR>", opts)
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>so", [[<cmd>lua require('telescope.builtin').lsp_document_symbols()<CR>]], opts)
|
|
vim.cmd([[ command! Format execute 'lua vim.lsp.buf.formatting()' ]])
|
|
|
|
-- lspsaga keymappings
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "gr", "<cmd>Lspsaga rename<cr>", opts)
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "gx", "<cmd>Lspsaga code_action<cr>", opts)
|
|
vim.api.nvim_buf_set_keymap(bufnr, "x", "gx", ":<c-u>Lspsaga range_code_action<cr>", opts)
|
|
-- map(bufnr, "n", "gd", "<cmd>Lspsaga peek_definition<CR>", opts)
|
|
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "gf", "<cmd>Lspsaga lsp_finder<CR>", opts)
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "K", "<cmd>Lspsaga hover_doc<cr>", opts)
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "go", "<cmd>Lspsaga show_line_diagnostics<cr>", opts)
|
|
-- use goto preview instead as below.
|
|
-- map(bufnr, "n", "gp", "<cmd>Lspsaga preview_definition<cr>", opts)
|
|
-- map(bufnr, "n", "gs", "<cmd>Lspsaga signature_help<cr>", opts)
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "gj", "<cmd>Lspsaga diagnostic_jump_next<cr>", opts)
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "gk", "<cmd>Lspsaga diagnostic_jump_prev<cr>", opts)
|
|
-- map(bufnr, "n", "<C-u>", "<cmd>lua require('lspsaga.action').smart_scroll_with_saga(-1, '<c-u>')<cr>", opts)
|
|
-- map(bufnr, "n", "<C-d>", "<cmd>lua require('lspsaga.action').smart_scroll_with_saga(1, '<c-d>')<cr>", opts)
|
|
-- goto preview keymappings
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "gp", "<cmd>lua require('goto-preview').goto_preview_definition()<CR>", opts)
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "gpi", "<cmd>lua require('goto-preview').goto_preview_implementation()<CR>", opts)
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "gP", "<cmd>lua require('goto-preview').close_all_win()<CR>", opts)
|
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "gF", "<cmd>lua require('goto-preview').goto_preview_references()<CR>", opts)
|
|
end
|
|
|
|
-- nvim-cmp supports additional completion capabilities
|
|
local capabilities = vim.lsp.protocol.make_client_capabilities()
|
|
capabilities = require("cmp_nvim_lsp").default_capabilities(capabilities)
|
|
capabilities.textDocument.foldingRange = {
|
|
dynamicRegistration = false,
|
|
lineFoldingOnly = true,
|
|
}
|
|
|
|
local lsp_flags = {
|
|
-- This is the default in Nvim 0.7+
|
|
debounce_text_changes = 150,
|
|
}
|
|
|
|
local has_win32 = vim.fn.has('win32')
|
|
local has_linux = vim.fn.has('linux')
|
|
local has_freebsd = vim.fn.has('freebsd')
|
|
local os_arch = ''
|
|
local is_amd = true
|
|
local is_arm = false
|
|
|
|
if 1 == has_linux or 1 == has_freebsd then
|
|
-- Deprecated api
|
|
-- os_arch = vim.api.nvim_exec([[!uname -m]], true)
|
|
local res = vim.api.nvim_exec2([[!uname -m]], { output = true })
|
|
os_arch = res.output
|
|
-- is_arm = (("aarch64" == os_arch) or ("aarch" == os_arch) or ("arm" == os_arch) or ("arm64" == os_arch))
|
|
-- is_amd = (("amd64" == os_arch) or ("amd" == os_arch))
|
|
if string.find(os_arch, "aarch") then
|
|
is_arm = true
|
|
elseif string.find(os_arch, "arm") then
|
|
is_arm = true
|
|
end
|
|
end
|
|
|
|
-- print(is_arm)
|
|
-- print(is_amd)
|
|
|
|
local servers = {
|
|
'jsonls',
|
|
'marksman',
|
|
'lemminx',
|
|
'lua_ls',
|
|
'yamlls',
|
|
}
|
|
|
|
if has_cmd('gcc') or has_cmd('clang') then
|
|
table.insert(servers, "clangd")
|
|
lspconfig.clangd.setup({
|
|
on_attach = on_attach,
|
|
flags = lsp_flags,
|
|
})
|
|
end
|
|
|
|
if has_cmd("python") or has_cmd("python3") then
|
|
table.insert(servers, "pyright")
|
|
lspconfig.pyright.setup({
|
|
on_attach = on_attach,
|
|
flags = lsp_flags,
|
|
})
|
|
end
|
|
|
|
if has_cmd("go") then
|
|
table.insert(servers, "gopls")
|
|
table.insert(servers, "sqls")
|
|
lspconfig.gopls.setup({
|
|
on_attach = on_attach,
|
|
capabilities = capabilities,
|
|
root_dir = lspconfig.util.root_pattern('.git', 'go.mod'),
|
|
init_options = {
|
|
usePlaceholders = true,
|
|
completeUnimported = true,
|
|
},
|
|
})
|
|
end
|
|
|
|
if has_cmd("rustc") then
|
|
table.insert(servers, "rust_analyzer")
|
|
lspconfig.rust_analyzer.setup({
|
|
on_attach = on_attach,
|
|
flags = lsp_flags,
|
|
settings = {
|
|
["rust-analyzer"] = {}
|
|
}
|
|
})
|
|
end
|
|
|
|
-- vim.g.rustaceanvim = {
|
|
-- server = {
|
|
-- cmd = function()
|
|
-- local mason_registry = require('mason-registry')
|
|
-- if mason_registry.is_installed('rust-analyzer') then
|
|
-- -- This may need to be tweaked depending on the operating system.
|
|
-- local ra = mason_registry.get_package('rust-analyzer')
|
|
-- local ra_filename = ra:get_receipt():get().links.bin['rust-analyzer']
|
|
-- return { ('%s/%s'):format(ra:get_install_path(), ra_filename or 'rust-analyzer') }
|
|
-- else
|
|
-- -- global installation
|
|
-- return { 'rust-analyzer' }
|
|
-- end
|
|
-- end,
|
|
-- },
|
|
-- }
|
|
|
|
mason.setup({
|
|
PATH = "prepend",
|
|
install_root_dir = vim.fn.stdpath('data') .. '/mason',
|
|
github = {
|
|
download_url_template = ghproxy .. "https://github.com/%s/releases/download/%s/%s",
|
|
}
|
|
})
|
|
|
|
installer.setup({
|
|
ensure_installed = servers,
|
|
})
|
|
|
|
-- installer.setup_handlers({
|
|
-- ["rust-analyzer"] = function ()
|
|
--
|
|
-- end
|
|
-- })
|
|
|
|
lspconfig.lua_ls.setup({
|
|
on_attach = function(client, bufnr)
|
|
client.server_capabilities.document_formatting = false
|
|
client.server_capabilities.document_range_formatting = false
|
|
on_attach(client, bufnr)
|
|
end,
|
|
flags = {
|
|
debounce_text_changes = 150,
|
|
},
|
|
capabilities = capabilities,
|
|
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',
|
|
'require'
|
|
},
|
|
},
|
|
workspace = {
|
|
-- Make the server aware of Neovim runtime files
|
|
library = vim.api.nvim_get_runtime_file("", true),
|
|
},
|
|
-- Do not send telemetry data containing a randomized but unique identifier
|
|
telemetry = {
|
|
enable = false,
|
|
},
|
|
},
|
|
},
|
|
})
|