Skip to content

[Fix #445] def form with strings and docstrings #505

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 1 commit into from
Feb 27, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bugs fixed

* Dynamic vars whose names contain non-alphanumeric characters are now font-locked correctly.
* [#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.

## 5.10.0 (2019-01-05)

Expand Down
8 changes: 7 additions & 1 deletion clojure-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,13 @@ highlighted region)."
(setq docelt (1- docelt)))))
(and (zerop docelt) (<= (point) startpos)
(progn (forward-comment (point-max)) t)
(= (point) (nth 8 state)))))
(= (point) (nth 8 state))))
;; In a def, at last position is not a docstring
(not (and (string= "def" firstsym)
(save-excursion
(goto-char startpos)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary given the goto-char below?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first goto-char looks redundant but I think it is not, because there is a (sexp-at-point) below, and it needs the point at the correct position beforehand.

Also, the (sexp-at-point) form does not take any arguments so I cannot pass startpos to it.

Anyway, I tried without it and it didn't work. Happy to remove it if we find the way to make it work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, great point. I didn't think of that! Yeah, we definitely need it because of the (sexp-at-point).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go ahead and squash if you like. I will merge once I have the rights to do so.

(goto-char (+ startpos (length (sexp-at-point)) 2))
(looking-at "[ \r\n\t]*\)")))))
font-lock-doc-face
font-lock-string-face))))
font-lock-comment-face))
Expand Down
28 changes: 28 additions & 0 deletions test/clojure-mode-font-lock-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,34 @@ POS."
(should (eq (clojure-test-face-at 6 8 "(def foo 10)")
'font-lock-variable-name-face)))

(ert-deftest clojure-mode-syntax-table/variable-def-string ()
:tags '(fontification syntax-table)
(should (eq (clojure-test-face-at 10 16 "(def foo \"hello\")")
'font-lock-string-face))
(should (eq (clojure-test-face-at 10 16 "(def foo \"hello\" )")
'font-lock-string-face))
(should (eq (clojure-test-face-at 13 19 "(def foo \n \"hello\")")
'font-lock-string-face))
(should (eq (clojure-test-face-at 13 19 "(def foo \n \"hello\"\n)")
'font-lock-string-face)))

(ert-deftest clojure-mode-syntax-table/variable-def-string-with-docstring ()
:tags '(fontification syntax-table)
(should (eq (clojure-test-face-at 10 16 "(def foo \"usage\" \"hello\")")
'font-lock-doc-face))
(should (eq (clojure-test-face-at 18 24 "(def foo \"usage\" \"hello\")")
'font-lock-string-face))
(should (eq (clojure-test-face-at 18 24 "(def foo \"usage\" \"hello\" )")
'font-lock-string-face))
(should (eq (clojure-test-face-at 21 27 "(def foo \"usage\" \n \"hello\")")
'font-lock-string-face))
(should (eq (clojure-test-face-at 13 19 "(def foo \n \"usage\" \"hello\")")
'font-lock-doc-face))
(should (eq (clojure-test-face-at 13 19 "(def foo \n \"usage\" \n \"hello\")")
'font-lock-doc-face))
(should (eq (clojure-test-face-at 24 30 "(def foo \n \"usage\" \n \"hello\")")
'font-lock-string-face)))

(ert-deftest clojure-mode-syntax-table/type-def ()
:tags '(fontification syntax-table)
(clojure-test-with-temp-buffer "(deftype Foo)"
Expand Down
5 changes: 5 additions & 0 deletions test/clojure-mode-indentation-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ values of customisable variables."
(search-forward "|")
(delete-char -1)
(clojure-mode)
(font-lock-ensure)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can this affect an indentation test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is because the indentation command behaves differently depending on whether font lock mode is ON or OFF, in the case of docstrings.

This branch is master with the test added: https://github.com/carlosgeos/clojure-mode/tree/temp-branch. The tests are fine, but if (font-lock-ensure) is added, the test with the docstring fails (like it should).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that’s odd. Turning on the mode should enable the font locking.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually yes, but apparently not when using temp-buffer:

https://stackoverflow.com/a/23569337
https://emacs.stackexchange.com/a/44301

(indent-according-to-mode)

(should (equal expected-state (buffer-string)))
Expand Down Expand Up @@ -118,6 +119,10 @@ values of customisable variables."
(->>
|expr)")

(check-indentation no-indent-for-def-string
"(def foo \"hello|\")"
"(def foo \"hello|\")")

(check-indentation doc-strings-without-indent-specified
"
(defn some-fn
Expand Down