Skip to content

Fix #438 and better detection of doc-strings. #442

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 4 commits into from
Aug 20, 2017
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 @@ -4,6 +4,7 @@

### Bugs fixed

* [#438](https://github.com/clojure-emacs/clojure-mode/issues/438): Filling within a doc-string doesn't affect surrounding code.
* Fix fill-paragraph in multi-line comments.
* [#429](https://github.com/clojure-emacs/clojure-mode/issues/429): Fix a bug causing last occurrence of expression sometimes is not replaced when using `move-to-let`.
* [#423](https://github.com/clojure-emacs/clojure-mode/issues/423): Make `clojure-match-next-def` more robust against zero-arity def-like forms.
Expand Down
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
CASK = cask
EMACS = emacs
export EMACS ?= emacs
EMACSFLAGS =
TESTFLAGS =

export EMACS

PKGDIR := $(shell EMACS=$(EMACS) $(CASK) package-directory)

SRCS = clojure-mode.el
Expand Down
19 changes: 15 additions & 4 deletions clojure-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -558,11 +558,15 @@ This could cause problems.

(defsubst clojure-in-docstring-p ()
"Check whether point is in a docstring."
(eq (get-text-property (point) 'face) 'font-lock-doc-face))
(let ((ppss (syntax-ppss)))
;; are we in a string?
(when (nth 3 ppss)
;; check font lock at the start of the string
(eq (get-text-property (nth 8 ppss) 'face)
'font-lock-doc-face))))

(defsubst clojure-docstring-fill-prefix ()
"The prefix string used by `clojure-fill-paragraph'.

It is simply `clojure-docstring-fill-prefix-width' number of spaces."
(make-string clojure-docstring-fill-prefix-width ? ))

Expand All @@ -574,7 +578,6 @@ This only takes care of filling docstring correctly."

(defun clojure-fill-paragraph (&optional justify)
"Like `fill-paragraph', but can handle Clojure docstrings.

If JUSTIFY is non-nil, justify as well as fill the paragraph."
(if (clojure-in-docstring-p)
(let ((paragraph-start
Expand All @@ -584,7 +587,15 @@ If JUSTIFY is non-nil, justify as well as fill the paragraph."
(concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
(fill-column (or clojure-docstring-fill-column fill-column))
(fill-prefix (clojure-docstring-fill-prefix)))
(fill-paragraph justify))
;; we are in a string and string start pos (8th element) is non-nil
(let* ((beg-doc (nth 8 (syntax-ppss)))
(end-doc (save-excursion
(goto-char beg-doc)
(or (ignore-errors (forward-sexp) (point))
(point-max)))))
(save-restriction
(narrow-to-region beg-doc end-doc)
(fill-paragraph justify))))
(let ((paragraph-start (concat paragraph-start
"\\|\\s-*\\([(:\"[]\\|`(\\|#'(\\)"))
(paragraph-separate
Expand Down
29 changes: 29 additions & 0 deletions test/clojure-mode-syntax-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
(insert (car form))
(equal (symbol-name (symbol-at-point)) (cdr form)))))


(ert-deftest clojure-syntax-skip-prefixes ()
(dolist (form '("#?@aaa" "#?aaa" "#aaa" "'aaa"))
(with-temp-buffer
Expand Down Expand Up @@ -105,4 +106,32 @@
(let ((fill-column 80))
(fill-paragraph)))

(when (fboundp 'font-lock-ensure)
(def-refactor-test test-paragraph-fill-not-altering-surrounding-code
"(def my-example-variable
\"It has a very long docstring. So long, in fact, that it wraps onto multiple lines! This is to demonstrate what happens when the docstring wraps over three lines.\"
nil)"
"(def my-example-variable
\"It has a very long docstring. So long, in fact, that it wraps onto multiple
lines! This is to demonstrate what happens when the docstring wraps over three
lines.\"
nil)"
(font-lock-ensure)
(goto-char 40)
(let ((clojure-docstring-fill-column 80)
(fill-column 80))
(fill-paragraph)))

(ert-deftest test-clojure-in-docstring-p ()
(with-temp-buffer
(insert "(def my-example-variable
\"Doc here and `doc-here`\"
nil)")
(clojure-mode)
(font-lock-ensure)
(goto-char 32)
(should (clojure-in-docstring-p))
(goto-char 46)
(should (clojure-in-docstring-p)))))

(provide 'clojure-mode-syntax-test)