Skip to content

Commit

Permalink
refactor(config): group some config options
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgrieser committed Dec 3, 2024
1 parent 3c327fe commit bad6556
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 42 deletions.
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,24 @@ The `.setup()` call is optional if you are fine with the defaults below.
```lua
-- default config
require("various-textobjs").setup {
-- set to 0 to only look in the current line
lookForwardSmall = 5,
lookForwardBig = 15,

-- use suggested keymaps (see overview table in README)
useDefaultKeymaps = false,

-- disable only some default keymaps, e.g. { "ai", "ii" }
---@type string[]
disabledKeymaps = {},

-- display notification if a text object is not found
notifyNotFound = true,
-- Number of lines to seek forwards for a text object. See the overview table
-- in the README for which text object uses which value.
forwardLooking = {
small = 5,
big = 15,
},

-- only relevant when using notification plugins like `nvim-notify`
notificationIcon = "󰠱"
notify = {
icon = "󰠱", -- only used with notification plugins like `nvim-notify`
whenObjectNotFound = true,
},
}
```

Expand Down
38 changes: 19 additions & 19 deletions lua/various-textobjs/charwise-textobjs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ end
function M.toNextClosingBracket()
local pattern = "().([]})])"

local _, endPos = M.searchTextobj(pattern, "inner", config.lookForwardSmall)
local _, endPos = M.searchTextobj(pattern, "inner", config.forwardLooking.small)
if not endPos then
u.notFoundMsg(config.lookForwardSmall)
u.notFoundMsg(config.forwardLooking.small)
return
end
local startPos = vim.api.nvim_win_get_cursor(0)
Expand All @@ -199,9 +199,9 @@ function M.toNextQuotationMark()
local quoteEscape = vim.opt_local.quoteescape:get() -- default: \
local pattern = ([[()[^%s](["'`])]]):format(quoteEscape)

local _, endPos = M.searchTextobj(pattern, "inner", config.lookForwardSmall)
local _, endPos = M.searchTextobj(pattern, "inner", config.forwardLooking.small)
if not endPos then
u.notFoundMsg(config.lookForwardSmall)
u.notFoundMsg(config.forwardLooking.small)
return
end
local startPos = vim.api.nvim_win_get_cursor(0)
Expand All @@ -223,7 +223,7 @@ function M.anyQuote(scope)
("([^%s]`).-[^%s](`)"):format(escape, escape), -- ``
}

M.selectClosestTextobj(patterns, scope, config.lookForwardSmall)
M.selectClosestTextobj(patterns, scope, config.forwardLooking.small)

-- pattern accounts for escape char, so move to right to account for that
local isAtStart = vim.api.nvim_win_get_cursor(0)[2] == 1
Expand All @@ -237,7 +237,7 @@ function M.anyBracket(scope)
"(%[).-(%])", -- []
"({).-(})", -- {}
}
M.selectClosestTextobj(patterns, scope, config.lookForwardSmall)
M.selectClosestTextobj(patterns, scope, config.forwardLooking.small)
end

---near end of the line, ignoring trailing whitespace
Expand Down Expand Up @@ -307,9 +307,9 @@ function M.value(scope)
-- or css pseudo-elements :: are not matched
local pattern = "(%s*%f[!<>~=:][=:]%s*)[^=:].*()"

local startPos, endPos = M.searchTextobj(pattern, scope, config.lookForwardSmall)
local startPos, endPos = M.searchTextobj(pattern, scope, config.forwardLooking.small)
if not startPos or not endPos then
u.notFoundMsg(config.lookForwardSmall)
u.notFoundMsg(config.forwardLooking.small)
return
end

Expand All @@ -335,7 +335,7 @@ end
---@param scope "inner"|"outer" outer key includes the `:` or `=` after the key
function M.key(scope)
local pattern = "()%S.-( ?[:=] ?)"
M.selectClosestTextobj(pattern, scope, config.lookForwardSmall)
M.selectClosestTextobj(pattern, scope, config.forwardLooking.small)
end

---@param scope "inner"|"outer" inner number consists purely of digits, outer number factors in decimal points and includes minus sign
Expand All @@ -344,14 +344,14 @@ function M.number(scope)
-- before and after the decimal dot. enforcing digital after dot so outer
-- excludes enumrations.
local pattern = scope == "inner" and "%d+" or "%-?%d*%.?%d+"
M.selectClosestTextobj(pattern, "outer", config.lookForwardSmall)
M.selectClosestTextobj(pattern, "outer", config.forwardLooking.small)
end

-- make URL pattern available for external use
-- INFO mastodon URLs contain `@`, neovim docs urls can contain a `'`, special
-- urls like https://docs.rs/regex/1.*/regex/#syntax can have a `*`
M.urlPattern = "%l%l%l-://[A-Za-z0-9_%-/.#%%=?&'@+*:]+"
function M.url() M.selectClosestTextobj(M.urlPattern, "outer", config.lookForwardBig) end
function M.url() M.selectClosestTextobj(M.urlPattern, "outer", config.forwardLooking.big) end

---@param scope "inner"|"outer" inner excludes the leading dot
function M.chainMember(scope)
Expand All @@ -361,7 +361,7 @@ function M.chainMember(scope)
"([:.])[%w_][%w_]-()", -- following member w/ call
"([:.])[%w_][%w_]-%b()()", -- following member w/ call
}
M.selectClosestTextobj(patterns, scope, config.lookForwardSmall)
M.selectClosestTextobj(patterns, scope, config.forwardLooking.small)
end

function M.lastChange()
Expand All @@ -382,7 +382,7 @@ end
---@param scope "inner"|"outer" inner link only includes the link title, outer link includes link, url, and the four brackets.
function M.mdlink(scope)
local pattern = "(%[)[^%]]-(%]%b())"
M.selectClosestTextobj(pattern, scope, config.lookForwardSmall)
M.selectClosestTextobj(pattern, scope, config.forwardLooking.small)
end

---@param scope "inner"|"outer" inner selector only includes the content, outer selector includes the type.
Expand All @@ -399,7 +399,7 @@ function M.mdEmphasis(scope)
"(^==).-[^\\](==)", -- ==
"(^~~).-[^\\](~~)", -- ~~
}
M.selectClosestTextobj(patterns, scope, config.lookForwardSmall)
M.selectClosestTextobj(patterns, scope, config.forwardLooking.small)

-- pattern accounts for escape char, so move to right to account for that
local isAtStart = vim.api.nvim_win_get_cursor(0)[2] == 1
Expand All @@ -409,13 +409,13 @@ end
---@param scope "inner"|"outer" inner selector excludes the brackets themselves
function M.doubleSquareBrackets(scope)
local pattern = "(%[%[).-(%]%])"
M.selectClosestTextobj(pattern, scope, config.lookForwardSmall)
M.selectClosestTextobj(pattern, scope, config.forwardLooking.small)
end

---@param scope "inner"|"outer" outer selector includes trailing comma and whitespace
function M.cssSelector(scope)
local pattern = "()[#.][%w-_]+(,? ?)"
M.selectClosestTextobj(pattern, scope, config.lookForwardSmall)
M.selectClosestTextobj(pattern, scope, config.forwardLooking.small)
end

---@param scope "inner"|"outer" inner selector is only the value of the attribute inside the quotation marks.
Expand All @@ -424,7 +424,7 @@ function M.htmlAttribute(scope)
'([%w-]+=").-(")',
"([%w-]+=').-(')",
}
M.selectClosestTextobj(pattern, scope, config.lookForwardSmall)
M.selectClosestTextobj(pattern, scope, config.forwardLooking.small)
end

