magma-nvim icon indicating copy to clipboard operation
magma-nvim copied to clipboard

Request - Manual cell delimiters

Open Lun4m opened this issue 3 years ago • 7 comments

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

##
1st cell here
##
2nd cell here
##

Lun4m avatar Aug 18 '21 14:08 Lun4m

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

  • You use some text object (which could be provided by some plugin such as https://github.com/nvim-treesitter/nvim-treesitter-textobjects) to have this sort of functionality, so you could do something like \ric (for some fictional text object ic) to run that cell (you could also map this to some other keybinding, of course);
  • Magma automatically registers as cells everything between the delimiters; the big difference is that in this case, your cells would have the background highlight from the get-go, instead of you having to run them first.

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).

dccsillag avatar Aug 18 '21 14:08 dccsillag

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!

Lun4m avatar Aug 18 '21 14:08 Lun4m

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.

statquant avatar Dec 18 '21 22:12 statquant

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.

tbung avatar Jan 06 '22 20:01 tbung

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

dccsillag avatar Jan 06 '22 20:01 dccsillag

Any progress on this @tbung?

freemagma avatar Feb 08 '22 00:02 freemagma

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

tbung avatar Feb 08 '22 07:02 tbung