From b5e14de3077c251ad0b04308fdc59da812f1df9d Mon Sep 17 00:00:00 2001 From: EGRrqq <86194387+EGRrqq@users.noreply.github.com> Date: Sun, 24 Sep 2023 13:38:43 -0500 Subject: [PATCH] feat(cfg): add prettierd formatting on save --- init.lua | 4 +-- lua/custom/plugins/null-ls.lua | 43 +++++++++++++++++++++++++++++++++ lua/custom/plugins/prettier.lua | 43 +++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 lua/custom/plugins/null-ls.lua create mode 100644 lua/custom/plugins/prettier.lua diff --git a/init.lua b/init.lua index 6fa9ff19..f70cfae8 100644 --- a/init.lua +++ b/init.lua @@ -215,7 +215,7 @@ require('lazy').setup({ -- NOTE: Next Step on Your Neovim Journey: Add/Configure additional "plugins" for kickstart -- These are some example plugins that I've included in the kickstart repository. -- Uncomment any of the lines below to enable them. - require 'kickstart.plugins.autoformat', + -- require 'kickstart.plugins.autoformat', require 'kickstart.plugins.debug', -- NOTE: The import below can automatically add your own plugins, configuration, etc from `lua/custom/plugins/*.lua` @@ -454,7 +454,7 @@ local servers = { -- gopls = {}, -- pyright = {}, -- rust_analyzer = {}, - tsserver = {}, + -- tsserver = {}, -- html = { filetypes = { 'html', 'twig', 'hbs'} }, -- requirement for https://github.com/olrtg/nvim-emmet diff --git a/lua/custom/plugins/null-ls.lua b/lua/custom/plugins/null-ls.lua new file mode 100644 index 00000000..24a1df0a --- /dev/null +++ b/lua/custom/plugins/null-ls.lua @@ -0,0 +1,43 @@ +return { + 'jose-elias-alvarez/null-ls.nvim', + dependencies = { + "nvim-lua/plenary.nvim", + }, + + config = function() + + local null_ls = require("null-ls") + + local group = vim.api.nvim_create_augroup("lsp_format_on_save", { clear = false }) + local event = "BufWritePre" -- or "BufWritePost" + local async = event == "BufWritePost" + + null_ls.setup({ + on_attach = function(client, bufnr) + if client.supports_method("textDocument/formatting") then + vim.keymap.set("n", "f", function() + vim.lsp.buf.format({ bufnr = vim.api.nvim_get_current_buf() }) + end, { buffer = bufnr, desc = "[lsp] format" }) + + -- format on save + vim.api.nvim_clear_autocmds({ buffer = bufnr, group = group }) + vim.api.nvim_create_autocmd(event, { + buffer = bufnr, + group = group, + callback = function() + vim.lsp.buf.format({ bufnr = bufnr, async = async }) + end, + desc = "[lsp] format on save", + }) + end + + if client.supports_method("textDocument/rangeFormatting") then + vim.keymap.set("x", "f", function() + vim.lsp.buf.format({ bufnr = vim.api.nvim_get_current_buf() }) + end, { buffer = bufnr, desc = "[lsp] format" }) + end + end, + }) + + end, +} diff --git a/lua/custom/plugins/prettier.lua b/lua/custom/plugins/prettier.lua new file mode 100644 index 00000000..cf6dfb95 --- /dev/null +++ b/lua/custom/plugins/prettier.lua @@ -0,0 +1,43 @@ +return { + 'MunifTanjim/prettier.nvim', + + config = function() + + local prettier = require("prettier") + + prettier.setup({ + bin = 'prettier', -- or `'prettierd'` (v0.23.3+) + filetypes = { + "css", + "graphql", + "html", + "javascript", + "javascriptreact", + "json", + "less", + "markdown", + "scss", + "typescript", + "typescriptreact", + "yaml", + }, + }) + + prettier.setup({ + ["null-ls"] = { + condition = function() + return prettier.config_exists({ + -- if `false`, skips checking `package.json` for `"prettier"` key + check_package_json = true, + }) + end, + runtime_condition = function(params) + -- return false to skip running prettier + return true + end, + timeout = 5000, + } + }) + + end, +}