gleam-lang / gleam-mode

🐙 Gleam support for Emacs
Apache License 2.0
81 stars 23 forks source link

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

Closed J3RN closed 6 months ago

J3RN commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

hochata commented 1 year ago

@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 commented 1 year ago

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 commented 1 year ago

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.

etnt commented 1 year ago

@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 commented 1 year ago

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 :stuck_out_tongue_closed_eyes:

hochata commented 1 year ago

@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 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 commented 1 year ago

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 commented 1 year ago

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 :grin:

hochata commented 1 year ago

@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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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! :pray:

chuckberrypi commented 12 months ago

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 commented 12 months ago

A few things:

lephyrus commented 11 months ago

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 commented 8 months ago

@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 commented 8 months ago

@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 commented 8 months ago

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 commented 6 months ago

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!