From d34624a4962fa948fc6321c5bf9d3b80c13e562d Mon Sep 17 00:00:00 2001 From: Kevin Traver Date: Fri, 6 Oct 2023 02:08:26 -0600 Subject: [PATCH] feature: add option to indentation textobject to include or restrict blanklines default behavior remains the same and will include blanklines --- README.md | 7 +++++++ lua/various-textobjs/init.lua | 7 +++++-- lua/various-textobjs/linewise-textobjs.lua | 13 ++++++++++--- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 20e4339..d0f70b2 100644 --- a/README.md +++ b/README.md @@ -150,6 +150,13 @@ vim.keymap.set( "ai", 'lua require("various-textobjs").indentation("outer", "inner")' ) + +-- an additional parameter can be passed to control whether blank lines are included +vim.keymap.set( + { "o", "x" }, + "ai", + 'lua require("various-textobjs").indentation("outer", "inner", "noBlanks")' +) ``` For your convenience, here the code to create mappings for all text objects. You can copypaste this list and enter your own bindings. diff --git a/lua/various-textobjs/init.lua b/lua/various-textobjs/init.lua index 3372d5d..fba886b 100644 --- a/lua/various-textobjs/init.lua +++ b/lua/various-textobjs/init.lua @@ -67,8 +67,11 @@ function M.entireBuffer() linewise.entireBuffer() end ---indentation textobj ---@param startBorder "inner"|"outer" exclude the startline ---@param endBorder "inner"|"outer" exclude the endline -function M.indentation(startBorder, endBorder) - linewise.indentation(argConvert(startBorder), argConvert(endBorder)) +---@param blankLines? "withBlanks"|"noBlanks" +function M.indentation(startBorder, endBorder, blankLines) + local includeBlankLines = true + if blankLines == "noBlanks" then includeBlankLines = false end + linewise.indentation(argConvert(startBorder), argConvert(endBorder), includeBlankLines) end ---from cursor position down all lines with same or higher indentation; diff --git a/lua/various-textobjs/linewise-textobjs.lua b/lua/various-textobjs/linewise-textobjs.lua index 1794cb6..799356a 100644 --- a/lua/various-textobjs/linewise-textobjs.lua +++ b/lua/various-textobjs/linewise-textobjs.lua @@ -141,7 +141,8 @@ end ---indentation textobj ---@param startBorder "inner"|"outer" ---@param endBorder "inner"|"outer" -function M.indentation(startBorder, endBorder) +---@param includeBlankLines? boolean +function M.indentation(startBorder, endBorder, includeBlankLines) local curLnum = fn.line(".") local lastLine = fn.line("$") while isBlankLine(curLnum) do -- when on blank line, use next line @@ -158,10 +159,16 @@ function M.indentation(startBorder, endBorder) local prevLnum = curLnum - 1 local nextLnum = curLnum + 1 - while prevLnum > 0 and (isBlankLine(prevLnum) or fn.indent(prevLnum) >= indentOfStart) do + while + prevLnum > 0 + and ((includeBlankLines and isBlankLine(prevLnum)) or fn.indent(prevLnum) >= indentOfStart) + do prevLnum = prevLnum - 1 end - while nextLnum <= lastLine and (isBlankLine(nextLnum) or fn.indent(nextLnum) >= indentOfStart) do + while + nextLnum <= lastLine + and ((includeBlankLines and isBlankLine(nextLnum)) or fn.indent(nextLnum) >= indentOfStart) + do nextLnum = nextLnum + 1 end