From f4eef95fbc4452c83526f7642261136011e651c2 Mon Sep 17 00:00:00 2001 From: Vitalie Spinu Date: Sat, 5 Aug 2017 14:30:44 +0200 Subject: [PATCH 1/4] [Fix #438] Narrow to doc-strings before fill --- CHANGELOG.md | 1 + clojure-mode.el | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb5b86af..7e58874e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/clojure-mode.el b/clojure-mode.el index 6d7cf404..1b7ccf75 100644 --- a/clojure-mode.el +++ b/clojure-mode.el @@ -574,7 +574,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 @@ -584,7 +583,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 From 68f95a6ea392371b77a901c7e18fca2ece9e9f0a Mon Sep 17 00:00:00 2001 From: Vitalie Spinu Date: Sat, 5 Aug 2017 14:34:05 +0200 Subject: [PATCH 2/4] Improve detection of doc-strings --- clojure-mode.el | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/clojure-mode.el b/clojure-mode.el index 1b7ccf75..a3fc0a69 100644 --- a/clojure-mode.el +++ b/clojure-mode.el @@ -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 ? )) From 062bf49dc90e4555313be211c47ecec955ec7826 Mon Sep 17 00:00:00 2001 From: Vitalie Spinu Date: Sat, 5 Aug 2017 14:53:54 +0200 Subject: [PATCH 3/4] Add doc-string tests --- test/clojure-mode-syntax-test.el | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/clojure-mode-syntax-test.el b/test/clojure-mode-syntax-test.el index 4cf5f352..6a7f03bf 100644 --- a/test/clojure-mode-syntax-test.el +++ b/test/clojure-mode-syntax-test.el @@ -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 @@ -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) From a09847d128c539064dc2ded262fc0475149ab81c Mon Sep 17 00:00:00 2001 From: Vitalie Spinu Date: Sat, 5 Aug 2017 18:46:53 +0200 Subject: [PATCH 4/4] Makefile: Don't re-set EMACS if already set --- Makefile | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 7c067067..69ba9531 100644 --- a/Makefile +++ b/Makefile @@ -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