222 lines
6.1 KiB
Lua
222 lines
6.1 KiB
Lua
local lspconfig = require("lspconfig")
|
|
|
|
local keymap = vim.keymap
|
|
|
|
local capabilities = nil
|
|
if pcall(require, "cmp_nvim_lsp") then
|
|
capabilities = require("cmp_nvim_lsp").default_capabilities()
|
|
end
|
|
|
|
|
|
local servers = {
|
|
gopls = {
|
|
settings = {
|
|
gopls = {
|
|
hints = {
|
|
assignVariableTypes = true,
|
|
compositeLiteralFields = true,
|
|
compositeLiteralTypes = true,
|
|
constantValues = true,
|
|
functionTypeParameters = true,
|
|
parameterNames = true,
|
|
rangeVariableTypes = true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
ruff = {
|
|
init_options = {
|
|
settings = {
|
|
-- Ruff language server settings go here
|
|
logLevel = "debug",
|
|
},
|
|
},
|
|
},
|
|
pyright = {
|
|
settings = {
|
|
pyright = {
|
|
-- Using Ruff's import organizer
|
|
disableOrganizeImports = true,
|
|
},
|
|
python = {
|
|
analysis = {
|
|
-- Ignore all files for analysis to exclusively use Ruff for linting
|
|
-- ignore = { "*" },
|
|
diagnosticMode = "off",
|
|
-- typeCheckingMode = "off",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
lua_ls = {
|
|
capabilities = capabilities,
|
|
settings = {
|
|
Lua = {
|
|
-- make the language server recognize "vim" global
|
|
diagnostics = {
|
|
globals = { "vim" },
|
|
},
|
|
completion = {
|
|
callSnippet = "Replace",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
clangd = {
|
|
filetypes = { "c" },
|
|
cmd = {
|
|
"clangd",
|
|
"--offset-encoding=utf-16",
|
|
},
|
|
},
|
|
}
|
|
|
|
local servers_to_install = vim.tbl_filter(function(key)
|
|
local t = servers[key]
|
|
if type(t) == "table" then
|
|
return not t.manual_install
|
|
else
|
|
return t
|
|
end
|
|
end, vim.tbl_keys(servers))
|
|
|
|
|
|
|
|
require("mason").setup()
|
|
local ensure_installed = {
|
|
-- Normal settings
|
|
"stylua",
|
|
"lua_ls",
|
|
"gofumpt",
|
|
"golines",
|
|
"prettier",
|
|
}
|
|
|
|
local function get_python_version()
|
|
local handle = io.popen("python --version 2>&1") -- Redirect stderr to stdout
|
|
if handle then
|
|
local result = handle:read("*a")
|
|
handle:close()
|
|
-- Check if the output contains a version number
|
|
local version = result:match("Python (%d+%.%d+%.%d+)")
|
|
if version then
|
|
return version
|
|
end
|
|
end
|
|
|
|
handle = io.popen("python3 --version 2>&1") -- Redirect stderr to stdout
|
|
if handle then
|
|
local result = handle:read("*a")
|
|
handle:close()
|
|
-- Check if the output contains a version number
|
|
local version = result:match("Python (%d+%.%d+%.%d+)")
|
|
if version then
|
|
return version
|
|
end
|
|
end
|
|
-- TODO: Add some exception handling if python not installed on user machine
|
|
return "Python 0.0.0"
|
|
end
|
|
|
|
-- Now we need to make things work in Astra
|
|
-- Main Astra cockblock - low python version, that prevents installation of
|
|
-- some modules, so in order to prevent mason from whining - disable some
|
|
-- autoinstall modules depending on currently installed python version
|
|
-- NOTE: You must install removed dependencies manually
|
|
local python_version = get_python_version()
|
|
if python_version == nil then
|
|
python_version = "Python 3.13"
|
|
end
|
|
local _, minor, _ = python_version:match(".*(%d+)%.(%d+)%.(%d+)")
|
|
if tonumber(minor) < 8 then
|
|
for i, k in pairs(servers_to_install) do
|
|
-- Ruff is not supported for python lower than 3.8
|
|
if k == "ruff" then
|
|
table.remove(servers_to_install, i)
|
|
end
|
|
end
|
|
end
|
|
vim.list_extend(ensure_installed, servers_to_install)
|
|
require("mason-tool-installer").setup { ensure_installed = ensure_installed }
|
|
|
|
for name, config in pairs(servers) do
|
|
if config == true then
|
|
config = {}
|
|
end
|
|
config = vim.tbl_deep_extend("force", {}, {
|
|
capabilities = capabilities,
|
|
}, config)
|
|
|
|
lspconfig[name].setup(config)
|
|
end
|
|
|
|
-- Here it is used the LSP server attaches to file
|
|
vim.api.nvim_create_autocmd("LspAttach", {
|
|
-- Grouping together autocommands.
|
|
-- Here we creating new group and calling it "UserLspConfig"
|
|
group = vim.api.nvim_create_augroup("UserLspConfig", {}),
|
|
-- callback defining logic to execute on the event
|
|
callback = function(ev)
|
|
-- Buffer local mappings.
|
|
-- See `:help vim.lsp.*` for documentation on any of the below functions
|
|
local opts = { buffer = ev.buf, silent = true }
|
|
|
|
-- set keybinds
|
|
-- show definition, references
|
|
opts.desc = "Show LSP references"
|
|
keymap.set("n", "gR", "<cmd>Telescope lsp_references<CR>", opts)
|
|
|
|
-- go to declaration
|
|
opts.desc = "Go to declaration"
|
|
keymap.set("n", "gD", vim.lsp.buf.declaration, opts)
|
|
|
|
-- show lsp definitions
|
|
opts.desc = "Show LSP definitions"
|
|
keymap.set("n", "gd", "<cmd>Telescope lsp_definitions<CR>", opts)
|
|
|
|
-- show lsp implementations
|
|
opts.desc = "Show LSP implementations"
|
|
keymap.set("n", "gi", "<cmd>Telescope lsp_implementations<CR>", opts)
|
|
|
|
-- show lsp type definitions
|
|
opts.desc = "Show LSP type definitions"
|
|
keymap.set("n", "gt", "<cmd>Telescope lsp_type_definitions<CR>", opts)
|
|
|
|
-- see available code actions, in visual mode will apply to selection
|
|
opts.desc = "See available code actions"
|
|
keymap.set({ "n", "v" }, "<leader>ca", vim.lsp.buf.code_action, opts)
|
|
|
|
-- smart rename
|
|
opts.desc = "Smart rename"
|
|
keymap.set("n", "<leader>rn", vim.lsp.buf.rename, opts)
|
|
|
|
-- show diagnostics for file
|
|
opts.desc = "Show buffer diagnostics"
|
|
keymap.set("n", "<leader>D", "<cmd>Telescope diagnostics bufnr=0<CR>", opts)
|
|
|
|
-- show diagnostics for line
|
|
opts.desc = "Show line diagnostics"
|
|
keymap.set("n", "<leader>d", vim.diagnostic.open_float, opts)
|
|
|
|
-- jump to previous diagnostic in buffer
|
|
opts.desc = "Go to previous diagnostic"
|
|
keymap.set("n", "[d", vim.diagnostic.goto_prev, opts)
|
|
|
|
-- jump to next diagnostic in buffer
|
|
opts.desc = "Go to next diagnostic"
|
|
keymap.set("n", "]d", vim.diagnostic.goto_next, opts)
|
|
|
|
-- show documentation for what is under cursor
|
|
opts.desc = "Show documentation for what is under cursor"
|
|
keymap.set("n", "K", vim.lsp.buf.hover, opts)
|
|
|
|
-- mapping to restart lsp if necessary
|
|
opts.desc = "Restart LSP"
|
|
keymap.set("n", "<leader>rs", ":LspRestart<CR>", opts)
|
|
|
|
opts.desc = "Run formatting on current buffer"
|
|
keymap.set("n", "<space>f", function()
|
|
vim.lsp.buf.format({ async = true })
|
|
end, opts)
|
|
end,
|
|
})
|