From 63dc6fb88808c3d46ea9da96c028b7b8b29c83f3 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sun, 16 Jun 2024 21:27:01 +0200 Subject: [PATCH] nvim: Set up quarto for interactive python work --- nvim/.config/nvim/after/ftplugin/quarto.lua | 112 ++++-------------- .../nvim/lua/plugins/data_analysis.lua | 27 ++++- 2 files changed, 49 insertions(+), 90 deletions(-) diff --git a/nvim/.config/nvim/after/ftplugin/quarto.lua b/nvim/.config/nvim/after/ftplugin/quarto.lua index 18d1d03..8840c65 100644 --- a/nvim/.config/nvim/after/ftplugin/quarto.lua +++ b/nvim/.config/nvim/after/ftplugin/quarto.lua @@ -6,14 +6,13 @@ local default_buffer_session = function() if vim.fn.getftype(dir) ~= "dir" then vim.fn.mkdir(dir, "p") end - return temp_path end -- Start quarto session local startsession = function(file, args) file = file or default_buffer_session() - local path = require("core.util").get_python_venv() + local path = require("core.util").get_python_venv_bin() vim.g["python3_host_prog"] = path if vim.fn.executable("jupyter-console") ~= 1 then @@ -41,92 +40,27 @@ vim.api.nvim_create_user_command("JupyterStart", function() startsession() end, {}) -local map = vim.keymap.set - -local function molten_mappings_set() - -- Operate jupyter notebooks from within vim - map("n", "cc", ":MoltenEvaluateLine", { silent = true }) - map( - "n", - "C", - require("quarto.runner").run_cell, - { silent = true, desc = "Evaluate current code cell" } - ) - map("x", "c", ":MoltenEvaluateVisual", { silent = true }) - map( - "n", - "c", - "nvim_exec('MoltenEvaluateOperator', v:true)", - { expr = true, silent = true, desc = "+code-evaluation" } - ) - map("n", "cr", ":MoltenReevaluateCell", { silent = true }) - map("n", "cu", ":MoltenShowOutput", { silent = true }) - map("n", "cU", ":noautocmd :MoltenEnterOutput", { silent = true, desc = "Molten enter output" }) - map("n", "cd", ":MoltenDelete", { silent = true }) - map("n", "ci", ":MoltenInterrupt") - map("n", "cN", ":MoltenInit ") - map("n", "cD", ":MoltenDeinit") - map("n", "cR", ":MoltenRestart") - map("n", "cA", require("quarto.runner").run_all, { silent = true, desc = "Evaluate all code cells" }) - map( - "n", - "ca", - require("quarto.runner").run_above, - { silent = true, desc = "Evaluate cells above current" } - ) - map( - "n", - "cb", - require("quarto.runner").run_below, - { silent = true, desc = "Evaluate cells below current" } - ) - - -- jump to beginning of previous/ next cell code - map("n", "]c", "/^```{}:nohl", { desc = "Next quarto cell" }) - map("n", "[c", "?^```n}:nohl", { desc = "Previous quarto cell" }) - -- insert cell header above/below - map("n", "co", "o```{python}```k", { desc = "Insert quarto cell below" }) - map("n", "cO", "O```{python}```k", { desc = "Insert quarto cell above" }) -end - -vim.api.nvim_create_autocmd("User", { - pattern = "MoltenInitPost", - callback = molten_mappings_set, +vim.api.nvim_create_autocmd({"InsertEnter", "BufEnter"}, { + callback = function() + if vim.b["sessionfile"] == nil then + local path = default_buffer_session() + vim.b["sessionfile"] = path + vim.schedule_wrap(startsession(path)) + end + end, }) -local bufnr = 0 -map("n", "[d", "lua vim.diagnostic.goto_prev()", { buffer = bufnr, desc = "Previous diagnostic" }) -map("n", "]d", "lua vim.diagnostic.goto_next()", { buffer = bufnr, desc = "Next diagnostic" }) -map( - "n", - "[e", - "lua vim.diagnostic.goto_prev({severity = vim.diagnostic.severity.ERROR})", - { buffer = bufnr, desc = "Previous error" } -) -map( - "n", - "]e", - "lua vim.diagnostic.goto_next({severity = vim.diagnostic.severity.ERROR})", - { buffer = bufnr, desc = "Next error" } -) +-- -- -- TODO find better way to enable lsp key mappings for quarto buffers +-- -- local prefix = require("which-key").register +-- -- prefix({ ["l"] = { name = "+lsp" } }) +-- -- map("n", "li", "LspInfo", { buffer = bufnr, desc = "Lsp Info" }) +-- -- map("n", "ld", "lua vim.diagnostic.open_float()", { buffer = bufnr, desc = "Line diagnostics" }) +-- -- map("n", "la", "lua vim.lsp.buf.code_action()", { buffer = bufnr, desc = "Codeactions" }) +-- -- map("n", "ln", "lua vim.lsp.buf.rename()", { buffer = bufnr, desc = "Rename element" }) +-- -- map("n", "lr", "lua vim.lsp.buf.references()", { buffer = bufnr, desc = "References" }) +-- -- +-- -- map("n", "gD", "lua vim.lsp.buf.declaration()", { buffer = bufnr, desc = "Declaration" }) +-- -- map("n", "gs", "lua vim.lsp.buf.signature_help()", { buffer = bufnr, desc = "Signature help" }) +-- -- map("n", "gI", "lua vim.lsp.buf.implementation()", { buffer = bufnr, desc = "Implementation" }) +-- -- map("n", "gt", "lua vim.lsp.buf.type_definition()", { buffer = bufnr, desc = "Type definition" }) --- TODO find better way to enable lsp key mappings for quarto buffers -local prefix = require("which-key").register -prefix({ ["l"] = { name = "+lsp" } }) -map("n", "li", "LspInfo", { buffer = bufnr, desc = "Lsp Info" }) -map("n", "ld", "lua vim.diagnostic.open_float()", { buffer = bufnr, desc = "Line diagnostics" }) -map("n", "la", "lua vim.lsp.buf.code_action()", { buffer = bufnr, desc = "Codeactions" }) -map("n", "ln", "lua vim.lsp.buf.rename()", { buffer = bufnr, desc = "Rename element" }) -map("n", "lr", "lua vim.lsp.buf.references()", { buffer = bufnr, desc = "References" }) - -map("n", "K", "lua vim.lsp.buf.hover()", { buffer = bufnr, desc = "Hover definition" }) -map("n", "gd", "lua vim.lsp.buf.definition()", { buffer = bufnr, desc = "Definition" }) -map("n", "gD", "lua vim.lsp.buf.declaration()", { buffer = bufnr, desc = "Declaration" }) -map("n", "gs", "lua vim.lsp.buf.signature_help()", { buffer = bufnr, desc = "Signature help" }) -map("n", "gI", "lua vim.lsp.buf.implementation()", { buffer = bufnr, desc = "Implementation" }) -map("n", "gt", "lua vim.lsp.buf.type_definition()", { buffer = bufnr, desc = "Type definition" }) - -if vim.b["sessionfile"] == nil then - local path = default_buffer_session() - vim.b["sessionfile"] = path - vim.schedule_wrap(startsession(path)) -end +-- vim.g["python3_host_prog"] = vim.fn.expand(require("core.util").get_python_venv()) diff --git a/nvim/.config/nvim/lua/plugins/data_analysis.lua b/nvim/.config/nvim/lua/plugins/data_analysis.lua index aa3c1e0..6d76169 100644 --- a/nvim/.config/nvim/lua/plugins/data_analysis.lua +++ b/nvim/.config/nvim/lua/plugins/data_analysis.lua @@ -1,4 +1,15 @@ return { + { + "jmbuhr/otter.nvim", + config = function() + require("otter").setup({ + buffers = { + set_filetype = true, + write_to_disk = false, + }, + }) + end, + }, { "quarto-dev/quarto-nvim", dependencies = { @@ -7,6 +18,7 @@ return { "vim-pandoc/vim-pandoc-syntax", "hrsh7th/nvim-cmp", "nvim-treesitter/nvim-treesitter", + { "benlubas/molten-nvim", optional = true }, }, config = function() require("core.util").set_python_env() @@ -18,9 +30,22 @@ return { }, codeRunner = { enabled = true, - default_method = "molten", + default_method = "slime", + ft_runners = { + python = "molten", + quarto = "molten", + }, }, }) + local map = vim.keymap.set + map("n", "C", require("quarto.runner").run_cell, { desc = "run cell" }) + map("n", "ca", require("quarto.runner").run_above, { desc = "run cells above" }) + map("n", "cb", require("quarto.runner").run_below, { desc = "run cells below" }) + map("n", "cA", require("quarto.runner").run_all, { desc = "run all similar cells" }) + map("n", "]c", "/^```{}:nohl", { desc = "Codecell forward" }) + map("n", "[c", "?^```n}:nohl", { desc = "Codecell last" }) + map("n", "co", "o```{python}```k", { desc = "Insert quarto cell below" }) + map("n", "cO", "O```{python}```k", { desc = "Insert quarto cell above" }) end, ft = { "quarto" }, },