Skip to content

Commit

Permalink
Fix for character literal font-lock (#588)
Browse files Browse the repository at this point in the history
When inside a vector or before white space, some escaped characters,
like comma, square and curly brackets, weren't being formatted.

* Added tests.
* Added examples in test.clj for visual inspection.
  • Loading branch information
rvlo authored and bbatsov committed May 4, 2021
1 parent 8280e44 commit e440080
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* [#574](https://github.com/clojure-emacs/clojure-mode/issues/574): Remove `clojure-view-grimoire` command.
* Stop `clojure-sort-ns` from calling `redisplay`.
* [#584](https://github.com/clojure-emacs/clojure-mode/issues/584): Align to recent `pcase` changes on Emacs master.
* [#588](https://github.com/clojure-emacs/clojure-mode/pull/588): Fix font-lock for character literals.

## 5.12.0 (2020-08-13)

Expand Down
20 changes: 10 additions & 10 deletions clojure-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -801,8 +801,6 @@ any number of matches of `clojure--sym-forbidden-rest-chars'."))
"\\(\\sw+\\)?" )
(1 font-lock-keyword-face)
(2 font-lock-function-name-face nil t))
;; lambda arguments - %, %&, %1, %2, etc
("\\<%[&1-9]?" (0 font-lock-variable-name-face))
;; Special forms
(,(concat
"("
Expand Down Expand Up @@ -862,14 +860,16 @@ any number of matches of `clojure--sym-forbidden-rest-chars'."))
"\\>")
0 font-lock-constant-face)
;; Character literals - \1, \a, \newline, \u0000
(,(rx "\\" (or any
"newline" "space" "tab" "formfeed" "backspace"
"return"
(: "u" (= 4 (char "0-9a-fA-F")))
(: "o" (repeat 1 3 (char "0-7"))))
word-boundary)
0 'clojure-character-face)

(,(rx (group "\\" (or any
"newline" "space" "tab" "formfeed" "backspace"
"return"
(: "u" (= 4 (char "0-9a-fA-F")))
(: "o" (repeat 1 3 (char "0-7")))))
(or (not word) word-boundary))
1 'clojure-character-face)
;; lambda arguments - %, %&, %1, %2, etc
;; must come after character literals for \% to be handled properly
("\\<%[&1-9]?" (0 font-lock-variable-name-face))
;; namespace definitions: (ns foo.bar)
(,(concat "(\\<ns\\>[ \r\n\t]*"
;; Possibly metadata, shorthand and/or longhand
Expand Down
2 changes: 1 addition & 1 deletion test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
minnow))

;; character literals
[\a \newline \u0032 \/ \+ \,, \;]
[\a \newline \u0032 \/ \+ \,, \; \( \% \)]

;; TODO change font-face for sexps starting with @,#
(comment ;; examples
Expand Down
16 changes: 16 additions & 0 deletions test/clojure-mode-font-lock-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,22 @@ DESCRIPTION is the description of the spec."
("\\"
(1 2 clojure-character-face)))

(when-fontifying-it "should handle characters not by themselves"
("[\\,,]"
(1 1 nil)
(2 3 clojure-character-face)
(4 5 nil))

("[\\[]"
(1 1 nil)
(2 3 clojure-character-face)
(4 4 nil)))

(when-fontifying-it "should handle % character literal"
("#(str \\% %)"
(7 8 clojure-character-face)
(10 10 font-lock-variable-name-face)))

(when-fontifying-it "should handle referred vars"
("foo/var"
(1 3 font-lock-type-face))
Expand Down

0 comments on commit e440080

Please sign in to comment.