Use nvim-format-buffer

This commit is contained in:
Jan Hamal Dvořák 2024-06-13 11:25:00 +02:00
parent d1cf560b88
commit 4b48b3fa3c
10 changed files with 22 additions and 54 deletions

3
.gitmodules vendored
View file

@ -22,3 +22,6 @@
[submodule "opt/lua-language-server"]
path = opt/lua-language-server
url = https://github.com/LuaLS/lua-language-server.git
[submodule "bundle/nvim-format-buffer"]
path = bundle/nvim-format-buffer
url = https://github.com/acro5piano/nvim-format-buffer.git

@ -0,0 +1 @@
Subproject commit b3b6cbbdc78ab73ab8256e37a9e6203ac875866a

View file

@ -1,12 +0,0 @@
vim.api.nvim_create_autocmd( { "BufWritePre" }, {
pattern = { "*.py", "*.pyi" },
callback = function(ev)
local old_cursor = vim.api.nvim_win_get_cursor(0)
vim.api.nvim_command([[ :silent! %!/usr/bin/env black -q --stdin-filename % - ]])
local new_cursor = vim.api.nvim_win_get_cursor(0)
if old_cursor[1] ~= new_cursor[1] or old_cursor[2] ~= new_cursor[2] then
vim.cmd.normal(vim.api.nvim_replace_termcodes('<C-o>', true, true, true))
end
end
})

View file

@ -1,12 +0,0 @@
vim.api.nvim_create_autocmd( { "BufWritePre" }, {
pattern = { "*.c", "*.h" },
callback = function(ev)
local old_cursor = vim.api.nvim_win_get_cursor(0)
vim.api.nvim_command([[ :silent! %!/usr/bin/env clang-format ]])
local new_cursor = vim.api.nvim_win_get_cursor(0)
if old_cursor[1] ~= new_cursor[1] or old_cursor[2] ~= new_cursor[2] then
vim.cmd.normal(vim.api.nvim_replace_termcodes('<C-o>', true, true, true))
end
end
})

13
lua/config/format.lua Normal file
View file

@ -0,0 +1,13 @@
require('nvim-format-buffer').setup({
verbose = false,
format_rules = {
{ pattern = { '*.c', '*.h' }, command = 'clang-format' },
{ pattern = { '*.py', '*.pyi' }, command = 'black -q --stdin-filename % - | isort -q -' },
{
pattern = { '*.js', '*.ts', '*.tsx', '*.vue', '*.css' },
command = function()
return 'prettier --stdin-filepath ' .. vim.api.nvim_buf_get_name(0)
end,
},
},
})

3
lua/config/init.lua Normal file
View file

@ -0,0 +1,3 @@
require("config.tab")
require("config.format")
require("config.lsp.init")

View file

@ -1,12 +0,0 @@
vim.api.nvim_create_autocmd( { "BufWritePre" }, {
pattern = { "*.py", "*.pyi" },
callback = function(ev)
local old_cursor = vim.api.nvim_win_get_cursor(0)
vim.api.nvim_command([[ :silent! %!/usr/bin/env isort -q - ]])
local new_cursor = vim.api.nvim_win_get_cursor(0)
if old_cursor[1] ~= new_cursor[1] or old_cursor[2] ~= new_cursor[2] then
vim.cmd.normal(vim.api.nvim_replace_termcodes('<C-o>', true, true, true))
end
end
})

View file

@ -1,12 +0,0 @@
vim.api.nvim_create_autocmd( { "BufWritePre" }, {
pattern = { "*.vue", "*.js", "*.ts" },
callback = function(ev)
local old_cursor = vim.api.nvim_win_get_cursor(0)
vim.api.nvim_command([[ :silent! %!/usr/bin/env prettier --stdin-filepath % ]])
local new_cursor = vim.api.nvim_win_get_cursor(0)
if old_cursor[1] ~= new_cursor[1] or old_cursor[2] ~= new_cursor[2] then
vim.cmd.normal(vim.api.nvim_replace_termcodes('<C-o>', true, true, true))
end
end
})

View file

@ -32,3 +32,5 @@ require('nvim-treesitter.configs').setup {
enable = true,
}
}
require("config.init")

View file

@ -34,11 +34,5 @@ let g:jsx_ext_required = 0
set viminfo='20,<1000,s100
lua require("init")
lua require("config.tab")
lua require("config.black")
lua require("config.isort")
lua require("config.prettier")
lua require("config.lsp")
lua require("config.clang-format")
" EOF