From 69493a7d8449d3307ed0c4bb5926106d922edb0c Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Thu, 17 Apr 2025 23:56:44 +0200 Subject: [PATCH] feat(home-manager): rework neovim configuration Use new neovim option introduced in 0.11 --- modules/home-manager/cli/neovim/default.nix | 37 ++-- .../cli/neovim/files/lsp/bash.lua | 17 ++ .../cli/neovim/files/lsp/helm.lua | 30 +++ .../home-manager/cli/neovim/files/lsp/lua.lua | 32 ++++ .../cli/neovim/files/lsp/marksman.lua | 5 + .../cli/neovim/files/lsp/nills.lua | 15 ++ .../cli/neovim/files/lsp/pylsp.lua | 21 +++ .../cli/neovim/files/lsp/terraformls.lua | 5 + .../cli/neovim/files/lsp/tflint.lua | 5 + .../cli/neovim/files/lsp/yaml.lua | 33 ++++ .../cli/neovim/files/lspconfig.lua | 174 ------------------ .../cli/neovim/files/nvim-k8s-lsp.lua | 6 + .../home-manager/cli/neovim/files/options.lua | 22 +++ 13 files changed, 208 insertions(+), 194 deletions(-) create mode 100644 modules/home-manager/cli/neovim/files/lsp/bash.lua create mode 100644 modules/home-manager/cli/neovim/files/lsp/helm.lua create mode 100644 modules/home-manager/cli/neovim/files/lsp/lua.lua create mode 100644 modules/home-manager/cli/neovim/files/lsp/marksman.lua create mode 100644 modules/home-manager/cli/neovim/files/lsp/nills.lua create mode 100644 modules/home-manager/cli/neovim/files/lsp/pylsp.lua create mode 100644 modules/home-manager/cli/neovim/files/lsp/terraformls.lua create mode 100644 modules/home-manager/cli/neovim/files/lsp/tflint.lua create mode 100644 modules/home-manager/cli/neovim/files/lsp/yaml.lua create mode 100644 modules/home-manager/cli/neovim/files/nvim-k8s-lsp.lua diff --git a/modules/home-manager/cli/neovim/default.nix b/modules/home-manager/cli/neovim/default.nix index 7cf7d45..77b4e28 100644 --- a/modules/home-manager/cli/neovim/default.nix +++ b/modules/home-manager/cli/neovim/default.nix @@ -10,13 +10,13 @@ let url = "https://ftp.nluug.nl/vim/runtime/spell/fr.utf-8.sug"; sha256 = "0294bc32b42c90bbb286a89e23ca3773b7ef50eff1ab523b1513d6a25c6b3f58"; }; - yaml-companion = pkgs.vimUtils.buildVimPlugin { - pname = "yaml-companion"; + nvim-k8s-lsp = pkgs.vimUtils.buildVimPlugin { + pname = "nvim-k8s-lsp"; version = "main"; src = builtins.fetchGit { - url = "https://github.com/someone-stole-my-name/yaml-companion.nvim.git"; - rev = "131b0d67bd2e0f1a02e0daf2f3460482221ce3c0"; - ref = "main"; + url = "https://github.com/tonychg/nvim-k8s-lsp.git"; + rev = "5e8221cce09cb71b7604c0c7469bf9053dd877ca"; + ref = "feat/add-helm-ls-support"; }; }; in @@ -27,6 +27,16 @@ in config = mkIf cfg.enable { home.file."${config.xdg.configHome}/nvim/spell/fr.utf-8.spl".source = nvim-spell-fr-utf8-dictionary; home.file."${config.xdg.configHome}/nvim/spell/fr.utf-8.sug".source = nvim-spell-fr-utf8-suggestions; + + home.file."${config.xdg.configHome}/nvim/lsp/bash.lua".source = ./files/lsp/bash.lua; + home.file."${config.xdg.configHome}/nvim/lsp/helm.lua".source = ./files/lsp/helm.lua; + home.file."${config.xdg.configHome}/nvim/lsp/lua.lua".source = ./files/lsp/lua.lua; + home.file."${config.xdg.configHome}/nvim/lsp/nills.lua".source = ./files/lsp/nills.lua; + home.file."${config.xdg.configHome}/nvim/lsp/pylsp.lua".source = ./files/lsp/pylsp.lua; + home.file."${config.xdg.configHome}/nvim/lsp/terraformls.lua".source = ./files/lsp/terraformls.lua; + home.file."${config.xdg.configHome}/nvim/lsp/tflint.lua".source = ./files/lsp/tflint.lua; + home.file."${config.xdg.configHome}/nvim/lsp/yaml.lua".source = ./files/lsp/yaml.lua; + programs.neovim = { enable = true; defaultEditor = true; @@ -103,16 +113,6 @@ in } ''; } - { - plugin = lsp_lines-nvim; - type = "lua"; - config = (builtins.readFile ./files/lsp-line.lua); - } - { - plugin = nvim-lspconfig; - type = "lua"; - config = (builtins.readFile ./files/lspconfig.lua); - } { plugin = lualine-nvim; type = "lua"; @@ -197,12 +197,9 @@ in config = ( builtins.readFile ./files/whichkey.lua ); } { - plugin = yaml-companion.overrideAttrs { - dependencies = [ - nvim-lspconfig - ]; - }; + plugin = nvim-k8s-lsp ; type = "lua"; + config = ( builtins.readFile files/nvim-k8s-lsp.lua ); } vim-helm ]; diff --git a/modules/home-manager/cli/neovim/files/lsp/bash.lua b/modules/home-manager/cli/neovim/files/lsp/bash.lua new file mode 100644 index 0000000..27f9131 --- /dev/null +++ b/modules/home-manager/cli/neovim/files/lsp/bash.lua @@ -0,0 +1,17 @@ +return { + cmd = { 'bash-language-server', 'start' }, + settings = { + bashIde = { + -- Glob pattern for finding and parsing shell script files in the workspace. + -- Used by the background analysis features across files. + + -- Prevent recursive scanning which will cause issues when opening a file + -- directly in the home directory (e.g. ~/foo.sh). + -- + -- Default upstream pattern is "**/*@(.sh|.inc|.bash|.command)". + globPattern = vim.env.GLOB_PATTERN or '*@(.sh|.inc|.bash|.command)', + }, + }, + filetypes = { 'bash', 'sh' }, + root_markers = { '.git' }, +} diff --git a/modules/home-manager/cli/neovim/files/lsp/helm.lua b/modules/home-manager/cli/neovim/files/lsp/helm.lua new file mode 100644 index 0000000..8086a4a --- /dev/null +++ b/modules/home-manager/cli/neovim/files/lsp/helm.lua @@ -0,0 +1,30 @@ +return { + cmd = { 'helm_ls', 'serve' }, + filetypes = { 'helm' }, + root_markers = { 'Chart.yaml' }, + capabilities = { + workspace = { + didChangeWatchedFiles = { + dynamicRegistration = true, + }, + }, + }, + settings = { + ['helm-ls'] = { + yamlls = { + enabled = true, + enabledforfilesglob = "*.{yaml,yml}", + diagnosticslimit = 50, + showdiagnosticsdirectly = false, + path = "yaml-language-server", + config = { + schemas = { + kubernetes = "template/**", + }, + completion = true, + hover = true, + } + } + } + } +} diff --git a/modules/home-manager/cli/neovim/files/lsp/lua.lua b/modules/home-manager/cli/neovim/files/lsp/lua.lua new file mode 100644 index 0000000..4ab1145 --- /dev/null +++ b/modules/home-manager/cli/neovim/files/lsp/lua.lua @@ -0,0 +1,32 @@ +return { + cmd = { 'lua-language-server' }, + filetypes = { 'lua' }, + root_markers = { + '.luarc.json', + '.luarc.jsonc', + '.luacheckrc', + '.stylua.toml', + 'stylua.toml', + 'selene.toml', + 'selene.yml', + '.git', + }, + settings = { + Lua = { + diagnostics = { + globals = {'vim'} -- Add any globals you want to ignore as undefined + }, + workspace = { + library = { + [vim.fn.expand('$VIMRUNTIME/lua')] = true + } + }, + telemetry = { + enable = false -- Disable telemetry + } + } + }, + flags = { + debounce_text_changes = 150, + } +} diff --git a/modules/home-manager/cli/neovim/files/lsp/marksman.lua b/modules/home-manager/cli/neovim/files/lsp/marksman.lua new file mode 100644 index 0000000..4c74725 --- /dev/null +++ b/modules/home-manager/cli/neovim/files/lsp/marksman.lua @@ -0,0 +1,5 @@ +return { + cmd = { 'marksman' }, + filetypes = { 'markdown', 'markdown.mdx' }, + root_markers = { '.marksman.toml', '.git' }, +} diff --git a/modules/home-manager/cli/neovim/files/lsp/nills.lua b/modules/home-manager/cli/neovim/files/lsp/nills.lua new file mode 100644 index 0000000..2829674 --- /dev/null +++ b/modules/home-manager/cli/neovim/files/lsp/nills.lua @@ -0,0 +1,15 @@ +---@brief +--- +-- https://github.com/oxalica/nil +-- +-- A new language server for Nix Expression Language. +-- +-- If you are using Nix with Flakes support, run `nix profile install github:oxalica/nil` to install. +-- Check the repository README for more information. +-- +-- _See an example config at https://github.com/oxalica/nil/blob/main/dev/nvim-lsp.nix._ +return { + cmd = { 'nil' }, + filetypes = { 'nix' }, + root_markers = { 'flake.nix', '.git' }, +} diff --git a/modules/home-manager/cli/neovim/files/lsp/pylsp.lua b/modules/home-manager/cli/neovim/files/lsp/pylsp.lua new file mode 100644 index 0000000..1da4a02 --- /dev/null +++ b/modules/home-manager/cli/neovim/files/lsp/pylsp.lua @@ -0,0 +1,21 @@ +return { + cmd = { 'pylsp' }, + filetypes = { 'python' }, + root_markers = { + 'pyproject.toml', + 'setup.py', + 'setup.cfg', + 'requirements.txt', + 'Pipfile', + '.git', + }, + settings = { + pylsp = { + plugins = { + pylint = { + enabled = true + } + } + } + } +} diff --git a/modules/home-manager/cli/neovim/files/lsp/terraformls.lua b/modules/home-manager/cli/neovim/files/lsp/terraformls.lua new file mode 100644 index 0000000..4a875c3 --- /dev/null +++ b/modules/home-manager/cli/neovim/files/lsp/terraformls.lua @@ -0,0 +1,5 @@ +return { + cmd = { 'terraform-ls', 'serve' }, + filetypes = { 'terraform', 'terraform-vars' }, + root_markers = { '.terraform', '.git' }, +} diff --git a/modules/home-manager/cli/neovim/files/lsp/tflint.lua b/modules/home-manager/cli/neovim/files/lsp/tflint.lua new file mode 100644 index 0000000..0074cdf --- /dev/null +++ b/modules/home-manager/cli/neovim/files/lsp/tflint.lua @@ -0,0 +1,5 @@ +return { + cmd = { 'tflint', '--langserver' }, + filetypes = { 'terraform' }, + root_markers = { '.terraform', '.git', '.tflint.hcl' }, +} diff --git a/modules/home-manager/cli/neovim/files/lsp/yaml.lua b/modules/home-manager/cli/neovim/files/lsp/yaml.lua new file mode 100644 index 0000000..9e304dd --- /dev/null +++ b/modules/home-manager/cli/neovim/files/lsp/yaml.lua @@ -0,0 +1,33 @@ +-- most used yaml schemas +local gitlab_ci = "https://gitlab.com/gitlab-org/gitlab/-/raw/master/app/assets/javascripts/editor/schema/ci.json" +local taskfile = "https://taskfile.dev/schema.json" +local lefthook = "https://raw.githubusercontent.com/evilmartians/lefthook/refs/heads/master/schema.json" +local github_workflow = "https://json.schemastore.org/github-workflow.json" + +return { + cmd = { 'yaml-language-server', '--stdio' }, + filetypes = { 'yaml', 'yaml.docker-compose', 'yaml.gitlab' }, + root_markers = { '.git' }, + settings = { + yaml = { + schemas = { + [gitlab_ci] = { + "ci/*.{yaml,yml}", + ".gitlab/**/*.{yaml,yml}", + ".gitlab-ci.{yaml,yml}", + }, + [taskfile] = { + "Taskfile*.{yaml,yml}", + "taskfile*.{yaml,yml}", + "taskfiles/**/*.{yaml,yml}", + }, + [lefthook] = { + "lefthook.{yaml,yml}", + }, + [github_workflow] = { + ".github/workflow/**/*.{yaml,yml}", + } + }, + }, + }, +} diff --git a/modules/home-manager/cli/neovim/files/lspconfig.lua b/modules/home-manager/cli/neovim/files/lspconfig.lua index 8a08268..805d625 100644 --- a/modules/home-manager/cli/neovim/files/lspconfig.lua +++ b/modules/home-manager/cli/neovim/files/lspconfig.lua @@ -2,10 +2,6 @@ local lspconfig = require('lspconfig') local capabilities = require("cmp_nvim_lsp").default_capabilities() require("yaml-companion").open_ui_select() -lspconfig.bashls.setup { - capabilities = capabilities, -} - lspconfig.clangd.setup { capabilities = capabilities, } @@ -14,178 +10,8 @@ lspconfig.eslint.setup{ capabilities = capabilities, } --- YAML variables and Helm LS (quite big!) -local schemas = {} - -local kubernetes = "https://raw.githubusercontent.com/yannh/kubernetes-json-schema/refs/heads/master/v1.29.6-standalone-strict/all.json" -local flux2 = "https://raw.githubusercontent.com/fluxcd-community/flux2-schemas/refs/heads/main/all.json" -local gitlab_ci = "https://gitlab.com/gitlab-org/gitlab/-/raw/master/app/assets/javascripts/editor/schema/ci.json" -local taskfile = "https://taskfile.dev/schema.json" -local podmonitor = "https://raw.githubusercontent.com/datreeio/CRDs-catalog/refs/heads/main/monitoring.coreos.com/podmonitor_v1.json" -local servicemonitor = "https://raw.githubusercontent.com/datreeio/CRDs-catalog/refs/heads/main/monitoring.coreos.com/servicemonitor_v1.json" -local prometheus = "https://raw.githubusercontent.com/datreeio/CRDs-catalog/refs/heads/main/monitoring.coreos.com/prometheus_v1.json" -local ingress = "https://raw.githubusercontent.com/yannh/kubernetes-json-schema/refs/heads/master/v1.30.1/ingress-networking-v1.json" -local lefthook = "https://raw.githubusercontent.com/evilmartians/lefthook/refs/heads/master/schema.json" -local github_workflow = "https://json.schemastore.org/github-workflow.json" - -schemas[kubernetes] = { - "templates/*deployment.yaml", - "templates/*service.yaml", - "templates/*secret.yaml", - "templates/*configmap.yaml", - "templates/*daemonset.yaml", - "templates/*replicaset.yaml", - "templates/*persistentvolume.yaml", - "templates/*persistentvolumeclaim.yaml", - "templates/*serviceaccount.yaml", - "templates/*role.yaml", - "templates/*rolebinding.yaml", - "templates/*clusterrole.yaml", - "templates/*clusterrolebinding.yaml", - "templates/*job*.yaml", -} -schemas[podmonitor] = "templates/*podmonitor*" -schemas[servicemonitor] = "templates/*servicemonitor*" -schemas[ingress] = "templates/*ingress*" - -lspconfig.helm_ls.setup { - capabilities = capabilities, - settings = { - ['helm-ls'] = { - yamlls = { - enabled = true, - enabledForFilesGlob = "*.{yaml,yml}", - diagnosticsLimit = 50, - showDiagnosticsDirectly = false, - path = "yaml-language-server", - config = { - schemas = schemas, - completion = true, - hover = true, - } - } - } - } -} - --- restart LSP server avoir problems with Helm-ls -vim.api.nvim_create_autocmd("FileType", { - pattern = "helm", - command = "LspRestart", -}) - -lspconfig.lua_ls.setup { - capabilities = capabilities, - single_file_support = true, - settings = { - Lua = { - diagnostics = { - globals = {'vim'} -- Add any globals you want to ignore as undefined - }, - workspace = { - library = { - [vim.fn.expand('$VIMRUNTIME/lua')] = true - } - }, - telemetry = { - enable = false -- Disable telemetry - } - } - }, - flags = { - debounce_text_changes = 150, - } -} - -lspconfig.marksman.setup{ - capabilities = capabilities, -} - -lspconfig.nil_ls.setup { - capabilities = capabilities, -} - -lspconfig.pylsp.setup { - capabilities = capabilities, - settings = { - pylsp = { - plugins = { - pylint = { - enabled = true - } - } - } - } -} - -lspconfig.terraformls.setup{ - capabilities = capabilities, -} - -lspconfig.tflint.setup{ - capabilities = capabilities, -} - -- YAML configuration (quite big!) -local cfg = require("yaml-companion").setup({ - schemas = { - { name = "Flux2", uri = flux2 }, - { name = "Gitlab", uri = gitlab_ci }, - { name = "Taskfile", uri = taskfile }, - { name = "PodMonitor", uri = podmonitor }, - { name = "ServiceMonitor", uri = servicemonitor }, - { name = "Prometheus", uri = prometheus }, - { name = "Kubernetes", uri = kubernetes }, - { name = "Lefthook", uri = lefthook }, - { name = "Github Workflow", uri = github_workflow}, - }, - lspconfig = { - capabilities = capabilities, - settings = { - yaml = { - schemas = { - [flux2] = { - "*.{alert,provider,receiver,helmrelease,helmrepository,gitrepository}.yaml", - "gotk-sync.yaml", - "gotk-*.yaml", - }, - [gitlab_ci] = { - "ci/*.{yaml,yml}", - ".gitlab/**/*.{yaml,yml}", - ".gitlab-ci.{yaml,yml}", - }, - [taskfile] = { - "Taskfile*.{yaml,yml}", - "taskfile*.{yaml,yml}", - "taskfiles/**/*.{yaml,yml}", - }, - [podmonitor] = { - "*podmonitor*.{yaml,yml}", - }, - [servicemonitor] = { - "*servicemonitor*.{yaml,yml}", - }, - [prometheus] = { - "*prometheus*.{yaml,yml}", - }, - [ingress] = { - "*ingress*.{yaml,yml}", - }, - [lefthook] = { - "lefthook.{yaml,yml}", - }, - [github_workflow] = { - ".github/workflow/**/*.{yaml,yml}", - } - }, - }, - }, - }, -}) - -lspconfig.yamlls.setup{cfg} - vim.api.nvim_create_autocmd('LspAttach', { desc = 'LSP actions', callback = function(ev) diff --git a/modules/home-manager/cli/neovim/files/nvim-k8s-lsp.lua b/modules/home-manager/cli/neovim/files/nvim-k8s-lsp.lua new file mode 100644 index 0000000..a2176c6 --- /dev/null +++ b/modules/home-manager/cli/neovim/files/nvim-k8s-lsp.lua @@ -0,0 +1,6 @@ +require("nvim-k8s-lsp").setup({ + kubernetes_version = "v1.32.2", + integrations = { + lualine = false, + } +}) diff --git a/modules/home-manager/cli/neovim/files/options.lua b/modules/home-manager/cli/neovim/files/options.lua index a33b8c4..0c75a47 100644 --- a/modules/home-manager/cli/neovim/files/options.lua +++ b/modules/home-manager/cli/neovim/files/options.lua @@ -121,3 +121,25 @@ vim.lsp.set_log_level("off") -- define a timeout for match parens vim.g.matchparen_timeout = 2 vim.g.matchparen_insert_timeout = 2 + +-- activate virtual lines for diagnistics +-- no more plugins needed +vim.diagnostic.config({ + virtual_lines = true + + -- Alternatively, customize specific options + -- virtual_lines = { + -- -- Only show virtual line diagnostics for the current cursor line + -- current_line = true, + -- }, +}) + +-- LSP configuration +vim.lsp.enable('bash') +vim.lsp.enable('helm') +vim.lsp.enable('lua') +vim.lsp.enable('nills') +vim.lsp.enable('pylsp') +vim.lsp.enable('terraformls') +vim.lsp.enable('tflint') +vim.lsp.enable('yaml')