jdtsmith / indent-bars

Fast, configurable indentation guide-bars for Emacs
GNU General Public License v3.0
272 stars 7 forks source link

Feature: highlight block/scope #24

Closed magthe closed 2 months ago

magthe commented 9 months ago

A colleague pointed me to indent-blankline for Neovim, and it has a rather cool feature in that it can highlight the current block/scope by using information from treesitter:

indent-blankline

There are more examples on the project's GitHub page.

Maybe this is a worthy addition to indent-bars.

jdtsmith commented 9 months ago

I've thought of that; might be hard to do in a performant manner. How does the user configure enclosing block level types? It doesn't seem to use any treesitter config?

jdtsmith commented 9 months ago

Notes on how to implement:

Awkwardly, in addition to the usual (next-single-property-change (point) 'face) rigamarole, you must also check inside any 'display strings in the region (tabs, blank lines), and perform the same face-swap operation in any that exist there. If this sounds like so much work you should just throw away the formatting and redo it, you can't really use just font-lock for this, because that is optimized to update fonts on text changes, whereas this must run in a post-command-hook even after simple movements within the buffer.

Not at all obvious how to do this well. Perhaps you could go line-by-line within the regions, looking directly at the character of interest (at depth, if the line has that depth) and altering in place, taking care of tabs and blank lines, that is. The font-lock functions would also need to be aware of the current depth and region of interest, so they don't overwrite the work you did. This could get slow if the block regions are large (classes, for example).

magthe commented 9 months ago

I'm pretty sure that something like combobulate could offer a lot of insights into working with tree-sitter grammars for this.

I'm sorry I can't offer anything more detailed.

Anyway, it was just a suggestion. It was rather cool to see it working in Neovim so I thought I'd suggest it. No pressure at all.

jdtsmith commented 9 months ago

No problem, those are mostly notes to myself. Treesitter is not an issue, I can easily get the bounds of the method/function/if-block etc. at point (well, I'll make the user write a list of such block types of interest). It's more a drawing problem with efficiently updating the faces as you move about. I'll leave this open for a while as a tickler, to see if anyone has any ideas.

jdtsmith commented 3 months ago

v0.5 now includes tree-sitter based scope awareness, with full style configuration both in- and out-of-scope. Give a try and let me know of any problems.