From 848408a2a432de862a96aba9e0bff8c0f7ef5201 Mon Sep 17 00:00:00 2001 From: Neeraj A Date: Wed, 22 May 2024 18:31:39 +0530 Subject: [PATCH] replace null-ls with conform and nvim-lint --- lua/custom/plugins/formatting.lua | 39 ++++++++++++++++++++ lua/custom/plugins/linting.lua | 29 +++++++++++++++ lua/custom/plugins/null-ls.lua | 60 ------------------------------- 3 files changed, 68 insertions(+), 60 deletions(-) create mode 100644 lua/custom/plugins/formatting.lua create mode 100644 lua/custom/plugins/linting.lua delete mode 100644 lua/custom/plugins/null-ls.lua diff --git a/lua/custom/plugins/formatting.lua b/lua/custom/plugins/formatting.lua new file mode 100644 index 00000000..75691135 --- /dev/null +++ b/lua/custom/plugins/formatting.lua @@ -0,0 +1,39 @@ +return { + "stevearc/conform.nvim", + event = { "BufReadPre", "BufNewFile" }, + config = function() + local conform = require("conform") + + conform.setup({ + formatters_by_ft = { + javascript = { "prettierd" }, + typescript = { "prettierd" }, + javascriptreact = { "prettierd" }, + typescriptreact = { "prettierd" }, + svelte = { "prettierd" }, + css = { "prettierd" }, + html = { "prettierd" }, + json = { "prettierd" }, + yaml = { "prettierd" }, + markdown = { "prettierd" }, + graphql = { "prettierd" }, + liquid = { "prettierd" }, + lua = { "stylua" }, + python = { "isort", "black" }, + }, + format_on_save = { + lsp_fallback = true, + async = false, + timeout_ms = 1000, + }, + }) + + vim.keymap.set({ "n", "v" }, "mp", function() + conform.format({ + lsp_fallback = true, + async = false, + timeout_ms = 1000, + }) + end, { desc = "Format file or range (in visual mode)" }) + end, +} diff --git a/lua/custom/plugins/linting.lua b/lua/custom/plugins/linting.lua new file mode 100644 index 00000000..e9fea9bc --- /dev/null +++ b/lua/custom/plugins/linting.lua @@ -0,0 +1,29 @@ +return { + "mfussenegger/nvim-lint", + event = { "BufReadPre", "BufNewFile" }, + config = function() + local lint = require("lint") + + lint.linters_by_ft = { + javascript = { "eslint_d" }, + typescript = { "eslint_d" }, + javascriptreact = { "eslint_d" }, + typescriptreact = { "eslint_d" }, + svelte = { "eslint_d" }, + python = { "pylint" }, + } + + local lint_augroup = vim.api.nvim_create_augroup("lint", { clear = true }) + + vim.api.nvim_create_autocmd({ "BufEnter", "BufWritePost", "InsertLeave" }, { + group = lint_augroup, + callback = function() + lint.try_lint() + end, + }) + + vim.keymap.set("n", "l", function() + lint.try_lint() + end, { desc = "Trigger linting for current file" }) + end, +} diff --git a/lua/custom/plugins/null-ls.lua b/lua/custom/plugins/null-ls.lua deleted file mode 100644 index 82353aec..00000000 --- a/lua/custom/plugins/null-ls.lua +++ /dev/null @@ -1,60 +0,0 @@ -return { - "jose-elias-alvarez/null-ls.nvim", - dependencies = {"nvim-lua/plenary.nvim"}, - config = function() - local null_ls = require("null-ls") - - local formatting = null_ls.builtins.formatting - local diagnostics = null_ls.builtins.diagnostics - local codeActions = null_ls.builtins.code_actions - - local sources = { - formatting.eslint_d, formatting.prettierd.with({ - env = { - PRETTIERD_DEFAULT_CONFIG = vim.fn - .expand "~/.config/nvim/utils/linter-config/.prettierrc.js" - } - }), -- formatting.prettier - -- .with({extra_args = {'--single-quote', '--tab-width 2', '--arrow-parens avoid'}}), - formatting.lua_format.with({ - extra_args = { - '--no-keep-simple-function-one-line', '--no-break-after-operator', '--column-limit=100', - '--break-after-table-lb', '--indent-width=2' - } - }), diagnostics.eslint_d.with({diagnostics_format = "[#{c}] #{m} (#{s})"}), - codeActions.eslint_d - } - - local lsp_formatting = function(bufnr) - vim.lsp.buf.format({ - -- async = true, - filter = function(client) - -- apply whatever logic you want (in this example, we'll only use null-ls) - return client.name == "null-ls" - end, - bufnr = bufnr - }) - end - - -- if you want to set up formatting on save, you can use this as a callback - local augroup = vim.api.nvim_create_augroup("LspFormatting", {}) - - local on_attach = function(client, bufnr) - -- if client.name == "tsserver" or client.name == "cssls" or client.name == "html" then - -- client.server_capabilities.documentFormattingProvider = false -- 0.8 and later - -- end - if client.supports_method("textDocument/formatting") then - vim.api.nvim_clear_autocmds({group = augroup, buffer = bufnr}) - vim.api.nvim_create_autocmd("BufWritePre", { - group = augroup, - buffer = bufnr, - callback = function() - lsp_formatting(bufnr) - end - }) - end - end - - null_ls.setup({sources = sources, debug = true, on_attach = on_attach}) - end -}