wishlist icon indicating copy to clipboard operation
wishlist copied to clipboard

Regexp Railroad Diagrams

Open bennypowers opened this issue 2 years ago • 5 comments

What? I’m a refugee from Atom and I’ve been thoroughly enjoying my experience with Neovim so far. I’m down so a scant few of my favourite Atom features I’ve yet to find answers for with nvim. One of those features is atom-regex-railroad-diagrams 2. With this atom plugin installed, any time the cursor is on a regular expression, a graphical railroad diagram 4 appears at the bottom of the window.

Is there something comparable for neovim? Surely, terminal-based plugins would have to use ascii art or something like that, but perhaps something could be done anyways?

Some cursory googling didn’t turn up much, there’s too much history with vim and regexp search terms to break through the noise of ‘how to regexp in vim’ tutorials.

see also https://neovim.discourse.group/t/regexp-railroad-diagrams/1984/3

Why? Regexp is confusing, railroad diagrams help

Potential existing implementations:

  • inccommand
  • https://github.com/tabatkins/railroad-diagrams/issues/94 could be adapted to output text

Potential pitfalls: issue linked above - railroad-diagrams library could be adapted, but it wouldn't be trivial

bennypowers avatar Feb 01 '22 13:02 bennypowers

Just thinking, with this you could shell out to an app that will render the graphic, then display it in a float/split term buffer https://github.com/edluffy/hologram.nvim

CKolkey avatar Feb 15 '22 21:02 CKolkey

https://www.reddit.com/r/neovim/comments/ssam1h/comment/hwxxsmk/?utm_source=reddit&utm_medium=web2x&context=3

bennypowers avatar Feb 15 '22 21:02 bennypowers

The way I'm thinking:

  1. autocmd ON cursor move
  2. IF treesitter query for cursor is inside a regexp, THEN continue ELSE quit
  3. Get SVG by passing regexp string from ts to https://github.com/tabatkins/railroad-diagrams by basically adapting the code from the atom plugin
  4. Convert SVG to cairo via https://github.com/akrinke/svg2cairo
  5. Split the window with a new buffer, render the cairo image to the buffer with hologram

Now, I've no idea how nicely all these will play together, and suspect there will be performance pitfalls esp. if shelling out to node.js

But hey, it could just work

bennypowers avatar Feb 15 '22 21:02 bennypowers

Ha! Nice. That seems like a reasonable approach :)

CKolkey avatar Feb 15 '22 21:02 CKolkey

made some progress today on step 2:

https://github.com/bennypowers/nvim-regexp-railroad/blob/a75b70640cf4753af36673c5811cbcbbc4bc4ed0/lua/nvim-regexp-railroad/module.lua#L66-L83

bennypowers avatar Feb 16 '22 21:02 bennypowers