From a43308c2929479e1fc18be9cd991fc4a5a566ad9 Mon Sep 17 00:00:00 2001 From: Kiyoon Kim Date: Fri, 7 Jun 2024 16:17:48 +0900 Subject: [PATCH] feat: ignore notifications (#120) --- README.md | 10 ++++++++ lua/jupynium/init.lua | 8 ++++++ lua/jupynium/options.lua | 10 ++++++++ pyproject.toml | 2 +- src/jupynium/events_control.py | 5 +++- src/jupynium/lua/defaults.lua | 4 +++ src/jupynium/lua/helpers.lua | 17 ++++++++----- src/jupynium/lua/notify.lua | 45 ++++++++++++++++++++++------------ src/jupynium/nvim.py | 1 + src/jupynium/pynvim_helpers.py | 1 + 10 files changed, 80 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 4373739..dba088d 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lua/jupynium/init.lua b/lua/jupynium/init.lua index e12b3dc..bc1309d 100644 --- a/lua/jupynium/init.lua +++ b/lua/jupynium/init.lua @@ -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 diff --git a/lua/jupynium/options.lua b/lua/jupynium/options.lua index 9571fc6..d2f662f 100644 --- a/lua/jupynium/options.lua +++ b/lua/jupynium/options.lua @@ -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 diff --git a/pyproject.toml b/pyproject.toml index 41f554a..29f03e1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 = diff --git a/src/jupynium/events_control.py b/src/jupynium/events_control.py index 9aadb55..5178012 100644 --- a/src/jupynium/events_control.py +++ b/src/jupynium/events_control.py @@ -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 @@ -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). @@ -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( @@ -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}") diff --git a/src/jupynium/lua/defaults.lua b/src/jupynium/lua/defaults.lua index e0abcc8..8296f26 100644 --- a/src/jupynium/lua/defaults.lua +++ b/src/jupynium/lua/defaults.lua @@ -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 +end diff --git a/src/jupynium/lua/helpers.lua b/src/jupynium/lua/helpers.lua index a67cfd7..d212aeb 100644 --- a/src/jupynium/lua/helpers.lua +++ b/src/jupynium/lua/helpers.lua @@ -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 @@ -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 @@ -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() @@ -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 = @@ -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() @@ -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() diff --git a/src/jupynium/lua/notify.lua b/src/jupynium/lua/notify.lua index 84a7204..decf3cf 100644 --- a/src/jupynium/lua/notify.lua +++ b/src/jupynium/lua/notify.lua @@ -1,19 +1,28 @@ 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) @@ -21,25 +30,31 @@ Jupynium_notify.notify = function(msg, level) 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 diff --git a/src/jupynium/nvim.py b/src/jupynium/nvim.py index 3eca5fd..662eb65 100644 --- a/src/jupynium/nvim.py +++ b/src/jupynium/nvim.py @@ -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) diff --git a/src/jupynium/pynvim_helpers.py b/src/jupynium/pynvim_helpers.py index 1532906..cfe42dd 100644 --- a/src/jupynium/pynvim_helpers.py +++ b/src/jupynium/pynvim_helpers.py @@ -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, )