From 0f7c876e93bdf42337257e70b5c9c9039a40bb47 Mon Sep 17 00:00:00 2001 From: Abouzar Parvan Date: Sat, 10 Jul 2021 22:48:28 +0430 Subject: [PATCH] WIP: using formatter.nvim instead of neoformat (#781) --- lua/default-config.lua | 69 +++++++++++++++++++++++++-- lua/lv-formatter/init.lua | 61 +++++++++++++++++++++++ lua/lv-neoformat/init.lua | 23 --------- lua/lv-utils/init.lua | 2 +- lua/plugins.lua | 6 +-- utils/installer/lv-config.example.lua | 6 +++ 6 files changed, 137 insertions(+), 30 deletions(-) create mode 100644 lua/lv-formatter/init.lua delete mode 100644 lua/lv-neoformat/init.lua diff --git a/lua/default-config.lua b/lua/default-config.lua index d6ddfe6b..6b0dff73 100644 --- a/lua/default-config.lua +++ b/lua/default-config.lua @@ -88,7 +88,12 @@ O = { }, lang = { - cmake = {}, + cmake = { + formatter = { + exe = "clang-format", + args = {}, + }, + }, clang = { diagnostics = { virtual_text = { spacing = 0, prefix = "" }, @@ -97,12 +102,21 @@ O = { }, cross_file_rename = true, header_insertion = "never", + filetypes = { "c", "cpp", "objc" }, + formatter = { + exe = "clang-format", + args = {}, + }, }, css = { virtual_text = true, }, dart = { sdk_path = "/usr/lib/dart/bin/snapshots/analysis_server.dart.snapshot", + formatter = { + exe = "dart", + args = { "format" }, + }, }, docker = {}, efm = {}, @@ -110,7 +124,12 @@ O = { emmet = { active = true }, elixir = {}, graphql = {}, - go = {}, + go = { + formatter = { + exe = "gofmt", + args = {}, + }, + }, html = {}, java = { java_tools = { @@ -123,6 +142,10 @@ O = { signs = true, underline = true, }, + formatter = { + exe = "python", + args = { "-m", "json.tool" }, + }, }, kotlin = {}, latex = {}, @@ -132,6 +155,11 @@ O = { signs = true, underline = true, }, + formatter = { + exe = "stylua", + args = { "--stdin-filepath", vim.api.nvim_buf_get_name(0) }, + stdin = false, + }, }, php = { format = { @@ -148,6 +176,11 @@ O = { underline = true, }, filetypes = { "php", "phtml" }, + formatter = { + exe = "phpcbf", + args = { "--standard=PSR12", vim.api.nvim_buf_get_name(0) }, + stdin = false, + }, }, python = { linter = "", @@ -162,6 +195,10 @@ O = { auto_search_paths = true, use_library_code_types = true, }, + formatter = { + exe = "yapf", + args = {}, + }, }, ruby = { diagnostics = { @@ -170,6 +207,10 @@ O = { underline = true, }, filetypes = { "rb", "erb", "rakefile", "ruby" }, + formatter = { + exe = "rufo", + args = { "-x" }, + }, }, rust = { rust_tools = { @@ -177,6 +218,10 @@ O = { parameter_hints_prefix = "<-", other_hints_prefix = "=>", -- prefix for all the other hints (type, chaining) }, + formatter = { + exe = "rustfmt", + args = { "--emit=stdout" }, + }, linter = "", diagnostics = { virtual_text = { spacing = 0, prefix = "" }, @@ -193,6 +238,11 @@ O = { signs = true, underline = true, }, + formatter = { + exe = "shfmt", + args = { "-w" }, + stdin = false, + }, }, svelte = {}, tailwindcss = { @@ -206,6 +256,10 @@ O = { "typescript", "typescriptreact", }, + formatter = { + exe = "prettier", + args = { "--stdin-filepath", vim.api.nvim_buf_get_name(0), "--single-quote" }, + }, }, terraform = {}, tsserver = { @@ -216,9 +270,18 @@ O = { signs = true, underline = true, }, + formatter = { + exe = "prettier", + args = { "--stdin-filepath", vim.api.nvim_buf_get_name(0), "--single-quote" }, + }, }, vim = {}, - yaml = {}, + yaml = { + formatter = { + exe = "prettier", + args = { "--stdin-filepath", vim.api.nvim_buf_get_name(0), "--single-quote" }, + }, + }, }, } diff --git a/lua/lv-formatter/init.lua b/lua/lv-formatter/init.lua new file mode 100644 index 00000000..4c9a6ad0 --- /dev/null +++ b/lua/lv-formatter/init.lua @@ -0,0 +1,61 @@ +-- autoformat +if O.format_on_save then + require("lv-utils").define_augroups { + autoformat = { + { + "BufWritePost", + "*", + "FormatWrite", + }, + }, + } +end + +-- check if formatter has been defined for the language or not +function formatter_exists(lang_formatter) + if lang_formatter == nil then + return false + end + if lang_formatter.exe == nil or lang_formatter.args == nil then + return false + end + return true +end + +-- returns default formatter for given language +function formatter_return(lang_formatter) + return { + exe = lang_formatter.exe, + args = lang_formatter.args, + stdin = not (lang_formatter.stdin ~= nil), + } +end + +-- fill a table like this -> {rust: {exe:"sth",args:{"a","b"},stdin=true},go: {}...} +local formatter_filetypes = {} +for k, v in pairs(O.lang) do + if formatter_exists(v.formatter) then + local keys = v.filetypes + if keys == nil then + keys = {k} + end + for _, l in pairs(keys) do + formatter_filetypes[l] = { + function () + return formatter_return(v.formatter) + end, + } + end + end +end + +require("formatter").setup { + logging = false, + filetype = formatter_filetypes, +} + +if not O.format_on_save then + vim.cmd [[if exists('#autoformat#BufWritePost') + :autocmd! autoformat + endif]] +end diff --git a/lua/lv-neoformat/init.lua b/lua/lv-neoformat/init.lua deleted file mode 100644 index 4dbaad23..00000000 --- a/lua/lv-neoformat/init.lua +++ /dev/null @@ -1,23 +0,0 @@ --- autoformat -if O.format_on_save then - require("lv-utils").define_augroups { - autoformat = { - { - "BufWritePre", - "*", - [[try | undojoin | Neoformat | catch /^Vim\%((\a\+)\)\=:E790/ | finally | silent Neoformat | endtry]], - }, - }, - } -end - -vim.g.neoformat_run_all_formatters = 0 - -vim.g.neoformat_enabled_python = { "autopep8", "yapf", "docformatter" } -vim.g.neoformat_enabled_javascript = { "prettier" } - -if not O.format_on_save then - vim.cmd [[if exists('#autoformat#BufWritePre') - :autocmd! autoformat - endif]] -end diff --git a/lua/lv-utils/init.lua b/lua/lv-utils/init.lua index 00c806a2..d7482cd6 100644 --- a/lua/lv-utils/init.lua +++ b/lua/lv-utils/init.lua @@ -4,7 +4,7 @@ function lv_utils.reload_lv_config() vim.cmd "source ~/.config/nvim/lv-config.lua" vim.cmd "source ~/.config/nvim/lua/plugins.lua" vim.cmd "source ~/.config/nvim/lua/settings.lua" - vim.cmd "source ~/.config/nvim/lua/lv-neoformat/init.lua" + vim.cmd "source ~/.config/nvim/lua/lv-formatter/init.lua" vim.cmd ":PackerCompile" vim.cmd ":PackerInstall" end diff --git a/lua/plugins.lua b/lua/plugins.lua index d476f01c..779491db 100644 --- a/lua/plugins.lua +++ b/lua/plugins.lua @@ -75,11 +75,11 @@ return require("packer").startup(function(use) -- Treesitter use { "nvim-treesitter/nvim-treesitter" } - -- Neoformat + -- Formatter.nvim use { - "sbdchd/neoformat", + "mhartington/formatter.nvim", config = function() - require "lv-neoformat" + require "lv-formatter" end, event = "BufRead", } diff --git a/utils/installer/lv-config.example.lua b/utils/installer/lv-config.example.lua index b5ade7dd..234e3e77 100644 --- a/utils/installer/lv-config.example.lua +++ b/utils/installer/lv-config.example.lua @@ -40,6 +40,12 @@ O.lang.python.analysis.use_library_code_types = true -- javascript O.lang.tsserver.linter = nil +-- rust +O.lang.rust.formatter = { + exe = "rustfmt", + args = {"--emit=stdout"}, +} + -- Additional Plugins -- O.user_plugins = { -- {"folke/tokyonight.nvim"}, {