Skip to content

Commit

Permalink
feat: new textobj anyBracket
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgrieser committed Dec 23, 2023
1 parent 11cb208 commit 1826107
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<a href="https://dotfyle.com/plugins/chrisgrieser/nvim-various-textobjs">
<img alt="badge" src="https://dotfyle.com/plugins/chrisgrieser/nvim-various-textobjs/shield"/></a>

Bundle of more than two dozen new textobjects for Neovim.
Bundle of more than 30 new textobjects for Neovim.

<!-- toc -->

Expand Down Expand Up @@ -36,7 +36,8 @@ Bundle of more than two dozen new textobjects for Neovim.
| subword | like `iw`, but treating `-`, `_`, and `.` as word delimiters *and* only part of camelCase | outer includes trailing `_`,`-`, or space | \- | `iS`, `aS` | all |
| toNextClosingBracket | from cursor to next closing `]`, `)`, or `}` | \- | small | `C` | all |
| toNextQuotationMark | from cursor to next unescaped[^1] `"`, `'`, or `` ` `` | \- | small | `Q` | all |
| anyQuote | between any unescaped[^1] `"`, `'`, or `` ` `` *in a line* | outer includes the quotatin marks | small | `iq`/`aq` | all |
| anyQuote | between any unescaped[^1] `"`, `'`, or `` ` `` *in a line* | outer includes the quotation marks | small | `iq`/`aq` | all |
| anyBracket | between any `()`, `[]`, or `{}` *in a line* | outer includes the brackets | small | `io`/`ao` | all |
| restOfParagraph | like `}`, but linewise | \- | \- | `r` | all |
| multiCommentedLines | consecutive, fully commented lines | \- | big | `gc` | all |
| entireBuffer | entire buffer as one text object | \- | \- | `gG` | all |
Expand Down Expand Up @@ -210,6 +211,9 @@ keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').toNex
keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').anyQuote('inner')<CR>")
keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').anyQuote('outer')<CR>")

keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').anyBracket('inner')<CR>")
keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').anyBracket('outer')<CR>")

keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').restOfParagraph()<CR>")

keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').entireBuffer()<CR>")
Expand Down
17 changes: 14 additions & 3 deletions lua/various-textobjs/charwise-textobjs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ function M.anyQuote(scope, lookForwL)
-- the off-chance that the user has customized this.
local escape = vim.opt_local.quoteescape:get() -- default: \
local patterns = {
('([^%s]").-[^%s](")'):format(escape, escape),
("([^%s]').-[^%s](')"):format(escape, escape),
("([^%s]`).-[^%s](`)"):format(escape, escape),
('([^%s]").-[^%s](")'):format(escape, escape), -- "
("([^%s]').-[^%s](')"):format(escape, escape), -- '
("([^%s]`).-[^%s](`)"):format(escape, escape), -- `
}

selectTextobj(patterns, scope, lookForwL)
Expand All @@ -193,6 +193,17 @@ function M.anyQuote(scope, lookForwL)
if scope == "outer" then u.normal("ol") end
end

---@param scope "inner"|"outer"
---@param lookForwL integer
function M.anyBracket(scope, lookForwL)
local patterns = {
"(%().-(%))", -- ()
"(%[).-(%])", -- []
"({).-(})", -- {}
}
selectTextobj(patterns, scope, lookForwL)
end

---near end of the line, ignoring trailing whitespace
---(relevant for markdown, where you normally add a -space after the `.` ending a sentence.)
function M.nearEoL()
Expand Down
1 change: 1 addition & 0 deletions lua/various-textobjs/default-keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ local innerOuterMaps = {
lineCharacterwise = "_",
greedyOuterIndentation = "g",
anyQuote = "q",
anyBracket = "o",
}
local oneMaps = {
nearEoL = "n", -- does override the builtin "to next search match" textobj, but nobody really uses that
Expand Down
2 changes: 2 additions & 0 deletions lua/various-textobjs/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ function M.toNextQuotationMark() charwise.toNextQuotationMark(lookForwardSmall)

function M.anyQuote(scope) charwise.anyQuote(argConvert(scope), lookForwardSmall) end

function M.anyBracket(scope) charwise.anyBracket(argConvert(scope), lookForwardSmall) end

---current line (but characterwise)
function M.lineCharacterwise(scope) charwise.lineCharacterwise(argConvert(scope)) end

Expand Down

0 comments on commit 1826107

Please sign in to comment.