Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: new lua API (using metatables) #183

Merged
merged 6 commits into from
Aug 12, 2022
Merged

feat!: new lua API (using metatables) #183

merged 6 commits into from
Aug 12, 2022

Conversation

numToStr
Copy link
Owner

@numToStr numToStr commented Jul 17, 2022

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 numToStr marked this pull request as draft July 17, 2022 16:03
@numToStr
Copy link
Owner Author

numToStr commented Jul 18, 2022

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

@numToStr numToStr force-pushed the meta/api branch 5 times, most recently from a3a2ab1 to 4d97e28 Compare July 26, 2022 08:17
@numToStr numToStr force-pushed the meta/api branch 2 times, most recently from fbcc2e7 to 31e12c4 Compare August 2, 2022 10:13
This deprecates the old API and will be removed in the next tagged
releases probably v0.7

Currently every Lua API is a function call, which is fine, but most of
them are similar to others only having different arguments and uses same
call under the hood. This is not extensible if we want to create a new
mode #17 and have slight maintenance burden.

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

This also changes the `ctype` utility. If you are using it then you
might wanna migrate to the following:

- `U.ctype.line` → `U.ctype.linewise`
- `U.ctype.block` → `U.ctype.blockwise`

Resolves #180
@numToStr numToStr marked this pull request as ready for review August 12, 2022 14:47
@numToStr numToStr merged commit cd1c381 into master Aug 12, 2022
@numToStr numToStr deleted the meta/api branch August 12, 2022 14:49
@numToStr numToStr mentioned this pull request Sep 12, 2022
7 tasks
numToStr added a commit that referenced this pull request Oct 2, 2022
If you are using these old APIs, which I highly doubt that by now, then you should have a look at #183. Beside removing old APIs, this PR also includes some doc updates.
Fryuni added a commit to Fryuni/doom-nvim that referenced this pull request Oct 9, 2022
The call convention for the `Comment.nvim` plugin has changed on the
latest version.

Previously the methods were flattened in the required object with `_`
separators, now they are organized in a nested structure using
metatables.

- Added upstream on numToStr/Comment.nvim#183
NTBBloodbath pushed a commit to doom-neovim/doom-nvim that referenced this pull request Oct 9, 2022
The call convention for the `Comment.nvim` plugin has changed on the
latest version.

Previously the methods were flattened in the required object with `_`
separators, now they are organized in a nested structure using
metatables.

- Added upstream on numToStr/Comment.nvim#183
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

setmetatable based Lua API
1 participant