Skip to content

Commit

Permalink
nvim (formatting): first step to simplify formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
sfrieds3 committed Dec 12, 2024
1 parent 1c79f73 commit 58e51d6
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 112 deletions.
9 changes: 0 additions & 9 deletions nvim/dot-config/nvim/after/ftplugin/go.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@ vim.bo.shiftwidth = 2
vim.bo.softtabstop = 2
vim.bo.tabstop = 2

vim.api.nvim_create_autocmd("BufWritePre", {
callback = function(args)
require("conform").format({ formatters = { "gofmt" }, bufnr = args.bufnr })
require("conform").format({ formatters = { "goimports" }, bufnr = args.bufnr })
end,
buffer = vim.api.nvim_get_current_buf(),
group = augroup("goformat:" .. vim.api.nvim_get_current_buf()),
})

vim.bo.makeprg = "go run %"

local augroup_name = "sfrieds3:golang_runonsave"
Expand Down
8 changes: 0 additions & 8 deletions nvim/dot-config/nvim/after/ftplugin/json.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,3 @@ vim.bo.shiftwidth = 2
vim.bo.softtabstop = 2
vim.bo.formatprg = "jq"
vim.bo.commentstring = "// %s"

vim.api.nvim_create_autocmd("BufWritePre", {
callback = function(args)
require("conform").format({ lsp_format = "fallback", bufnr = args.bufnr })
end,
buffer = bufnr,
group = vim.api.nvim_create_augroup("jsonformat:" .. bufnr, {}),
})
10 changes: 0 additions & 10 deletions nvim/dot-config/nvim/after/ftplugin/lua.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,3 @@ vim.bo.shiftwidth = 2
vim.bo.softtabstop = 2
vim.bo.expandtab = true
vim.opt_local.formatoptions:remove("r")

if is_executable("stylua") then
vim.api.nvim_create_autocmd("BufWritePre", {
callback = function(args)
require("conform").format({ formatters = { "stylua" }, bufnr = args.bufnr })
end,
buffer = bufnr,
group = vim.api.nvim_create_augroup("stylua:" .. bufnr, {}),
})
end
1 change: 0 additions & 1 deletion nvim/dot-config/nvim/after/ftplugin/norg.lua

This file was deleted.

8 changes: 0 additions & 8 deletions nvim/dot-config/nvim/after/ftplugin/ocaml.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,3 @@ vim.bo.shiftwidth = 2

-- TODO: call module name here
vim.bo.makeprg = "dune build"

vim.api.nvim_create_autocmd("BufWritePre", {
callback = function(args)
require("conform").format({ formatters = { "ocamlformat" }, bufnr = args.bufnr })
end,
buffer = vim.api.nvim_get_current_buf(),
group = augroup("ocamlformat:" .. vim.api.nvim_get_current_buf()),
})
78 changes: 45 additions & 33 deletions nvim/dot-config/nvim/after/ftplugin/python.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
local is_executable = vim.fn.executable
local bufnr = vim.api.nvim_get_current_buf()

vim.g.python_highlight_space_errors = 0

vim.opt_local.shiftwidth = 4
vim.opt_local.softtabstop = 4
vim.opt_local.textwidth = 120
Expand All @@ -14,25 +12,26 @@ local python_dir_markers = { "pyprojec.toml", "setup.py", "setup.cfg", ".git" }
local disable_auto_format_files = { ".pynoautoformat", ".pydisableautoformat", ".pydisableformat" }

local function format_file(opts)
local project_root = vim.fs.root(vim.fn.expand("%"), python_dir_markers)
if #vim.fs.find(disable_auto_format_files, { path = project_root, type = "file", limit = 1, upward = true }) == 0 then
local has_formatted = false
local conform = require("conform")
if vim.g.disable_autoformat or vim.b[opts.buf].disable_autoformat then
return
end

-- respect local formatting packages first
if is_executable("isort") == 1 then
conform.format({ formatters = { "isort" }, bufnr = bufnr })
end
if is_executable("black") == 1 then
conform.format({ formatters = { "black" }, bufnr = bufnr })
has_formatted = true
end
local has_formatted = false
local conform = require("conform")

-- default to ruff if project does not use black
if not has_formatted then
conform.format({ formatters = { "ruff_organize_imports" }, bufnr = bufnr })
conform.format({ formatters = { "ruff_format" }, bufnr = bufnr })
end
-- respect local formatting packages first
if is_executable("isort") == 1 then
conform.format({ formatters = { "isort" }, bufnr = bufnr })
end
if is_executable("black") == 1 then
conform.format({ formatters = { "black" }, bufnr = bufnr })
has_formatted = true
end

