fix format
This commit is contained in:
@ -1,46 +1,5 @@
|
|||||||
local use_google = require("utils").use_google
|
local use_google = require("utils").use_google
|
||||||
|
|
||||||
vim.g.disable_autoformat = false
|
|
||||||
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_autocmd("BufWritePre", {
|
|
||||||
pattern = "*",
|
|
||||||
callback = function(args)
|
|
||||||
if vim.g.disable_autoformat or vim.b[args.buf].disable_autoformat then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
require("conform").format({ bufnr = args.buf })
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
|
|
||||||
vim.api.nvim_create_user_command("Format", function(args)
|
|
||||||
local range = nil
|
|
||||||
if args.count ~= -1 then
|
|
||||||
local end_line = vim.api.nvim_buf_get_lines(0, args.line2 - 1, args.line2, true)[1]
|
|
||||||
range = {
|
|
||||||
start = { args.line1, 0 },
|
|
||||||
["end"] = { args.line2, end_line:len() },
|
|
||||||
}
|
|
||||||
end
|
|
||||||
require("conform").format({ async = true, lsp_format = "fallback", range = range })
|
|
||||||
end, { range = true })
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
{
|
{
|
||||||
"stevearc/conform.nvim",
|
"stevearc/conform.nvim",
|
||||||
@ -51,13 +10,55 @@ return {
|
|||||||
{ "<leader>fmt", function() require("conform").format({ async = true, lsp_fallback = true }) end, mode = "", desc = "Format buffer", },
|
{ "<leader>fmt", function() require("conform").format({ async = true, lsp_fallback = true }) end, mode = "", desc = "Format buffer", },
|
||||||
{ "<leader>fj", ":%!python -m json.tool" },
|
{ "<leader>fj", ":%!python -m json.tool" },
|
||||||
},
|
},
|
||||||
|
config = function()
|
||||||
|
vim.g.disable_autoformat = false
|
||||||
|
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_autocmd("BufWritePre", {
|
||||||
|
pattern = "*",
|
||||||
|
callback = function(args)
|
||||||
|
if vim.g.disable_autoformat or vim.b[args.buf].disable_autoformat then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
require("conform").format({ bufnr = args.buf })
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
|
||||||
|
vim.api.nvim_create_user_command("Format", function(args)
|
||||||
|
local range = nil
|
||||||
|
if args.count ~= -1 then
|
||||||
|
local end_line = vim.api.nvim_buf_get_lines(0, args.line2 - 1, args.line2, true)[1]
|
||||||
|
range = {
|
||||||
|
start = { args.line1, 0 },
|
||||||
|
["end"] = { args.line2, end_line:len() },
|
||||||
|
}
|
||||||
|
end
|
||||||
|
require("conform").format({ async = true })
|
||||||
|
end, { range = true })
|
||||||
|
end,
|
||||||
-- This will provide type hinting with LuaLS
|
-- This will provide type hinting with LuaLS
|
||||||
---@module "conform"
|
---@module "conform"
|
||||||
---@type conform.setupOpts
|
---@type conform.setupOpts
|
||||||
opts = {
|
opts = {
|
||||||
log_level = vim.log.levels.DEBUG,
|
log_level = vim.log.levels.DEBUG,
|
||||||
formatters_by_ft = {
|
formatters_by_ft = {
|
||||||
rust = { "rustfmt", lsp_format = "fallback" },
|
rust = { "rustfmt" },
|
||||||
-- Conform will run multiple formatters sequentially
|
-- Conform will run multiple formatters sequentially
|
||||||
-- go = { "goimports", "gofmt" },
|
-- go = { "goimports", "gofmt" },
|
||||||
-- Use a sub-list to run only the first available formatter
|
-- Use a sub-list to run only the first available formatter
|
||||||
@ -65,7 +66,7 @@ return {
|
|||||||
-- Conform will run multiple formatters sequentially
|
-- Conform will run multiple formatters sequentially
|
||||||
python = { "isort" },
|
python = { "isort" },
|
||||||
-- Use a sub-list to run only the first available formatter
|
-- Use a sub-list to run only the first available formatter
|
||||||
javascript = { { "prettier" } },
|
javascript = { "prettier" },
|
||||||
gdscript = { "gdformat" },
|
gdscript = { "gdformat" },
|
||||||
ron = { "ronfmt" },
|
ron = { "ronfmt" },
|
||||||
dashboard = {},
|
dashboard = {},
|
||||||
|
Reference in New Issue
Block a user