summaryrefslogtreecommitdiff
path: root/lua/core/formatter.lua
blob: ad40dd21c7c58e8c2f5e380574e433bb426e9a46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
-- autoformat
if O.format_on_save then
  require("lv-utils").define_augroups {
    autoformat = {
      {
        "BufWritePost",
        "*",
        ":silent FormatWrite",
      },
    },
  }
end

-- check if formatter has been defined for the language or not
local 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
local 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