WIP: using formatter.nvim instead of neoformat (#781)

This commit is contained in:
Abouzar Parvan 2021-07-10 22:48:28 +04:30 committed by GitHub
parent 50202efd0d
commit 0f7c876e93
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 137 additions and 30 deletions

View file

@ -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" },
},
},
},
}

61
lua/lv-formatter/init.lua Normal file
View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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",
}

View file

@ -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"}, {