diff --git a/init.lua b/init.lua index 625c7875..436ae24f 100644 --- a/init.lua +++ b/init.lua @@ -206,6 +206,16 @@ vim.keymap.set('n', 'j', 'lprevzz') vim.keymap.set('n', 'x', '!chmod +x %', { silent = true }) + +-- formatter autoformat on save +local augroup = vim.api.nvim_create_augroup +local autocmd = vim.api.nvim_create_autocmd +augroup("__formatter__", { clear = true }) +autocmd("BufWritePost", { + group = "__formatter__", + command = ":FormatWrite", +}) + -- Keybinds to make split navigation easier. -- Use CTRL+ to switch between windows -- @@ -908,7 +918,7 @@ require('lazy').setup({ main = 'nvim-treesitter.configs', -- Sets main module to use for opts -- [[ Configure Treesitter ]] See `:help nvim-treesitter` opts = { - ensure_installed = { 'bash', 'c', 'diff', 'html', 'lua', 'luadoc', 'markdown', 'markdown_inline', 'query', 'vim', 'vimdoc' }, + ensure_installed = { 'bash', 'astro', 'scala', 'c', 'go', 'diff', 'html', 'lua', 'luadoc', 'markdown', 'markdown_inline', 'query', 'vim', 'vimdoc', 'java', 'javascript', 'typescript' }, -- Autoinstall languages that are not installed auto_install = true, highlight = { @@ -924,7 +934,7 @@ require('lazy').setup({ -- with nvim-treesitter. You should go explore a few and see what interests you: -- -- - Incremental selection: Included, see `:help nvim-treesitter-incremental-selection-mod` - -- - Show your current context: https://github.com/nvim-treesitter/nvim-treesitter-context + -- - Show your current context: https://:github.com/nvim-treesitter/nvim-treesitter-context -- - Treesitter + textobjects: https://github.com/nvim-treesitter/nvim-treesitter-textobjects }, @@ -944,6 +954,7 @@ require('lazy').setup({ require 'kickstart.plugins.neo-tree', require 'custom.plugins.harpoon', require 'custom.plugins.copilot', + require 'custom.plugins.formatter', -- require 'kickstart.plugins.gitsigns', -- adds gitsigns recommend keymaps -- NOTE: The import below can automatically add your own plugins, configuration, etc from `lua/custom/plugins/*.lua` diff --git a/lua/custom/plugins/formatter.lua b/lua/custom/plugins/formatter.lua new file mode 100644 index 00000000..941ef830 --- /dev/null +++ b/lua/custom/plugins/formatter.lua @@ -0,0 +1,108 @@ +-- You can add your own plugins here or in other files in this directory! +-- I promise not to create any merge conflicts in this directory :) +-- +-- See the kickstart.nvim README for more information +return { + 'mhartington/formatter.nvim', + config = function() + local util = require 'formatter.util' + + require('formatter').setup { + + + -- Enable or disable logging + logging = true, + -- Set the log level + log_level = vim.log.levels.WARN, + -- All formatter configurations are opt-in + filetype = { + typescript = { + function() + return { + exe = 'prettier', + args = { + '--stdin-filepath', + util.escape_path(util.get_current_buffer_file_path()), + }, + stdin = true, + try_node_modules = true, + } + end, + }, + + javascript = { + function() + return { + exe = 'prettier', + args = { + '--stdin-filepath', + util.escape_path(util.get_current_buffer_file_path()), + }, + stdin = true, + try_node_modules = true, + } + end, + }, + + javascriptreact = { + require('formatter.filetypes.javascriptreact').prettier, + + function() + return { + exe = 'prettier', + args = { + '--stdin-filepath', + util.escape_path(util.get_current_buffer_file_path()), + }, + stdin = true, + try_node_modules = true, + } + end, + }, + + typescriptreact = { + require('formatter.filetypes.typescriptreact').prettier, + + function() + return { + exe = 'prettier', + args = { + '--stdin-filepath', + util.escape_path(util.get_current_buffer_file_path()), + }, + stdin = true, + try_node_modules = true, + } + end, + }, + + go = { + require('formatter.filetypes.go').gofmt, + require('formatter.filetypes.go').goimports, + }, + + astro = { + function() + return { + exe = 'prettier', + args = { + '--stdin-filepath', + util.escape_path(util.get_current_buffer_file_path()), + }, + stdin = true, + try_node_modules = true, + } + end, + }, + + -- Use the special "*" filetype for defining formatter configurations on + -- any filetype + ['*'] = { + -- "formatter.filetypes.any" defines default configurations for any + -- filetype + require('formatter.filetypes.any').remove_trailing_whitespace, + }, + }, + } + end, +}