summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbouzar Parvan <[email protected]>2021-07-10 22:48:28 +0430
committerGitHub <[email protected]>2021-07-10 14:18:28 -0400
commit0f7c876e93bdf42337257e70b5c9c9039a40bb47 (patch)
treefd6794b949c2a0fd7dc7bef25b8b998b288de145
parent50202efd0d409e51d7a0b09d8c923482e7488351 (diff)
WIP: using formatter.nvim instead of neoformat (#781)
-rw-r--r--lua/default-config.lua69
-rw-r--r--lua/lv-formatter/init.lua61
-rw-r--r--lua/lv-neoformat/init.lua23
-rw-r--r--lua/lv-utils/init.lua2
-rw-r--r--lua/plugins.lua6
-rw-r--r--utils/installer/lv-config.example.lua6
6 files changed, 137 insertions, 30 deletions
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"}, {