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

Use Emacs 29 built-in Tree-Sitter Support #12

Closed
J3RN opened this issue Feb 12, 2023 · 21 comments
Closed

Use Emacs 29 built-in Tree-Sitter Support #12

J3RN opened this issue Feb 12, 2023 · 21 comments

Comments

@J3RN
Copy link
Member

J3RN commented Feb 12, 2023

Aside: This is most likely going to just be a note to myself.

Emacs 29 introduces first-party support for tree-sitter, so we'd no longer have to rely on a third-party package to provide syntax highlighting. We should transition to this new system when possible.

@etnt
Copy link

etnt commented Mar 3, 2023

So how do I get this tree-sitter requirement to work with Emacs 28.1?

I followed the README which talks about tree-sitter as a requirement…
So Emacs complains about: (file-missing Cannot open load file No such file or directory tree-sitter)
and my question is how I install this tree-sitter requirement?

@hochata
Copy link

hochata commented Mar 4, 2023

@etnt I'm having the same problem with Emacs 30, but I think this should be in a separate issue.

@hochata
Copy link

hochata commented Mar 4, 2023

@etnt It seems you need to manually install the Emacs Treesitter Packages as described here.

I think adding this to the documentation might be a good idea.

@akirak
Copy link

akirak commented Mar 4, 2023

Emacs 29 ships treesit.el, while the current gleam-mode depends on tree-sitter.el. They are slightly different in API, and recent packages supporting the former are named like typescript-ts-mode, elixir-ts-mode, etc. Hence if you wanted to add support for treesit.el to this package, the library would be named gleam-ts-mode.

With an extra effort, it would be possible for this mode to provide the two versions simultaneously, but it is such a burden to the maintainers. I would propose forking this repository to create a new gleam-ts-mode and, as soon as Emacs 29 is officially released, make this gleam-mode enter maintenance mode. In the meantime, the users should migrate to Emacs 29 and use gleam-ts-mode.

@J3RN
Copy link
Member Author

J3RN commented Mar 4, 2023

I couldn't have said it better myself 😁 Though if the immensity of elixir-ts-mode is anything to go by, it'll be a while before a gleam-ts-mode is finished.

@etnt
Copy link

etnt commented Mar 4, 2023

@etnt It seems you need to manually install the Emacs Treesitter Packages as described here.

I think adding this to the documentation might be a good idea.

Thanks, I got it to work with an additional Melpa install of 'tree-sitter-indent as well.

@hochata
Copy link

hochata commented Mar 11, 2023

I couldn't have said it better myself grin Though if the immensity of elixir-ts-mode is anything to go by, it'll be a while before a gleam-ts-mode is finished.

Well, for the Elixir mode, some 200 lines are just faces and constant definitions and some 150 are almost copy pasted from the upstream hightlights.scm queries.

The rest seems quite complex, though...

I guess the only way to check how hard it is would be to try to implement it 😝

@hochata
Copy link

hochata commented Mar 11, 2023

@akirak upstream, some modes were split between a base package, the original regex-based syntax/indentation and the new tree sitter package. For example, you have

  • python-base-mode
  • python-mode (that uses regex for syntax and indentation)
  • python-ts-mode

python-base-mode contains common functionalities. Both python-mode and python-ts-mode inherit from it. Maybe this package could have a similar structure and provide both versions. I don't know.

@akirak
Copy link

akirak commented Mar 11, 2023

python-base-mode contains common functionalities. Both python-mode and python-ts-mode inherit from it. Maybe this package could have a similar structure and provide both versions.

Separating out common functionalities would be nice. The situation is not exactly the same, though. The three Python-related modes are all part of python.el which is part of GNU Emacs. The current gleam-mode depends on tree-sitter.el which users of Emacs 29 wouldn't want to install. gleam-mode and gleam-ts-mode should reside in separate files and submitted separately to MELPA. You can put those files in the same repository, as MELPA recipes support :files spec.

It's just about packaging, so there is not much to worry about. gleam-mode is not on MELPA yet, so there wouldn't be much trouble however you change the structure of this repository.

@J3RN
Copy link
Member Author

J3RN commented Mar 11, 2023

Right. I wasn't sure if (and don't think) MELPA would allow git submodules, so I was waiting for a better solution before pushing to MELPA. gleam-ts-mode will likely be that solution 😁

@hochata
Copy link

hochata commented Mar 12, 2023

@akirak Oh right! I forgot about the dependencies, specially as tree-sitter-indent isn't packaged yet... two separate packages (even in the same repo) looks like the best idea.

@J3RN It seems the sub-module is required for the highlight queries and the tree sitter grammar. For the first one, the queries are usually placed inside the source file, and for the second one, you could copy what elixir-ts-mode does and use treesit-install-language-grammar to install the grammar directly from the repo url. Then I guess the sub-module could be removed.

