Skip to content
krap edited this page Mar 31, 2023 · 1 revision

Welcome to the knit wiki!

Neovim setup for format on save

You can install dprint via pnpm i in project root.

If you use null-ls to format

return function()
  local null_ls = require("null-ls")
  null_ls.setup({
    sources = {
      null_ls.builtins.formatting.stylua,
      null_ls.builtins.formatting.dprint,
    },
    on_attach = function(client, bufnr)
      if client.supports_method("textDocument/formatting") then
        local format_augroup = vim.api.nvim_create_augroup("format_augroup", { clear = true })
        vim.api.nvim_create_autocmd("BufWritePre", {
          group = format_augroup,
          buffer = bufnr,
          callback = function()
            vim.lsp.buf.format({
              filter = function(_client)
                return _client.name == "null-ls"
              end,
            })
          end,
        })
      end
    end,
  })
end

else

local supported_types = { 'js', 'jsx', 'ts', 'tsx', 'json', 'yaml' }
local filetype = vim.bo.filetype
if not vim.tbl_contains(supported_types, filetype) then
  return
end

local has_dprint = vim.fn.executable('dprint')

if has_dprint then
  vim.cmd("augroup dprint_autogroup")
  vim.cmd("autocmd!")
  vim.cmd("autocmd BufWritePre * silent! !dprint fmt %")
  vim.cmd("augroup END")
end
Clone this wiki locally