summaryrefslogtreecommitdiff
path: root/dot_config/nvim/lua/ibrahim/plugins/conform.lua
blob: c1b38ab0405e1459a6bfb3e89724800264940aec (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
-- Use prettierd if it's available or prettier otherwise, but don't
-- try to run them both.
local prettier = { { "prettierd", "prettier" } }

return {
    "stevearc/conform.nvim",
    event = { "BufReadPre", "BufNewFile" },
    cmd = { "ConformInfo", "Format" },
    opts = {
        formatters = {
            sqlfluff = {
                -- By default, SQLFluff uses "ansi" formatting. My projects
                -- right now use MySQL formatting instead.
                -- Still looking for a better way to manage this.
                args = { "fix", "--force", "--dialect=mysql", "-" },
            },
        },
        formatters_by_ft = {
            lua = { "stylua" },

            -- Ruff LSP handles formatting for Python code
            -- -- Multiple items in one table will be run sequentially
            python = { "ruff_format" },

            c = { "clang-format" },
            css = prettier,
            html = prettier,
            javascript = prettier,
            json = prettier,
            markdown = prettier,
            typescript = prettier,
            yaml = prettier,

            sql = { "sqlfluff" },
        },
    },
    config = function(_, opts)
        local conform = require("conform")
        require("conform").setup(opts)

        local function format()
            conform.format({ async = true, lsp_fallback = true })
        end

        vim.keymap.set({ "n", "v" }, "<leader>rf", format, { desc = "Reformat" })
        vim.api.nvim_create_user_command("Format", format, {})
    end,
}