Skip to content

Commit

Permalink
refactor: notification system
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgrieser committed Dec 3, 2024
1 parent bad6556 commit d789bfc
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 31 deletions.
4 changes: 2 additions & 2 deletions lua/various-textobjs/charwise-textobjs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ function M.diagnostic(wrap)
if target then
M.setSelection({ target.lnum + 1, target.col }, { target.end_lnum + 1, target.end_col - 1 })
else
u.notify("No diagnostic found.", "notfound")
u.notFoundMsg("No diagnostic found.")
end
end

Expand Down Expand Up @@ -369,7 +369,7 @@ function M.lastChange()
local changeEndPos = vim.api.nvim_buf_get_mark(0, "]")

if changeStartPos[1] == changeEndPos[1] and changeStartPos[2] == changeEndPos[2] then
u.notify("Last change was a deletion operation, aborting.", "warn")
u.warn("Last change was a deletion operation, aborting.")
return
end

Expand Down
12 changes: 6 additions & 6 deletions lua/various-textobjs/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,24 @@ 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
local warn = require("various-textobjs.utils").warn

-- DEPRECATION (2024-12-03)
---@diagnostic disable: undefined-field
if M.config.lookForwardSmall then
notify("The `lookForwardSmall` option is deprecated. Use `forwardLooking.small` instead.")
warn("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.")
warn("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.")
warn("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.")
warn("The `notificationIcon` option is deprecated. Use `config.notify.icon` instead.")
end
if M.config.notifyNotFound then
notify(
warn(
"The `notifyNotFound` option is deprecated. Use `config.notify.whenObjectNotFound` instead."
)
end
Expand Down
10 changes: 5 additions & 5 deletions lua/various-textobjs/linewise-textobjs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,16 @@ function M.indentation(startBorder, endBorder, blankLines)
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.notify("No indented line found.", "notfound")
u.notFoundMsg("No indented line found.")
return
end
curLnum = curLnum + 1
end

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

local prevLnum = curLnum - 1
Expand Down Expand Up @@ -221,7 +221,7 @@ function M.restOfIndentation()

local indentOfStart = vim.fn.indent(curLnum)
if indentOfStart == 0 then
u.notify("Current line is not indented.", "notfound")
u.warn("Current line is not indented.")
return
end

Expand Down Expand Up @@ -260,7 +260,7 @@ end
---@param scope "inner"|"outer" outer includes bottom cell border
function M.notebookCell(scope)
if vim.bo.commentstring == "" then
u.notify("Buffer has no commentstring set.", "warn")
u.warn("Buffer has no commentstring set.")
return
end

Expand Down
6 changes: 3 additions & 3 deletions lua/various-textobjs/treesitter-textobjs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ local u = require("various-textobjs.utils")
function M.pyTripleQuotes(scope)
-- GUARD
if vim.treesitter.get_node == nil then
u.notify("This textobj requires least nvim 0.9.", "warn")
u.warn("This textobj requires least nvim 0.9.")
return
end
local node = vim.treesitter.get_node()
if not node then
u.notify("No node found.", "notfound")
u.notFoundMsg("No node found.")
return
end

Expand All @@ -29,7 +29,7 @@ function M.pyTripleQuotes(scope)
then
strNode = node:parent():parent()
else
u.notify("Not on a triple quoted string.", "warn")
u.warn("Not on a triple quoted string.")
return
end
---@cast strNode TSNode
Expand Down
31 changes: 16 additions & 15 deletions lua/various-textobjs/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,27 @@ end
---@return string
function M.getline(lnum) return vim.api.nvim_buf_get_lines(0, lnum - 1, lnum, true)[1] end

---send notification
---@param msg string
---@param level? "info"|"trace"|"debug"|"warn"|"error"|"notfound"
function M.notify(msg, level)
if not level then level = "info" end
if level == "notfound" then
if not require("various-textobjs.config").config.notify.whenObjectNotFound then return end
level = "warn"
end
function M.warn(msg)
local icon = require("various-textobjs.config").config.notify.icon
vim.notify(msg, vim.log.levels[level:upper()], { title = "nvim-various-textobjs", icon = icon })
vim.notify(msg, vim.log.levels.WARN, { title = "various-textobjs", icon = icon })
end

---notification when no textobj could be found
---@param lookForwL integer number of lines the plugin tried to look forward
function M.notFoundMsg(lookForwL)
local msg = ("Textobject not found within the next %d lines."):format(lookForwL)
if lookForwL == 1 then msg = msg:gsub("s%.$", ".") end -- remove plural s
if lookForwL == 0 then msg = "Textobject not found within the line." end
M.notify(msg, "notfound")
---@param msg integer|string lines tried to look forward, or custom message
function M.notFoundMsg(msg)
if not require("various-textobjs.config").config.notify.whenObjectNotFound then return end
local notifyText
if type(msg) == "number" then
local lookForwLines = msg
notifyText = ("Textobject not found within the next %d lines."):format(lookForwLines)
if lookForwLines == 1 then notifyText = notifyText:gsub("s%.$", ".") end
if lookForwLines == 0 then notifyText = "Textobject not found within the line." end
elseif type(msg) == "string" then
notifyText = msg
end
local icon = require("various-textobjs.config").config.notify.icon
vim.notify(notifyText, vim.log.levels.WARN, { title = "various-textobjs", icon = icon })
end

--------------------------------------------------------------------------------
Expand Down

0 comments on commit d789bfc

Please sign in to comment.