chroma icon indicating copy to clipboard operation
chroma copied to clipboard

Allow Multiple HighlightLines

Open leosunmo opened this issue 5 years ago • 4 comments

What problem does this feature solve? I want to be able to highlight ranges using different styles (or at least different classes) when outputting HTML. This would allow for multiple colours of highlighting in the same code block. This is useful for code coverage tools for example.

What feature do you propose? This might be outside the scope of Chroma, in which case it would be nice to provide some kind of hook to allow for some kind of "tagging" (using classes or IDs in HTML for CSS I presume?) of lines with a unique identifier, like hl1, hl2 etc.

This could probably be achieved currently using the custom line classes, but you'd have to create CSS for all lines you wanted highlighted:

<style>
.line36 { display: block; width: 100%;background-color: #e03333 }
.line37 { display: block; width: 100%;background-color: #03fa4d }
.line59 { display: block; width: 100%;background-color: #03fa4d }
...

I'm not a front-end developer, so there might be a very elegant solution here using nicer CSS or HTML features, but I haven't found anything so far.

If there is a "nice enough" solution for this in Chroma already, please let me know! If there isn't a solution for this currently, and it's not out of scope of Chroma, please leave some hints of places for me to start looking at implementing this, either using some kind of hook for customising classes/ids, or to basically implementing the existing HighlightLines function with more options.

Thanks for this amazing project!

leosunmo avatar Mar 05 '20 01:03 leosunmo

This doesn't seem unreasonable. I think adding a new option HighlightLinesWithClass(cls string, ranges [][2]int) would be okay. You'd need to add that, and modify the highlightRanges field to be something like []struct {ranges [][2]int; cls string}.

alecthomas avatar Mar 05 '20 02:03 alecthomas

Thanks for those pointers @alecthomas, I will give this a try if I find some spare time.

leosunmo avatar Mar 05 '20 04:03 leosunmo

Hey @alecthomas, I've implemented the basics for this and spent some time looking at how the existing HighlightLines generates CSS. As far as I understand I couldn't add a custom CSS property without adding a custom TokenType as it uses chroma.StandardTypes to look up the CSS.

Am I missing something or would I need to implement a new type customStyleEntry or something similar and a new formatter Option along the lines of CustomStyleEntry(chroma.StyleEntry) in order to generate the proper CSS for my HighlightLinesWithClass() option?

leosunmo avatar Apr 27 '20 03:04 leosunmo

You won't need to add new TokenTypes, you'll need to either extend styleAttr() or add a second function that adds your custom classes.

alecthomas avatar Apr 27 '20 08:04 alecthomas