Skip to content

Commit

Permalink
feat: new textobj lastChange
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgrieser committed Dec 28, 2023
1 parent e04943a commit 1e41dbf
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Bundle of more than 30 new textobjects for Neovim.
| chainMember | field with the full call, like `.encode(param)` | outer includes the leading `.` (or `:`) | small | `im`, `am` | all |
| visibleInWindow | all lines visible in the current window | \- | \- | `gw` | all |
| restOfWindow | from the cursorline to the last line in the window | \- | \- | `gW` | all |
| lastChange | Last Change, yank, or paste.[^2] (Does not work with changes that are deletions.) | \- | \- | `g;` | all |
| mdlink | markdown link like `[title](url)` | inner is only the link title (between the `[]`) | small | `il`, `al` | markdown, toml |
| mdFencedCodeBlock | markdown fenced code (enclosed by three backticks) | outer includes the enclosing backticks | big | `iC`, `aC` | markdown |
| cssSelector | class in CSS like `.my-class` | outer includes trailing comma and space | small | `ic`, `ac` | css, scss |
Expand Down Expand Up @@ -228,6 +229,8 @@ keymap({ "o", "x" }, "YOUR_MAPPING", "<cmd>lua require('various-textobjs').entir

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

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

keymap(
{ "o", "x" },
"YOUR_MAPPING",
Expand Down Expand Up @@ -567,3 +570,8 @@ __Profiles__
/></a>

[^1]: This respects vim's [quoteescape option](https://neovim.io/doc/user/options.html#'quoteescape').

[^2]: The `lastChange` textobject does not work well with plugins that
manipulate paste operations such as
[yanky.nvim](https://github.com/gbprod/yanky.nvim) or plugins that auto-save
the buffer.
12 changes: 12 additions & 0 deletions lua/various-textobjs/charwise-textobjs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,18 @@ function M.chainMember(scope, lookForwL)
selectTextobj(pattern, scope, lookForwL)
end

function M.lastChange()
local changeStartPos = vim.api.nvim_buf_get_mark(0, "[")
local changeEndPos = vim.api.nvim_buf_get_mark(0, "]")

if changeStartPos[1] == changeEndPos[1] and changeStartPos[2] == changeEndPos[2] then
u.notify("Last Change was a deletion operation, aborting.", "warn")
return
end

setSelection(changeStartPos, changeEndPos)
end

--------------------------------------------------------------------------------
-- FILETYPE SPECIFIC TEXTOBJS

Expand Down
3 changes: 2 additions & 1 deletion lua/various-textobjs/default-keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ local oneMaps = {
column = "|",
entireBuffer = "gG", -- G + gg
url = "L", -- gu, gU, and U would conflict with gugu, gUgU, and gUU. u would conflict with gcu (undo comment)
multiCommentedLines = "gc" -- consistent with usual mapping for commenting lines
multiCommentedLines = "gc", -- consistent with usual mapping for commenting lines
lastChange = "g;", -- consistent with g; movement
}
local ftMaps = {
{
Expand Down
2 changes: 1 addition & 1 deletion lua/various-textobjs/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ local function argConvert(arg)
if arg == false then return "outer" end
if arg == true then return "inner" end
if arg == "outer" or arg == "inner" then return arg end

u.notify(
"Invalid argument for textobject, only 'outer' and 'inner' accepted. Falling back to outer textobject.",
"warn"
Expand Down Expand Up @@ -98,6 +97,7 @@ function M.toNextClosingBracket() charwise.toNextClosingBracket(config.lookForwa
function M.toNextQuotationMark() charwise.toNextQuotationMark(config.lookForwardSmall) end
function M.url() charwise.url(config.lookForwardBig) end
function M.diagnostic() charwise.diagnostic(config.lookForwardBig) end
function M.lastChange() charwise.lastChange() end

---@param scope "inner"|"outer"
function M.anyQuote(scope) charwise.anyQuote(argConvert(scope), config.lookForwardSmall) end
Expand Down

0 comments on commit 1e41dbf

Please sign in to comment.