highlight-indent-guides icon indicating copy to clipboard operation
highlight-indent-guides copied to clipboard

Improvement: Show indent guides only for the current function

Open Shooooooooo opened this issue 4 years ago • 6 comments

First of all, thank you for this project. I am really liking it. I am looking into how to only show indent guides for the function on which my cursor is. I guess the old issue #3 was referring to the responsive feature today, correct? It would be nice to either:

  1. (Unlikely) Support this feature for multiple languages. e.g. For C/C++ use mechanism in c-beginning/end-of-defun to find boundary of current function.
  2. (Preferred?) Allow user to provide their own function to define highlight scope.

Thank you!

Shooooooooo avatar Mar 18 '20 08:03 Shooooooooo

First of all, I like your avatar. I need to play those games again sometime, it's been too long.

Anyway, yes, #3 is the responsive feature that exists today. You're correct, I don't like the idea of supporting specific languages, since that necessarily means leaving out support for other languages. I would always prefer a general solution, and a user-provided function would certainly fit that criteria. In fact, there already is such a function: the custom highlighter function exists to allow you to do things like this. I see two problems here:

  1. To know whether a line is within a function, you would need to know the line number, which the function currently doesn't provide. This can be easily fixed by adding a new parameter to the function containing the line number.
  2. To make this work at all, you as the user would need to create (or more likely hijack) algorithms that detect functions in all the languages you use. This seems to me like it could be a lot of work for not much payoff, but maybe that's just me.

If you're comfortable with the latter, I'm happy to provide the line number. Thanks!

DarthFennec avatar Mar 18 '20 22:03 DarthFennec

Yes, line number parameter sounds good. Initially, I thought maybe allowing user to provide function which returns a range '(start end) can potentially reduce the overhead of iterating through all lines. Since, I am new to elisp and have not looked at the code in detail, I am not sure if this is feasible by design.

Shooooooooo avatar Mar 19 '20 08:03 Shooooooooo

I think the optimization you're describing is already sort of in place: whenever point is moved, only the lines in the innermost block being moved within are updated, so it only iterates through the lines in that block. In fact, the only reason your function idea can work at all is because functions correlate with blocks; if functions spanned multiple blocks or something like that, some of them may not be updated properly. As for the lines within the function, they have to be iterated over anyway to change the properties of each line, so there's nothing to optimize there.

Anyway, I'll add the parameter when I can and let you know.

DarthFennec avatar Mar 19 '20 19:03 DarthFennec

Sounds good. Thanks a lot!

Shooooooooo avatar Mar 19 '20 19:03 Shooooooooo

Hm ... now that I think about this more, it might not work entirely the way we expect. As an optimization, the results of the custom highlighter function are memoized. For this to work properly, a line with a given level, responsive context, and display method must always be colored the same way no matter what. However, there are some situations when the cursor is inside the function where those values are the same as if the cursor is outside the function, so that'll give you inconsistent results in those cases. Adding the line number as a parameter won't help here, because the line number doesn't change anyway. Essentially what this means is that while the current function will always be highlighted properly, lines outside of it may or may not believe they're actually inside, so that's a bit of a problem. I also don't want to do without the memoization, as that's an important optimization ... so, this may not be possible. Sorry.

I'll tell you what. You should be able to get the line number from within the custom highlighter function using (line-number-at-pos (match-beginning 0)). Try your idea using that, and if it works properly for you, and you still want the number passed as a parameter, let me know. Alternatively you can just use that code to get the line number, and you won't need the parameter. Either one is fine with me.

DarthFennec avatar Mar 19 '20 19:03 DarthFennec

Made some quick changes and got no luck yet. I will spend more time on that later. Thanks for the help!

Shooooooooo avatar Mar 20 '20 08:03 Shooooooooo