Skip to content

Commit a9a0276

Browse files
author
Carlos Requena López
committed
[Fix #445] More accurate font locking for strings in def forms
- def forms can now have docstrings and strings properly font-locked - they will not be incorrectly indented added tests as well
1 parent 7943b29 commit a9a0276

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Bugs fixed
66

77
* Dynamic vars whose names contain non-alphanumeric characters are now font-locked correctly.
8+
* [#445](https://github.com/clojure-emacs/clojure-mode/issues/445), [#405](https://github.com/clojure-emacs/clojure-mode/issues/405), [#469](https://github.com/clojure-emacs/clojure-mode/issues/469): Correct font-lock on string definitions with docstrings, e.g: `(def foo "doc" "value")`. Correct indentation as well.
89

910
## 5.10.0 (2019-01-05)
1011

clojure-mode.el

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,13 @@ highlighted region)."
955955
(setq docelt (1- docelt)))))
956956
(and (zerop docelt) (<= (point) startpos)
957957
(progn (forward-comment (point-max)) t)
958-
(= (point) (nth 8 state)))))
958+
(= (point) (nth 8 state))))
959+
;; In a def, at last position is not a docstring
960+
(not (and (string= "def" firstsym)
961+
(save-excursion
962+
(goto-char startpos)
963+
(goto-char (+ startpos (length (sexp-at-point)) 2))
964+
(looking-at "[ \r\n\t]*\)")))))
959965
font-lock-doc-face
960966
font-lock-string-face))))
961967
font-lock-comment-face))

test/clojure-mode-font-lock-test.el

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,34 @@ POS."
718718
(should (eq (clojure-test-face-at 6 8 "(def foo 10)")
719719
'font-lock-variable-name-face)))
720720

721+
(ert-deftest clojure-mode-syntax-table/variable-def-string ()
722+
:tags '(fontification syntax-table)
723+
(should (eq (clojure-test-face-at 10 16 "(def foo \"hello\")")
724+
'font-lock-string-face))
725+
(should (eq (clojure-test-face-at 10 16 "(def foo \"hello\" )")
726+
'font-lock-string-face))
727+
(should (eq (clojure-test-face-at 13 19 "(def foo \n \"hello\")")
728+
'font-lock-string-face))
729+
(should (eq (clojure-test-face-at 13 19 "(def foo \n \"hello\"\n)")
730+
'font-lock-string-face)))
731+
732+
(ert-deftest clojure-mode-syntax-table/variable-def-string-with-docstring ()
733+
:tags '(fontification syntax-table)
734+
(should (eq (clojure-test-face-at 10 16 "(def foo \"usage\" \"hello\")")
735+
'font-lock-doc-face))
736+
(should (eq (clojure-test-face-at 18 24 "(def foo \"usage\" \"hello\")")
737+
'font-lock-string-face))
738+
(should (eq (clojure-test-face-at 18 24 "(def foo \"usage\" \"hello\" )")
739+
'font-lock-string-face))
740+
(should (eq (clojure-test-face-at 21 27 "(def foo \"usage\" \n \"hello\")")
741+
'font-lock-string-face))
742+
(should (eq (clojure-test-face-at 13 19 "(def foo \n \"usage\" \"hello\")")
743+
'font-lock-doc-face))
744+
(should (eq (clojure-test-face-at 13 19 "(def foo \n \"usage\" \n \"hello\")")
745+
'font-lock-doc-face))
746+
(should (eq (clojure-test-face-at 24 30 "(def foo \n \"usage\" \n \"hello\")")
747+
'font-lock-string-face)))
748+
721749
(ert-deftest clojure-mode-syntax-table/type-def ()
722750
:tags '(fontification syntax-table)
723751
(clojure-test-with-temp-buffer "(deftype Foo)"

test/clojure-mode-indentation-test.el

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ values of customisable variables."
6868
(search-forward "|")
6969
(delete-char -1)
7070
(clojure-mode)
71+
(font-lock-ensure)
7172
(indent-according-to-mode)
7273

7374
(should (equal expected-state (buffer-string)))
@@ -118,6 +119,10 @@ values of customisable variables."
118119
(->>
119120
|expr)")
120121

122+
(check-indentation no-indent-for-def-string
123+
"(def foo \"hello|\")"
124+
"(def foo \"hello|\")")
125+
121126
(check-indentation doc-strings-without-indent-specified
122127
"
123128
(defn some-fn

0 commit comments

Comments
 (0)