From 6a7385ed266e20790f4472796980c45c72bb8e6b Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Wed, 7 Aug 2024 21:11:37 +0200 Subject: [PATCH] nvim: Split formatting off ide plugins --- nvim/.config/nvim/lua/plugins/config/lsp.lua | 242 ------------------- nvim/.config/nvim/lua/plugins/formatting.lua | 121 ++++++++++ nvim/.config/nvim/lua/plugins/ide.lua | 99 -------- 3 files changed, 121 insertions(+), 341 deletions(-) delete mode 100644 nvim/.config/nvim/lua/plugins/config/lsp.lua create mode 100644 nvim/.config/nvim/lua/plugins/formatting.lua diff --git a/nvim/.config/nvim/lua/plugins/config/lsp.lua b/nvim/.config/nvim/lua/plugins/config/lsp.lua deleted file mode 100644 index 94dee61..0000000 --- a/nvim/.config/nvim/lua/plugins/config/lsp.lua +++ /dev/null @@ -1,242 +0,0 @@ -vim.diagnostic.config({ virtual_text = true }) -vim.fn.sign_define("DiagnosticSignError", { text = "✘", texthl = "DiagnosticSignError" }) -vim.fn.sign_define("DiagnosticSignWarn", { text = "", texthl = "DiagnosticSignWarn" }) -vim.fn.sign_define("DiagnosticSignInfo", { text = "", texthl = "DiagnosticSignInfo" }) -vim.fn.sign_define("DiagnosticSignHint", { text = "", texthl = "DiagnosticSignHint" }) - -local lsp = require("lsp-setup") - -local servers = { - ansiblels = {}, - arduino_language_server = {}, - astro = {}, - bashls = {}, - beancount = {}, - clangd = {}, - cssls = {}, - docker_compose_language_service = {}, - dockerls = {}, - emmet_ls = {}, - eslint = {}, - gopls = {}, - julials = {}, - jsonls = {}, - ltex = { autostart = false }, - lua_ls = { - settings = { - Lua = { - diagnostics = { globals = { "vim" } }, - -- enable when working on neovim stuff. Takes *long* to load - -- workspace = { library = vim.api.nvim_get_runtime_file("", true) }, - telemetry = { enable = false }, - hint = { - enable = true, - setType = true, - }, - }, - }, - }, - marksman = {}, - basedpyright = {}, - ruff = {}, - serve_d = {}, - taplo = {}, - texlab = {}, - tsserver = {}, - yamlls = {}, -} - -local function on_attach(_, bufnr) - local map = vim.keymap.set - map("n", "[d", "lua vim.diagnostic.goto_prev()", { buffer = bufnr, desc = "Previous diagnostic" }) - map("n", "]d", "lua vim.diagnostic.goto_next()", { buffer = bufnr, desc = "Next diagnostic" }) - map( - "n", - "[D", - "lua vim.diagnostic.goto_prev({severity = vim.diagnostic.severity.ERROR})", - { buffer = bufnr, desc = "Previous error" } - ) - map( - "n", - "]D", - "lua vim.diagnostic.goto_next({severity = vim.diagnostic.severity.ERROR})", - { buffer = bufnr, desc = "Next error" } - ) - - if require("core.util").is_available("which-key") then - require("which-key").add({ "l", group = "language" }) - end - map( - "n", - "ld", - "lua vim.diagnostic.open_float()", - { buffer = bufnr, desc = "Line diagnostics" } - ) - map("n", "li", function() - vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled()) - end, { buffer = bufnr, desc = "Inlay hints" }) - map("n", "la", "lua vim.lsp.buf.code_action()", { buffer = bufnr, desc = "Codeactions" }) - map("n", "ln", "lua vim.lsp.buf.rename()", { buffer = bufnr, desc = "Rename element" }) - if vim.fn.exists(":Glance") then - map("n", "lr", "Glance references", { buffer = bufnr, desc = "References" }) - map("n", "lf", "Glance definitions", { buffer = bufnr, desc = "Definition" }) - map("n", "lt", "Glance type_definitions", { buffer = bufnr, desc = "Type definition" }) - map("n", "lm", "Glance implementations", { buffer = bufnr, desc = "Implementation" }) - elseif vim.fn.exists(":Telescope") then - map("n", "lr", "Telescope lsp_references", { buffer = bufnr, desc = "References" }) - map("n", "lf", "Telescope lsp_definitions", { buffer = bufnr, desc = "Definition" }) - map( - "n", - "lt", - "Telescope lsp_type_definitions", - { buffer = bufnr, desc = "Type definition" } - ) - map( - "n", - "lm", - "Telescope lsp_implementations", - { buffer = bufnr, desc = "Implementation" } - ) - else - map("n", "lr", "lua vim.lsp.buf.references()", { buffer = bufnr, desc = "References" }) - map("n", "lf", "lua vim.lsp.buf.definition()", { buffer = bufnr, desc = "Definition" }) - map( - "n", - "lt", - "lua vim.lsp.buf.type_definition()", - { buffer = bufnr, desc = "Type definition" } - ) - map( - "n", - "lm", - "lua vim.lsp.buf.implementation()", - { buffer = bufnr, desc = "Implementation" } - ) - end - map("n", "K", "lua vim.lsp.buf.hover()", { buffer = bufnr, desc = "Hover definition" }) - map("n", "lc", "lua vim.lsp.buf.declaration()", { buffer = bufnr, desc = "Declaration" }) - map( - "n", - "ls", - "lua vim.lsp.buf.signature_help()", - { buffer = bufnr, desc = "Signature help" } - ) - map("n", "lo", function() - if vim.diagnostic.is_disabled(0) then - vim.diagnostic.enable(0) - else - vim.diagnostic.disable(0) - end - end, { buffer = bufnr, desc = "Toggle Diagnostics" }) -end - --- Display diagnostics as virtual text only if not in insert mode --- /r/neovim/comments/12inp4c/disable_diagnostics_virtual_text_when_in_insert/jqqifwk/ -vim.api.nvim_create_autocmd("InsertEnter", { - callback = function() - vim.diagnostic.config({ virtual_text = false }) - end, -}) -vim.api.nvim_create_autocmd("InsertLeave", { - callback = function() - vim.diagnostic.config({ virtual_text = true }) - end, -}) - -lsp.setup({ - default_mappings = false, - servers = servers, - on_attach = on_attach, - inlay_hints = { - enabled = vim.fn.has("nvim-0.10") == true and true or false, - }, -}) - -local lspconfig = require("lspconfig") -lspconfig.nushell.setup({}) - -lspconfig.marksman.setup({ - filetypes = { "markdown", "quarto" }, - on_attach = function(client, bufnr) - -- TODO: for some reason this stays true even after rootdir switch? - if client.config.in_zk_notebook then - vim.defer_fn(function() - vim.lsp.buf_detach_client(bufnr, client.id) - end, 1000) - end - on_attach(client, bufnr) - end, - on_new_config = function(conf, new_root) - if require("lspconfig.util").root_pattern(".zk")(new_root) then - conf.in_zk_notebook = true - else - conf.in_zk_notebook = false - end - end, -}) - -local python_path --- ensure python virtualenv is determined automatically on lsp start -lspconfig.basedpyright.setup({ - on_attach = function(client, bufnr) - on_attach(client, bufnr) - require("core.util").set_python_env() - if python_path == nil then - python_path, _ = vim.fn.expand(require("core.util").get_python_venv_bin(client.config.root_dir)) - end - -- print(string.format("[PYTHON VENV]: %s", vim.inspect(python_path))) - client.config.settings.python = {} or client.config.settings.python - client.config.settings.python.pythonPath = python_path - end, - settings = { - -- disable imports and linting since, we use ruff for that - pyright = { - disableOrganizeImports = true, - }, - python = { - analysis = { - ignore = { "*" }, - }, - }, - }, -}) -lspconfig.ruff.setup({ - on_attach = function(client, bufnr) - on_attach(client, bufnr) - require("core.util").set_python_env() - client.server_capabilities.hoverProvider = false -- we use pyright for hover info - if python_path == nil then - python_path, _ = vim.fn.expand(require("core.util").get_python_venv_bin(client.config.root_dir)) - end - client.config.settings.python = {} or client.config.settings.python - client.config.settings.python.pythonPath = python_path - end, -}) - --- set up arduino with the help of arduino.nvim plugin -if require("core.util").is_available("arduino") then - lspconfig.arduino_language_server.setup({ - on_new_config = require("arduino").on_new_config, - }) -end - --- attach ltex for fitting ft only when spell checking becomes enabled -vim.api.nvim_create_autocmd("User", { - pattern = "SpellEnable", - callback = function() - lspconfig.ltex.setup({ - on_attach = function(client, bufnr) - on_attach(client, bufnr) - if require("core.util").is_available("ltex_extra") then - require("ltex_extra").setup() - end - end, - settings = { - ltex = { - language = vim.opt.spelllang:get(), - }, - }, - }) - vim.cmd("LspStart ltex") - end, -}) diff --git a/nvim/.config/nvim/lua/plugins/formatting.lua b/nvim/.config/nvim/lua/plugins/formatting.lua new file mode 100644 index 0000000..77f610d --- /dev/null +++ b/nvim/.config/nvim/lua/plugins/formatting.lua @@ -0,0 +1,121 @@ +---@param bufnr integer +---@param ... string +---@return string +local function first(bufnr, ...) + local conform = require("conform") + for i = 1, select("#", ...) do + local formatter = select(i, ...) + if conform.get_formatter_info(formatter, bufnr).available then + return formatter + end + end + return select(1, ...) +end +local formatters = { + angular = { "prettierd", "prettier", stop_after_first = true }, + astro = { "prettierd", "prettier", stop_after_first = true }, + bash = { "shfmt" }, + bib = { "bibtex-tidy" }, + css = function(bufnr) + return { first(bufnr, "prettierd", "prettier"), "rustywind" } + end, + graphql = { "prettierd", "prettier", stop_after_first = true }, + html = function(bufnr) + return { first(bufnr, "prettierd", "prettier"), "rustywind" } + end, + javascript = { "prettierd", "prettier", stop_after_first = true }, + javascriptreact = { "prettierd", "prettier", stop_after_first = true }, + json = { "jq" }, + liquid = { "prettierd", "prettier", stop_after_first = true }, + lua = { "stylua" }, + markdown = function(bufnr) + return { first(bufnr, "prettierd", "prettier"), "injected" } + end, + python = { "ruff_fix", "ruff_format", "ruff_organize_imports" }, + quarto = function(bufnr) + return { first(bufnr, "prettierd", "prettier"), "injected" } + end, + sh = { "shfmt" }, + sql = { "sleek" }, + svelte = { "prettierd", "prettier", stop_after_first = true }, + typescript = { "prettierd", "prettier", stop_after_first = true }, + typescriptreact = { "prettierd", "prettier", stop_after_first = true }, + vue = function(bufnr) + return { first(bufnr, "prettierd", "prettier"), "rustywind" } + end, + yaml = { "prettierd", "prettier", stop_after_first = true }, + zsh = { "shfmt" }, +} + +return { + + -- formatting setup + { + "zapling/mason-conform.nvim", + dependencies = { + { + "stevearc/conform.nvim", + config = function() + require("conform").setup({ + + lsp_format = "fallback", + + format_after_save = function(bufnr) + if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then + return + end + return { lsp_fallback = true } + end, + formatters_by_ft = formatters, + }) + vim.api.nvim_create_user_command("FormatDisable", function(args) + if args.bang then + vim.g.disable_autoformat = true + else + -- FormatDisable! will disable formatting globally + vim.b.disable_autoformat = true + end + end, { + desc = "Disable formatting on save", + bang = true, + }) + vim.api.nvim_create_user_command("FormatEnable", function() + vim.b.disable_autoformat = false + vim.g.disable_autoformat = false + end, { + desc = "Enable formatting on save", + }) + end, + cmd = { "ConformInfo" }, + keys = { + { + "ll", + function() + require("conform").format({ async = true, lsp_fallback = true }) + end, + mode = { "n", "v" }, + desc = "Format buffer", + }, + { + "lL", + function() + vim.g.disable_autoformat = not vim.g.disable_autoformat + end, + desc = "Toggle AutoFormat", + }, + { + "vf", + ":ConformInfo", + desc = "ConformInfo", + }, + }, + init = function() + -- If you want the formatexpr, here is the place to set it + vim.o.formatexpr = "v:lua.require'conform'.formatexpr()" + end, + }, + }, + event = { "BufWritePre" }, + opts = {}, + }, +} diff --git a/nvim/.config/nvim/lua/plugins/ide.lua b/nvim/.config/nvim/lua/plugins/ide.lua index e82bd18..5d24fe5 100644 --- a/nvim/.config/nvim/lua/plugins/ide.lua +++ b/nvim/.config/nvim/lua/plugins/ide.lua @@ -1,103 +1,4 @@ -} -local formatters = { - angular = { { "prettierd", "prettier" } }, - astro = { { "prettierd", "prettier" } }, - bash = { "shfmt" }, - bib = { "bibtex-tidy" }, - css = { { "prettierd", "prettier" }, "rustywind" }, - graphql = { { "prettierd", "prettier" } }, - html = { { "prettierd", "prettier" }, "rustywind" }, - javascript = { { "prettierd", "prettier" } }, - javascriptreact = { { "prettierd", "prettier" } }, - json = { "jq" }, - liquid = { { "prettierd", "prettier" } }, - lua = { "stylua" }, - markdown = { { "prettierd", "prettier" } }, - python = { "ruff_fix", "ruff_format", "ruff_organize_imports" }, - sh = { "shfmt" }, - svelte = { { "prettierd", "prettier" } }, - typescript = { { "prettierd", "prettier" } }, - typescriptreact = { { "prettierd", "prettier" } }, - vue = { { "prettierd", "prettier" }, "rustywind" }, - yaml = { { "prettierd", "prettier" } }, - zsh = { "shfmt" }, -} - return { - - - - -- formatting setup - { - "zapling/mason-conform.nvim", - dependencies = { - { - "stevearc/conform.nvim", - config = function() - require("conform").setup({ - - lsp_format = "fallback", - - format_after_save = function(bufnr) - if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then - return - end - return { lsp_fallback = true } - end, - formatters_by_ft = formatters, - }) - vim.api.nvim_create_user_command("FormatDisable", function(args) - if args.bang then - vim.g.disable_autoformat = true - else - -- FormatDisable! will disable formatting globally - vim.b.disable_autoformat = true - end - end, { - desc = "Disable formatting on save", - bang = true, - }) - vim.api.nvim_create_user_command("FormatEnable", function() - vim.b.disable_autoformat = false - vim.g.disable_autoformat = false - end, { - desc = "Enable formatting on save", - }) - end, - cmd = { "ConformInfo" }, - keys = { - { - "ll", - function() - require("conform").format({ async = true, lsp_fallback = true }) - end, - mode = { "n", "v" }, - desc = "Format buffer", - }, - { - "lL", - function() - vim.g.disable_autoformat = not vim.g.disable_autoformat - end, - desc = "Toggle AutoFormat", - }, - { - "vf", - ":ConformInfo", - desc = "ConformInfo", - }, - }, - init = function() - -- If you want the formatexpr, here is the place to set it - vim.o.formatexpr = "v:lua.require'conform'.formatexpr()" - end, - }, - }, - event = { "BufWritePre" }, - opts = {}, - }, - - -- automatic docstring creation for a variety of languages { "danymat/neogen",