Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
kiyoon committed Jun 7, 2024
1 parent d83c56a commit ae38542
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 37 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
5 changes: 5 additions & 0 deletions lua/jupynium/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ function M.setup(opts)

highlighter.setup(options.opts)

vim.g.jupynium_notify_ignore_codes = {}
for _, code in ipairs(options.opts.notify.ignore) do
vim.g.jupynium_notify_ignore_codes[code] = true
end

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 src/jupynium/cmds/jupynium.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def fallback_open_notebook_server(
"--no-browser",
"--NotebookApp.token",
notebook_token,
"--NotebookApp.show_banner=False",
# "--NotebookApp.show_banner=False",
]

if notebook_dir is not None:
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 @@ -716,6 +717,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 @@ -947,6 +949,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: 0 additions & 4 deletions src/jupynium/lua/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ vim.api.nvim_create_user_command("JupyniumScrollUp", "lua Jupynium_scroll_up()",
vim.api.nvim_create_user_command("JupyniumScrollDown", "lua Jupynium_scroll_down()", {})
vim.api.nvim_create_user_command("JupyniumAutoscrollToggle", "lua Jupynium_autoscroll_toggle()", {})

---@deprecated
vim.api.nvim_create_user_command("JupyniumRestartKernel", "lua Jupynium_restart_kernel()", {})
vim.api.nvim_create_user_command("JupyniumSelectKernel", "lua Jupynium_select_kernel()", {})

vim.api.nvim_create_user_command("JupyniumKernelRestart", "lua Jupynium_kernel_restart()", {})
vim.api.nvim_create_user_command("JupyniumKernelInterrupt", "lua Jupynium_kernel_interrupt()", {})
vim.api.nvim_create_user_command("JupyniumKernelSelect", "lua Jupynium_kernel_select()", {})
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.jupynium_notify_ignore_codes = {} --- @type table<string, boolean>
end
29 changes: 13 additions & 16 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 All @@ -110,6 +115,8 @@ function Jupynium_execute_javascript(bufnr, code)
return false, nil
end

assert(bufnr ~= nil, "bufnr should not be nil")

-- set ensure_syncing to false, because we checked that already.
return true, Jupynium_rpcrequest("execute_javascript", bufnr, false, code)
end
Expand Down Expand Up @@ -183,7 +190,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 +210,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 @@ -487,11 +494,6 @@ function Jupynium_kernel_change(bufnr, kernel_name)
Jupynium_rpcnotify("kernel_change", bufnr, true, kernel_name)
end

function Jupynium_restart_kernel(bufnr)
Jupynium_notify.warn { [[Sorry! Command name changed.]], [[Please use :JupyniumKernelRestart]] }
return Jupynium_kernel_restart(bufnr)
end

function Jupynium_kernel_restart(bufnr)
-- note that the kernel name is different from the display name in the kernel list in Jupyter Notebook.
if bufnr == nil or bufnr == 0 then
Expand Down Expand Up @@ -519,11 +521,6 @@ function Jupynium_kernel_interrupt(bufnr)
Jupynium_rpcnotify("kernel_interrupt", bufnr, true)
end

function Jupynium_select_kernel(bufnr)
Jupynium_notify.warn { [[Sorry! Command name changed.]], [[Please use :JupyniumKernelSelect]] }
return Jupynium_kernel_select(bufnr)
end

function Jupynium_kernel_select(bufnr)
-- note that the kernel name is different from the display name in the kernel list in Jupyter Notebook.
if bufnr == nil or bufnr == 0 then
Expand Down Expand Up @@ -582,7 +579,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 @@ -679,7 +676,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
46 changes: 31 additions & 15 deletions src/jupynium/lua/notify.lua
Original file line number Diff line number Diff line change
@@ -1,45 +1,61 @@
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
code = code or ""

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

local title
if code ~= "" 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 ae38542

Please sign in to comment.