aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/_defaults.lua
diff options
context:
space:
mode:
authorEvgeni Chasnovski <evgeni.chasnovski@gmail.com>2024-04-04 18:10:12 +0300
committerChristian Clason <c.clason@uni-graz.at>2024-04-05 18:07:43 +0200
commit73de98256cf3932dca156fbfd0c82c1cc10d487e (patch)
treeb865c060dc3132c5e42eb31df896e5cd167f95b4 /runtime/lua/vim/_defaults.lua
parent2b9d8dc87e84ea9c03eb8852af8fd9fe00a6033a (diff)
downloadrneovim-73de98256cf3932dca156fbfd0c82c1cc10d487e.tar.gz
rneovim-73de98256cf3932dca156fbfd0c82c1cc10d487e.tar.bz2
rneovim-73de98256cf3932dca156fbfd0c82c1cc10d487e.zip
feat(comment): add built-in commenting
Design - Enable commenting support only through `gc` mappings for simplicity. No ability to configure, no Lua module, no user commands. Yet. - Overall implementation is a simplified version of 'mini.comment' module of 'echasnovski/mini.nvim' adapted to be a better suit for core. It basically means reducing code paths which use only specific fixed set of plugin config. All used options are default except `pad_comment_parts = false`. This means that 'commentstring' option is used as is without forcing single space inner padding. As 'tpope/vim-commentary' was considered for inclusion earlier, here is a quick summary of how this commit differs from it: - **User-facing features**. Both implement similar user-facing mappings. This commit does not include `gcu` which is essentially a `gcgc`. There are no commands, events, or configuration in this commit. - **Size**. Both have reasonably comparable number of lines of code, while this commit has more comments in tricky areas. - **Maintainability**. This commit has (purely subjectively) better readability, tests, and Lua types. - **Configurability**. This commit has no user configuration, while 'vim-commentary' has some (partially as a counter-measure to possibly modifying 'commentstring' option). - **Extra features**: - This commit supports tree-sitter by computing `'commentstring'` option under cursor, which can matter in presence of tree-sitter injected languages. - This commit comments blank lines while 'tpope/vim-commentary' does not. At the same time, blank lines are not taken into account when deciding the toggle action. - This commit has much better speed on larger chunks of lines (like above 1000). This is thanks to using `nvim_buf_set_lines()` to set all new lines at once, and not with `vim.fn.setline()`.
Diffstat (limited to 'runtime/lua/vim/_defaults.lua')
-rw-r--r--runtime/lua/vim/_defaults.lua18
1 files changed, 18 insertions, 0 deletions
diff --git a/runtime/lua/vim/_defaults.lua b/runtime/lua/vim/_defaults.lua
index 0798ca8d16..90d05f67a5 100644
--- a/runtime/lua/vim/_defaults.lua
+++ b/runtime/lua/vim/_defaults.lua
@@ -114,6 +114,24 @@ do
do_open(table.concat(vim.iter(lines):map(vim.trim):totable()))
end, { desc = gx_desc })
end
+
+ --- Default maps for built-in commenting
+ do
+ local operator_rhs = function()
+ return require('vim._comment').operator()
+ end
+ vim.keymap.set({ 'n', 'x' }, 'gc', operator_rhs, { expr = true, desc = 'Toggle comment' })
+
+ local line_rhs = function()
+ return require('vim._comment').operator() .. '_'
+ end
+ vim.keymap.set('n', 'gcc', line_rhs, { expr = true, desc = 'Toggle comment line' })
+
+ local textobject_rhs = function()
+ require('vim._comment').textobject()
+ end
+ vim.keymap.set({ 'o' }, 'gc', textobject_rhs, { desc = 'Comment textobject' })
+ end
end
--- Default menus