Skip to content

Add support for syntax highlighting for variable interpolation. Fixes #93 #102

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

Merged
merged 2 commits into from
Aug 26, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions elixir-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,33 @@
"For use with atoms & map keys."
:group 'font-lock-faces)

(defun elixir-syntax-propertize-interpolation ()
(let* ((beg (match-beginning 0))
(context (save-excursion (save-match-data (syntax-ppss beg)))))
(put-text-property beg (1+ beg) 'elixir-interpolation
(cons (nth 3 context) (match-data)))))

(defun elixir-syntax-propertize-function (start end)
(let ((case-fold-search nil))
(goto-char start)
(remove-text-properties start end '(elixir-interpolation))
(funcall
(syntax-propertize-rules
((rx (group "#{" (0+ (not (any "}"))) "}"))
(0 (ignore (elixir-syntax-propertize-interpolation)))))
start end)))

(defun elixir-match-interpolation (limit)
(let ((pos (next-single-char-property-change (point) 'elixir-interpolation
nil limit)))
(when (and pos (> pos (point)))
(goto-char pos)
(let ((value (get-text-property pos 'elixir-interpolation)))
(if (eq (car value) ?\")
(progn
(set-match-data (cdr value))
t)
(elixir-match-interpolation limit))))))

(eval-when-compile
(defconst elixir-rx-constituents
Expand Down Expand Up @@ -353,8 +380,11 @@
(t
(rx-to-string (car sexps) t))))))

(defconst elixir-mode-font-lock-defaults
(defconst elixir-font-lock-keywords
`(
;; String interpolation
(elixir-match-interpolation 0 font-lock-variable-name-face t)

;; Module-defining & namespace builtins
(,(elixir-rx (or builtin-declaration builtin-namespace)
space
Expand Down Expand Up @@ -593,13 +623,17 @@ Argument END End of the region."
"Major mode for editing Elixir code.

\\{elixir-mode-map}"
(set (make-local-variable 'font-lock-defaults) '(elixir-mode-font-lock-defaults))
(set (make-local-variable 'font-lock-defaults)
'(elixir-font-lock-keywords))
(set (make-local-variable 'comment-start) "# ")
(set (make-local-variable 'comment-end) "")
(set (make-local-variable 'comment-use-syntax) t)
(set (make-local-variable 'tab-width) elixir-basic-offset)
(set (make-local-variable 'imenu-generic-expression) elixir-imenu-generic-expression)
(smie-setup elixir-smie-grammar 'verbose-elixir-smie-rules ; 'elixir-smie-rules
(set (make-local-variable 'syntax-propertize-function)
#'elixir-syntax-propertize-function)
(set (make-local-variable 'imenu-generic-expression)
elixir-imenu-generic-expression)
(smie-setup elixir-smie-grammar 'verbose-elixir-smie-rules
:forward-token 'elixir-smie-forward-token
:backward-token 'elixir-smie-backward-token))

Expand Down
9 changes: 9 additions & 0 deletions test/elixir-mode-font-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,12 @@ end"
(should (eq (elixir-test-face-at 4) 'elixir-atom-face))
(should (eq (elixir-test-face-at 9) 'elixir-atom-face))
(should (eq (elixir-test-face-at 10) 'elixir-atom-face))))

(ert-deftest elixir-mode-syntax-table/fontify-interpolation ()
:tags '(fontification interpolation syntax-table)
(elixir-test-with-temp-buffer
"\"#{1 + 2} is 3.\""
(should (eq (elixir-test-face-at 1) 'font-lock-string-face))
(should (eq (elixir-test-face-at 3) 'font-lock-variable-name-face))
(should (eq (elixir-test-face-at 11) 'font-lock-string-face))))