But I think you could do both things with the current implementation as well. Just move the hightlighs.scm file to the repo source code and maybe copy the treesit-install-language-grammar implementation.

@tsloughter
Copy link

Is the package supposed to work on emacs 29? I'm trying and get the tree-sitter-indent issue:

⛔ Error (use-package): gleam-mode/:catch: Cannot open load file: No such file or directory, tree-sitter-indent

I thought maybe it was supposed to use the submodule and work on 29+?

@chuckberrypi
Copy link

Just checking in to see if anyone has any better idea of when this package will work on emacs 29? Looking forward to learning this language, would really love it if it worked on my daily editor

@J3RN
Copy link
Member Author

J3RN commented Nov 26, 2023

Hello, friends. I've started work on a treesit version of this package, which you can find in the gleam-ts-mode branch here on GitHub. It's not finished, but syntax highlighting is (at least mostly) working. Please try it out and let me know how it goes. PRs are also welcome! 🙏

@chuckberrypi
Copy link

sorry if this is a really basic question, but can you walk me through giving this a spin? I put in (use-package gleam-mode :load-path "<path-to-cloned-repository-with-gleam-ts-mode-checked-out>"), but it doesn't seem to change anything...

@J3RN
Copy link
Member Author

J3RN commented Dec 5, 2023

A few things:

  • For debugging Emacs generally, I'd recommend creating a minimal startup file, say min.el and then starting Emacs with that by running emacs -q --load min.el. It just helps cut down the possibilities of what could be wrong, and a small init file can be pasted into a GitHub comment.
  • There's a typo in your use-package declaration, namely that you want gleam-ts-mode instead of gleam-mode.
  • I'd recommend trying to disable the current gleam-mode in all ways you can before trying gleam-ts-mode. I believe they both lay claim to .gleam files and heaven only knows how Emacs handles that.
  • At some point you'll probably need to run M-x gleam-ts-install-grammar. I forgot to put that in the README originally; I'm going to do that now.

@lephyrus
Copy link

lephyrus commented Dec 28, 2023

I'm generally out of my depth here (new to Gleam, not fluent in elisp whatsoever), but I'm pretty sure gleam-ts-mode.el is currently missing the necessary ;;;###autoload comments. Adding those, I can enable the mode and syntax highlighting seems to work (using Doom, Emacs 29.1).

@it-is-wednesday
Copy link

@J3RN Thank you so much! The new branch works perfectly.

For lurkers, in order to get LSP working I had to modify this file on my local machine and add gleam-ts-mode to :major-modes. On Doom, this file can be found at ~/.config/emacs/.local/straight/repos/lsp-mode/clients/lsp-gleam.el. Then I added this snippet to my personal config.el:

(load! "gleam-ts-mode.el")  ; just copy-pasted 
(add-to-list 'auto-mode-alist '("\\.gleam$" . gleam-ts-mode))
(add-to-list 'lsp-language-id-configuration '(gleam-ts-mode . "gleam"))
(add-hook 'gleam-ts-mode-hook #'lsp!)

Once you're comfortable merging this branch, I'll submit this patch to lsp-mode:) Thanks again!

@bhankas
Copy link

bhankas commented Mar 18, 2024

@it-is-wednesday doom uses straight.el under the hood which obviates the need to copy/paste and load gleam-ts-mode.el manually. Add this to packages.el:

(package! gleam-ts-mode
  :recipe (:type git
           :host github
           :repo "gleam-lang/gleam-mode"
           :branch "gleam-ts-mode"
           :files ("gleam-ts-mode.el")))

After next doom sync this will automatically clone the repo and add it to load path.

My other config is pretty simple too:

(use-package! gleam-ts-mode
  :commands gleam-ts-mode
  :bind (:map gleam-ts-mode-map
              ("C-c g f" . gleam-format))

  :init
  (add-to-list 'auto-mode-alist '("\\.gleam$" . gleam-ts-mode))

  :config
  (add-hook 'gleam-ts-mode-hook #'eglot-ensure 'append))

With 1.0.0 release, perhaps it will be better to publish gleam-ts-mode to MELPA? It should also encourage new contributions as things as not as easy to find ATM.

@J3RN
Copy link
Member Author

J3RN commented Mar 18, 2024

I have just updated the gleam-ts-mode with Imenu and commenting support. The only blocker currently is indentation, which I'll work on next.

@J3RN
Copy link
Member Author

J3RN commented May 30, 2024

I just merged the gleam-ts-mode branch. Please follow the instructions in the README for trying it out, and please open issues with any difficulties you encounter!

@J3RN J3RN closed this as completed May 30, 2024
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

9 participants