Skip to content

Integrating tree-sitter-mode directly into a new major-mode #70

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

Closed
silvanshade opened this issue Nov 10, 2020 · 2 comments
Closed

Integrating tree-sitter-mode directly into a new major-mode #70

silvanshade opened this issue Nov 10, 2020 · 2 comments
Labels
documentation Improvements or additions to documentation question Not a bug report or feature request

Comments

@silvanshade
Copy link

I'm writing a new major mode for WebAssembly text formats and would like to use the WebAssembly tree-sitter grammar (here) for highlighting.

Since this is a new mode being written from scratch, it sounds like it might be better to integrate support for the tree-sitter mode directly into this new major mode, rather than including the queries and other definitions in this repository.

However, I'm not quite sure what I need to do in order to build the tree-sitter functionality in this new mode. The documentation doesn't seem to have much to say about that.

Is it currently feasible to include support directly into a new mode? If so, can you give a description for how I need to set things up for that?

@shackra shackra added the question Not a bug report or feature request label Nov 11, 2020
@ubolonton ubolonton added the documentation Improvements or additions to documentation label Nov 11, 2020
@ubolonton
Copy link
Collaborator

ubolonton commented Nov 11, 2020

Since this is a new mode being written from scratch, it sounds like it might be better to integrate support for the tree-sitter mode directly into this new major mode, rather than including the queries and other definitions in this repository.

Yes, definitely!

Is it currently feasible to include support directly into a new mode? If so, can you give a description for how I need to set things up for that?

A basic major mode would look like this:

(require 'tree-sitter)
(require 'tree-sitter-hl)

;;;###autoload
(define-derived-mode wat-mode prog-mode "WAT"
  ;; It's up to the major mode to set this. It plays a role similar to that of
  ;; `font-lock-defaults'.
  (setq tree-sitter-hl-default-patterns
        [["module" "func" "param"] @keyword
         (module_field_func (identifier) @function)])
  (tree-sitter-hl-mode))

;;;###autoload
(progn
  ;; There are 3 ways to register/load the compiled grammar.
  ;;
  ;; 1. `wat-mode' distributes the compiled grammar files on its own:
  ;;
  ;; (tree-sitter-load 'wat PATH-TO-COMPILED-WAT-GRAMMAR)
  ;;
  ;; 2. Same as above, but with lazy loading:
  ;;
  ;; (add-to-list 'tree-sitter-load-path PATH-TO-DIR-OF-COMPILED-WAT-GRAMMAR)
  ;;
  ;; 3. `tree-sitter-langs' distributes the compiled grammar (not the queries):
  ;; Do nothing.

  ;; Register the association with `tree-sitter-mode'.
  (add-to-list 'tree-sitter-major-mode-language-alist '(wat-mode . wat))
  (add-to-list 'auto-mode-alist '("\\.wat\\'" . wat-mode)))

(provide 'wat-mode)
;;; wat-mode.el ends here

Compiling, packaging, and distributing grammars (for all major platforms) would be a hassle though, so you may want to rely on tree-sitter-langs to distribute the grammars (without the queries) for a while, until there is better tooling.

@silvanshade
Copy link
Author

Thanks for the info!

I think that clarifies things enough for me to get started. The suggestion to use tree-sitter-langs to distribute the grammars seems reasonable given the current status of things. I was wondering what to do about that.

I'll go ahead and make a PR to add the WebAssembly grammars shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Not a bug report or feature request
Projects
None yet
Development

No branches or pull requests

3 participants