Skip to content

Commit

Permalink
feat(format): use conform for code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamHsieh committed Aug 22, 2024
1 parent 92c5a93 commit 17593ca
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 37 deletions.
1 change: 1 addition & 0 deletions config/nvim/lazy-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"cmp-nvim-lsp": { "branch": "main", "commit": "39e2eda76828d88b773cc27a3f61d2ad782c922d" },
"cmp-path": { "branch": "main", "commit": "91ff86cd9c29299a64f968ebb45846c485725f23" },
"cmp_luasnip": { "branch": "master", "commit": "05a9ab28b53f71d1aece421ef32fee2cb857a843" },
"conform.nvim": { "branch": "master", "commit": "62eba813b7501b39612146cbf29cd07f1d4ac29c" },
"copilot-cmp": { "branch": "master", "commit": "b6e5286b3d74b04256d0a7e3bd2908eabec34b44" },
"copilot.lua": { "branch": "master", "commit": "86537b286f18783f8b67bccd78a4ef4345679625" },
"diffview.nvim": { "branch": "main", "commit": "4516612fe98ff56ae0415a259ff6361a89419b0a" },
Expand Down
31 changes: 0 additions & 31 deletions config/nvim/lua/core/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,37 +88,6 @@ function M.signal_handler(signum, callback)
end)
end

---@param client vim.lsp.Client vim.lsp.Client
---@param bufnr integer buffer number
function M.setup_formatting(client, bufnr)
if not client.supports_method("textDocument/formatting") then
return
end

local format = function()
if vim.g.lsp_formatting then
vim.lsp.buf.format()
end
end

local augroup = vim.api.nvim_create_augroup("FormatOnSave", { clear = false })
vim.api.nvim_clear_autocmds { group = augroup, buffer = bufnr }

vim.api.nvim_create_autocmd("BufWritePre", {
group = augroup,
buffer = bufnr,
callback = function()
format()
end,
})

vim.keymap.set("n", "<leader>lf", function()
vim.g.lsp_formatting = not vim.g.lsp_formatting
vim.notify("Format on save: " .. (vim.g.lsp_formatting and "on" or "off"))
format()
end, { buffer = bufnr, desc = "toggle auto formatting" })
end

function M.lazy_file()
-- This autocmd will only trigger when a file was loaded from the cmdline.
-- It will render the file as quickly as possible.
Expand Down
43 changes: 43 additions & 0 deletions config/nvim/lua/plugins/formatting.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
return {
{
"stevearc/conform.nvim",
dependencies = { "mason.nvim" },
event = "BufWritePre",
cmd = "ConformInfo",

keys = {
{
"<leader>lf",
function()
vim.g.autoformat = not vim.g.autoformat
vim.notify("Format on save: " .. (vim.g.autoformat and "on" or "off"))
if vim.g.autoformat then
require("conform").format()
end
end,
desc = "toggle auto formatting",
},
},

---@type conform.setupOpts
opts = {
formatters_by_ft = {
lua = { "stylua", lsp_format = "never" },
sh = { "shfmt" },
sql = { "sqlfluff" },
},
default_format_opts = {
timeout_ms = 3000,
async = false,
quiet = false,
lsp_format = "fallback",
},
format_on_save = function()
if not vim.g.autoformat then
return
end
return {}
end,
},
},
}
2 changes: 1 addition & 1 deletion config/nvim/lua/plugins/leetcode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ return {
["enter"] = {
function()
require("copilot.command").disable()
vim.g.lsp_formatting = true
vim.g.autoformat = true
vim.keymap.set("n", "<leader>cc", "<cmd>Leet run<cr>", { desc = "Leetcode run testcase" })
vim.keymap.set("n", "<leader>cp", "<cmd>Leet submit<cr>", { desc = "Leetcode submit" })
vim.keymap.set("n", [[<c-\>]], "<cmd>Leet console<cr>", { desc = "Leetcode console" })
Expand Down
2 changes: 0 additions & 2 deletions config/nvim/lua/plugins/lsp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ function M.config()
vim.lsp.inlay_hint.enable(not enabled)
vim.notify("LSP inlay hint: " .. (not enabled and "on" or "off"))
end, opts("toggle inlay hists"))

require("core.utils").setup_formatting(client, bufnr)
end

local capabilities = require('cmp_nvim_lsp').default_capabilities()
Expand Down
3 changes: 0 additions & 3 deletions config/nvim/lua/plugins/lsp/null-ls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ return {
"nvimtools/none-ls.nvim",
opts = {
border = "rounded",
on_attach = function(client, bufnr)
require("core.utils").setup_formatting(client, bufnr)
end,
},
},

Expand Down

0 comments on commit 17593ca

Please sign in to comment.