highlightjs / highlightjs-solidity

Do What The F*ck You Want To Public License
48 stars 17 forks source link

Add operator and punctuation highlighting #47

Closed haltman-at closed 3 years ago

haltman-at commented 3 years ago

This PR adds operator and punctuation highlighting for Solidity and Yul. Well -- I did some of this in maybe an iffy way, but, well, obviously it can be changed if need be.

For Yul this was straightforward; I added := and -> as operators, and the obvious things as punctuation. Arguably -> should be punctuation, but it seemed worth highlighting as an operator to me.

For Solidity... well, Solidity has a number of operators, but they're all made of the same few characters, so I just checked for those characters. (So, => will be highlighted as an operator, which makes sense to me.) For punctuation I put the obvious things.

Except... I did one thing that is maybe kind of off; I put ? and : as punctuation. Now, ? should be an operator, right? It's the ternary operator. Problem is, then : should be an operator too. And : occurs in a lot of contexts where it is not in fact an operator, but we don't really have a good way of checking for that right now. So I thought, what if I just classed them as punctuation instead? Maybe that'd be OK? Like I said, let me know what you think.

Note I didn't include punctuation highlighting inside function parameters.

Also I removed * as a possible lexeme and removed it from keywords, adding operator highlighting to those contexts instead so it'll be highlighted as an operator.

I also added a rudimentary test.

haltman-at commented 3 years ago

Oh, wait, I think I need to test this more.

haltman-at commented 3 years ago

OK, yup, tested it out and there was a problem. Fixed it now though.

The problem was that in certain contexts I had put operators above comments; it seems like highlight.js prioritizes things that are above. This caused certain comments not to be recognized. Oops. I moved it below in all contexts now to prevent the problem.

joshgoebel commented 3 years ago

Yes. The mode rule set is "executed" sequentially so the first rule that matches in a given mode is how it works. 

haltman-at commented 3 years ago

Ah, OK, good. Glad to know I can rely on that. That makes things easier than having to come up with some other way of ensuring that comment starts are never recognized as operators. (It'll come in handy too later, I'm intending to add doctag highlighting later, ordering provides a good way of ensuring that doctags are only highlighted in doc comments. :) )

haltman-at commented 3 years ago

Alright, @cds-amal says it's OK, so I'm going to merge and release this despite the punctuation weirdness! :)