diff options
| -rw-r--r-- | configs/nvim/init.lua | 7 | ||||
| -rw-r--r-- | configs/nvim/lua/crupest/nvim/plugins/formatter.lua | 141 | 
2 files changed, 105 insertions, 43 deletions
diff --git a/configs/nvim/init.lua b/configs/nvim/init.lua index de2f900..7a8f51e 100644 --- a/configs/nvim/init.lua +++ b/configs/nvim/init.lua @@ -89,7 +89,8 @@ require("toggleterm").setup {  require("nvim-autopairs").setup {}  -- setup formatter -require("crupest.nvim.plugins.formatter").setup_formatter() +local formatter = require("crupest.nvim.plugins.formatter") +formatter.setup_formatter()  -- setup lint  local lint = require("crupest.nvim.plugins.lint") @@ -156,9 +157,6 @@ vim.api.nvim_create_autocmd('LspAttach', {          vim.keymap.set('n', '<space>rn', vim.lsp.buf.rename, opts)          vim.keymap.set({ 'n', 'v' }, '<space>ca', vim.lsp.buf.code_action, opts)          vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts) -        vim.keymap.set('n', '<space>f', function() -            vim.lsp.buf.format { async = true } -        end, opts)      end,  }) @@ -183,6 +181,7 @@ vim.keymap.set('n', '<leader>l]', vim.diagnostic.goto_next)  vim.keymap.set('n', '<leader>ll', vim.diagnostic.setloclist)  vim.keymap.set('n', '<leader>lr', lint.run_lint) +vim.keymap.set('n', '<space>f', formatter.run_formatter)  vim.keymap.set("n", "<c-tab>", "<cmd>bnext<cr>")  vim.keymap.set("n", "<c-s-tab>", "<cmd>bNext<cr>") diff --git a/configs/nvim/lua/crupest/nvim/plugins/formatter.lua b/configs/nvim/lua/crupest/nvim/plugins/formatter.lua index 4bbb2ac..9f6138f 100644 --- a/configs/nvim/lua/crupest/nvim/plugins/formatter.lua +++ b/configs/nvim/lua/crupest/nvim/plugins/formatter.lua @@ -1,51 +1,114 @@  local fs = require("crupest.system.fs")  local find_npm_exe = require("crupest.system.find").find_npm_exe; -local function setup_formatter() -    local prettier_formatter = function() -        local current_buffer = vim.api.nvim_buf_get_name(0) -        local prettier_exe = find_npm_exe(current_buffer, "prettier") or "prettier" - -        if vim.fn.has("win32") ~= 0 then -            local escape = fs.escape_space -            current_buffer = escape(current_buffer) -            prettier_exe = escape(prettier_exe) -        end +local prettier_formatter = function() +    local current_buffer = vim.api.nvim_buf_get_name(0) +    local prettier_exe = find_npm_exe(current_buffer, "prettier") or "prettier" -        return { -            exe = prettier_exe, -            args = { -                "--stdin-filepath", -                current_buffer -            }, -            stdin = true, -        } +    if vim.fn.has("win32") ~= 0 then +        local escape = fs.escape_space +        current_buffer = escape(current_buffer) +        prettier_exe = escape(prettier_exe)      end +    return { +        exe = prettier_exe, +        args = { +            "--stdin-filepath", +            current_buffer +        }, +        stdin = true, +    } +end + +local formatters_for_filetype = { +    html = { +        prettier_formatter +    }, +    css = { +        prettier_formatter +    }, +    javascript = { +        prettier_formatter +    }, +    javascriptreact = { +        prettier_formatter +    }, +    typescript = { +        prettier_formatter +    }, +    typescriptreact = { +        prettier_formatter +    } +} + +local function get_formatter_name(formatter) +    if formatter == prettier_formatter then return "prettier" end +    return nil +end + +local function get_formatter_name_list(formatters) +    local result = {} +    for _, formatter in ipairs(formatters) do +        table.insert(result, get_formatter_name(formatter)) +    end +    return result +end + +local function setup_formatter()      require("formatter").setup { -        filetype = { -            html = { -                prettier_formatter -            }, -            css = { -                prettier_formatter -            }, -            javascript = { -                prettier_formatter -            }, -            javascriptreact = { -                prettier_formatter -            }, -            typescript = { -                prettier_formatter -            }, -            typescriptreact = { -                prettier_formatter -            } -        } +        filetype = formatters_for_filetype      }  end + +local function get_custom_formatters(bufnr) +    local filetype = vim.api.nvim_buf_get_option(bufnr, "filetype") +    for ft, formatters in pairs(formatters_for_filetype) do +        if filetype == ft then +            return true, get_formatter_name_list(formatters) +        end +    end +    return false, {} +end + +local function run_formatter(opt) +    if not opt then +        opt = {} +    end + +    if not opt.buf then +        opt.buf = 0 +    end + +    local has_custom_formatter, formatter_names = get_custom_formatters(opt.buf) + +    local formatter_name_str = "" +    for i, name in ipairs(formatter_names) do +        if i == 1 then +            formatter_name_str = name +        else +            formatter_name_str = formatter_name_str .. " " .. name +        end +    end + +    if has_custom_formatter then +        print("Use custom formatters: " .. formatter_name_str .. ".") +        vim.cmd("Format") +        return +    end + +    local has_lsp = vim.lsp.get_active_clients({ bufnr = 0 }) +    if has_lsp then +        print("Use lsp formatter.") +        vim.lsp.buf.format { async = true } +        return +    end + +    vim.notify("No formatters found.", vim.log.levels.ERROR); +end +  return { -    setup_formatter = setup_formatter +    setup_formatter = setup_formatter, +    run_formatter = run_formatter  }  | 
