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
This commit is contained in:
Abouzar Parvan 2021-07-14 04:19:36 +04:30 committed by GitHub
parent 6bbc082f57
commit de7ec62f5f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 164 additions and 38 deletions

View file

@ -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 if require("lv-utils").check_lsp_client_active "clangd" then
return return
end end

View file

@ -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 if require("lv-utils").check_lsp_client_active "dartls" then
return return
end end

View file

@ -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 if not require("lv-utils").check_lsp_client_active "gopls" then
require("lspconfig").gopls.setup { require("lspconfig").gopls.setup {
cmd = { DATA_PATH .. "/lspinstall/go/gopls" }, cmd = { DATA_PATH .. "/lspinstall/go/gopls" },

View file

@ -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 if require("lv-utils").check_lsp_client_active "jsonls" then
return return
end end

View file

@ -1,11 +1,9 @@
O.formatters.filetype["lua"] = { O.formatters.filetype["lua"] = {
-- prettier
function() function()
return { return {
exe = "stylua", exe = O.lang.lua.formatter.exe,
-- TODO: append to this for args don't overwrite args = O.lang.lua.formatter.args,
args = {}, stdin = not (O.lang.lua.formatter.stdin ~= nil),
stdin = false,
} }
end, end,
} }
@ -48,16 +46,4 @@ if not require("lv-utils").check_lsp_client_active "sumneko_lua" then
} }
end 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" vim.cmd "setl ts=2 sw=2"

View file

@ -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 if require("lv-utils").check_lsp_client_active "intelephense" then
return return
end end

View file

@ -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 = {} local python_arguments = {}
-- TODO: replace with path argument -- TODO: replace with path argument

View file

@ -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 if require("lv-utils").check_lsp_client_active "solargraph" then
return return
end end

View file

@ -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 if require("lv-utils").check_lsp_client_active "rust_analyzer" then
return return
end end
@ -81,7 +95,7 @@ else
cmd = { DATA_PATH .. "/lspinstall/rust/rust-analyzer" }, cmd = { DATA_PATH .. "/lspinstall/rust/rust-analyzer" },
on_attach = require("lsp").common_on_attach, on_attach = require("lsp").common_on_attach,
filetypes = { "rust" }, 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 end

View file

@ -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 if not require("lv-utils").check_lsp_client_active "bashls" then
-- npm i -g bash-language-server -- npm i -g bash-language-server
require("lspconfig").bashls.setup { require("lspconfig").bashls.setup {
@ -26,7 +40,7 @@ if not require("lv-utils").check_lsp_client_active "efm" then
-- init_options = {initializationOptions}, -- init_options = {initializationOptions},
cmd = { DATA_PATH .. "/lspinstall/efm/efm-langserver" }, cmd = { DATA_PATH .. "/lspinstall/efm/efm-langserver" },
init_options = { documentFormatting = true, codeAction = false }, init_options = { documentFormatting = true, codeAction = false },
root_dir = require("lspconfig").util.root_pattern(".git/"), root_dir = require("lspconfig").util.root_pattern ".git/",
filetypes = { "sh" }, filetypes = { "sh" },
settings = { settings = {
rootMarkers = { ".git/" }, rootMarkers = { ".git/" },

View file

@ -33,7 +33,6 @@ vim.api.nvim_exec(
]], ]],
false false
) )
if (O.lang.latex.auto_save) if O.lang.latex.auto_save then
then vim.api.nvim_exec([[au FocusLost * :wa]], false)
vim.api.nvim_exec([[au FocusLost * :wa]],false)
end end

View file

@ -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 if require("lv-utils").check_lsp_client_active "yamlls" then
return return
end end

View file

@ -24,7 +24,7 @@ if not require("lv-utils").check_lsp_client_active "efm" then
-- init_options = {initializationOptions}, -- init_options = {initializationOptions},
cmd = { DATA_PATH .. "/lspinstall/efm/efm-langserver" }, cmd = { DATA_PATH .. "/lspinstall/efm/efm-langserver" },
init_options = { documentFormatting = true, codeAction = false }, init_options = { documentFormatting = true, codeAction = false },
root_dir = require("lspconfig").util.root_pattern(".git/"), root_dir = require("lspconfig").util.root_pattern ".git/",
filetypes = { "zsh" }, filetypes = { "zsh" },
settings = { settings = {
rootMarkers = { ".git/" }, rootMarkers = { ".git/" },

View file

@ -53,8 +53,6 @@ if not status_ok then
return return
end end
formatter.setup {}
if not O.format_on_save then if not O.format_on_save then
vim.cmd [[if exists('#autoformat#BufWritePost') vim.cmd [[if exists('#autoformat#BufWritePost')
:autocmd! autoformat :autocmd! autoformat

View file

@ -258,11 +258,6 @@ O = {
"typescript", "typescript",
"typescriptreact", "typescriptreact",
}, },
formatter = {
exe = "prettier",
args = { "--write", "--stdin-filepath", vim.api.nvim_buf_get_name(0), "--single-quote" },
stdin = false,
},
}, },
terraform = {}, terraform = {},
tsserver = { tsserver = {
@ -275,8 +270,7 @@ O = {
}, },
formatter = { formatter = {
exe = "prettier", exe = "prettier",
args = { "--write", "--stdin-filepath", vim.api.nvim_buf_get_name(0), "--single-quote" }, args = { "--stdin-filepath", vim.api.nvim_buf_get_name(0), "--single-quote" },
stdin = false,
}, },
}, },
vim = {}, vim = {},

View file

@ -1,18 +1,23 @@
vim.cmd "let proj = FindRootDirectory()" vim.cmd "let proj = FindRootDirectory()"
print(vim.api.nvim_get_var "proj")
local root_dir = 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"] = { O.formatters.filetype["javascriptreact"] = {
-- vim.cmd "let root_dir "
-- prettier
function() function()
return { return {
exe = root_dir .. "/node_modules/.bin/prettier", exe = prettier_instance,
-- TODO: append to this for args don't overwrite -- TODO: allow user to override this
args = { "--stdin-filepath", vim.api.nvim_buf_get_name(0), "--single-quote" }, args = { "--stdin-filepath", vim.api.nvim_buf_get_name(0), "--single-quote" },
stdin = true, stdin = true
} }
end, end,
} }
O.formatters.filetype["javascript"] = O.formatters.filetype["javascriptreact"]
require("formatter.config").set_defaults { require("formatter.config").set_defaults {
logging = false, logging = false,