Skip to content

Commit

Permalink
feat: ignore notifications (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
kiyoon authored Jun 7, 2024
1 parent 369b58f commit a43308c
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 23 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,16 @@ require("jupynium").setup({
border = "none",
},
},
notify = {
ignore = {
-- "download_ipynb",
-- "error_download_ipynb",
-- "attach_and_init",
-- "error_close_main_page",
-- "notebook_closed",
},
},
})
-- You can link highlighting groups.
Expand Down
8 changes: 8 additions & 0 deletions lua/jupynium/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ function M.setup(opts)

highlighter.setup(options.opts)

-- NOTE: if you don't define a local variable, the vim.g. variable won't have any value. Weird.
-- So we define a local variable and then assign it to the global variable.
local notify_ignore_codes = {}
for _, code in ipairs(options.opts.notify.ignore) do
notify_ignore_codes[code] = true
end
vim.g.jupynium_notify_ignore_codes = notify_ignore_codes

vim.g.__jupynium_setup_completed = true
end

Expand Down
10 changes: 10 additions & 0 deletions lua/jupynium/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ M.default_opts = {
border = "none",
},
},

notify = {
ignore = {
-- "download_ipynb",
-- "error_download_ipynb",
-- "attach_and_init",
-- "error_close_main_page",
-- "notebook_closed",
},
},
}

