dccsillag / magma-nvim

Interact with Jupyter from NeoVim.
GNU General Public License v3.0
1.04k stars 53 forks source link

Request - Manual cell delimiters #19

Open Lun4m opened 3 years ago

Lun4m commented 3 years ago

Would it be possible to have manual cell delimeters? Something like:

  ##
  1st cell here
  ##
  2nd cell here
  ##
dccsillag commented 3 years ago

How exactly are you envisioning this? I feel like there are two ways:

While the second option does sound attractive, it scares me -- we'd need to continuously check for where cells should be, which promises to be costly (unless we use that "new" nvim_buf_attach, but I have no idea how that works and it may still be a bit complicated). Or we can try to use treesitter, but I feel like you are thinking of delimiters which are not captured by treesitter (e.g., #:begin and #:end in Python).

Lun4m commented 3 years ago

As I mentioned in the first closed issue, I've been using nvim-ipy and there you can define a customizable cell delimiter with let g:ipy_celldef = '^##'. I guess in this case the background highlight won't be that necessary since you already know where the cell starts and ends. But I don't know if that's easily removable. Also in practice I think you only need to evaluate where the delimeters are when you run :MagmaReevaluateCell.

Anyway this is a very marginal feature request (mostly for preserving the "muscle memory" from the previous plugin I was using) since magma manages cell identification quite well!

statquant commented 2 years ago

This seems to me that what @Lun4m is asking is in essence close what jupytext is providing. There are many formats that allow isomorphic transformation from text to jupyter-notebook. My prefered format is the one used in the quarto project, I think using those formats for delimiters would make sense.

tbung commented 2 years ago

My proposal would be simply to add a configurable cell delimiter, to not add highlighting for this, and only change MagmaReevaluateCell to add a extmark cell with all lines inbetween the delimiters if it is run outside of an extmark cell (or error if no delimiters are found, basically the behavior it has right now if outside of a cell).

I would love to implement this based on https://github.com/bfredl/nvim-ipy, if you agree to it @dccsillag.

dccsillag commented 2 years ago

@tbung Yeah, that seems like a great idea actually. Go ahead.

freemagma commented 2 years ago

Any progress on this @tbung?

tbung commented 2 years ago

I have something working in lua in my dotfiles but didn't have the time yet to clean it up and translate it to python due to work. I might get around to it later this week. Until then you can check out these two files for a working solution:

https://github.com/tbung/dotfiles/blob/main/.config/nvim/lua/tbung/jupyter.lua https://github.com/tbung/dotfiles/blob/main/.config/nvim/after/ftplugin/python.lua