From 0626a1888ababd37630466f09c44378fff05fa6b Mon Sep 17 00:00:00 2001 From: Chris Grieser <73286100+chrisgrieser@users.noreply.github.com> Date: Fri, 6 Dec 2024 13:23:11 +0100 Subject: [PATCH] refactor: `indentation` textobj uses config instead of `noBlanks` param --- README.md | 21 ++++++++++----------- lua/various-textobjs/config.lua | 6 ++++++ lua/various-textobjs/textobjs/linewise.lua | 22 ++++++++++++++-------- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 80567f2..38ee8df 100644 --- a/README.md +++ b/README.md @@ -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, @@ -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 @@ -188,13 +194,6 @@ 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")' -) ``` ## Advanced usage / API diff --git a/lua/various-textobjs/config.lua b/lua/various-textobjs/config.lua index 0744d3c..3890c28 100644 --- a/lua/various-textobjs/config.lua +++ b/lua/various-textobjs/config.lua @@ -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, }, diff --git a/lua/various-textobjs/textobjs/linewise.lua b/lua/various-textobjs/textobjs/linewise.lua index 708637d..f34c349 100644 --- a/lua/various-textobjs/textobjs/linewise.lua +++ b/lua/various-textobjs/textobjs/linewise.lua @@ -148,19 +148,25 @@ 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 @@ -168,7 +174,7 @@ function M.indentation(startBorder, endBorder, blankLines) 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 @@ -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 @@ -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