-- default to ruff if project does not use black
if not has_formatted then
conform.format({ formatters = { "ruff_organize_imports" }, bufnr = bufnr })
conform.format({ formatters = { "ruff_format" }, bufnr = bufnr })
end
end

Expand All @@ -46,21 +45,34 @@ local function lint_file()
end
end

vim.api.nvim_create_autocmd("BufWritePre", {
callback = function(opts)
format_file(opts)
end,
buffer = bufnr,
group = vim.api.nvim_create_augroup("pyformat:" .. bufnr, {}),
})
local function set_python_format_config()
local project_root = vim.fs.root(vim.fn.expand("%"), python_dir_markers)

vim.api.nvim_create_autocmd({ "BufWritePost" }, {
callback = function()
lint_file()
end,
buffer = bufnr,
group = vim.api.nvim_create_augroup("pylint:" .. bufnr, {}),
})
if #vim.fs.find(disable_auto_format_files, { path = project_root, type = "file", limit = 1, upward = true }) ~= 0 then
vim.b[bufnr].disable_autoformat = true
end

if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
return
end

vim.api.nvim_create_autocmd("BufWritePre", {
callback = function(opts)
format_file(opts)
end,
buffer = bufnr,
group = vim.api.nvim_create_augroup("pyformat:" .. bufnr, {}),
})

vim.api.nvim_create_autocmd({ "BufWritePost" }, {
callback = function()
lint_file()
end,
buffer = bufnr,
group = vim.api.nvim_create_augroup("pylint:" .. bufnr, {}),
})
end
set_python_format_config()

-- set up python test config
require("plugins.test.python").setup()
Expand Down
8 changes: 0 additions & 8 deletions nvim/dot-config/nvim/after/ftplugin/rust.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ local bufnr = vim.api.nvim_get_current_buf()
vim.bo.makeprg = "cargo build"
-- vim.bo.compiler = "cargo"

vim.api.nvim_create_autocmd("BufWritePre", {
callback = function(args)
require("conform").format({ lsp_format = "fallback", bufnr = args.bufnr })
end,
buffer = bufnr,
group = vim.api.nvim_create_augroup("rustformat:" .. bufnr, {}),
})

vim.api.nvim_create_user_command("CargoTest", function(params)
-- Insert args at the '$*' in the grepprg
local task = require("overseer").new_task({
Expand Down
9 changes: 0 additions & 9 deletions nvim/dot-config/nvim/after/ftplugin/toml.lua

This file was deleted.

11 changes: 0 additions & 11 deletions nvim/dot-config/nvim/after/ftplugin/typescript.lua

This file was deleted.

8 changes: 0 additions & 8 deletions nvim/dot-config/nvim/after/ftplugin/typescriptreact.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
local bufnr = vim.api.nvim_get_current_buf()
vim.bo.shiftwidth = 2
vim.bo.softtabstop = 2

vim.api.nvim_create_autocmd("BufWritePre", {
callback = function(args)
require("conform").format({ lsp_format = "fallback", bufnr = args.bufnr })
end,
buffer = bufnr,
group = vim.api.nvim_create_augroup("typescriptreact:" .. bufnr, {}),
})
33 changes: 26 additions & 7 deletions nvim/dot-config/nvim/lua/plugins/formatting.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ return {

config = function()
require("conform").setup({
format_on_save = function(bufnr)
-- Disable with a global or buffer-local variable
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
return
end
return { timeout_ms = 500, lsp_format = "fallback" }
end,
formatters_by_ft = {
c = { "clang_format" },
clojure = { "joker" },
Expand Down Expand Up @@ -44,17 +51,29 @@ return {

vim.o.formatexpr = "v:lua.require'conform'.formatexpr()"

vim.api.nvim_create_autocmd("BufWritePre", {
callback = function(args)
require("conform").format({ formatters = { "trim_whitespace", "trim_newlines" }, bufnr = args.bufnr })
end,
group = vim.api.nvim_create_augroup("conform:allformat", {}),
})

vim.keymap.set({ "n", "o", "x", "v" }, "gq", function()
require("conform").format({ lsp_format = "fallback" })
end, { desc = "Conform Format" })

vim.api.nvim_create_user_command("FormatDisable", function(args)
if args.bang then
-- FormatDisable! will disable formatting just for this buffer
vim.b.disable_autoformat = true
else
vim.g.disable_autoformat = true
end
end, {
desc = "Disable autoformat-on-save",
bang = true,
})

vim.api.nvim_create_user_command("FormatEnable", function()
vim.b.disable_autoformat = false
vim.g.disable_autoformat = false
end, {
desc = "Re-enable autoformat-on-save",
})

vim.api.nvim_create_user_command("Format", function(args)
local range = nil
if args.count ~= -1 then
Expand Down

0 comments on commit 58e51d6

Please sign in to comment.