From 2fb3e86bcf30f86e458829dcb5cc67676e1dfa0f Mon Sep 17 00:00:00 2001 From: Kyren223 Date: Sun, 4 Aug 2024 00:53:58 +0300 Subject: feat(nvim): added some user commands and global functions for utility --- .config/nvim/lua/custom/config/autocmds.lua | 16 --------- .config/nvim/lua/custom/config/commands.lua | 50 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 16 deletions(-) delete mode 100644 .config/nvim/lua/custom/config/autocmds.lua create mode 100644 .config/nvim/lua/custom/config/commands.lua (limited to '.config/nvim/lua/custom/config') diff --git a/.config/nvim/lua/custom/config/autocmds.lua b/.config/nvim/lua/custom/config/autocmds.lua deleted file mode 100644 index a9a9dc0..0000000 --- a/.config/nvim/lua/custom/config/autocmds.lua +++ /dev/null @@ -1,16 +0,0 @@ -vim.api.nvim_create_autocmd('TextYankPost', { - desc = 'Highlight when yanking text', - group = vim.api.nvim_create_augroup('highlight-yank', { clear = true }), - callback = function() - vim.highlight.on_yank() - end, -}) - -vim.api.nvim_create_autocmd('BufEnter', { - desc = 'Disable newlines on commented lines from continuing the comment', - group = vim.api.nvim_create_augroup('disable-comments-continuation', { clear = true }), - callback = function() - -- vim.opt_local.formatoptions:remove('r') -- no comments on enter - vim.opt_local.formatoptions:remove('o') -- no comments on `o` or `O` - end, -}) diff --git a/.config/nvim/lua/custom/config/commands.lua b/.config/nvim/lua/custom/config/commands.lua new file mode 100644 index 0000000..9cec025 --- /dev/null +++ b/.config/nvim/lua/custom/config/commands.lua @@ -0,0 +1,50 @@ +vim.api.nvim_create_autocmd('TextYankPost', { + desc = 'Highlight when yanking text', + group = vim.api.nvim_create_augroup('highlight-yank', { clear = true }), + callback = function() + vim.highlight.on_yank() + end, +}) + +vim.api.nvim_create_autocmd('BufEnter', { + desc = 'Disable newlines on commented lines from continuing the comment', + group = vim.api.nvim_create_augroup('disable-comments-continuation', { clear = true }), + callback = function() + -- vim.opt_local.formatoptions:remove('r') -- no comments on enter + vim.opt_local.formatoptions:remove('o') -- no comments on `o` or `O` + 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 }) + +vim.api.nvim_create_user_command('FormatToggle', function(args) + local is_global = not args.bang + if is_global then + vim.g.disable_autoformat = not vim.g.disable_autoformat + if vim.g.disable_autoformat then + Notify('Autoformat-on-save disabled globally', 'info') + else + Notify('Autoformat-on-save enabled globally', 'info') + end + else + vim.b.disable_autoformat = not vim.b.disable_autoformat + if vim.b.disable_autoformat then + Notify('Autoformat-on-save disabled for this buffer', 'info') + else + Notify('Autoformat-on-save enabled for this buffer', 'info') + end + end +end, { + desc = 'Toggle autoformat-on-save', + bang = true, +}) -- cgit v1.3.1