aboutsummaryrefslogtreecommitdiff
path: root/nvim/lua/plugins/format.lua
blob: 714e8878ae6c278b5bbffa4d86c4af37c98e0cea (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
62
63
-- Verfügbare Formater: https://github.com/stevearc/conform.nvim/tree/master/lua/conform/formatters

local clang = {}
local djlint = { "djlint" } -- pipx install djlint (macOS & FreeBSD); unter Windows kein winget-Paket, siehe neovim-windows-setup.md
local gawk = { "gawk" } -- brew install gawk, pkg install gawk
local jq = { "jq" } -- brew install jq, pkg install jq
local prettier = { "prettier" } -- npm install -g prettier
local ruff = { "ruff_format" } -- brew install ruff, pkg install ruff
local shfmt = { "shfmt" } -- brew install shfmt, pkg install shfmt
local stylua = { "stylua" } -- brew install stylua, pkg install stylua
local xmllint = { "xmllint" } -- xmllint ist Teil von libxml2 (standardmäßig installiert unter MacOS; pkg install libxml2)
local yamlfmt = { "yamlfmt" } -- brew install yamlfmt, pkg install yamlfmt

-- prettier leitet den Parser aus dem Dateinamen ab: .json landet beim strikten
-- json-Parser, der Kommentare ablehnt. Im Filetype jsonc erzwingen wir deshalb
-- den kommentartoleranten jsonc-Parser (prettier >= 3.0).
local function prettier_parser_args(_, ctx)
  if vim.bo[ctx.buf].filetype == "jsonc" then return { "--parser", "jsonc" } end

  return {}
end

return {
  "stevearc/conform.nvim",
  event = { "BufReadPre", "BufNewFile" },
  keys = {
    {
      "<leader>cf",
      function()
        require("conform").format({ async = true })
      end,
      desc = "Format file",
    },
  },
  opts = {
    formatters_by_ft = {
      awk = gawk,
      bash = shfmt,
      c = clang,
      cpp = clang,
      html = prettier,
      javascript = prettier,
      jinja = djlint,
      json = jq,
      jsonc = prettier,
      lua = stylua,
      python = ruff,
      sh = shfmt,
      typescript = prettier,
      xml = xmllint,
      xsd = xmllint,
      yaml = yamlfmt,
    },
    formatters = {
      prettier = {
        prepend_args = prettier_parser_args,
      },
    },
    default_format_opts = {
      lsp_format = "fallback",
    },
  },
}