From 473423929c3e2546144fb1afd2a97818cc7b0c70 Mon Sep 17 00:00:00 2001 From: tiyn Date: Thu, 27 Feb 2025 02:20:47 +0100 Subject: [PATCH] nvim: added way to use ipynb in terminal --- .config/nvim/after/ftplugin/markdown.lua | 3 + .config/nvim/lazy-lock.json | 8 ++- .config/nvim/lua/autocmd.lua | 90 ++++++++++++++++++++++++ .config/nvim/lua/keymap.lua | 8 +++ .config/nvim/lua/plugins/molten.lua | 61 ++++++++++++++++ 5 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 .config/nvim/lua/plugins/molten.lua diff --git a/.config/nvim/after/ftplugin/markdown.lua b/.config/nvim/after/ftplugin/markdown.lua index 66569cd..491c552 100644 --- a/.config/nvim/after/ftplugin/markdown.lua +++ b/.config/nvim/after/ftplugin/markdown.lua @@ -36,3 +36,6 @@ vim.o.textwidth = 100 -- frabjous/knap vim.keymap.set('n', 'p', require("knap").toggle_autopreviewing, { noremap = true }) + +-- benlubas/molten-nvim +require("quarto").activate() diff --git a/.config/nvim/lazy-lock.json b/.config/nvim/lazy-lock.json index 43a4554..d126515 100644 --- a/.config/nvim/lazy-lock.json +++ b/.config/nvim/lazy-lock.json @@ -15,9 +15,12 @@ "git-blame.nvim": { "branch": "master", "commit": "2883a7460f611c2705b23f12d58d398d5ce6ec00" }, "gitsigns.nvim": { "branch": "main", "commit": "863903631e676b33e8be2acb17512fdc1b80b4fb" }, "goto-preview": { "branch": "main", "commit": "1519ea3512828c944eed5b2bbb66a8f8deb92447" }, + "hererocks": { "branch": "master", "commit": "c9c5444dea1e07e005484014a8231aa667be30b6" }, + "image.nvim": { "branch": "master", "commit": "6ffafab2e98b5bda46bf227055aa84b90add8cdc" }, "indent-blankline.nvim": { "branch": "master", "commit": "e7a4442e055ec953311e77791546238d1eaae507" }, + "jupytext.nvim": { "branch": "main", "commit": "c8baf3ad344c59b3abd461ecc17fc16ec44d0f7b" }, "knap": { "branch": "main", "commit": "0040760bb504a713ea5988c7c490a15faf477c57" }, - "lazy.nvim": { "branch": "main", "commit": "1159bdccd8910a0fd0914b24d6c3d186689023d9" }, + "lazy.nvim": { "branch": "main", "commit": "7e6c863bc7563efbdd757a310d17ebc95166cef3" }, "leap.nvim": { "branch": "main", "commit": "c6bfb191f1161fbabace1f36f578a20ac6c7642c" }, "lsp_signature.nvim": { "branch": "master", "commit": "fc38521ea4d9ec8dbd4c2819ba8126cea743943b" }, "lspkind-nvim": { "branch": "master", "commit": "59c3f419af48a2ffb2320cea85e44e5a95f71664" }, @@ -27,6 +30,7 @@ "mason-null-ls.nvim": { "branch": "main", "commit": "de19726de7260c68d94691afb057fa73d3cc53e7" }, "mason.nvim": { "branch": "main", "commit": "e2f7f9044ec30067bc11800a9e266664b88cda22" }, "modes.nvim": { "branch": "main", "commit": "326cff3282419b3bcc745061978c1e592cae055d" }, + "molten-nvim": { "branch": "main", "commit": "a286aa914d9a154bc359131aab788b5a077a5a99" }, "none-ls.nvim": { "branch": "main", "commit": "6bdd9b3364e12ffb346ac224db3df37de0b1429b" }, "nvim-autopairs": { "branch": "master", "commit": "ee297f215e95a60b01fde33275cc3c820eddeebe" }, "nvim-cmp": { "branch": "main", "commit": "ae644feb7b67bf1ce4260c231d1d4300b19c6f30" }, @@ -44,10 +48,12 @@ "nvim-ufo": { "branch": "main", "commit": "203c9f434feec57909ab4b1e028abeb3349b7847" }, "nvim-web-devicons": { "branch": "master", "commit": "19d257cf889f79f4022163c3fbb5e08639077bd8" }, "nvim-yarp": { "branch": "master", "commit": "bb5f5e038bfe119d3b777845a76b0b919b35ebc8" }, + "otter.nvim": { "branch": "main", "commit": "213d1f7a47be788f430099a110456a06167ab0f4" }, "plenary.nvim": { "branch": "master", "commit": "2d9b06177a975543726ce5c73fca176cedbffe9d" }, "popup-menu.nvim": { "branch": "master", "commit": "b618bd0c824a20417d845fa0022327228e6988a2" }, "projections.nvim": { "branch": "pre_release", "commit": "889914169fa1f5c00fb8659653b5a8330fb223e6" }, "promise-async": { "branch": "main", "commit": "119e8961014c9bfaf1487bf3c2a393d254f337e2" }, + "quarto-nvim": { "branch": "main", "commit": "93b1f5a1abc1b1da653f353947f62b294574759c" }, "renamer.nvim": { "branch": "master", "commit": "1614d466df53899f11dd5395eaac3c09a275c384" }, "spelunker.vim": { "branch": "master", "commit": "a0bc530f62798bbe053905555a4aa9ed713485eb" }, "symbols-outline.nvim": { "branch": "master", "commit": "564ee65dfc9024bdde73a6621820866987cbb256" }, diff --git a/.config/nvim/lua/autocmd.lua b/.config/nvim/lua/autocmd.lua index 165bef7..91d1534 100644 --- a/.config/nvim/lua/autocmd.lua +++ b/.config/nvim/lua/autocmd.lua @@ -12,6 +12,7 @@ vim.api.nvim_create_autocmd({ "VimLeavePre" }, { Session.store(vim.loop.cwd()) end, }) + vim.api.nvim_create_autocmd({ "VimEnter" }, { callback = function() if vim.fn.argc() ~= 0 then @@ -24,3 +25,92 @@ vim.api.nvim_create_autocmd({ "VimEnter" }, { end, desc = "Restore last session automatically", }) + +-- benlubas/molten-nvim +local imb = function(e) + vim.schedule(function() + local kernels = vim.fn.MoltenAvailableKernels() + local try_kernel_name = function() + local metadata = vim.json.decode(io.open(e.file, "r"):read("a"))["metadata"] + return metadata.kernelspec.name + end + local ok, kernel_name = pcall(try_kernel_name) + if not ok or not vim.tbl_contains(kernels, kernel_name) then + kernel_name = nil + local venv = os.getenv("VIRTUAL_ENV") or os.getenv("CONDA_PREFIX") + if venv ~= nil then + kernel_name = string.match(venv, "/.+/(.+)") + end + end + if kernel_name ~= nil and vim.tbl_contains(kernels, kernel_name) then + vim.cmd(("MoltenInit %s"):format(kernel_name)) + end + vim.cmd("MoltenImportOutput") + end) +end + +vim.api.nvim_create_autocmd("BufAdd", { + pattern = { "*.ipynb" }, + callback = imb, +}) + +vim.api.nvim_create_autocmd("BufEnter", { + pattern = { "*.ipynb" }, + callback = function(e) + if vim.api.nvim_get_vvar("vim_did_enter") ~= 1 then + imb(e) + end + end, +}) + +local default_notebook = [[ + { + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython" + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 + } +]] + +local function new_notebook(filename) + local path = filename .. ".ipynb" + local file = io.open(path, "w") + if file then + file:write(default_notebook) + file:close() + vim.cmd("edit " .. path) + else + print("Error: Could not open new notebook file for writing.") + end +end + +vim.api.nvim_create_user_command('NewNotebook', function(opts) + new_notebook(opts.args) +end, { + nargs = 1, + complete = 'file' +}) diff --git a/.config/nvim/lua/keymap.lua b/.config/nvim/lua/keymap.lua index 4bf58da..642c0ee 100644 --- a/.config/nvim/lua/keymap.lua +++ b/.config/nvim/lua/keymap.lua @@ -13,6 +13,7 @@ wk.add({ {mode = "n", "gd", desc = "Git: diff"}, {mode = "n", "s", desc = "Substitute"}, {mode = "n", "t", desc = "Terminal"}, + {mode = "n", "r", desc = "Quarto"}, {mode = "n", "", desc = "Navigation"}, -- unmap unwanted commands {mode = "n", "Zt", "", noremap = true}, @@ -103,4 +104,11 @@ wk.add({ {mode = "n", "gf", "(leap-from-window)", desc = "Navigation: enter leap mode for other windows", noremap = true}, -- gnikdroy/projections.nvim {mode = "n", "fp", function() vim.cmd("Telescope projections") end, desc = "Telescope: find projects", noremap = true}, + -- quarto-dev/quarto-nvim + {mode = "n", "rc", require("quarto.runner").run_cell, desc = "Quarto: Run cell", noremap = true, silent = true}, + {mode = "n", "ra", require("quarto.runner").run_above, desc = "Quarto: Run cell and above", noremap = true, silent = true}, + {mode = "n", "rA", require("quarto.runner").run_all, desc = "Quarto: Run all cells", noremap = true, silent = true}, + {mode = "n", "rl", require("quarto.runner").run_line, desc = "Quarto: Run line", noremap = true, silent = true}, + {mode = "n", "rr", require("quarto.runner").run_range, desc = "Quarto: Run visual range", noremap = true, silent = true}, + {mode = "n", "rL", function() require("quarto.runner").run_all(true) end, desc = "Quarto: Run all cells of all languages", noremap = true, silent = true}, }) diff --git a/.config/nvim/lua/plugins/molten.lua b/.config/nvim/lua/plugins/molten.lua new file mode 100644 index 0000000..d383f84 --- /dev/null +++ b/.config/nvim/lua/plugins/molten.lua @@ -0,0 +1,61 @@ +return { + { + "benlubas/molten-nvim", + version = "^1.0.0", + dependencies = { + { + "3rd/image.nvim", + opts = { + backend = "ueberzug", + max_width = 100, + max_height = 12, + max_height_window_percentage = math.huge, + max_width_window_percentage = math.huge, + window_overlap_clear_enabled = true, + window_overlap_clear_ft_ignore = { "cmp_menu", "cmp_docs", "" }, + } + }, + }, + build = ":UpdateRemotePlugins", + init = function() + vim.g.molten_auto_open_output = false + vim.g.molten_wrap_output = true + vim.g.molten_virt_text_output = true + vim.g.molten_virt_lines_off_by_1 = true + vim.g.molten_image_provider = "image.nvim" + vim.g.molten_output_win_max_height = 20 + end, + }, + { + "GCBallesteros/jupytext.nvim", + opts = { + style = "markdown", + output_extension = "md", + force_ft = "markdown" + } + }, + { + "quarto-dev/quarto-nvim", + dependencies = { + "jmbuhr/otter.nvim", + "nvim-treesitter/nvim-treesitter", + }, + opts = { + lspFeatures = { + languages = { "python" }, + chunks = "all", + diagnostics = { + enabled = true, + triggers = { "BufWritePost" }, + }, + completion = { + enabled = true, + }, + }, + codeRunner = { + enabled = true, + default_method = "molten", + } + } + } +}