Skip to content

Commit 9120fb4

Browse files
committed
Add syntax propertization for string interpolation
1 parent a153981 commit 9120fb4

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

elixir-mode.el

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,33 @@
243243
"For use with atoms & map keys."
244244
:group 'font-lock-faces)
245245

246+
(defun elixir-syntax-propertize-interpolation ()
247+
(let* ((beg (match-beginning 0))
248+
(context (save-excursion (save-match-data (syntax-ppss beg)))))
249+
(put-text-property beg (1+ beg) 'elixir-interpolation
250+
(cons (nth 3 context) (match-data)))))
251+
252+
(defun elixir-syntax-propertize-function (start end)
253+
(let ((case-fold-search nil))
254+
(goto-char start)
255+
(remove-text-properties start end '(elixir-interpolation))
256+
(funcall
257+
(syntax-propertize-rules
258+
((rx (group "#{" (0+ (not (any "}"))) "}"))
259+
(0 (ignore (elixir-syntax-propertize-interpolation)))))
260+
start end)))
261+
262+
(defun elixir-match-interpolation (limit)
263+
(let ((pos (next-single-char-property-change (point) 'elixir-interpolation
264+
nil limit)))
265+
(when (and pos (> pos (point)))
266+
(goto-char pos)
267+
(let ((value (get-text-property pos 'elixir-interpolation)))
268+
(if (eq (car value) ?\")
269+
(progn
270+
(set-match-data (cdr value))
271+
t)
272+
(elixir-match-interpolation limit))))))
246273

247274
(eval-when-compile
248275
(defconst elixir-rx-constituents
@@ -353,8 +380,11 @@
353380
(t
354381
(rx-to-string (car sexps) t))))))
355382

356-
(defconst elixir-mode-font-lock-defaults
383+
(defconst elixir-font-lock-keywords
357384
`(
385+
;; String interpolation
386+
(elixir-match-interpolation 0 font-lock-variable-name-face t)
387+
358388
;; Module-defining & namespace builtins
359389
(,(elixir-rx (or builtin-declaration builtin-namespace)
360390
space
@@ -593,13 +623,17 @@ Argument END End of the region."
593623
"Major mode for editing Elixir code.
594624

595625
\\{elixir-mode-map}"
596-
(set (make-local-variable 'font-lock-defaults) '(elixir-mode-font-lock-defaults))
626+
(set (make-local-variable 'font-lock-defaults)
627+
'(elixir-font-lock-keywords))
597628
(set (make-local-variable 'comment-start) "# ")
598629
(set (make-local-variable 'comment-end) "")
599630
(set (make-local-variable 'comment-use-syntax) t)
600631
(set (make-local-variable 'tab-width) elixir-basic-offset)
601-
(set (make-local-variable 'imenu-generic-expression) elixir-imenu-generic-expression)
602-
(smie-setup elixir-smie-grammar 'verbose-elixir-smie-rules ; 'elixir-smie-rules
632+
(set (make-local-variable 'syntax-propertize-function)
633+
#'elixir-syntax-propertize-function)
634+
(set (make-local-variable 'imenu-generic-expression)
635+
elixir-imenu-generic-expression)
636+
(smie-setup elixir-smie-grammar 'verbose-elixir-smie-rules
603637
:forward-token 'elixir-smie-forward-token
604638
:backward-token 'elixir-smie-backward-token))
605639

0 commit comments

Comments
 (0)