From bc702160c26bfe61a6106418651ddd0accb911d6 Mon Sep 17 00:00:00 2001 From: PeteChu Date: Sun, 9 Apr 2023 14:18:18 +0700 Subject: [PATCH] Add Mason and Null-ls plugins for Neovim --- init.lua | 12 ---------- lua/custom/plugins/mason-null-ls.lua | 19 +++++++++++++++ lua/custom/plugins/null-ls.lua | 36 ++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 12 deletions(-) create mode 100644 lua/custom/plugins/mason-null-ls.lua create mode 100644 lua/custom/plugins/null-ls.lua diff --git a/init.lua b/init.lua index da694988..75f608ea 100644 --- a/init.lua +++ b/init.lua @@ -467,18 +467,6 @@ local servers = { -- pyright = {}, -- rust_analyzer = {}, -- tsserver = {}, - efm = { - init_options = { documentFormatting = true }, - settings = { - rootMarkers = { ".git/" }, - languages = { - prettier = { - formatCommand = 'prettierd "${INPUT}"', - formatStdin = true, - } - } - } - }, lua_ls = { Lua = { workspace = { checkThirdParty = false }, diff --git a/lua/custom/plugins/mason-null-ls.lua b/lua/custom/plugins/mason-null-ls.lua new file mode 100644 index 00000000..0e4c1b6d --- /dev/null +++ b/lua/custom/plugins/mason-null-ls.lua @@ -0,0 +1,19 @@ +return { + "jay-babu/mason-null-ls.nvim", + event = { "BufReadPre", "BufNewFile" }, + dependencies = { + "williamboman/mason.nvim", + "jose-elias-alvarez/null-ls.nvim", + }, + config = function() + require("mason-null-ls").setup({ + -- list of formatters & linters for mason to install + ensure_installed = { + "prettierd", -- ts/js formatter + "eslint_d", -- ts/js linter + }, + -- auto-install configured formatters & linters (with null-ls) + automatic_installation = true, + }) + end, +} diff --git a/lua/custom/plugins/null-ls.lua b/lua/custom/plugins/null-ls.lua new file mode 100644 index 00000000..aeabcd70 --- /dev/null +++ b/lua/custom/plugins/null-ls.lua @@ -0,0 +1,36 @@ +return { + "jose-elias-alvarez/null-ls.nvim", + config = function() + local null_ls = require("null-ls") + + local formatting = null_ls.builtins.formatting -- to setup formatters + local diagnostics = null_ls.builtins.diagnostics -- to setup linters + + local augroup = vim.api.nvim_create_augroup("LspFormatting", {}) + + null_ls.setup({ + sources = { + formatting.prettierd, + diagnostics.eslint_d + }, + on_attach = function(current_client, bufnr) + if current_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() + vim.lsp.buf.format({ + filter = function(client) + -- only use null-ls for formatting instead of lsp server + return client.name == "null-ls" + end, + bufnr = bufnr, + }) + end, + }) + end + end + }) + end +}