michaelb / sniprun

A neovim plugin to run lines/blocs of code (independently of the rest of the file), supporting multiples languages
MIT License
1.41k stars 46 forks source link

Append code block results to markdown document #152

Open daephx opened 2 years ago

daephx commented 2 years ago

As an extension to #50: Much like the the orgmode screenshot supplied, is it possible to have a display option, specifically for source code blocks in markup where the result is printed to the document itself?

Example:

```python
print('Hello, World!')

Result:

Hello, World!


This way you can save the output for sharing/publishing, similar to Orgmode or Jupyter Notebooks.

You could keep virtual text on but have this as a compile step for your individual cells.
As I imagine such a feature should be exempt from REPL capabilities, otherwise it could cause some problems.
michaelb commented 2 years ago

Linking that here while I try to find some free time https://github.com/michaelb/sniprun/issues/111#issuecomment-963051868

michaelb commented 2 years ago

153 adds supports for multiple code blocs in one selection, meaning that you can now do even :%SnipRun (in markdown or orgmode) and have sensible outputs. (one per code bloc)

There's no support for modifying the buffer in the meantime though, so it doesn't do what you want. With the VirtualText display option, it looks okay-ish, but nothing is inserted nor savable in the file..

daephx commented 2 years ago

Alright thanks for the consideration, that sounds interesting in the mean time. I'll take a look when I get a moment!

minhd-vu commented 5 months ago

Hey! So I've been trying to get similar functionality by copying the output to the * buffer to be able to paste with p. Here's what I have so far:

    {
      "michaelb/sniprun",
      cmd = "SnipRun",
      branch = "master",
      build = "sh install.sh",
      config = function()
        require("sniprun").setup {
          -- your options
        }
      end,
      keys = {
        { "<leader>sr", "<cmd>:redir @* | :SnipRun | redir END<cr>", mode = { "n" }, desc = "SnipRun" },
        { "<leader>sr", "<cmd>:redir @* | :'<,'>SnipRun | redir END<cr>", mode = { "v" }, desc = "SnipRun" },
        { "<leader>sc", "<Plug>SnipClose", desc = "SnipClose" },
      },
    },

This works so far but I'd like to add ```plain\n{msg}\n``` around the message. Here's what I tried so far, but I'm having some trouble:

        {
          "<leader>sr",
          function()
            vim.cmd "redir @a | SnipRun | redir END"
            local a = vim.fn.getreg "a"
            vim.fn.setreg("b", a)
          end,
          mode = { "n" },
          desc = "SnipRun",
        },

This doesn't look like it's writing to the correct register, and when I check the a register with :reg it looks like it's empty. Wondering if y'all are experiencing similar issues. It also tried stuff with nvim_exec and nvim_command but had similar results.

It may be something with my neovim config itself. Pretty new to this deep configuration so let me know if I'm missing something obvious 😄 .

michaelb commented 5 months ago

Hi! You're charting mostly unexplored terrain, but if your current solution works somehow, well I see no reason to change it

The 'clean' approach would be to use the API 's register_listener to create your own (lua) function to handle output however you want. It should also be relatively easy to customize the output to get wrapped with "plain\n{msg}\n" or whatever you want and continue using the rest of your scripts

PlatyPew commented 1 month ago

It's a bit late but here's my take on this

local api_listener = function(d)
    if vim.bo.filetype ~= "markdown" then
        return
    end

    if d.status ~= "ok" or d.message == "" then
        return
    end

    local output = string.format("```plain\n%s\n```\n", d.message)
    vim.fn.setreg('"', output)
end

require("sniprun.api").register_listener(api_listener)