gleam-mode
gleam-mode copied to clipboard
No syntax highlighting with gleam-mode
I have just installed gleam-mode followed the instruction, and it loads fine (gleam tree-sitter company) when I open a .gleam file but there is no syntax highlighting. Not sure how to fix this nor to give more debug info.
@gleam-lang/emacs-team Any ideas?
Sorry for the delay on this!
@vbmithr Sadly getting this setup can be a bit of a pain. My first guess would be that the tree-sitter grammar was not compiled. You can check this by looking in the tree-sitter-gleam directory inside of where you cloned gleam-mode (e.g. if you cloned gleam-mode to ~/Code/gleam-mode/, look inside ~/Code/gleam-mode/tree-sitter-gleam). There should be a gleam.so file in there; if not, the grammar was not compiled.
Some reasons why that may have failed:
- No C compiler is available (
which ccreturns something like "no cc in: ...") - You're using Windows (I've never tested this on Windows, and I'm going to guess it doesn't work)
For whatever it's worth, a future revision will download a compiled asset instead of trying compile it on the host machine. The strength of the current approach is that it works anywhere a C compiler is available, whereas the downloading approach means that I'll have to maintain a repository of compiled assets for different OSes and architectures.
gleam.so is there. Iām using Linux. Current approach of compiling on host machine is fine for me!
Also, I have checked that emacs indeed uses the file. Let me check with your latest commits.
Also, highighting works with tree-sitter cmdline program.
I have the same issue too, I have the gleam.so file in tree-sitter-gleam directory too, but syntax highlighting does not work. gleam-format is working fine.
And I checked, tree-sitter does work for other languages.
I have been able to replicate this issue in a bare-bones config, though I have syntax highlighting with the config I use daily. I'll be digging into this today to see if I can determine what setting I usually have enabled that causes syntax highlighting to work.
Figured I'd post an update on debugging since it's taken awhile:
tree-sitter-hl--setupis being called when a gleam-mode buffer loads. Why the buffer is not fontified at that time is still unknown.- Buffers can be fontified manually by running
(tree-sitter-hl--highlight-region (point-min) (point-max)). Unsurprisingly, this does not account for updates made to the buffer, but proves that the current tree-sitter highlighting implementation can work, it's just being annoying at the moment.
I figured out what part of my config was causing syntax highlighting to work, and it's a doozy: whitespace-mode. If you don't mind @vbmithr, @Bhanukamax, try opening a Gleam file and enabling whitespace-mode (i.e. M-x whitespace-mode) to see if that triggers syntax highlighting. In my experience, syntax highlighting continues to function even when whitespace-mode is disabled, so long as it was at one time enabled in the buffer.
I honestly have no idea why syntax highlighting would be predicated on whitespace-mode, and I suspect that this is a bug upstream somewhere.
I figured out what part of my config was causing syntax highlighting to work, and it's a doozy:
whitespace-mode. If you don't mind @vbmithr, @Bhanukamax, try opening a Gleam file and enablingwhitespace-mode(i.e.M-x whitespace-mode) to see if that triggers syntax highlighting. In my experience, syntax highlighting continues to function even whenwhitespace-modeis disabled, so long as it was at one time enabled in the buffer.I honestly have no idea why syntax highlighting would be predicated on
whitespace-mode, and I suspect that this is a bug upstream somewhere.
wow!, that infact does it, thanks a lot, that's some weird stuff though, but it's great, finally, I get to see some gleam on its glory :slightly_smiling_face:. Thanks a lot @J3RN for looking into this.
I can confirm it works with whitespace-mode indeed.
Sorry for the delay on this!
@vbmithr Sadly getting this setup can be a bit of a pain. My first guess would be that the tree-sitter grammar was not compiled. You can check this by looking in the
tree-sitter-gleamdirectory inside of where you clonedgleam-mode(e.g. if you clonedgleam-modeto~/Code/gleam-mode/, look inside~/Code/gleam-mode/tree-sitter-gleam). There should be agleam.sofile in there; if not, the grammar was not compiled.Some reasons why that may have failed:
- No C compiler is available (
which ccreturns something like "no cc in: ...")- You're using Windows (I've never tested this on Windows, and I'm going to guess it doesn't work)
For whatever it's worth, a future revision will download a compiled asset instead of trying compile it on the host machine. The strength of the current approach is that it works anywhere a C compiler is available, whereas the downloading approach means that I'll have to maintain a repository of compiled assets for different OSes and architectures.
I feel dumb for not being able to figure this out, but how do I compile the grammar? I've checked and I have a C compiler. (which cc works for me), and I'm running on Mac OS. I can see that the gleam.so file is missing.
@quarkw Are you using an M1/M2 mac? I only have access to an older Intel mac at the moment, but I can try to get ahold of an M1/M2 if that's the case.
@quarkw Are you using an M1/M2 mac? I only have access to an older Intel mac at the moment, but I can try to get ahold of an M1/M2 if that's the case.
Ah yes, Iām on an M1 Mac. Usually I mention that in bug reports but forgot this time.
Similar situation here, and it was a slight pain to fix it (and fair warning: I did it in the hackiest way possible). Here's a step-by-step in case someone else can benefit.
- Clone https://github.com/gleam-lang/tree-sitter-gleam and make a symlink to it in the
gleam-modedirectory you cloned from here (it wasn't cloned when I gotgleam-modefromelpaca, but I might have written a bad recipe). - In
tree-sitter-gleam, runtree-sitter test. Note that you need to havetree-sitter-cliinstalled to do this (I got it fromcargo, see https://github.com/tree-sitter/tree-sitter/blob/master/cli/README.md). - The previous step should have generated
gleam.soin yourtree-sittercache directory (for me, this is~/.cache/tree-sitter/), as indicated here: https://github.com/tree-sitter/tree-sitter/issues/1868. - Make a symlink to the
gleam.sofile in thetree-sitter-gleamdirectory.
After this, I just restart Emacs (or just revert the buffer), and I have all the goodies from tree-sitter-gleam running just fine.
Seeing this too, I have gleam.so in tree-sitter-gleam and the package loaded as per the Readme, no syntax highlighting in Gleam files and formatting seems super weird.
Edit:
Somehow rm -rfing my clone and recloning solved the issue for me.
Just in case this helps Doom users out there, I was also having some troubles getting the syntax highlighting to work correctly, so I hacked my way through it.
After adding these 2 entries:
;; Gleam-lang support
(package! gleam-mode :recipe
(:host github
:repo "gleam-lang/gleam-mode"
:files ("gleam-mode.el")))
(package! gleam-ts-mode :recipe
(:host github
:repo "gleam-lang/gleam-mode"
:files ("gleam-ts-mode.el")))
I ran the usual doom sync && doom run. But when I was trying to activate gleam-mode from a doom session, I was getting this error in the Messages buffer:
gleam-mode--compile-grammar: Setting current directory: No such file or directory, /home/vv/.config/emacs/.local/straight/build-29.4/gleam-mode/tree-sitter-gleam/
I could repro that issue easily from a Elisp REPL:
(gleam-mode--compile-grammar)
*** Eval error *** Setting current directory: No such file or directory, /home/vv/.config/emacs/.local/straight/build-29.4/gleam-mode/tree-sitter-gleam/
However, after looking in /home/vv/.config/emacs/.local/straight/repos/gleam-mode I saw that I do in fact have a tree-sitter-gleam directory there:
$ cd ~/.config/emacs/.local/straight
$ find . -type d -name 'tree-sitter-gleam'
./repos/gleam-mode/.git/modules/tree-sitter-gleam
./repos/gleam-mode/tree-sitter-gleam
So gleam-mode was looking in
~/.config/emacs/.local/straight/build-29.4/gleam-mode/tree-sitter-gleam
but the source code repo was actually installed in
~/.config/emacs/.local/straight/repos/gleam-mode/tree-sitter-gleam
So I just ran:
$ cd ~/.config/emacs/.local/straight/build-29.4/gleam-mode
$ ln -s ../../repos/gleam-mode/tree-sitter-gleam .
and then things started to work fine!
I didn't have time to investigate more why gleam-mode was looking in that dir, and if that's a doom issue or a gleam-mode issue, but for now this works, and lets me hack with gleam, which was the original goal. Once I'll move to a new emacs version I'll just patch it again, I guess :D