Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No syntax highlighting with gleam-mode #10

Open
vbmithr opened this issue May 25, 2022 · 16 comments
Open

No syntax highlighting with gleam-mode #10

vbmithr opened this issue May 25, 2022 · 16 comments

Comments

@vbmithr
Copy link

vbmithr commented May 25, 2022

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.

@lpil
Copy link
Member

lpil commented Jun 7, 2022

@gleam-lang/emacs-team Any ideas?

@J3RN
Copy link
Member

J3RN commented Jun 8, 2022

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.

@vbmithr
Copy link
Author

vbmithr commented Jun 23, 2022

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
Copy link
Author

vbmithr commented Jun 23, 2022

Also, highighting works with tree-sitter cmdline program.

@Bhanukamax
Copy link

Bhanukamax commented Jun 25, 2022

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.

@J3RN
Copy link
Member

J3RN commented Jun 25, 2022

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
Copy link
Member

J3RN commented Jun 27, 2022

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
Copy link
Member

J3RN commented Jun 27, 2022

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.

@Bhanukamax
Copy link

Bhanukamax commented Jun 27, 2022

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 🙂. Thanks a lot @J3RN for looking into this.

@vbmithr
Copy link
Author

vbmithr commented Jul 31, 2022

I can confirm it works with whitespace-mode indeed.

@quarkw
Copy link

quarkw commented Feb 17, 2023

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.

@J3RN
Copy link
Member

J3RN commented Mar 10, 2023

@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
Copy link

quarkw commented Mar 10, 2023

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

@eppolito
Copy link

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: how to generate .so file for finished parser tree-sitter/tree-sitter#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.

@slondr
Copy link

slondr commented May 12, 2024

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.

@vvzen
Copy link

vvzen commented Sep 8, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants