Skip to content

Commit

Permalink
chore!: remove old lua APIs (#238)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
numToStr authored Oct 2, 2022
1 parent d9cfae1 commit a85ca1b
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 335 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
- uses: actions/checkout@v2

- name: Generating help
shell: bash
run: |
curl -Lq https://github.com/numToStr/lemmy-help/releases/latest/download/lemmy-help-x86_64-unknown-linux-gnu.tar.gz | tar xz
./lemmy-help -fact \
Expand Down
35 changes: 14 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,36 +225,29 @@ There are two hook methods i.e `pre_hook` and `post_hook` which are called befor

<a id="pre-hook"></a>

- `pre_hook` - Called with a `ctx` argument (Read `:h comment.utils.CommentCtx`) before (un)comment. Can optionally return a `commentstring` to be used for (un)commenting. You can use [nvim-ts-context-commentstring](https://github.com/JoosepAlviste/nvim-ts-context-commentstring) to easily comment `tsx/jsx` files.
- `pre_hook` - Called with a `ctx` argument (Read `:h comment.utils.CommentCtx`) before (un)comment. Can optionally return a `commentstring` to be used for (un)commenting.

```lua
{
pre_hook = function(ctx)
-- Only calculate commentstring for tsx filetypes
if vim.bo.filetype == 'typescriptreact' then
local U = require('Comment.utils')

-- Determine whether to use linewise or blockwise commentstring
local type = ctx.ctype == U.ctype.linewise and '__default' or '__multiline'

-- Determine the location where to calculate commentstring from
local location = nil
if ctx.ctype == U.ctype.blockwise then
location = require('ts_context_commentstring.utils').get_cursor_location()
elseif ctx.cmotion == U.cmotion.v or ctx.cmotion == U.cmotion.V then
location = require('ts_context_commentstring.utils').get_visual_start_location()
end

return require('ts_context_commentstring.internal').calculate_commentstring({
key = type,
location = location,
})
if ctx.range.srow == ctx.range.erow then
-- do something with the current line
else
-- do something with lines range
end
end,
}
```

> **Note** - `Comment.nvim` already supports [`treesitter`](#treesitter) out-of-the-box except for `tsx/jsx`.
You can also integrate [nvim-ts-context-commentstring](https://github.com/JoosepAlviste/nvim-ts-context-commentstring#commentnvim) using `pre_hook` to easily comment `tsx/jsx` files.

> **Note** - `Comment.nvim` already supports [`treesitter`](#treesitter) out-of-the-box for all the languages except `tsx/jsx`.
```lua
{
pre_hook = require('ts_context_commentstring.integrations.comment_nvim').create_pre_hook(),
}
```

<a id="post-hook"></a>

Expand Down
293 changes: 11 additions & 282 deletions lua/Comment/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,267 +11,7 @@ local Op = require('Comment.opfunc')
local Ex = require('Comment.extra')
local A = vim.api

local api = {}

------------ OLD START ------------

local function D(name, alt)
vim.deprecate("require('Comment.api')." .. name, "require('Comment.api')." .. alt, '0.7', 'Comment.nvim', false)
end

--====================================
--============ CORE API ==============
--====================================

--######### LINEWISE #########--

---@private
---@deprecated
---Toggle linewise-comment on the current line
---@param cfg? CommentConfig
function api.toggle_current_linewise(cfg)
D('toggle_current_linewise({cfg})', 'toggle.linewise.current(nil, {cfg})')
api.toggle.linewise.current(nil, cfg)
end

---@private
---@deprecated
---(Operator-Pending) Toggle linewise-comment on the current line
---@param opmode OpMotion
---@param cfg? CommentConfig
function api.toggle_current_linewise_op(opmode, cfg)
D('toggle_current_linewise_op({opmode}, {cfg})', 'toggle.linewise.current({opmode}, {cfg})')
api.toggle.linewise.current(opmode, cfg)
end

---@private
---@deprecated
---(Operator-Pending) Toggle linewise-comment over multiple lines
---@param opmode OpMotion
---@param cfg? CommentConfig
function api.toggle_linewise_op(opmode, cfg)
D('toggle_linewise_op({opmode}, {cfg})', 'toggle.linewise({opmode}, {cfg})')
api.toggle.linewise(opmode, cfg)
end

---@private
---@deprecated
---Toggle linewise-comment over multiple lines using `vim.v.count`
---@param cfg? CommentConfig
function api.toggle_linewise_count(cfg)
D('toggle_linewise_count({cfg})', 'toggle.linewise.count(vim.v.count, {cfg})')
api.toggle.linewise.count(vim.v.count, cfg)
end

--######### BLOCKWISE #########--

---@private
---@deprecated
---Toggle blockwise comment on the current line
---@param cfg? CommentConfig
function api.toggle_current_blockwise(cfg)
D('toggle_current_blockwise({cfg})', 'toggle.blockwise.current(nil, {cfg})')
api.toggle.blockwise.current(nil, cfg)
end

---@private
---@deprecated
---(Operator-Pending) Toggle blockwise comment on the current line
---@param opmode OpMotion
---@param cfg? CommentConfig
function api.toggle_current_blockwise_op(opmode, cfg)
D('toggle_current_blockwise_op({opmode}, {cfg})', 'toggle.blockwise.current({opmode}, {cfg})')
api.toggle.blockwise.current(opmode, cfg)
end

---@private
---@deprecated
---(Operator-Pending) Toggle blockwise-comment over multiple lines
---@param opmode OpMotion
---@param cfg? CommentConfig
function api.toggle_blockwise_op(opmode, cfg)
D('toggle_blockwise_op({opmode}, {cfg})', 'toggle.blockwise({opmode}, {cfg})')
api.toggle.blockwise(opmode, cfg)
end

---@private
---@deprecated
---Toggle blockwise-comment over multiple lines using `vim.v.count`
---@param cfg? CommentConfig
function api.toggle_blockwise_count(cfg)
D('toggle_blockwise_count({cfg})', 'toggle.blockwise.count(vim.v.count, {cfg})')
api.toggle.blockwise.count(vim.v.count, cfg)
end

--=====================================
--============ EXTRA API ==============
--=====================================

--######### LINEWISE #########--

---@private
---@deprecated
---Insert a linewise-comment below
---@param cfg? CommentConfig
function api.insert_linewise_below(cfg)
D('insert_linewise_below({cfg})', 'insert.linewise.below({cfg})')
api.insert.linewise.below(cfg)
end

---@private
---@deprecated
---Insert a linewise-comment above
---@param cfg? CommentConfig
function api.insert_linewise_above(cfg)
D('insert_linewise_above({cfg})', 'insert.linewise.above({cfg})')
api.insert.linewise.above(cfg)
end

---@private
---@deprecated
---Insert a linewise-comment at the end-of-line
---@param cfg? CommentConfig
function api.insert_linewise_eol(cfg)
D('insert_linewise_eol({cfg})', 'insert.linewise.eol({cfg})')
api.insert.linewise.eol(cfg)
end

--######### BLOCKWISE #########--

---@private
---@deprecated
---Insert a blockwise-comment below
---@param cfg? CommentConfig
function api.insert_blockwise_below(cfg)
D('insert_blockwise_below({cfg})', 'insert.blockwise.below({cfg})')
api.insert.blockwise.below(cfg)
end

---@private
---@deprecated
---Insert a blockwise-comment above
---@param cfg? CommentConfig
function api.insert_blockwise_above(cfg)
D('insert_blockwise_above({cfg})', 'insert.blockwise.above({cfg})')
api.insert.blockwise.above(cfg)
end

---@private
---@deprecated
---Insert a blockwise-comment at the end-of-line
---@param cfg? CommentConfig
function api.insert_blockwise_eol(cfg)
D('insert_blockwise_eol({cfg})', 'insert.blockwise.eol({cfg})')
api.insert.blockwise.eol(cfg)
end

--========================================
--============ EXTENDED API ==============
--========================================

--######### LINEWISE #########--

---@private
---@deprecated
---Comment current line using linewise-comment
---@param cfg? CommentConfig
function api.comment_current_linewise(cfg)
D('comment_current_linewise({cfg})', 'comment.linewise.current(nil, {cfg})')
api.comment.linewise.current(nil, cfg)
end

---@private
---@deprecated
---(Operator-Pending) Comment current line using linewise-comment
---@param opmode OpMotion
---@param cfg? CommentConfig
function api.comment_current_linewise_op(opmode, cfg)
D('comment_current_linewise_op({opmode}, {cfg})', 'comment.linewise.current({opmode}, {cfg})')
api.comment.linewise.current(opmode, cfg)
end

---@private
---@deprecated
---(Operator-Pending) Comment multiple line using linewise-comment
---@param opmode OpMotion
---@param cfg? CommentConfig
function api.comment_linewise_op(opmode, cfg)
D('comment_linewise_op({opmode}, {cfg})', 'comment.linewise({opmode}, {cfg})')
api.comment.linewise(opmode, cfg)
end

---@private
---@deprecated
---Uncomment current line using linewise-comment
---@param cfg? CommentConfig
function api.uncomment_current_linewise(cfg)
D('uncomment_current_linewise({cfg})', 'uncomment.linewise.current({nil}, {cfg})')
api.uncomment.linewise.current(nil, cfg)
end

---@private
---@deprecated
---(Operator-Pending) Uncomment current line using linewise-comment
---@param opmode OpMotion
---@param cfg? CommentConfig
function api.uncomment_current_linewise_op(opmode, cfg)
D('uncomment_current_linewise_op({opmode}, {cfg})', 'uncomment.linewise.current({opmode}, {cfg})')
api.uncomment.linewise.current(opmode, cfg)
end

---@private
---@deprecated
---(Operator-Pending) Uncomment multiple line using linewise-comment
---@param opmode OpMotion
---@param cfg? CommentConfig
function api.uncomment_linewise_op(opmode, cfg)
D('uncomment_linewise_op({opmode}, {cfg})', 'uncomment.linewise({opmode}, {cfg})')
api.uncomment.linewise(opmode, cfg)
end

--######### BLOCKWISE #########--

---@private
---@deprecated
---Comment current line using linewise-comment
---@param cfg? CommentConfig
function api.comment_current_blockwise(cfg)
D('comment_current_blockwise({cfg})', 'comment.blockwise.current(nil, {cfg})')
api.comment.blockwise.current(nil, cfg)
end

---@private
---@deprecated
---(Operator-Pending) Comment current line using blockwise-comment
---@param opmode OpMotion
---@param cfg? CommentConfig
function api.comment_current_blockwise_op(opmode, cfg)
D('comment_current_blockwise_op({opmode}, {cfg})', 'comment.blockwise.current({opmode}, {cfg})')
api.comment.blockwise.current(opmode, cfg)
end

---@private
---@deprecated
---Uncomment current line using blockwise-comment
---@param cfg? CommentConfig
function api.uncomment_current_blockwise(cfg)
D('uncomment_current_blockwise({cfg})', 'uncomment.blockwise.current(nil, {cfg})')
api.uncomment.blockwise.current(nil, cfg)
end

---@private
---@deprecated
---(Operator-Pending) Uncomment current line using blockwise-comment
---@param opmode OpMotion
---@param cfg? CommentConfig
function api.uncomment_current_blockwise_op(opmode, cfg)
D('uncomment_current_blockwise_op({opmode}, {cfg})', 'uncomment.blockwise.current({opmode}, {cfg})')
api.uncomment.blockwise.current(opmode, cfg)
end

------------ OLD END ------------

local core = {}
local api, core = {}, {}

function core.__index(that, ctype)
local idxd = {}
Expand Down Expand Up @@ -440,12 +180,9 @@ api.insert = setmetatable({}, {
end,
})

-- TODO: After removing old API
-- 1. make `api.locked` a simple function call
-- 2. fix emmylua doc

---Wraps the given API function with 'lockmarks' to preserve marks/jumps
---@type fun(cb:string):fun(motion:OpMotion)
---@param cb string Name of API function
---@return fun(motion:OpMotion) #Callback function
---@see lockmarks
---@see comment.opfunc.OpMotion
---@usage [[
Expand All @@ -468,29 +205,21 @@ api.insert = setmetatable({}, {
--- lockmarks lua require('Comment.api').toggle.linewise.current()
---]])
---@usage ]]
api.locked = setmetatable({}, {
__index = function(this, cb)
D(string.format('locker.%s(args...)', cb), string.format('locked(%q)(args...)', cb))
return this(cb)
end,
__call = function(_, cb)
---operatorfunc callback
---@param motion OpMotion
return function(motion)
return A.nvim_command(
('lockmarks lua require("Comment.api").%s(%s)'):format(cb, motion and ('%q'):format(motion))
)
end
end,
})
function api.locked(cb)
return function(motion)
return A.nvim_command(
('lockmarks lua require("Comment.api").%s(%s)'):format(cb, motion and ('%q'):format(motion))
)
end
end

---Callback function which does the following
--- 1. Sets 'operatorfunc' for dot-repeat
--- 2. Preserves jumps and marks
--- 3. Stores last cursor position
---@param cb string Name of the API function to call
---@param op 'g@'|'g@$' Operator string to execute
---@return fun():string _ Keymap RHS callback
---@return fun():string #Keymap RHS callback
---@see g@
---@see operatorfunc
---@usage [[
Expand Down
Loading

0 comments on commit a85ca1b

Please sign in to comment.