return M
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ legacy_tox_ini = """
3.9: python3.9
3.10: python3.10
3.11: python3.11
3.11: python3.12
3.12: python3.12
[testenv]
setenv =
Expand Down
5 changes: 4 additions & 1 deletion src/jupynium/events_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ def process_events(nvim_info: NvimInfo, driver: WebDriver):
if nvim_info.home_window not in driver.window_handles:
nvim_info.nvim.lua.Jupynium_notify.error(
["Do not close the main page. Detaching the nvim from Jupynium.."],
"error_close_main_page",
async_=True,
)
return False, None
Expand Down Expand Up @@ -237,7 +238,7 @@ def start_sync_with_filename(
buf_filetype: str,
conda_or_venv_path: str | None,
nvim_info: NvimInfo,
driver,
driver: WebDriver,
):
"""
Start sync using a filename (not tab index).
Expand Down Expand Up @@ -715,6 +716,7 @@ def process_notification_event(
except OSError as e:
nvim_info.nvim.lua.Jupynium_notify.error(
["Failed to download ipynb file to", output_ipynb_path],
"error_download_ipynb",
async_=True,
)
logger.error(
Expand Down Expand Up @@ -946,6 +948,7 @@ def download_ipynb(
)
nvim_info.nvim.lua.Jupynium_notify.info(
["Downloaded ipynb file to", output_ipynb_path],
"download_ipynb",
async_=True,
)
logger.info(f"Downloaded ipynb to {output_ipynb_path}")
Expand Down
4 changes: 4 additions & 0 deletions src/jupynium/lua/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ end
if vim.g.jupynium_channel_id == nil then
vim.g.jupynium_channel_id = -1
end

if vim.g.jupynium_notify_ignore_codes == nil then
vim.g.jupynium_notify_ignore_codes = {} --- @type table<string, boolean>
end
17 changes: 11 additions & 6 deletions src/jupynium/lua/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ function Jupynium_rpcnotify(event, buf, ensure_syncing, ...)
rpc(vim.rpcnotify, event, buf, ...)
end

-- block until jupynium responds to the message
---block until jupynium responds to the message
---@param event string
---@param buf integer?
---@param ensure_syncing boolean
---@param ... any
---@return any
function Jupynium_rpcrequest(event, buf, ensure_syncing, ...)
if ensure_syncing then
if Jupynium_syncing_bufs[buf] == nil then
Expand All @@ -93,7 +98,7 @@ end
--- API: Execute javascript in the browser. It will switch to the correct tab before executing.
---@param bufnr integer | nil If given, before executing the code it will switch to the tab of this buffer. Requires syncing in advance.
---@param code string Javascript code
---@return boolean, object: Success, response
---@return boolean, any?: Success, response
function Jupynium_execute_javascript(bufnr, code)
local ensure_syncing = true
if bufnr == nil then
Expand Down Expand Up @@ -183,7 +188,7 @@ end
---Start synchronising the buffer with the ipynb file
---@param bufnr integer buffer number
---@param ipynb_filename string name of the ipynb file
---@param ask boolean whether to ask for confirmation
---@param ask boolean? whether to ask for confirmation
function Jupynium_start_sync(bufnr, ipynb_filename, ask)
if bufnr == nil or bufnr == 0 then
bufnr = vim.api.nvim_get_current_buf()
Expand All @@ -203,7 +208,7 @@ function Jupynium_start_sync(bufnr, ipynb_filename, ask)
local content = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)

-- Used for choosing the correct kernel
local buf_filetype = vim.api.nvim_buf_get_option(bufnr, "filetype")
local buf_filetype = vim.bo[bufnr].filetype
local conda_or_venv_path = vim.env.CONDA_PREFIX or vim.env.VIRTUAL_ENV

local response =
Expand Down Expand Up @@ -572,7 +577,7 @@ end
---@param bufnr integer
---@param code_line string
---@param col integer 0-indexed
---@return table | nil
---@return table?
function Jupynium_kernel_inspect(bufnr, code_line, col)
if bufnr == nil or bufnr == 0 then
bufnr = vim.api.nvim_get_current_buf()
Expand Down Expand Up @@ -669,7 +674,7 @@ end
---@param code_line string
---@param col integer 0-indexed
---@param callback function nvim-cmp complete callback.
---@return table | nil
---@return table?
function Jupynium_kernel_complete_async(bufnr, code_line, col, callback)
if bufnr == nil or bufnr == 0 then
bufnr = vim.api.nvim_get_current_buf()
Expand Down
45 changes: 30 additions & 15 deletions src/jupynium/lua/notify.lua
Original file line number Diff line number Diff line change
@@ -1,45 +1,60 @@
local notify_ok, nvim_notify = pcall(require, "notify")

local PLUGIN_NAME = notify_ok and "Jupynium" or "Jupynium"

Jupynium_notify = {}

---Wraper for vim.notify and nvim-notify
---@param msg table
---@param msg string[]
---@param level number vim.levels[level]
---@vararg string Strings for substitute
Jupynium_notify.notify = function(msg, level)
---@param code string?
Jupynium_notify.notify = function(msg, level, code)
level = level or vim.log.levels.INFO

if code ~= nil and vim.g.jupynium_notify_ignore_codes[code] then
return
end

local title
if code ~= nil then
title = ("Jupynium [%s]"):format(code)
else
title = "Jupynium"
end

if notify_ok then
-- Make it possible to use newline within the message table
lines = {}
local lines = {}
for _, str in ipairs(msg) do
for s in str:gmatch "[^\r\n]+" do
table.insert(lines, s)
end
end

nvim_notify(lines, level, {
title = PLUGIN_NAME,
title = title,
on_open = function(win)
local buf = vim.api.nvim_win_get_buf(win)
vim.api.nvim_buf_set_option(buf, "filetype", "markdown")
vim.bo[buf].filetype = "markdown"
end,
})
else
vim.notify(("[%s]: %s"):format(PLUGIN_NAME, table.concat(msg, " ")), level)
vim.notify(("%s: %s"):format(title, table.concat(msg, " ")), level)
end
end

Jupynium_notify.error = function(msg)
Jupynium_notify.notify(msg, vim.log.levels.ERROR)
---@param msg string[]
---@param code string?
Jupynium_notify.error = function(msg, code)
Jupynium_notify.notify(msg, vim.log.levels.ERROR, code)
end

Jupynium_notify.warn = function(msg)
Jupynium_notify.notify(msg, vim.log.levels.WARN)
---@param msg string[]
---@param code string?
Jupynium_notify.warn = function(msg, code)
Jupynium_notify.notify(msg, vim.log.levels.WARN, code)
end

Jupynium_notify.info = function(msg)
Jupynium_notify.notify(msg, vim.log.levels.INFO)
---@param msg string[]
---@param code string?
Jupynium_notify.info = function(msg, code)
Jupynium_notify.notify(msg, vim.log.levels.INFO, code)
end
1 change: 1 addition & 0 deletions src/jupynium/nvim.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def check_window_alive_and_update(self, driver: WebDriver):
"Notebook closed.",
f"Detaching the buffer {buf_id} from Jupynium..",
],
"notebook_closed",
async_=True,
)
self.nvim.lua.Jupynium_stop_sync(buf_id)
Expand Down
1 change: 1 addition & 0 deletions src/jupynium/pynvim_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def attach_and_init(nvim_listen_addr: str | PathLike):
"Jupynium successfully attached and initialised.",
"Run `:JupyniumStartSync`",
],
"attach_and_init",
async_=True,
)

Expand Down

0 comments on commit a43308c

Please sign in to comment.