diff options
author | Abouzar Parvan <[email protected]> | 2021-07-10 22:48:28 +0430 |
---|---|---|
committer | GitHub <[email protected]> | 2021-07-10 14:18:28 -0400 |
commit | 0f7c876e93bdf42337257e70b5c9c9039a40bb47 (patch) | |
tree | fd6794b949c2a0fd7dc7bef25b8b998b288de145 /lua/lv-formatter/init.lua | |
parent | 50202efd0d409e51d7a0b09d8c923482e7488351 (diff) |
WIP: using formatter.nvim instead of neoformat (#781)
Diffstat (limited to 'lua/lv-formatter/init.lua')
-rw-r--r-- | lua/lv-formatter/init.lua | 61 |
1 files changed, 61 insertions, 0 deletions
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 |