gleam-mode icon indicating copy to clipboard operation
gleam-mode copied to clipboard

No syntax highlighting with gleam-mode

Open vbmithr opened this issue 3 years ago • 16 comments

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.

vbmithr avatar May 25 '22 09:05 vbmithr

@gleam-lang/emacs-team Any ideas?

lpil avatar Jun 07 '22 12:06 lpil

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 cc returns 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.

J3RN avatar Jun 08 '22 05:06 J3RN

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.

vbmithr avatar Jun 23 '22 13:06 vbmithr

Also, highighting works with tree-sitter cmdline program.

vbmithr avatar Jun 23 '22 14:06 vbmithr

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.

Bhanukamax avatar Jun 25 '22 10:06 Bhanukamax

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.

J3RN avatar Jun 25 '22 15:06 J3RN

Figured I'd post an update on debugging since it's taken awhile:

  • tree-sitter-hl--setup is 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.

J3RN avatar Jun 27 '22 15:06 J3RN

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.

J3RN avatar Jun 27 '22 17:06 J3RN

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.

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.

Bhanukamax avatar Jun 27 '22 20:06 Bhanukamax

I can confirm it works with whitespace-mode indeed.

vbmithr avatar Jul 31 '22 13:07 vbmithr

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 cc returns 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 avatar Feb 17 '23 06:02 quarkw

@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.

J3RN avatar Mar 10 '23 20:03 J3RN

@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.

quarkw avatar Mar 10 '23 20:03 quarkw

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.

  1. Clone https://github.com/gleam-lang/tree-sitter-gleam and make a symlink to it in the gleam-mode directory you cloned from here (it wasn't cloned when I got gleam-mode from elpaca, but I might have written a bad recipe).
  2. In tree-sitter-gleam, run tree-sitter test. Note that you need to have tree-sitter-cli installed to do this (I got it from cargo, see https://github.com/tree-sitter/tree-sitter/blob/master/cli/README.md).
  3. The previous step should have generated gleam.so in your tree-sitter cache directory (for me, this is ~/.cache/tree-sitter/), as indicated here: https://github.com/tree-sitter/tree-sitter/issues/1868.
  4. Make a symlink to the gleam.so file in the tree-sitter-gleam directory.

After this, I just restart Emacs (or just revert the buffer), and I have all the goodies from tree-sitter-gleam running just fine.

eppolito avatar Aug 22 '23 04:08 eppolito

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.

slondr avatar May 12 '24 23:05 slondr

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

vvzen avatar Sep 08 '24 03:09 vvzen