From 0aa4cbf377e6de555d9064a2947c8e6cfe8f781e Mon Sep 17 00:00:00 2001 From: gegoune <69750637+gegoune@users.noreply.github.com> Date: Tue, 27 Jul 2021 10:43:45 +0200 Subject: [PATCH] Add hook functionality, update docs (#23) - implement `hook` setting allowing users to pass in arbitrary function to call before reading `commentstring` - format README file with prettier and fix all issues reported by markdownlint - use local variable for `vim.api` - use `` instead of `:` in key mappings to avoid changing modes --- CHANGELOG.md | 1 + README.md | 107 +++++++++++++++++++++++++++++-------------- lua/nvim_comment.lua | 16 +++++-- 3 files changed, 84 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 554c245..de54ee1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ Changelog ====================== +2021-07-10 - Add hook function to be called before reading `commentstring` 2021-02-16 - Fix issue with `gv` causing entire buffer range to be acted on 2021-02-12 - Fix issue with motion acting on last visual selection diff --git a/README.md b/README.md index 3e0ce79..f07bd8e 100644 --- a/README.md +++ b/README.md @@ -1,38 +1,47 @@ # nvim-comment -Toggle comments in Neovim, using built in **commentstring** filetype option; written in Lua. -Without a doubt this plugin **is not required** and is a rip off of [TPope's Commentary](https://github.com/tpope/vim-commentary) with less features! What makes this plugin stand out over the numerous other comment plugins written in Lua are: - -* Doesn't require nightly build (works on NeoVim 0.4.x) -* Comments each line, rather than adds block comments; making it easier to toggle code when debugging -* Uses the built in **commentstring** buffer option to define comment markers -* Where a marker doesn't have a **space** character as padding this is added, configurable -** This can be disabled in the options, see below but if useful when workig with numerous linters -* Supports motions - -When the plugin is called it works out the range to comment/uncomment; if all lines in the given range are commented then it uncomments, otherwise it comments the range. This is useful when commenting a block out for testing with a real like comment in it; as for the plugin a comment is a comment. - -# Usage +Toggle comments in Neovim, using built in `commentstring` filetype option; +written in Lua. Without a doubt this plugin **is not required** and is a rip off +of [TPope's Commentary](https://github.com/tpope/vim-commentary) with less +features! What makes this plugin stand out over the numerous other comment +plugins written in Lua are: + +- Comments each line, rather than adds block comments; making it easier to + toggle code when debugging +- Uses the built in `commentstring` buffer option to define comment markers +- Where a marker doesn't have a **space** character as padding this is added, + configurable (this can be disabled in the options, see below but if useful + when working with numerous linters) +- Supports motions +- Dot `.` repeatable + +When the plugin is called it works out the range to comment/uncomment; if all +lines in the given range are commented then it uncomments, otherwise it comments +the range. This is useful when commenting a block out for testing with a real +like comment in it; as for the plugin a comment is a comment. + +## Usage Either use the command `CommentToggle`, e.g.: -* `CommentToggle` comment/uncomment current line -* `67,69CommentToggle` comment/uncomment a range -* `'<,'>CommentToggle` comment/uncomment a visual selection +- `CommentToggle` comment/uncomment current line +- `67,69CommentToggle` comment/uncomment a range +- `'<,'>CommentToggle` comment/uncomment a visual selection Or use the default mappings: -* `gcc` comment/uncomment current line, this does not take a count, if you want a count use the `gc{count}{motion}` -* `gc{motion}` comment/uncomment selection defined by a motion: -** As lines are commented, any comment toggling actions will default to a linewise. -** `gcc` comment/uncomment current line -** `gcip` comment/uncomment a paragraph -** `gc4w` comment/uncomment current line -** `gc4j` comment/uncomment 4 lines below the current line +- `gcc` comment/uncomment current line, this does not take a count, if you want + a count use the `gc{count}{motion}` +- `gc{motion}` comment/uncomment selection defined by a motion (as lines are + commented, any comment toggling actions will default to a linewise): + - `gcip` comment/uncomment a paragraph + - `gc4w` comment/uncomment current line + - `gc4j` comment/uncomment 4 lines below the current line -## Configure +### Configure The comment plugin needs to be initialised using: + ```lua require('nvim_comment').setup() ``` @@ -50,40 +59,65 @@ However you can pass in some config options, the defaults are: -- Normal mode mapping left hand side line_mapping = "gcc", -- Visual/Operator mapping left hand side - operator_mapping = "gc" + operator_mapping = "gc", + -- Hook function to call before commenting takes place + hook = nil } ``` -**Ignore Empty Lines** +- Ignore Empty Lines + ```lua require('nvim_comment').setup({comment_empty = false}) ``` -**Disable mappings** +- Disable mappings + ```lua require('nvim_comment').setup({create_mappings = false}) ``` -**Custom mappings** +- Custom mappings + ```lua require('nvim_comment').setup({line_mapping = "cl", operator_mapping = "c"}) ``` -**Disable marker padding** +- Disable marker padding + ```lua require('nvim_comment').setup({marker_padding = false}) ``` -**Changing/Setting commentstring** +- Hook function called before reading `commentstring` -If you want to override the comment markers or add a new filetype just set the **commentstring** options: +You can run arbitrary function which will be called before plugin reads value of +`commentstring`. This can be used to integrate with +[JoosepAlviste/nvim-ts-context-commentstring](https://github.com/JoosepAlviste/nvim-ts-context-commentstring): + +```lua +require('nvim_comment').setup({ + hook = function() + if vim.api.nvim_buf_get_option(0, "filetype") == "vue" then + require("ts_context_commentstring.internal").update_commentstring() + end + end +}) +``` + +- Changing/Setting `commentstring` + +If you want to override the comment markers or add a new filetype just set the +`commentstring` options: ```lua -- Assumes this is being run in the context of the filetype... vim.api.nvim_buf_set_option(0, "commentstring", "# %s") ``` -You can also use an autocommand to automatically load your **commentstring** for certain file types: +You can also use an autocommand to automatically load your `commentstring` for +certain file types: + ```vim " when you enter a (new) buffer augroup set-commentstring-ag @@ -95,6 +129,7 @@ auground END ``` Or add the comment string option in the relevant `filetype` file: + ```vim let commentstring="# %s" ``` @@ -103,11 +138,11 @@ let commentstring="# %s" vim.api.nvim_buf_set_option(0, "commentstring", "# %s") ``` -# Installation +## Installation Install just as you would a normal plugin, here are some options: -**Built in package manager** +### Built in package manager ```bash mkdir -p ~/.local/share/nvim/site/pack/plugins/start @@ -115,9 +150,10 @@ cd ~/.local/share/nvim/site/pack/plugins/start git clone https://github.com/terrortylor/nvim-comment ``` -**Via a plugin manager** +### Via a plugin manager Using [nvim-pluginman](https://github.com/terrortylor/nvim-pluginman): + ```lua plug.add({ url = "terrortylor/nvim-comment", @@ -128,6 +164,7 @@ plug.add({ ``` Using [packer.nvim](https://github.com/wbthomason/packer.nvim): + ```lua use "terrortylor/nvim-comment" require('nvim_comment').setup() diff --git a/lua/nvim_comment.lua b/lua/nvim_comment.lua index 270027c..eef88f3 100644 --- a/lua/nvim_comment.lua +++ b/lua/nvim_comment.lua @@ -13,7 +13,9 @@ M.config = { -- Normal mode mapping left hand side line_mapping = "gcc", -- Visual/Operator mapping left hand side - operator_mapping = "gc" + operator_mapping = "gc", + -- Hook function to call before commenting takes place + hook = nil } function M.get_comment_wrapper() @@ -88,6 +90,10 @@ function M.operator(mode) end function M.comment_toggle(line_start, line_end) + if type(M.config.hook) == 'function' then + M.config.hook() + end + local left, right = M.get_comment_wrapper() if not left or not right then return end @@ -138,7 +144,7 @@ function M.setup(user_opts) M.config = vim.tbl_extend('force', M.config, user_opts or {}) -- Messy, change with nvim_exec once merged - vim.api.nvim_command('let g:loaded_text_objects_plugin = 1') + vim.g.loaded_text_objects_plugin = 1 local vim_func = [[ function! CommentOperator(type) abort let reg_save = @@ @@ -151,9 +157,9 @@ function M.setup(user_opts) if M.config.create_mappings then local opts = {noremap = true, silent = true} - vim.api.nvim_set_keymap("n", M.config.line_mapping, ":set operatorfunc=CommentOperatorg@l", opts) - vim.api.nvim_set_keymap("n", M.config.operator_mapping, ":set operatorfunc=CommentOperatorg@", opts) - vim.api.nvim_set_keymap("v", M.config.operator_mapping, ":call CommentOperator(visualmode())", opts) + api.nvim_set_keymap("n", M.config.line_mapping, "set operatorfunc=CommentOperatorg@l", opts) + api.nvim_set_keymap("n", M.config.operator_mapping, "set operatorfunc=CommentOperatorg@", opts) + api.nvim_set_keymap("v", M.config.operator_mapping, ":call CommentOperator(visualmode())", opts) end end