Better lsp support that checks wheter a needed binary is installed before installing the lsp with mason to avoid errors

This commit is contained in:
2025-08-29 15:49:38 +02:00
parent 36afaec832
commit faad6ae58b
4 changed files with 189 additions and 97 deletions

View File

@@ -3,3 +3,26 @@
You can configure the current semester that you are in for university integrations. You can configure the current semester that you are in for university integrations.
There are certain things required like `make` and `npm` for various plugins. There are certain things required like `make` and `npm` for various plugins.
## Dependencies
To install proper snippets and get the basic lsp the following programs are needed in path.
```
make
curl
gcc
libc
bash
```
Optionally for more lsp servers.
```
go
npm
```
## General
This configuration is based on the lazy plugin manager.

7
lua/conf.lua Normal file
View File

@@ -0,0 +1,7 @@
local config = {}
config.season = "S3"
config.projects = vim.fn.expand("~/projects")
config.uni_dir = vim.fn.expand("~/projects/university/" .. config.season)
return config

View File

@@ -3,8 +3,7 @@
require("utils.functions") require("utils.functions")
require("custom.uni") require("custom.uni")
-- TODO: move this to a config file local conf = require("conf")
local season = "S3"
local links = require("utils.linker") local links = require("utils.linker")
local user = vim.fn.system('whoami'):gsub('\n', '') local user = vim.fn.system('whoami'):gsub('\n', '')
@@ -23,13 +22,6 @@ end
--------------------- KEYMAPS ------------------------- --------------------- KEYMAPS -------------------------
------------------------------------------------------- -------------------------------------------------------
-- Fast quitter
vim.keymap.set('n', '<leader>q', function()
pcall(function()
vim.cmd('wa')
end)
vim.cmd('qa!')
end)
-- Fast window switch -- Fast window switch
vim.keymap.set("n", "<leader>w", "<C-w>w") vim.keymap.set("n", "<leader>w", "<C-w>w")
@@ -46,9 +38,6 @@ vim.keymap.set("n", "<leader>me", ":mes<CR>")
vim.keymap.set("n", "<leader>snt", "<cmd>set nu<CR>") vim.keymap.set("n", "<leader>snt", "<cmd>set nu<CR>")
vim.keymap.set("n", "<leader>snf", "<cmd>set nonu<CR>") vim.keymap.set("n", "<leader>snf", "<cmd>set nonu<CR>")
vim.keymap.set("n", "<leader>nv", function()
select_course_directory()
end, { desc = "Open UniCourse menu" })
vim.keymap.set("n", "n", "nzz", { silent = true }) vim.keymap.set("n", "n", "nzz", { silent = true })
vim.keymap.set("n", "N", "Nzz", { silent = true }) vim.keymap.set("n", "N", "Nzz", { silent = true })
@@ -58,26 +47,15 @@ vim.keymap.set("n", "<C-u>", "<C-u>zz", { silent = true })
vim.keymap.set("n", "<C-d>", "<C-d>zz", { silent = true }) vim.keymap.set("n", "<C-d>", "<C-d>zz", { silent = true })
vim.keymap.set('n', '<leader>a', 'm9ggVG"+y`9') vim.keymap.set('n', '<leader>a', 'm9ggVG"+y`9')
vim.keymap.set('n', '<leader>va', 'ggVG') vim.keymap.set('n', '<leader>va', 'ggVG')
-- Get a ready to use terminal
vim.keymap.set('n', '<leader>tr', ':tabnew<CR>:term<CR>i') vim.keymap.set('n', '<leader>tr', ':tabnew<CR>:term<CR>i')
-- This needs to be refined for quick access to a new file or a recently edited one -- Indent all will be replaced by the formatting of lsp where the lsp is installed
vim.keymap.set('n', '<leader>ov', vim.keymap.set('n', '<leader>ia', 'gg=G<C-o>zz')
function()
local uni_dir = vim.fn.expand("~/projects/university/" .. season) vim.keymap.set('n', '<leader>ya', 'ggVG"+y<C-o>')
vim.keymap.set('n', '<leader>ss', ':wa<CR>')
require('telescope.builtin').find_files {
prompt_title = "Select Vorlesung in " .. season,
cwd = uni_dir,
find_command = {
"eza", "-1", "-D"
},
}
end
)
-- new quick note file -- new quick note file
-- TODO: make this smarter -- TODO: make this smarter
vim.keymap.set("n", "<leader>nn", ":e ~/synced/brainstore/zettelkasten/quick<CR>", { silent = true }) vim.keymap.set("n", "<leader>nn", ":e ~/synced/brainstore/zettelkasten/quick<CR>", { silent = true })
@@ -119,30 +97,9 @@ vim.keymap.set('n', '<leader>oho', ':e ~/configuration/nixos/hosts<CR>')
vim.keymap.set('n', '<leader>os', ':e ~/configuration/nixos/modules/server<CR>') vim.keymap.set('n', '<leader>os', ':e ~/configuration/nixos/modules/server<CR>')
vim.keymap.set('n', '<leader>ot', ':e ~/synced/brainstore/todos/todo.txt<CR>`.zz') vim.keymap.set('n', '<leader>ot', ':e ~/synced/brainstore/todos/todo.txt<CR>`.zz')
vim.keymap.set('n', '<leader>od', ':e ~/synced/brainstore/todos/done.txt<CR>`.zz') vim.keymap.set('n', '<leader>od', ':e ~/synced/brainstore/todos/done.txt<CR>`.zz')
vim.keymap.set('n', '<leader>ou', ':e ~/projects/university/' .. season .. '/input.txt<CR>`.zz') vim.keymap.set('n', '<leader>ou', ':e ~/projects/university/' .. conf.season .. '/input.txt<CR>`.zz')
vim.keymap.set('n', '<leader>oz', ':e ~/.zshrc<CR>`.zz') vim.keymap.set('n', '<leader>oz', ':e ~/.zshrc<CR>`.zz')
vim.keymap.set('n', '<leader>oaa', ':e ~/.common_shell<CR>`.zz') vim.keymap.set('n', '<leader>oaa', ':e ~/.common_shell<CR>`.zz')
vim.keymap.set("n", "<leader>or",
function()
local last_file_path = vim.fn.stdpath('data') .. "/lastfile.txt"
local file = io.open(last_file_path, "r")
if file then
local last_file = file:read("*line")
file:close()
if last_file and vim.fn.filereadable(last_file) == 1 then
vim.cmd("edit " .. last_file)
pcall(function()
vim.cmd('normal! `.') -- Go to the last edit position
vim.cmd('normal! zz') -- Center the cursor on the screen
end)
else
print("Last file does not exist or is not readable")
end
else
print("No last file found")
end
end
, { noremap = true, silent = true })
vim.keymap.set('n', '<leader>ok', open_cal) vim.keymap.set('n', '<leader>ok', open_cal)
@@ -156,12 +113,6 @@ vim.keymap.set("n", "<leader>lc", links.insert_contact_link, { desc = "Link Cont
vim.keymap.set("n", "<leader>ld", links.insert_date_link, { desc = "Link Contact" }) vim.keymap.set("n", "<leader>ld", links.insert_date_link, { desc = "Link Contact" })
-- Indent all will be replaced by the formatting of lsp where the lsp is installed
vim.keymap.set('n', '<leader>ia', 'gg=G<C-o>zz')
vim.keymap.set('n', '<leader>ya', 'ggVG"+y<C-o>')
vim.keymap.set('n', '<leader>ss', ':wa<CR>')
vim.keymap.set('n', '<leader>sw', function() vim.keymap.set('n', '<leader>sw', function()
local word = vim.fn.expand("<cword>") local word = vim.fn.expand("<cword>")
local replacement = vim.fn.input("Replace '" .. word .. "' with: ") local replacement = vim.fn.input("Replace '" .. word .. "' with: ")
@@ -196,26 +147,24 @@ end, { desc = "Substitute selection in file" })
vim.keymap.set('n', '<leader>pp', function() vim.keymap.set('n', '<leader>pp', function()
vim.api.nvim_command('normal! "+p') vim.api.nvim_command('normal! "+p')
end, { desc = 'Paste from system clipboard' }) end, { desc = 'Paste from system clipboard' })
vim.keymap.set('v', '<leader>p', function() vim.keymap.set('v', '<leader>p', function()
vim.cmd('normal! "+p') vim.cmd('normal! "+p')
end, { desc = 'Yank to clipboard and keep the selection' }) end, { desc = 'Yank to clipboard and keep the selection' })
vim.keymap.set("n", "<leader>nv", function()
------------------------ VISUAL ------------------ select_course_directory()
end, { desc = "Open UniCourse menu" })
vim.keymap.set('v', 'p', function() vim.keymap.set('v', 'p', function()
local unnamed_content = vim.fn.getreg('""') local unnamed_content = vim.fn.getreg('""')
vim.api.nvim_command('normal! p') vim.api.nvim_command('normal! p')
vim.fn.setreg('""', unnamed_content) vim.fn.setreg('""', unnamed_content)
end, { desc = 'Paste from unnamed register (don\'t overwrite it) in visual mode' }) end, { desc = 'Paste from unnamed register (don\'t overwrite it) in visual mode' })
vim.keymap.set('v', '<leader>y', function() vim.keymap.set('v', '<leader>y', function()
vim.cmd('normal! "+y') vim.cmd('normal! "+y')
vim.cmd('normal! gv') vim.cmd('normal! gv')
end, { desc = 'Yank to clipboard and keep the selection' }) end, { desc = 'Yank to clipboard and keep the selection' })
---------------------------- INSERT ---------------------------
vim.keymap.set('i', '<C-k>', function() vim.keymap.set('i', '<C-k>', function()
local col = vim.fn.col('.') local col = vim.fn.col('.')
local line = vim.fn.line('.') local line = vim.fn.line('.')
@@ -228,6 +177,7 @@ vim.keymap.set('i', '<C-k>', function()
end end
end end
end) end)
-- Move left with wrapping -- Move left with wrapping
vim.keymap.set('i', '<C-j>', function() vim.keymap.set('i', '<C-j>', function()
local col = vim.fn.col('.') local col = vim.fn.col('.')
@@ -242,3 +192,46 @@ vim.keymap.set('i', '<C-j>', function()
end end
end end
end, { noremap = true, silent = true }) end, { noremap = true, silent = true })
vim.keymap.set("n", "<leader>or",
function()
local last_file_path = vim.fn.stdpath('data') .. "/lastfile.txt"
local file = io.open(last_file_path, "r")
if file then
local last_file = file:read("*line")
file:close()
if last_file and vim.fn.filereadable(last_file) == 1 then
vim.cmd("edit " .. last_file)
pcall(function()
vim.cmd('normal! `.') -- Go to the last edit position
vim.cmd('normal! zz') -- Center the cursor on the screen
end)
else
print("Last file does not exist or is not readable")
end
else
print("No last file found")
end
end
, { noremap = true, silent = true })
-- Fast quitter
vim.keymap.set('n', '<leader>q', function()
pcall(function()
vim.cmd('wa')
end)
vim.cmd('qa!')
end)
-- This needs to be refined for quick access to a new file or a recently edited one
vim.keymap.set('n', '<leader>ov',
function()
require('telescope.builtin').find_files({
prompt_title = "Select lecture in " .. conf.season,
cwd = conf.uni_dir,
find_command = {
"eza", "-1", "-D"
}
})
end
)

View File

@@ -1,5 +1,66 @@
-- Prefilled with lsp that have no dependencies
local servers = { "lua_ls", "rust_analyzer", "denols" }
local function populate_servers()
if vim.fn.executable("go") == 1 then
table.insert(servers, "gopls")
else
vim.notify("[mason] Skipping gopls (go not found)", vim.log.levels.WARN)
end
if vim.fn.executable("npm") == 1 then
table.insert(servers, "pyright")
table.insert(servers, "clangd")
table.insert(servers, "bashls")
else
vim.notify("[mason] Skipping install of some lsp (npm not found)", vim.log.levels.WARN)
end
if vim.fn.executable("cargo") == 1 then
table.insert(servers, "nil_ls")
else
vim.notify("[mason] Skipping nil (cargo not found)", vim.log.levels.WARN)
end
end
populate_servers()
return { return {
{ "stevearc/aerial.nvim", config = true }, {
"stevearc/aerial.nvim",
dependencies = {
"nvim-treesitter/nvim-treesitter",
"nvim-tree/nvim-web-devicons"
},
config = true,
keys = {
{
"<leader>ae",
function()
require("aerial")
vim.cmd("AerialToggle")
end
},
{
"}",
function()
require("aerial")
vim.cmd("AerialNext")
end
},
{
"{",
function()
require("aerial")
vim.cmd("AerialPrev")
end
},
},
},
{ {
"nvimdev/lspsaga.nvim", "nvimdev/lspsaga.nvim",
opts = { opts = {
@@ -12,36 +73,31 @@ return {
}, },
}, },
{ {
"neovim/nvim-lspconfig", "williamboman/mason.nvim",
dependencies = { dependencies = {
"williamboman/mason.nvim", "williamboman/mason-lspconfig.nvim"
"williamboman/mason-lspconfig.nvim", },
priority = -10;
config = function()
require("mason").setup()
"hrsh7th/nvim-cmp",
require("mason-lspconfig").setup({
ensure_installed = servers,
automatic_installation = true,
})
end,
},
{
"hrsh7th/nvim-cmp",
dependencies = {
"hrsh7th/cmp-path",
"saadparwaiz1/cmp_luasnip",
"hrsh7th/cmp-nvim-lsp", "hrsh7th/cmp-nvim-lsp",
"hrsh7th/cmp-buffer", "hrsh7th/cmp-buffer",
"nvimdev/lspsaga.nvim",
"hrsh7th/cmp-path",
"L3MON4D3/LuaSnip",
"saadparwaiz1/cmp_luasnip",
}, },
config = function() config = function()
local lspconfig = require("lspconfig")
-- Declarative important have npm and other tools installed
local servers = { "gopls", "pyright", "lua_ls", "rust_analyzer", "clangd" }
-- Custom overwrites for servers
local server_settings = {
lua_ls = {
settings = {
Lua = {
diagnostics = { globals = { "vim" } },
},
},
},
}
local cmp = require("cmp") local cmp = require("cmp")
cmp.setup({ cmp.setup({
snippet = { snippet = {
@@ -61,6 +117,28 @@ return {
{ name = "path" }, { name = "path" },
}), }),
}) })
end
},
{
"neovim/nvim-lspconfig",
dependencies = {
"hrsh7th/cmp-nvim-lsp",
"L3MON4D3/LuaSnip",
},
config = function()
local lspconfig = require("lspconfig")
-- Custom overwrites for servers
local server_settings = {
lua_ls = {
settings = {
Lua = {
diagnostics = { globals = { "vim" } },
},
},
},
}
local capabilities = require("cmp_nvim_lsp").default_capabilities() local capabilities = require("cmp_nvim_lsp").default_capabilities()
@@ -93,8 +171,6 @@ return {
end, opts) end, opts)
end end
-- Setup servers manually -- Setup servers manually
for _, server in ipairs(servers) do for _, server in ipairs(servers) do
local config = { local config = {
@@ -107,13 +183,6 @@ return {
lspconfig[server].setup(config) lspconfig[server].setup(config)
end end
-- Mason for autoinstall of servers
require("mason").setup()
require("mason-lspconfig").setup({
ensure_installed = servers,
automatic_installation = true,
})
-- Add text in diagnostics -- Add text in diagnostics
vim.diagnostic.config({ vim.diagnostic.config({
virtual_text = true, virtual_text = true,