diff options
author | Abouzar Parvan <[email protected]> | 2021-07-14 04:19:36 +0430 |
---|---|---|
committer | GitHub <[email protected]> | 2021-07-13 19:49:36 -0400 |
commit | de7ec62f5fde20ca1d7e53ed83a0077f8945f67c (patch) | |
tree | 1d54b2232489e53a98025825ed550c28e7adea95 | |
parent | 6bbc082f5758cdb57ecb25f270e5558ab63d3754 (diff) |
Added default formatters (#908)
* added default formatters for a couple of langs
* fix: sugesstions from tastyep
* allow users to change default formatters
* suggestion: mellow pointed out the telescope issue
* we don't need to use formatter.setup
-rw-r--r-- | ftplugin/c.lua | 16 | ||||
-rw-r--r-- | ftplugin/dart.lua | 14 | ||||
-rw-r--r-- | ftplugin/go.lua | 15 | ||||
-rw-r--r-- | ftplugin/json.lua | 14 | ||||
-rw-r--r-- | ftplugin/lua.lua | 20 | ||||
-rw-r--r-- | ftplugin/php.lua | 14 | ||||
-rw-r--r-- | ftplugin/python.lua | 15 | ||||
-rw-r--r-- | ftplugin/ruby.lua | 14 | ||||
-rw-r--r-- | ftplugin/rust.lua | 16 | ||||
-rw-r--r-- | ftplugin/sh.lua | 16 | ||||
-rw-r--r-- | ftplugin/tex.lua | 5 | ||||
-rw-r--r-- | ftplugin/yaml.lua | 14 | ||||
-rw-r--r-- | ftplugin/zsh.lua | 2 | ||||
-rw-r--r-- | lua/core/formatter.lua | 2 | ||||
-rw-r--r-- | lua/default-config.lua | 8 | ||||
-rw-r--r-- | lua/lsp/tsserver-ls.lua | 17 |
16 files changed, 164 insertions, 38 deletions
diff --git a/ftplugin/c.lua b/ftplugin/c.lua index f9064213..8beaae5d 100644 --- a/ftplugin/c.lua +++ b/ftplugin/c.lua @@ -1,3 +1,19 @@ +O.formatters.filetype["c"] = { + function() + return { + exe = O.lang.c.formatter.exe, + args = O.lang.c.formatter.args, + stdin = not (O.lang.c.formatter.stdin ~= nil), + } + end, +} +O.formatters.filetype["cpp"] = O.formatters.filetype["c"] + +require("formatter.config").set_defaults { + logging = false, + filetype = O.formatters.filetype, +} + if require("lv-utils").check_lsp_client_active "clangd" then return end diff --git a/ftplugin/dart.lua b/ftplugin/dart.lua index a0de0fd5..f9b68e9d 100644 --- a/ftplugin/dart.lua +++ b/ftplugin/dart.lua @@ -1,3 +1,17 @@ +O.formatters.filetype["dart"] = { + function() + return { + exe = O.lang.dart.formatter.exe, + args = O.lang.dart.formatter.args, + stdin = not (O.lang.dart.formatter.stdin ~= nil), + } + end, +} + +require("formatter.config").set_defaults { + logging = false, + filetype = O.formatters.filetype, +} if require("lv-utils").check_lsp_client_active "dartls" then return end diff --git a/ftplugin/go.lua b/ftplugin/go.lua index fbad5a2b..47de932f 100644 --- a/ftplugin/go.lua +++ b/ftplugin/go.lua @@ -1,3 +1,18 @@ +O.formatters.filetype["go"] = { + function() + return { + exe = O.lang.go.formatter.exe, + args = O.lang.go.formatter.args, + stdin = not (O.lang.go.formatter.stdin ~= nil), + } + end, +} + +require("formatter.config").set_defaults { + logging = false, + filetype = O.formatters.filetype, +} + if not require("lv-utils").check_lsp_client_active "gopls" then require("lspconfig").gopls.setup { cmd = { DATA_PATH .. "/lspinstall/go/gopls" }, diff --git a/ftplugin/json.lua b/ftplugin/json.lua index c7246e76..cb73995c 100644 --- a/ftplugin/json.lua +++ b/ftplugin/json.lua @@ -1,3 +1,17 @@ +O.formatters.filetype["json"] = { + function() + return { + exe = O.lang.json.formatter.exe, + args = O.lang.json.formatter.args, + stdin = not (O.lang.json.formatter.stdin ~= nil), + } + end, +} + +require("formatter.config").set_defaults { + logging = false, + filetype = O.formatters.filetype, +} if require("lv-utils").check_lsp_client_active "jsonls" then return end diff --git a/ftplugin/lua.lua b/ftplugin/lua.lua index 8305dc2c..b098a77d 100644 --- a/ftplugin/lua.lua +++ b/ftplugin/lua.lua @@ -1,11 +1,9 @@ O.formatters.filetype["lua"] = { - -- prettier function() return { - exe = "stylua", - -- TODO: append to this for args don't overwrite - args = {}, - stdin = false, + exe = O.lang.lua.formatter.exe, + args = O.lang.lua.formatter.args, + stdin = not (O.lang.lua.formatter.stdin ~= nil), } end, } @@ -48,16 +46,4 @@ if not require("lv-utils").check_lsp_client_active "sumneko_lua" then } end -if O.lang.lua.autoformat then - require("lv-utils").define_augroups { - _lua_autoformat = { - { - "BufWritePre", - "*.lua", - "lua vim.lsp.buf.formatting_sync(nil, 1000)", - }, - }, - } -end - vim.cmd "setl ts=2 sw=2" diff --git a/ftplugin/php.lua b/ftplugin/php.lua index 713ef44e..054dff79 100644 --- a/ftplugin/php.lua +++ b/ftplugin/php.lua @@ -1,3 +1,17 @@ +O.formatters.filetype["php"] = { + function() + return { + exe = O.lang.php.formatter.exe, + args = O.lang.php.formatter.args, + stdin = not (O.lang.php.formatter.stdin ~= nil), + } + end, +} + +require("formatter.config").set_defaults { + logging = false, + filetype = O.formatters.filetype, +} if require("lv-utils").check_lsp_client_active "intelephense" then return end diff --git a/ftplugin/python.lua b/ftplugin/python.lua index d6b98116..d2ade7e2 100644 --- a/ftplugin/python.lua +++ b/ftplugin/python.lua @@ -1,3 +1,18 @@ +O.formatters.filetype["python"] = { + function() + return { + exe = O.lang.python.formatter.exe, + args = O.lang.python.formatter.args, + stdin = not (O.lang.python.formatter.stdin ~= nil), + } + end, +} + +require("formatter.config").set_defaults { + logging = false, + filetype = O.formatters.filetype, +} + local python_arguments = {} -- TODO: replace with path argument diff --git a/ftplugin/ruby.lua b/ftplugin/ruby.lua index 7f86ffb9..ff7e7c8b 100644 --- a/ftplugin/ruby.lua +++ b/ftplugin/ruby.lua @@ -1,3 +1,17 @@ +O.formatters.filetype["ruby"] = { + function() + return { + exe = O.lang.ruby.formatter.exe, + args = O.lang.ruby.formatter.args, + stdin = not (O.lang.ruby.formatter.stdin ~= nil), + } + end, +} + +require("formatter.config").set_defaults { + logging = false, + filetype = O.formatters.filetype, +} if require("lv-utils").check_lsp_client_active "solargraph" then return end diff --git a/ftplugin/rust.lua b/ftplugin/rust.lua index 04fc4249..dd34483e 100644 --- a/ftplugin/rust.lua +++ b/ftplugin/rust.lua @@ -1,3 +1,17 @@ +O.formatters.filetype["rust"] = { + function() + return { + exe = O.lang.rust.formatter.exe, + args = O.lang.rust.formatter.args, + stdin = not (O.lang.rust.formatter.stdin ~= nil), + } + end, +} + +require("formatter.config").set_defaults { + logging = false, + filetype = O.formatters.filetype, +} if require("lv-utils").check_lsp_client_active "rust_analyzer" then return end @@ -81,7 +95,7 @@ else cmd = { DATA_PATH .. "/lspinstall/rust/rust-analyzer" }, on_attach = require("lsp").common_on_attach, filetypes = { "rust" }, - root_dir = require("lspconfig.util").root_pattern("Cargo.toml", "rust-project.json"), + root_dir = require("lspconfig.util").root_pattern("Carrust.toml", "rust-project.json"), } end diff --git a/ftplugin/sh.lua b/ftplugin/sh.lua index 2c96dfd1..54002f19 100644 --- a/ftplugin/sh.lua +++ b/ftplugin/sh.lua @@ -1,3 +1,17 @@ +O.formatters.filetype["sh"] = { + function() + return { + exe = O.lang.sh.formatter.exe, + args = O.lang.sh.formatter.args, + stdin = not (O.lang.sh.formatter.stdin ~= nil), + } + end, +} + +require("formatter.config").set_defaults { + logging = false, + filetype = O.formatters.filetype, +} if not require("lv-utils").check_lsp_client_active "bashls" then -- npm i -g bash-language-server require("lspconfig").bashls.setup { @@ -26,7 +40,7 @@ if not require("lv-utils").check_lsp_client_active "efm" then -- init_options = {initializationOptions}, cmd = { DATA_PATH .. "/lspinstall/efm/efm-langserver" }, init_options = { documentFormatting = true, codeAction = false }, - root_dir = require("lspconfig").util.root_pattern(".git/"), + root_dir = require("lspconfig").util.root_pattern ".git/", filetypes = { "sh" }, settings = { rootMarkers = { ".git/" }, diff --git a/ftplugin/tex.lua b/ftplugin/tex.lua index a81a6ff7..c5748189 100644 --- a/ftplugin/tex.lua +++ b/ftplugin/tex.lua @@ -33,7 +33,6 @@ vim.api.nvim_exec( ]], false ) -if (O.lang.latex.auto_save) -then - vim.api.nvim_exec([[au FocusLost * :wa]],false) +if O.lang.latex.auto_save then + vim.api.nvim_exec([[au FocusLost * :wa]], false) end diff --git a/ftplugin/yaml.lua b/ftplugin/yaml.lua index 219022f5..f1dfc5dc 100644 --- a/ftplugin/yaml.lua +++ b/ftplugin/yaml.lua @@ -1,3 +1,17 @@ +O.formatters.filetype["yaml"] = { + function() + return { + exe = O.lang.yaml.formatter.exe, + args = O.lang.yaml.formatter.args, + stdin = not (O.lang.yaml.formatter.stdin ~= nil), + } + end, +} + +require("formatter.config").set_defaults { + logging = false, + filetype = O.formatters.filetype, +} if require("lv-utils").check_lsp_client_active "yamlls" then return end diff --git a/ftplugin/zsh.lua b/ftplugin/zsh.lua index 6960608b..750e8ede 100644 --- a/ftplugin/zsh.lua +++ b/ftplugin/zsh.lua @@ -24,7 +24,7 @@ if not require("lv-utils").check_lsp_client_active "efm" then -- init_options = {initializationOptions}, cmd = { DATA_PATH .. "/lspinstall/efm/efm-langserver" }, init_options = { documentFormatting = true, codeAction = false }, - root_dir = require("lspconfig").util.root_pattern(".git/"), + root_dir = require("lspconfig").util.root_pattern ".git/", filetypes = { "zsh" }, settings = { rootMarkers = { ".git/" }, diff --git a/lua/core/formatter.lua b/lua/core/formatter.lua index 06fcb56d..05de74ab 100644 --- a/lua/core/formatter.lua +++ b/lua/core/formatter.lua @@ -53,8 +53,6 @@ if not status_ok then return end -formatter.setup {} - if not O.format_on_save then vim.cmd [[if exists('#autoformat#BufWritePost') :autocmd! autoformat diff --git a/lua/default-config.lua b/lua/default-config.lua index 8eca53c9..df17d0af 100644 --- a/lua/default-config.lua +++ b/lua/default-config.lua @@ -258,11 +258,6 @@ O = { "typescript", "typescriptreact", }, - formatter = { - exe = "prettier", - args = { "--write", "--stdin-filepath", vim.api.nvim_buf_get_name(0), "--single-quote" }, - stdin = false, - }, }, terraform = {}, tsserver = { @@ -275,8 +270,7 @@ O = { }, formatter = { exe = "prettier", - args = { "--write", "--stdin-filepath", vim.api.nvim_buf_get_name(0), "--single-quote" }, - stdin = false, + args = { "--stdin-filepath", vim.api.nvim_buf_get_name(0), "--single-quote" }, }, }, vim = {}, diff --git a/lua/lsp/tsserver-ls.lua b/lua/lsp/tsserver-ls.lua index c95b7810..22c58950 100644 --- a/lua/lsp/tsserver-ls.lua +++ b/lua/lsp/tsserver-ls.lua @@ -1,18 +1,23 @@ vim.cmd "let proj = FindRootDirectory()" -print(vim.api.nvim_get_var "proj") local root_dir = vim.api.nvim_get_var "proj" + +-- use the global prettier if you didn't find the local one +local prettier_instance = root_dir .. "/node_modules/.bin/prettier" +if vim.fn.executable(prettier_instance) ~= 1 then + prettier_instance = O.lang.tsserver.formatter.exe +end + O.formatters.filetype["javascriptreact"] = { - -- vim.cmd "let root_dir " - -- prettier function() return { - exe = root_dir .. "/node_modules/.bin/prettier", - -- TODO: append to this for args don't overwrite + exe = prettier_instance, + -- TODO: allow user to override this args = { "--stdin-filepath", vim.api.nvim_buf_get_name(0), "--single-quote" }, - stdin = true, + stdin = true } end, } +O.formatters.filetype["javascript"] = O.formatters.filetype["javascriptreact"] require("formatter.config").set_defaults { logging = false, |