Skip to content

Commit

Permalink
feat: add blanklines option to indentation textobject
Browse files Browse the repository at this point in the history
Merge pull request #54 from kevintraver/feature/indentation-blank-lines-option
  • Loading branch information
chrisgrieser authored Oct 7, 2023
2 parents 65b12ed + d34624a commit 6f86a95
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ vim.keymap.set(
"ai",
'<cmd>lua require("various-textobjs").indentation("outer", "inner")<CR>'
)

-- an additional parameter can be passed to control whether blank lines are included
vim.keymap.set(
{ "o", "x" },
"ai",
'<cmd>lua require("various-textobjs").indentation("outer", "inner", "noBlanks")<CR>'
)
```

For your convenience, here the code to create mappings for all text objects. You can copypaste this list and enter your own bindings.
Expand Down
7 changes: 5 additions & 2 deletions lua/various-textobjs/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 10 additions & 3 deletions lua/various-textobjs/linewise-textobjs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 6f86a95

Please sign in to comment.