commit 986151b157b8d88328eba5b638252525352e35db
parent 38dacc2f28a09e3d68a5f85e03439892285b0959
Author: Wilfred Hughes <me@wilfred.me.uk>
Date: Sat, 28 Apr 2018 14:10:13 +0100
Ensure we don't linkify escaped backticks
Fixes #93
Diffstat:
3 files changed, 17 insertions(+), 2 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
@@ -5,6 +5,9 @@ URLs in docstrings are now converted to buttons.
Function signatures are now correct when a function uses
`(declare (advertised-calling-convention ...))`.
+Fixed an issue where escaped backticks were confused with unescaped
+backticks in docstrings.
+
# v0.9
Much better handling of aliases: show aliases differently to their
diff --git a/helpful.el b/helpful.el
@@ -609,12 +609,15 @@ blank line afterwards."
"Convert `foo' in docstrings to buttons (if bound) or else highlight."
(replace-regexp-in-string
;; Replace all text of the form `foo'.
- (rx "`" (+? (not (any "`" "'"))) "'")
+ (rx (? "\\=") "`" (+? (not (any "`" "'"))) "'")
(lambda (it)
(let* ((sym-name
(s-chop-prefix "`" (s-chop-suffix "'" it)))
(sym (intern sym-name)))
(cond
+ ;; If the quote is escaped, don't modify it.
+ ((s-starts-with-p "\\=" it)
+ it)
;; Only create a link if this is a symbol that is bound as a
;; variable or callable.
((or (boundp sym) (fboundp sym))
@@ -831,13 +834,15 @@ unescaping too."
(defun helpful--format-docstring (docstring)
"Replace cross-references with links in DOCSTRING."
(-> docstring
- (helpful--format-command-keys)
(helpful--split-first-line)
(helpful--propertize-info)
(helpful--propertize-links)
(helpful--propertize-bare-links)
(helpful--propertize-keywords)
(helpful--propertize-quoted)
+ ;; This needs to happen after we've replaced quoted chars, so we
+ ;; don't confuse \\=` with `.
+ (helpful--format-command-keys)
(s-trim)))
(define-button-type 'helpful-link-button
diff --git a/test/unit-test.el b/test/unit-test.el
@@ -182,6 +182,13 @@ symbol (not a form)."
(let* ((formatted (helpful--format-docstring "foo `C-M-\\'")))
(should (equal formatted "foo C-M-\\"))))
+(ert-deftest helpful--format-docstring-escapes ()
+ "Ensure we handle escaped quotes correctly."
+ (let* ((formatted (helpful--format-docstring "foo \\=`message\\='."))
+ (m-position (s-index-of "m" formatted)))
+ (should (equal formatted "foo `message'."))
+ (should (not (get-text-property m-position 'button formatted)))))
+
(ert-deftest helpful--format-docstring-command-keys ()
"Ensure we propertize references to command key sequences."
;; This test will fail in your current Emacs instance if you've