Elixir Syntax Highlighting on Stackoverflow

I would really like to see Syntax Highlighting support for Elixir on Stackoverflow. Someone already created a topic on Meta StackExchange but since SE relies on an external syntax highlighting tool (Google’s code-prettify), there hasn’t been any work to support Elixir (even though an issue was also opened there over a year ago).

I’m interested in making this happen (as someone who spends 5+ hours daily on SO - especially in the Elixir tag). So my questions are:

  • How do I get started in adding Elixir support to code-prettify?
  • How do I represent Elixir constructs that need to be highlighted?
  • And should I even send in a pull request? There seems to be minimal activity in the project, and many pull requests haven’t been merged or closed for a while.
4 Likes

I was thinking about the same thing and slowly started looking into that. It seems like the syntax highlighting definitions for erlang or Scala aren’t really that complicated, and I would argue Scala has a more ambiguous syntax than Elixir.

It also seems like the code-prettify project is still pretty alive and they do accept PRs, so that’s all looking good.

Is there a good place to get Elixir’s grammar or something similar? It would probably help make the highlighter more comprehensive.

2 Likes

In the Elixir issue for code-prettify (here), they link to the atom editor elixir grammar (here) which looks quite useful.

2 Likes

I looked into how the google-prettify works and prepared a plugin for elixir that seems to be working. Now I could use you guys’ help making sure it’s nice and robust.

Here’s how you can try it out: I put an editable example in the live-preview branch of my fork of the repo. Clone it and open it in a browser or go to rawgit and you can play around with the source code and see how the prettifier behaves.

Let me know what you think! Please point out what needs improving.

Notes:

  • The main things that can be broken is probably false positives on some classes. For example 1_000 is a correct integer, but 1__000 isn’t and shouldn’t be highlighted like one.
  • Things to be treated as “keywords” in Elixir are a bit of a judgement call, since most of the functionality is implemented with macros. I used my gut on this one - let me know if you think any constructs should (not) be treated as keywords but are(n’t).
  • Before I submit a PR to the main fork I’ll want to add some unit tests for the elixir prettifier - I just wanted to get some feedback first.
  • If you want to submit some changes to the prettifier in my fork, submit it to the lang-elixir branch - the live preview is just a utility and won’t be included in the PR for the main project.
  • I’m not sure what class the sigils should be highlighted as - they’re currently being coloured the same as atoms, because it’s consistent with the highlighting I have in my main editor.
  • I don’t think it’s worth it to support highlighting inside string interpolation - It would probably be more trouble than it’s worth using their createSimpleLexer
1 Like

As long as their lexer is not something stupid like regex it should not be hard.

I really really hate the idiotic highlighting libraries that think everything can be lexed with regex, morons…

1 Like

Don’t look in the source then :slight_smile: It is what it is though, as long as it makes the StackOverflow questions/answers more readable, I’m a happy person.

1 Like

I have tried the highlighting with some more code and ironed out some issues. An updated interactive demo still available on rawgit.

I’m aiming to write some tests and submit a PR this weekend, so if you have any feedback, speak up :wink:

1 Like

It feels like I’m mostly talking to myself here, but here’s the PR for the Elixir support: https://github.com/google/code-prettify/pull/478

4 Likes

Cool, better syntax highlighting on popular syntax highlighters, even if the syntax highlighter itself is stupidly made in regex but nothing you can do about that, is still a great help due to the visibility! :slight_smile:

1 Like

Breaks when I type this in:

"I love me some #{ "string" <> "y" } interpolation"

It might not support hilighting inside string interpolation, but it should at least ignore quotes inside of an interpolated part, so it doesn’t just look wrong.

3 Likes

Your example here:

%{foo: :bar, "yes, this compiles": :"also an atom"}

Both of them are atoms; :“also an atom” and :“yes, this compiles” are both atoms. Your syntax hilighter thinks that “yes, this compiles” is a string.

iex(1)> %{ "yes, this compiles": :"also an atom" } |> Enum.map(fn {key, val} -> is_atom key end)
[true]
3 Likes

Thanks for the feedback, @notriddle!

You’re definitely right about the string inside string interpolation case. I’ll try and fix it soon.

I was in two minds whether to make %{"THIS": :foo} highlight as an atom or not. I decided to go with the string highlighting because it made the string detection much faster, using some short circuiting logic in the code. But in the end I think you’re right on that one as well, and I should make it display as atom as well.

2 Likes

Allow me to bump the topic, since I run into ugly, non-highligthed code on SO once more and it boiled my blood a bit again.

To summarise the situation, the feature has been requested four and a half years ago and about a year ago I wrote the highlighter necessary for the SO people to integrate Elixir highlighting with their site without breaking much of a sweat. Still, there hasn’t been any progress on the issue since, and it looks like SO has a policy on not improving syntax highlighting on the site ever again.

While I have no reason to be optimistic about the whole situation, it feels like it would be good to raise the profile of the feature request, or otherwise nothing is going to change. From what I’m reading the best bet is to upvote the request and add/or add a bounty for it. I’d happily do it myself, but I don’t have enough karma on Meta Stack Exchange…

Soo I guess what I’m saying is: Does anyone have some Meta Stack Exchange karma to use for a good cause? :]

Edit: I’m curious, which of the other Most Loved Languages according to SO Developer Survey 2017 don’t have the syntax highlighting support :smiley:

4 Likes

We’re half way through 2018 and there’s still no Elixir syntax highlighting on Stackoverflow – That’s disappointing. I just opened a support thread on Meta, asking for help on the topic:

3 Likes