Highlighter dreaming

When coding, I face often a situation where I comment part of the code either temporarily or where some of the “language features” are actually used by coding something into the comments (e.g. Haskell doctests can be used to test and give working examples and there that tool find outs code from the comments).

It would be really cool to be able to highlight also those sections. Say, by using different tones of the comment color or something. The difficulty is of course to recognize what is normal comment text, and what is doctest part. This might be actually doable on haskell-mode as most of the time the doctests start with a special start string.

Anyhow, let’s not stop here. Often also, there are several languages involved. E.g. the comments may contain html-code while the actual code implements something that produces the html-code in the comments. It would be really cools if the html in the comments were highlighted, e.g. by using different tones of the comment color.
Doable at the moment? The html-snippets in the comments might also be recognizable so maybe.

Further, at least in haskell the comments can contain markdown-like-formatted text and that would be also nice to highlight.

Also, there are cases where I’d like to put code made in some other language into the comments. So maybe the question is that is there a tool that could be used to recognize, what language the particular lines in the comments is using? (A tool that should probably work probabilistically.) And then based on the information, feed the lines to highlighter. And in addition, this could be used to classify the text sections at the comments to natural language styled with markdown and to code made in various languages…

You can alread nest highlighters. For an example check the highlighter for kakrc files that’s comes bundled with kakoune. When you write a %sh{ } block it applies the shell highlighters inside. To get it to work you need to define the region you want to highlight, this is quite easy for something like markdown where code blocks start with triple grave accent and the filetype.

Ok, thanks, if you think that it is possible, then I probably need to take another look on those highlighters (and also reread the very cool intro made by Screwtape Intro to Kakoune highlighters - Screwtape's Notepad).

I’m referring to the following situation (the code is blaa-blaa -code, just made up to work as an example):

This doesn’t have the doctest part in there but it could be there as well. This example does not show the md-like parts (alike, not sure how close this is to normal md, see e.g. Documentation and Markup — Haddock 1.0 documentation)

Anyhow, the html-region inside comments could be catched and highlighted, e.g. by assuming that the start and end tags are the only things on their lines.

That aFun in there is a bit more difficult. Maybe using the ::-characters and intendation levels somehow. ?

unrelated to your question but that documentation seems very involved for something that’s written inline in your source code. I feel like if I wanted to document something that thoroughly I’d do it in a seperate file or something.

It’s also looks very complicated to write regex for, if haskell code isn’t delimited I don’t really see how it’s even possible. Small bit of advice if you do pursue this thing though, use the {|- -} style comments or else the leading -- on each line will mess things up.

Reason for wishing those is that sometimes it is convenient to have that kind of code blocks close by (e.g. at module headers).

Played with highlighters couple of hours and then found Support references to captured groups in regex · Issue #2486 · mawww/kakoune · GitHub. If that was supported, then it would be more or less easy to try pattern match the indented function names with same intendation level/amount (maybe?). Maybe this needs to be done e.g. by building sh{ } code…