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

WIP: Add treesit font-lock support #495

Closed
wants to merge 11 commits into from
43 changes: 37 additions & 6 deletions elixir-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@
"Hook that runs when switching to major mode"
:type 'hook)

(defcustom elixir-use-tree-sitter nil
"If non-nil, `elixir-mode' tries to use tree-sitter.
Currently `elixir-mode' uses tree-sitter for font-locking, imenu,
and movement functions."
:type 'boolean
:version "29.1")

(defvar elixir-mode-map
(let ((map (make-sparse-keymap)))
map)
Expand Down Expand Up @@ -562,21 +569,42 @@ just return nil."
["Elixir homepage" elixir-mode-open-elixir-home]
["About" elixir-mode-version]))

(defun elixir--treesit-setup ()
"Ensure tree-sitter can be used."
(progn
(require 'elixir-tree-sitter)

(setq-local treesit-mode-supported t)
(setq-local treesit-required-languages '(elixir))
(setq-local treesit-font-lock-settings elixir--treesit-font-lock-settings)
(setq-local treesit-font-lock-feature-list
'(( comment string )
( keyword unary-operator operator doc)
( call constant )
( sigil string-escape)
( string-interpolation )))

(setq-local treesit-imenu-function #'elixir--imenu-treesit-create-index)
(cond
((treesit-ready-p 'elixir)
(treesit-major-mode-setup))
(t
(message "Tree-sitter for Elixir isn't available")))))

;;;###autoload
(define-derived-mode elixir-mode prog-mode "Elixir"
:group 'elixir
"Major mode for editing Elixir code.

\\{elixir-mode-map}"
(setq-local font-lock-defaults
'(elixir-font-lock-keywords
nil nil nil nil
(font-lock-syntactic-face-function
. elixir-font-lock-syntactic-face-function)))

(setq-local comment-start "# ")
(setq-local comment-end "")
(setq-local comment-start-skip "#+ *")
(setq-local comment-use-syntax t)

(setq-local syntax-propertize-function #'elixir-syntax-propertize-function)

(setq-local imenu-generic-expression elixir-imenu-generic-expression)

(setq-local beginning-of-defun-function #'elixir-beginning-of-defun)
Expand All @@ -587,7 +615,10 @@ just return nil."
:backward-token 'elixir-smie-backward-token)
;; https://github.com/elixir-editors/emacs-elixir/issues/363
;; http://debbugs.gnu.org/cgi/bugreport.cgi?bug=35496
(setq-local smie-blink-matching-inners nil))
(setq-local smie-blink-matching-inners nil)

(if (<= 29 emacs-major-version)
(elixir--treesit-setup)))

;; Invoke elixir-mode when appropriate

Expand Down
Loading