Skip to content

Commit

Permalink
refactor: indentation textobj uses config instead of noBlanks param
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgrieser committed Dec 6, 2024
1 parent 7108b38 commit 0626a18
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ The `.setup()` call is optional if you are fine with the defaults below.
```lua
-- default config
require("various-textobjs").setup {
-- See overview table in README for the defaults keymaps.
-- See overview table in README for the defaults keymaps.
-- (Note that lazy-loading this plugin, the default keymaps cannot be set up.
-- if you set this to `true`, you thus need to add `event = "VeryLazy"` to your
-- if you set this to `true`, you thus need to add `lazy = false` to your
-- lazy.nvim config.)
useDefaultKeymaps = false,

Expand All @@ -140,9 +140,15 @@ require("various-textobjs").setup {
whenObjectNotFound = true,
},

-- extra configuration for specific text objects
textobjs = {
diagnostic = {
wrap = true
indentation = {
-- `false`: only indentation changes delimit the text object
-- `true`: indentation changes as well as blanks delimit the text object
blanksAreDelimiter = false,
},
diagnostic = {
wrap = true,
},
subword = {
-- When deleting the start of a camelCased word, the result should
Expand Down Expand Up @@ -188,13 +194,6 @@ 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>'
)
```

## Advanced usage / API
Expand Down
6 changes: 6 additions & 0 deletions lua/various-textobjs/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ local defaultConfig = {
whenObjectNotFound = true,
},

-- extra configuration for specific text objects
textobjs = {
indentation = {
-- `false`: only indentation changes delimit the text object
-- `true`: indentation changes as well as blanks delimit the text object
blanksAreDelimiter = false,
},
diagnostic = {
wrap = true,
},
Expand Down
22 changes: 14 additions & 8 deletions lua/various-textobjs/textobjs/linewise.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,27 +148,33 @@ end

--------------------------------------------------------------------------------

---indentation textobj
---@param startBorder "inner"|"outer"
---@param endBorder "inner"|"outer"
---@param blankLines? "withBlanks"|"noBlanks"
function M.indentation(startBorder, endBorder, blankLines)
if not blankLines then blankLines = "withBlanks" end
---@return boolean|nil success
function M.indentation(startBorder, endBorder, oldBlankSetting)
-- DEPRECATION (2024-12-06)
if oldBlankSetting ~= nil then
u.warn(
"`.indentation()` does not use a 3rd argument anymore. Use the config `textobjs.indent.blanksAreDelimiter` instead."
)
end
local blanksAreDelimiter =
require("various-textobjs.config").config.textobjs.indentation.blanksAreDelimiter

local curLnum = vim.api.nvim_win_get_cursor(0)[1]
local lastLine = vim.api.nvim_buf_line_count(0)
while isBlankLine(curLnum) do -- when on blank line, use next line
if lastLine == curLnum then
u.notFoundMsg("No indented line found.")
return
return false
end
curLnum = curLnum + 1
end

local indentOfStart = vim.fn.indent(curLnum)
if indentOfStart == 0 then
u.warn("Current line is not indented.")
return false -- return value needed for `greedyOuterIndentation` textobj
return false
end

local prevLnum = curLnum - 1
Expand All @@ -177,7 +183,7 @@ function M.indentation(startBorder, endBorder, blankLines)
while
prevLnum > 0
and (
(blankLines == "withBlanks" and isBlankLine(prevLnum))
(isBlankLine(prevLnum) and not blanksAreDelimiter)
or vim.fn.indent(prevLnum) >= indentOfStart
)
do
Expand All @@ -186,7 +192,7 @@ function M.indentation(startBorder, endBorder, blankLines)
while
nextLnum <= lastLine
and (
(blankLines == "withBlanks" and isBlankLine(nextLnum))
(isBlankLine(nextLnum) and not blanksAreDelimiter)
or vim.fn.indent(nextLnum) >= indentOfStart
)
do
Expand Down

0 comments on commit 0626a18

Please sign in to comment.