Skip to content

Commit 6ddde66

Browse files
committed
Merge pull request #266 from syohex/fix-quotes-in-sigils
Fix quotes in sigils
2 parents aee2b9b + 4356c5f commit 6ddde66

File tree

2 files changed

+75
-6
lines changed

2 files changed

+75
-6
lines changed

elixir-mode.el

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,30 @@ is used to limit the scan."
260260
(put-text-property beg (1+ beg) 'elixir-interpolation
261261
(cons (nth 3 context) (match-data)))))
262262

263-
(defconst elixir-syntax-propertize-function
264-
(syntax-propertize-rules
265-
((elixir-rx string-delimiter)
266-
(0 (ignore (elixir-syntax-stringify))))
267-
((rx (group "#{" (0+ (not (any "}"))) "}"))
268-
(0 (ignore (elixir-syntax-propertize-interpolation))))))
263+
(defconst elixir-sigil-delimiter-pair
264+
'((?\( . ")")
265+
(?\{ . "}")
266+
(?\< . ">")
267+
(?\[ . "]")))
268+
269+
(defun elixir-syntax-replace-property-in-sigil ()
270+
(let ((heredoc-p (save-excursion
271+
(goto-char (match-beginning 0))
272+
(looking-at-p "~s\"\"\""))))
273+
(unless heredoc-p
274+
(forward-char 1)
275+
(let* ((start-delim (char-after (1- (point))))
276+
(end-delim (or (assoc-default start-delim elixir-sigil-delimiter-pair)
277+
(char-to-string start-delim)))
278+
(end (save-excursion
279+
(skip-chars-forward (concat "^" end-delim))
280+
(point)))
281+
(word-syntax (string-to-syntax "w")))
282+
(when (memq start-delim '(?' ?\"))
283+
(setq end (1+ end))
284+
(forward-char -1))
285+
(while (re-search-forward "[\"']" end t)
286+
(put-text-property (1- (point)) (point) 'syntax-table word-syntax))))))
269287

270288
(defun elixir-syntax-propertize-function (start end)
271289
(let ((case-fold-search nil))
@@ -274,6 +292,8 @@ is used to limit the scan."
274292
(syntax-propertize-rules
275293
((elixir-rx string-delimiter)
276294
(0 (ignore (elixir-syntax-stringify))))
295+
((elixir-rx sigils)
296+
(0 (ignore (elixir-syntax-replace-property-in-sigil))))
277297
((rx (group "#{" (0+ (not (any "}"))) "}"))
278298
(0 (ignore (elixir-syntax-propertize-interpolation)))))
279299
start end)))

test/elixir-mode-font-test.el

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,55 @@ when"
299299
(should-not (eq (elixir-test-face-at 17) 'font-lock-comment-face))
300300
(should-not (eq (elixir-test-face-at 25) 'font-lock-comment-face))))
301301

302+
(ert-deftest elixir-mode-syntax-table/quotes-in-sigils ()
303+
"https://github.com/elixir-lang/emacs-elixir/issues/265"
304+
:tags '(fontification syntax-table)
305+
(elixir-test-with-temp-buffer
306+
"~s/\"/
307+
~r|'|
308+
~c\"'\"
309+
~w'\"'
310+
~s(\")
311+
~r[\"]
312+
~c{\"}
313+
~w<\">
314+
~s\"\"\"
315+
foo
316+
\"\"\""
317+
(should-not (eq (elixir-test-face-at 5) 'font-lock-string-face)) ; ~s//
318+
319+
(should-not (eq (elixir-test-face-at 7) 'font-lock-string-face)) ; ~r||
320+
(should (eq (elixir-test-face-at 7) 'font-lock-builtin-face))
321+
(should-not (eq (elixir-test-face-at 11) 'font-lock-string-face))
322+
323+
(should-not (eq (elixir-test-face-at 13) 'font-lock-string-face)) ; ~c""
324+
(should (eq (elixir-test-face-at 13) 'font-lock-builtin-face))
325+
(should-not (eq (elixir-test-face-at 17) 'font-lock-string-face))
326+
327+
(should-not (eq (elixir-test-face-at 19) 'font-lock-string-face)) ; ~w''
328+
(should (eq (elixir-test-face-at 19) 'font-lock-builtin-face))
329+
(should-not (eq (elixir-test-face-at 23) 'font-lock-string-face))
330+
331+
(should-not (eq (elixir-test-face-at 25) 'font-lock-string-face)) ; ~s()
332+
(should-not (eq (elixir-test-face-at 29) 'font-lock-string-face))
333+
334+
(should-not (eq (elixir-test-face-at 31) 'font-lock-string-face)) ; ~r[]
335+
(should (eq (elixir-test-face-at 31) 'font-lock-builtin-face))
336+
(should-not (eq (elixir-test-face-at 35) 'font-lock-string-face))
337+
338+
(should-not (eq (elixir-test-face-at 37) 'font-lock-string-face)) ; ~c{}
339+
(should (eq (elixir-test-face-at 37) 'font-lock-builtin-face))
340+
(should-not (eq (elixir-test-face-at 41) 'font-lock-string-face))
341+
342+
(should-not (eq (elixir-test-face-at 43) 'font-lock-string-face)) ; ~w<>
343+
(should (eq (elixir-test-face-at 43) 'font-lock-builtin-face))
344+
(should-not (eq (elixir-test-face-at 47) 'font-lock-string-face))
345+
346+
(should (eq (elixir-test-face-at 51) 'font-lock-string-face)) ; ~s""" """
347+
(should (eq (elixir-test-face-at 52) 'font-lock-string-face))
348+
(should (eq (elixir-test-face-at 53) 'font-lock-string-face))
349+
(should (eq (elixir-test-face-at 55) 'font-lock-string-face))))
350+
302351
(provide 'elixir-mode-font-test)
303352

304353
;;; elixir-mode-font-test.el ends here

0 commit comments

Comments
 (0)