Skip to content

Commit

Permalink
♻ Refactor auto format
Browse files Browse the repository at this point in the history
In the past, every time we added a new language, we need to
modified autocmd to support this new file type.
  • Loading branch information
Nguyen-Hoang-Nam committed Feb 12, 2022
1 parent 2e394a6 commit 70d8015
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
34 changes: 23 additions & 11 deletions lua/autocommands.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
local augroup = vim.api.nvim_exec
local cmd = vim.api.nvim_command

vim.api.nvim_command("command! -range CommentToggle lua require('utils.comment').comment_toggle(<line1>, <line2>)")
-- Auto format
local setting_formatter_filetypes = require('settings').efm.filetypes
local map_filetype_filename = require('utils.core').map_filetype_filename

local autoformat_cmd = 'autocmd BufWritePre '
local i = 1
for _, filetype in pairs(setting_formatter_filetypes) do
if i > 1 then
autoformat_cmd = autoformat_cmd .. ','
end

autoformat_cmd = autoformat_cmd .. map_filetype_filename[filetype]

i = i + 1
end

autoformat_cmd = autoformat_cmd .. [[ lua require('format').format()]]

cmd(autoformat_cmd)

-- Toggle comment
cmd("command! -range CommentToggle lua require('utils.comment').comment_toggle(<line1>, <line2>)")

augroup(
[[
Expand All @@ -15,16 +37,6 @@ augroup END
true
)

augroup(
[[
augroup FormatAutogroup
autocmd!
autocmd BufWritePre *.cpp,CMakeLists.txt,Dockerfile,*.md,*.php,*.py,*.js,*.jsx,*.ts,*.tsx,*.svelte,*.go,*.lua,*.rs,*.tex,*.css,*.html,*.yaml,*.yml,*.json lua require('format').format()
augroup END
]],
true
)

augroup(
[[
augroup UpdateGlobal
Expand Down
2 changes: 1 addition & 1 deletion lua/format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local M = {}

local settings = require('settings')
local setting_languages = require('languages.languages')
local filetypes = settings.autoformat.filetypes
local filetypes = settings.efm.filetypes

local formatters = {}
local default_formatter = {}
Expand Down
1 change: 1 addition & 0 deletions lua/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ return {

autoformat = {
filetypes = {
'cmake',
'css',
'cpp',
'dockerfile',
Expand Down
24 changes: 24 additions & 0 deletions lua/utils/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,28 @@ function M.rest()
end
end

M.map_filetype_filename = {
cmake = 'CMakeLists.txt',
css = '*.css',
cpp = '*.cpp,*.hpp',
dockerfile = 'Dockerfile',
go = '*.go',
html = '*.html',
java = '*.java',
javascript = '*.js',
javascriptreact = '*.jsx',
json = '*.json',
lua = '*.lua',
markdown = '*.md',
php = '*.php',
python = '*.py',
rust = '*.rs',
svelte = '*.svelte',
tex = '*.tex',
typescript = '*.ts',
typescriptreact = '*.tsx',
xml = '*.xml',
yaml = '*.yaml,*.yml',
}

return M

0 comments on commit 70d8015

Please sign in to comment.