Skip to content

Commit

Permalink
Merge pull request #38 from kiyoon/jupynium-select-kernel
Browse files Browse the repository at this point in the history
feat: JupyniumSelectKernel UI without Telescope dependency
  • Loading branch information
kiyoon authored Feb 19, 2023
2 parents 9f7714e + 615e387 commit 6edeb7c
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 115 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,17 @@ Install with vim-plug:
```vim
Plug 'kiyoon/jupynium.nvim', { 'do': 'pip3 install --user .' }
" Plug 'kiyoon/jupynium.nvim', { 'do': 'conda run --no-capture-output -n jupynium pip install .' }
Plug 'rcarriga/nvim-notify' " optional
Plug 'rcarriga/nvim-notify' " optional
Plug 'stevearc/dressing.nvim' " optional, UI for :JupyniumSelectKernel
```

Install with packer.nvim:

```lua
use { "kiyoon/jupynium.nvim", run = "pip3 install --user ." }
-- use { "kiyoon/jupynium.nvim", run = "conda run --no-capture-output -n jupynium pip install ." }
use { "rcarriga/nvim-notify" } -- optional
use { "rcarriga/nvim-notify" } -- optional
use { "stevearc/dressing.nvim" } -- optional, UI for :JupyniumSelectKernel
```

Install with 💤lazy.nvim
Expand All @@ -73,7 +75,8 @@ Install with 💤lazy.nvim
-- build = "conda run --no-capture-output -n jupynium pip install .",
-- enabled = vim.fn.isdirectory(vim.fn.expand "~/miniconda3/envs/jupynium"),
},
"rcarriga/nvim-notify", -- optional
"rcarriga/nvim-notify", -- optional
"stevearc/dressing.nvim", -- optional, UI for :JupyniumSelectKernel
```

Setup is optional for system python users and here are the defaults. Conda users need to change the `python_host`.
Expand Down Expand Up @@ -309,7 +312,7 @@ If you want custom keymaps, add `textobjects = { use_default_keybindings = false
:JupyniumToggleSelectedCellsOutputsScroll
:JupyniumRestartKernel
:Telescope jupynium_kernels " change kernel
:JupyniumSelectKernel
" Highlight
:JupyniumShortsightedToggle
Expand Down
5 changes: 0 additions & 5 deletions lua/jupynium/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ function M.setup(opts)
end
shortsighted.add_commands()

local status, telescope = pcall(require, "telescope")
if status then
telescope.load_extension "jupynium_kernels"
end

vim.g.__jupynium_setup_completed = true
end

Expand Down
12 changes: 0 additions & 12 deletions lua/telescope/_extensions/jupynium_kernels.lua

This file was deleted.

94 changes: 0 additions & 94 deletions lua/telescope/_extensions/jupynium_kernels/main.lua

This file was deleted.

1 change: 1 addition & 0 deletions src/jupynium/lua/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ vim.api.nvim_create_user_command("JupyniumScrollDown", "lua Jupynium_scroll_down
vim.api.nvim_create_user_command("JupyniumAutoscrollToggle", "lua Jupynium_autoscroll_toggle()", {})

vim.api.nvim_create_user_command("JupyniumRestartKernel", "lua Jupynium_restart_kernel()", {})
vim.api.nvim_create_user_command("JupyniumSelectKernel", "lua Jupynium_select_kernel()", {})
51 changes: 51 additions & 0 deletions src/jupynium/lua/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,54 @@ function Jupynium_restart_kernel(bufnr)

Jupynium_rpcnotify("restart_kernel", bufnr, true)
end

function Jupynium_select_kernel(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
bufnr = vim.api.nvim_get_current_buf()
end
if Jupynium_syncing_bufs[bufnr] == nil then
Jupynium_notify.error { [[Cannot select kernel without synchronising.]], [[Run `:JupyniumStartSync`]] }
return
end

local jupynium_kernel_name_and_spec = Jupynium_get_kernel_spec(bufnr)
local current_kernel_name = jupynium_kernel_name_and_spec[1]
local kernel_spec = jupynium_kernel_name_and_spec[2]
local kernel_display_names, kernel_dispname_to_name = {}, {}
for kernel_name, kern in pairs(kernel_spec) do
-- filter by language
if kern.spec.language:lower() == vim.bo.filetype then
if kernel_name ~= current_kernel_name then
table.insert(kernel_display_names, kern.spec.display_name)
else
-- current kernel is always first (default)
table.insert(kernel_display_names, 1, kern.spec.display_name)
end
kernel_dispname_to_name[kern.spec.display_name] = kernel_name
end
end

if kernel_dispname_to_name[kernel_display_names[1]] ~= current_kernel_name then
-- by applying filtering, we dropped the current kernel.
-- cancel the filtering and include everything.
kernel_display_names = {}
kernel_dispname_to_name = {}
for kernel_name, kern in pairs(kernel_spec) do
if kernel_name ~= current_kernel_name then
table.insert(kernel_display_names, kern.spec.display_name)
else
-- current kernel is always first (default)
table.insert(kernel_display_names, 1, kern.spec.display_name)
end
kernel_dispname_to_name[kern.spec.display_name] = kernel_name
end
end

-- Use dressing.nvim to use Telescope, fzf-lua etc.
vim.ui.select(kernel_display_names, {
prompt = "Select a kernel for Jupynium (Jupyter Notebook)",
}, function(selected)
Jupynium_change_kernel(bufnr, kernel_dispname_to_name[selected])
end)
end

0 comments on commit 6edeb7c

Please sign in to comment.