---@param scope "inner"|"outer" outer selector includes the pipe
Expand All @@ -433,7 +433,7 @@ function M.shellPipe(scope)
"()[^|%s][^|]-( ?| ?)", -- trailing pipe, 1st char non-space to exclude indentation
"( ?| ?)[^|]*()", -- leading pipe
}
M.selectClosestTextobj(patterns, scope, config.lookForwardSmall)
M.selectClosestTextobj(patterns, scope, config.forwardLooking.small)
end

---@param scope "inner"|"outer" inner selector only affects the color value
Expand All @@ -444,7 +444,7 @@ function M.cssColor(scope)
"(hsla?%()[%%%d,./deg ]-(%))", -- hsl(123, 23, 23) or hsl(123deg, 123%, 123% / 100)
"(rgba?%()[%d,./ ]-(%))", -- rgb(123, 123, 123) or rgb(50%, 50%, 50%)
}
M.selectClosestTextobj(pattern, scope, config.lookForwardSmall)
M.selectClosestTextobj(pattern, scope, config.forwardLooking.small)
end

--------------------------------------------------------------------------------
Expand Down
42 changes: 33 additions & 9 deletions lua/various-textobjs/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ local M = {}
--------------------------------------------------------------------------------
---@class VariousTextobjs.Config
local defaultConfig = {
-- set to 0 to only look in the current line
lookForwardSmall = 5,
lookForwardBig = 15,

-- use suggested keymaps (see overview table in README)
useDefaultKeymaps = false,

-- disable only some default keymaps, e.g. { "ai", "ii" }
---@type string[]
disabledKeymaps = {},

-- display notification if a text object is not found
notifyNotFound = true,

-- only relevant when using notification plugins like `nvim-notify`
notificationIcon = "󰠱",
-- Number of lines to seek forwards for a text object. See the overview table
-- in the README for which text object uses which value.
forwardLooking = {
small = 5,
big = 15,
},

notify = {
icon = "󰠱", -- only used with notification plugins like `nvim-notify`
whenObjectNotFound = true,
},
}
M.config = defaultConfig

