From 9315c6e55de0e303e79a9630113c87e385188a38 Mon Sep 17 00:00:00 2001 From: js0ny Date: Wed, 2 Apr 2025 20:09:30 +0100 Subject: [PATCH] feat!(nvim): Migrate to v0.11 built-in lsp configs --- Justfile | 31 +++++++++++++-- tools/nvim/init.lua | 1 + tools/nvim/lsp/bashls.lua | 15 ++++++++ tools/nvim/lsp/clangd.lua | 29 +++++++++++++- tools/nvim/lsp/gopls.lua | 19 ++++++++++ tools/nvim/lsp/jsonls.lua | 18 +++++++++ tools/nvim/lsp/luals.lua | 38 ++++++++++++++++++- tools/nvim/lsp/pyright.lua | 27 +++++++++++++ tools/nvim/lsp/rust_analyzer.lua | 33 ++++++++++++++++ tools/nvim/lsp/taplo.lua | 32 ++++++++++++++++ tools/nvim/lsp/vimls.lua | 19 ++++++++++ tools/nvim/lsp/yamlls.lua | 9 +++++ tools/nvim/lua/config/migration.lua | 25 ++++++++++-- tools/nvim/lua/plugins/lsp.lua | 26 ++++++------- tools/nvim/lua/plugins/mod/nvim-lspconfig.lua | 8 ---- tools/nvim/readme.md | 4 ++ 16 files changed, 303 insertions(+), 31 deletions(-) create mode 100644 tools/nvim/lsp/bashls.lua create mode 100644 tools/nvim/lsp/gopls.lua create mode 100644 tools/nvim/lsp/jsonls.lua create mode 100644 tools/nvim/lsp/pyright.lua create mode 100644 tools/nvim/lsp/rust_analyzer.lua create mode 100644 tools/nvim/lsp/taplo.lua create mode 100644 tools/nvim/lsp/vimls.lua create mode 100644 tools/nvim/lsp/yamlls.lua create mode 100644 tools/nvim/readme.md diff --git a/Justfile b/Justfile index 8746a66..f27306c 100644 --- a/Justfile +++ b/Justfile @@ -1,8 +1,9 @@ -set shell := ["fish", "-c"] +# set shell := ["fish", "-c"] +set shell := ["bash", "-c"] set windows-shell := ["pwsh", "-c"] -PLATFORM := \ +OS := \ if os() == "linux" { if env("WSL_DISTRO_NAME","") != "" { "wsl" @@ -93,7 +94,7 @@ vivaldi: [linux] systemd: - ln -sf {{DOTFILES}}/platforms/{{PLATFORM}}/systemd {{XDG_CONFIG_HOME}}/systemd/ + ln -sf {{DOTFILES}}/platforms/{{OS}}/systemd {{XDG_CONFIG_HOME}}/systemd/ uv: @@ -129,6 +130,12 @@ neovim: {{LN}} {{DOTFILES}}/tools/nvim {{XDG_CONFIG_HOME}}/nvim nvim --headless +checkhealth +"w nvim-healthcheck.txt" +qall +[unix] +neovide: + which neovide + -mkdir -p {{XDG_CONFIG_HOME}}/neovide + {{LN}} {{DOTFILES}}/platforms/{{OS}}/neovide.toml {{XDG_CONFIG_HOME}}/neovide/config.toml + [unix] thunderbird: curl -L https://github.com/wshanks/tbkeys/releases/download/v2.4.0/tbkeys.xpi -o "$HOME/Downloads/tbkeys.xpi" @@ -168,6 +175,24 @@ kitty: zotero: curl -L https://github.com/syt2/zotero-addons/releases/download/V1.8.1/zotero-addons.xpi -o "$HOME/Downloads/zotero-addons.xpi" +[unix] +fish: + {{LN}} {{DOTFILES}}/tools/fish {{XDG_CONFIG_HOME}}/fish + curl -L https://raw.githubusercontent.com/SpaceAceMonkey/dotenv-for-fish/refs/heads/main/dotenv.fish -o "{{DOTFILES}}/tools/fish/functions/dotenv.fish" + + +[unix] +zsh: + -[[ -f /etc/zshenv ]] && sudo cp {{DOTFILES}}/tools/zsh/global.zshenv /etc/zshenv # or /etc/zsh/zshenv + -[[ -f /etc/zsh/zshenv ]] && sudo cp {{DOTFILES}}/tools/zsh/global.zshenv /etc/zsh/zshenv + mkdir -p {{XDG_CONFIG_HOME}}/zsh + {{LN}} {{DOTFILES}}/tools/zsh/zshenv {{XDG_CONFIG_HOME}}/zsh/.zshenv + {{LN}} {{DOTFILES}}/tools/zsh/zshrc {{XDG_CONFIG_HOME}}/zsh/.zshrc + {{LN}} {{DOTFILES}}/tools/zsh/zprofile {{XDG_CONFIG_HOME}}/zsh/.zprofile + test -d {{XDG_CONFIG_HOME}}/zsh/plugins/zsh-autosuggestions || git clone --depth 1 https://github.com/zsh-users/zsh-autosuggestions.git {{XDG_CONFIG_HOME}}/zsh/plugins/zsh-autosuggestions + test -d {{XDG_CONFIG_HOME}}/zsh/plugins/zsh-syntax-highlighting || git clone --depth 1 https://github.com/zsh-users/zsh-syntax-highlighting.git {{XDG_CONFIG_HOME}}/zsh/plugins/zsh-syntax-highlighting + test -d {{XDG_CONFIG_HOME}}/zsh/plugins/zsh-history-substring-search || git clone --depth 1 https://github.com/zsh-users/zsh-history-substring-search.git {{XDG_CONFIG_HOME}}/zsh/plugins/zsh-history-substring-search + test -d {{XDG_CONFIG_HOME}}/zsh/plugins/zsh-completions || git clone --depth 1 https://github.com/zsh-users/zsh-completions.git {{XDG_CONFIG_HOME}}/zsh/plugins/zsh-completions [linux] flatpak: diff --git a/tools/nvim/init.lua b/tools/nvim/init.lua index 215b740..364ba8a 100644 --- a/tools/nvim/init.lua +++ b/tools/nvim/init.lua @@ -16,6 +16,7 @@ else -- Vanilla neovim end require("config.keymaps") require("config.diagnostics") +require("config.migration") -- If current session is spawn by neovide, do: if vim.g.neovide then diff --git a/tools/nvim/lsp/bashls.lua b/tools/nvim/lsp/bashls.lua new file mode 100644 index 0000000..5a759cc --- /dev/null +++ b/tools/nvim/lsp/bashls.lua @@ -0,0 +1,15 @@ +-- Bash +--[[ Installation +brew install bash-language-server +npm i -g bash-language-server +dnf install -y nodejs-bash-language-server # Fedora Linux +--]] +return { + cmd = { "bash-language-server", "start" }, + filetypes = { "bash", "sh" }, + settings = { + bashIde = { + globPattern = vim.env.GLOB_PATTERN or "*@(.sh|.inc|.bash|.command)", + }, + }, +} diff --git a/tools/nvim/lsp/clangd.lua b/tools/nvim/lsp/clangd.lua index 427d4b4..d9b1b41 100644 --- a/tools/nvim/lsp/clangd.lua +++ b/tools/nvim/lsp/clangd.lua @@ -1,5 +1,30 @@ +-- C/C++ +-- Clangd requires compile_commands.json to work and the easiest way to generate it is to use CMake. +-- How to use clangd C/C++ LSP in any project: https://gist.github.com/Strus/042a92a00070a943053006bf46912ae9 + return { - cmd = { "clangd", "--background-index" }, - root_markers = { "compile_commands.json", "compile_flags.txt", ".clangd" }, + cmd = { + "clangd", + "--clang-tidy", + "--header-insertion=iwyu", + "--completion-style=detailed", + "--function-arg-placeholders", + "--fallback-style=none", + }, filetypes = { "c", "cpp" }, + root_markers = { + ".clangd", + ".clang-format", + "compile_commands.json", + "compile_flags.txt", + ".git", + }, + capabilities = { + textDocument = { + completion = { + editsNearCursor = true, + }, + }, + offsetEncoding = { "utf-8", "utf-16" }, + }, } diff --git a/tools/nvim/lsp/gopls.lua b/tools/nvim/lsp/gopls.lua new file mode 100644 index 0000000..0833e6c --- /dev/null +++ b/tools/nvim/lsp/gopls.lua @@ -0,0 +1,19 @@ +-- Golang +--[[ Installation +go install golang.org/x/tools/gopls@latest +brew install gopls +--]] +return { + cmd = { "gopls" }, + filetypes = { "go", "gomod", "gowork", "gotmpl" }, + root_markers = { "go.work", "go.mod", ".git" }, + settings = { + gopls = { + analyses = { + unusedparams = true, + }, + staticcheck = true, + -- semanticTokens = true, -- go's semantic token highlight is not accurate so far + }, + }, +} diff --git a/tools/nvim/lsp/jsonls.lua b/tools/nvim/lsp/jsonls.lua new file mode 100644 index 0000000..38b625d --- /dev/null +++ b/tools/nvim/lsp/jsonls.lua @@ -0,0 +1,18 @@ +-- JSON +--[[ +npm i vscode-json-languageserver +--]] +return { + cmd = { "vscode-json-language-server", "--stdio" }, + filetypes = { "json", "jsonc" }, + root_markers = { ".git" }, + init_options = { + provideFormatter = true, + }, + settings = { + -- See setting options + -- https://github.com/microsoft/vscode/tree/main/extensions/json-language-features/server#settings + json = { + }, + }, +} diff --git a/tools/nvim/lsp/luals.lua b/tools/nvim/lsp/luals.lua index a564707..f59f77a 100644 --- a/tools/nvim/lsp/luals.lua +++ b/tools/nvim/lsp/luals.lua @@ -1 +1,37 @@ -return {} +-- Lua +--[[ Installation +scoop install lua-language-server +brew install lua-language-server +sudo port install lua-language-server +--]] +--[[ Build: Ninja & C++17 Required +git clone https://github.com/LuaLS/lua-language-server --depth 1 +cd lua-language-server +./make.sh +--]] +--[[ Note: For building from source, wrapper script is required +Accompanied with a wrapper script +#!/bin/bash +exec "$HOME/.local/build/lua-language-server/bin/lua-language-server" "$@" +--]] +return { + cmd = { "lua-language-server" }, + root_markers = { + ".luarc.json", + ".luarc.jsonc", + ".luacheckrc", + ".stylua.toml", + "stylua.toml", + "selene.toml", + "selene.yml", + }, + filetypes = { "lua" }, + settings = { + Lua = { + hint = { + enable = true, + setType = true, + }, + }, + }, +} diff --git a/tools/nvim/lsp/pyright.lua b/tools/nvim/lsp/pyright.lua new file mode 100644 index 0000000..8967cfc --- /dev/null +++ b/tools/nvim/lsp/pyright.lua @@ -0,0 +1,27 @@ +-- Python +--[[ Installation +uv tool install pyright +--]] +return { + cmd = { "pyright-langserver", "--stdio" }, + filetypes = { "python" }, + root_markers = { + ".python_version", + "pyproject.toml", + "setup.py", + "setup.cfg", + "requirements.txt", + "Pipfile", + "pyrightconfig.json", + ".git", + }, + settings = { + python = { + analysis = { + autoSearchPaths = true, + useLibraryCodeForTypes = true, + diagnosticMode = "openFilesOnly", + }, + }, + }, +} diff --git a/tools/nvim/lsp/rust_analyzer.lua b/tools/nvim/lsp/rust_analyzer.lua new file mode 100644 index 0000000..a2ba78f --- /dev/null +++ b/tools/nvim/lsp/rust_analyzer.lua @@ -0,0 +1,33 @@ +-- Rust +--[[ Installation +rustup component add rust-src +--]] +return { + cmd = { "rust-analyzer" }, + filetypes = { "rust" }, + root_dir = function(bufnr, cb) + local root = vim.fs.root(bufnr, { "Cargo.toml" }) + if root then + vim.system({ "cargo", "metadata", "--no-depts", "--format-version", "1" }, { cwd = root }, function(obj) + if obj.code ~= 0 then + cb(root) + else + local success, result = pcall(vim.json.decode, obj.stdout) + if success and result.workspace_root then + cb(result.workspace_root) + else + cb(root) + end + end + end) + else + cb(vim.fs.root(bufnr, { "rust-project.json", ".git" })) + end + end, + before_init = function(init_params, config) + -- See https://github.com/rust-lang/rust-analyzer/blob/eb5da56d839ae0a9e9f50774fa3eb78eb0964550/docs/dev/lsp-extensions.md?plain=1#L26 + if config.settings and config.settings["rust-analyzer"] then + init_params.initializationOptions = config.settings["rust-analyzer"] + end + end, +} diff --git a/tools/nvim/lsp/taplo.lua b/tools/nvim/lsp/taplo.lua new file mode 100644 index 0000000..1b9f634 --- /dev/null +++ b/tools/nvim/lsp/taplo.lua @@ -0,0 +1,32 @@ +-- TOML +--[[ Installation +cargo install taplo-cli --locked +brew install taplo +pnpm install -g @taplo/cli +--]] +return { + cmd = { "taplo", "lsp", "stdio" }, + filetypes = { "toml" }, + root_markers = { ".git" }, + settings = { + -- See all the setting options + -- https://github.com/tamasfe/taplo/blob/master/editors/vscode/package.json + evenBetterToml = { + taplo = { + configFile = { + enabled = true, + }, + }, + schema = { + enabled = true, + catalogs = { + "https://www.schemastore.org/api/json/catalog.json", + }, + cache = { + memoryExpiration = 60, + diskExpiration = 600, + }, + }, + }, + }, +} diff --git a/tools/nvim/lsp/vimls.lua b/tools/nvim/lsp/vimls.lua new file mode 100644 index 0000000..6e3de79 --- /dev/null +++ b/tools/nvim/lsp/vimls.lua @@ -0,0 +1,19 @@ +return { + cmd = { "vim-language-server", "--stdio" }, + filetypes = { "vim" }, + root_markers = { ".git" }, + init_options = { + isNeovim = true, + iskeyword = "@,48-57,_,192-255,-#", + vimruntime = "", + runtimepath = "", + diagnostic = { enable = true }, + indexes = { + runtimepath = true, + gap = 100, + count = 3, + projectRootPatterns = { "runtime", "nvim", ".git", "autoload", "plugin" }, + }, + suggest = { fromVimruntime = true, fromRuntimepath = true }, + }, +} diff --git a/tools/nvim/lsp/yamlls.lua b/tools/nvim/lsp/yamlls.lua new file mode 100644 index 0000000..b85e6ee --- /dev/null +++ b/tools/nvim/lsp/yamlls.lua @@ -0,0 +1,9 @@ +return { + cmd = { "yaml-language-server", "--stdio" }, + filetypes = { "yaml", "yaml.docker-compose", "yaml.gitlab" }, + root_markers = { ".git" }, + settings = { + yaml = { + }, + }, +} diff --git a/tools/nvim/lua/config/migration.lua b/tools/nvim/lua/config/migration.lua index cbc7c1e..9cb9236 100644 --- a/tools/nvim/lua/config/migration.lua +++ b/tools/nvim/lua/config/migration.lua @@ -1,14 +1,31 @@ local nvim_version = vim.version() + if nvim_version.minor ~= 11 then return end + vim.diagnostic.config({ virtual_lines = true, }) -vim.lsp.enable({ - "clangd", - "luals", -}) +-- vim.lsp.enable({ +-- "clangd", +-- "luals", +-- }) + +local lsp_configs = {} +for _, v in ipairs(vim.api.nvim_get_runtime_file('lsp/*', true)) do + local name = vim.fn.fnamemodify(v, ':t:r') + lsp_configs[name] = true +end + +vim.lsp.enable(vim.tbl_keys(lsp_configs)) + +-- Delete 0.11 new gr- keymaps +vim.keymap.del({ "n" }, "grn") +vim.keymap.del({ "n", "x" }, "gra") +vim.keymap.del({ "n" }, "gri") + + diff --git a/tools/nvim/lua/plugins/lsp.lua b/tools/nvim/lua/plugins/lsp.lua index ef3e472..8af33e4 100644 --- a/tools/nvim/lua/plugins/lsp.lua +++ b/tools/nvim/lua/plugins/lsp.lua @@ -1,5 +1,5 @@ return { - { import = "plugins.mod.nvim-lspconfig" }, + -- { import = "plugins.mod.nvim-lspconfig" }, { "lervag/vimtex", ft = { "tex", "bib" }, @@ -27,18 +27,18 @@ return { -- ensure_installed = require("config.servers").servers, }, }, - { - "williamboman/mason-lspconfig.nvim", - event = "BufReadPre", - dependencies = { - { "williamboman/mason.nvim" }, - { "neovim/nvim-lspconfig" }, - }, - opts = { - -- ensure_installed = require("config.servers").servers, - -- automatic_installation = false, - }, - }, + -- { + -- "williamboman/mason-lspconfig.nvim", + -- event = "BufReadPre", + -- dependencies = { + -- { "williamboman/mason.nvim" }, + -- { "neovim/nvim-lspconfig" }, + -- }, + -- opts = { + -- -- ensure_installed = require("config.servers").servers, + -- -- automatic_installation = false, + -- }, + -- }, { import = "plugins.mod.conform-nvim" }, { "nvim-treesitter/nvim-treesitter-context", lazy = true }, { diff --git a/tools/nvim/lua/plugins/mod/nvim-lspconfig.lua b/tools/nvim/lua/plugins/mod/nvim-lspconfig.lua index 3136516..4a9d190 100644 --- a/tools/nvim/lua/plugins/mod/nvim-lspconfig.lua +++ b/tools/nvim/lua/plugins/mod/nvim-lspconfig.lua @@ -38,11 +38,3 @@ return { end end, - -- example calling setup directly for each LSP - -- config = function() - -- local capabilities = require('blink.cmp').get_lsp_capabilities() - -- local lspconfig = require('lspconfig') - -- - -- lspconfig['lua_ls'].setup({ capabilities = capabilities }) - -- end -} diff --git a/tools/nvim/readme.md b/tools/nvim/readme.md new file mode 100644 index 0000000..72823e0 --- /dev/null +++ b/tools/nvim/readme.md @@ -0,0 +1,4 @@ +## Language Servers (v0.11) + +[References](https://github.com/rockyzhang24/dotfiles/tree/master/.config/nvim/lsp) +