Comment.nvim icon indicating copy to clipboard operation
Comment.nvim copied to clipboard

feat!: new lua API (using metatables)

Open numToStr opened this issue 2 years ago • 1 comments

TL;DR - If you are not using the Lua API or <Plug> mappings then you don't need to worry about anything :)


This deprecates the old Lua API and will be removed in the next tagged release of the plugin, probably v0.7.0.

Currently every Lua API is a function call, which is fine, and they are similar to one another having few different arguments. This is not extensible and have slight maintenance burden if we want to create new mode (ref: #17) or introduce new API functions.

Using setmetatable we can build-up the API as we go. This will be extensible and have less maintenance overhead.

Following is the new Lua API:

All API functions are dot repeatable except *.count()

require('Comment.api').toggle.linewise()
require('Comment.api').toggle.linewise.current()
require('Comment.api').toggle.linewise.count()

require('Comment.api').toggle.blockwise()
require('Comment.api').toggle.blockwise.current()
require('Comment.api').toggle.blockwise.count()

require('Comment.api').comment.linewise()
require('Comment.api').comment.linewise.current()
require('Comment.api').comment.linewise.count()

require('Comment.api').comment.blockwise()
require('Comment.api').comment.blockwise.current()
require('Comment.api').comment.blockwise.count()

require('Comment.api').uncomment.linewise()
require('Comment.api').uncomment.linewise.current()
require('Comment.api').uncomment.linewise.count()

require('Comment.api').uncomment.blockwise()
require('Comment.api').uncomment.blockwise.current()
require('Comment.api').uncomment.blockwise.count()

Old API have proper deprecation message which suggests equivalent New API


This also introduces couple of (breaking) changes apart from the lua API:

  1. Rename the following <Plug> mappings (to be consistent with API)

    • <Plug>(comment_toggle_current_linewise) -> <Plug>(comment_toggle_linewise_current)
    • <Plug>(comment_toggle_current_blockwise) -> <Plug>(comment_toggle_blockwise_current)
  2. Changed field names of utils.ctype object

    • U.ctype.lineU.ctype.linewise
    • U.ctype.blockU.ctype.blockwise
  3. Now require('Comment.api').locked is a function which takes the name of the new lua API call (old signature is deprecated)

-- OLD
require("Comment.api").locked.toggle_current_linewise()
require("Comment.api").locked.comment_linewise_op(vim.fn.visualmode())

-- NEW
require("Comment.api").locked('toggle.linewise.current')()
require("Comment.api").locked('comment.linewise')(vim.fn.visualmode())
" NOTE: `locked` interface is just a wrapper around `lockmarks`
lockmarks lua require("Comment.api").toggle.linewise.current()
  1. Removed redundant cmotion (last) argument from require('Comment.opfunc').opfunc() function

Resolves #180

numToStr avatar Jul 17 '22 15:07 numToStr

If anyone finds this destructive, then they can stick with https://github.com/numToStr/Comment.nvim/releases/tag/v0.6.1

numToStr avatar Jul 18 '22 13:07 numToStr