Expand All @@ -27,6 +29,28 @@ M.config = defaultConfig
---@param userConfig? VariousTextobjs.Config
function M.setup(userConfig)
M.config = vim.tbl_deep_extend("force", M.config, userConfig or {})
local notify = require("various-textobjs.utils").notify

-- DEPRECATION (2024-12-03)
---@diagnostic disable: undefined-field
if M.config.lookForwardSmall then
notify("The `lookForwardSmall` option is deprecated. Use `forwardLooking.small` instead.")
end
if M.config.lookForwardBig then
notify("The `lookForwardBig` option is deprecated. Use `forwardLooking.big` instead.")
end
if M.config.lookForwardBig then
notify("The `lookForwardBig` option is deprecated. Use `forwardLooking.big` instead.")
end
if M.config.notificationIcon then
notify("The `notificationIcon` option is deprecated. Use `config.notify.icon` instead.")
end
if M.config.notifyNotFound then
notify(
"The `notifyNotFound` option is deprecated. Use `config.notify.whenObjectNotFound` instead."
)
end
---@diagnostic enable: undefined-field

if M.config.useDefaultKeymaps then
require("various-textobjs.default-keymaps").setup(M.config.disabledKeymaps)
Expand Down
8 changes: 4 additions & 4 deletions lua/various-textobjs/linewise-textobjs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ function M.closedFold(scope)
else
foldStart = startLnum
repeat
if foldStart >= lastLine or foldStart > (config.lookForwardBig + startLnum) then
u.notFoundMsg(config.lookForwardBig)
if foldStart >= lastLine or foldStart > (config.forwardLooking.big + startLnum) then
u.notFoundMsg(config.forwardLooking.big)
return
end
foldStart = foldStart + 1
Expand Down Expand Up @@ -117,13 +117,13 @@ function M.mdFencedCodeBlock(scope)
repeat
j = j + 1
if j > #cbBegin then
u.notFoundMsg(config.lookForwardBig)
u.notFoundMsg(config.forwardLooking.big)
return
end
local cursorInBetween = (cbBegin[j] <= cursorLnum) and (cbEnd[j] >= cursorLnum)
-- seek forward for a codeblock
local cursorInFront = (cbBegin[j] > cursorLnum)
and (cbBegin[j] <= cursorLnum + config.lookForwardBig)
and (cbBegin[j] <= cursorLnum + config.forwardLooking.big)
until cursorInBetween or cursorInFront

local start = cbBegin[j]
Expand Down
4 changes: 2 additions & 2 deletions lua/various-textobjs/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ function M.getline(lnum) return vim.api.nvim_buf_get_lines(0, lnum - 1, lnum, tr
function M.notify(msg, level)
if not level then level = "info" end
if level == "notfound" then
if not require("various-textobjs.config").config.notifyNotFound then return end
if not require("various-textobjs.config").config.notify.whenObjectNotFound then return end
level = "warn"
end
local icon = require("various-textobjs.config").config.notificationIcon
local icon = require("various-textobjs.config").config.notify.icon
vim.notify(msg, vim.log.levels[level:upper()], { title = "nvim-various-textobjs", icon = icon })
end

Expand Down

0 comments on commit bad6556

Please sign in to comment.