Skip to content

Commit

Permalink
feature: add option to indentation textobject to include or restrict …
Browse files Browse the repository at this point in the history
…blanklines

default behavior remains the same and will include blanklines
  • Loading branch information
kevintraver committed Oct 6, 2023
1 parent eba7c5d commit 95e2eed
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
7 changes: 4 additions & 3 deletions lua/various-textobjs/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ local function argConvert(arg)
if arg == false then return "outer" end
if arg == true then return "inner" end
if arg ~= "outer" and arg ~= "inner" then
u.notify(
u.notify(
"Invalid argument for textobject, only 'outer' and 'inner' accepted. Falling back to outer textobject.",
"warn"
)
Expand Down Expand Up @@ -67,8 +67,9 @@ 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 includeBlankLines false|true include blanklines in the selection
function M.indentation(startBorder, endBorder, includeBlankLines)
linewise.indentation(argConvert(startBorder), argConvert(endBorder), includeBlankLines)
end

---from cursor position down all lines with same or higher indentation;
Expand Down
24 changes: 19 additions & 5 deletions lua/various-textobjs/linewise-textobjs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ function M.mdFencedCodeBlock(scope, lookForwL)
end

---lines visible in window textobj
function M.visibleInWindow()
function M.visibleInWindow()
local start = fn.line("w0")
local ending = fn.line("w$")
setLinewiseSelection(start, ending)
end

-- from cursor line to last visible line in window
function M.restOfWindow()
function M.restOfWindow()
local start = fn.line(".")
local ending = fn.line("w$")
setLinewiseSelection(start, ending)
Expand All @@ -141,7 +141,9 @@ end
---indentation textobj
---@param startBorder "inner"|"outer"
---@param endBorder "inner"|"outer"
function M.indentation(startBorder, endBorder)
---@param includeBlankLines? "true"|"false"
function M.indentation(startBorder, endBorder, includeBlankLines)
includeBlankLines = includeBlankLines == nil and "true" or includeBlankLines
local curLnum = fn.line(".")
local lastLine = fn.line("$")
while isBlankLine(curLnum) do -- when on blank line, use next line
Expand All @@ -158,10 +160,22 @@ 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 == "true" 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 == "true" and isBlankLine(nextLnum))
or fn.indent(nextLnum) >= indentOfStart
)
do
nextLnum = nextLnum + 1
end

Expand Down

0 comments on commit 95e2eed

Please sign in to comment.