diff options
| author | Wilfred Hughes <me@wilfred.me.uk> | 2019-03-16 16:56:36 +0000 |
|---|---|---|
| committer | Wilfred Hughes <me@wilfred.me.uk> | 2019-03-16 16:56:36 +0000 |
| commit | 0a83a7b028881a7a463ba5dfc7646ca98afc48c7 (patch) | |
| tree | 1ff13a3d3ee7b9255da8cbfa3040f6e9b327d722 | |
| parent | ade085ac805845f70d5b46acca552071f42782cb (diff) | |
Ensure backslashes in string literals are escaped
Fixes #197
| -rw-r--r-- | CHANGELOG.md | 3 | ||||
| -rw-r--r-- | helpful.el | 18 | ||||
| -rw-r--r-- | test/helpful-unit-test.el | 14 |
3 files changed, 27 insertions, 8 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 77d9da0..26236f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ on their own line. Symbols of the form `foo-functions` (e.g. `after-change-functions`) are now rendered as hooks. +String literals are now rendered correctly in docstrings. Previously +command substitution applied inside literals. + # v0.16 Improved wording when looking at aliases. @@ -969,15 +969,19 @@ unescaping too." (while (not (eobp)) (cond ((looking-at - (rx "\"")) - (looking-at ;; Text of the form "foo" (rx "\"")) - ;; Don't do anything with literal strings. - ;; Step over opening doublequote. - (forward-char 1) - ;; Move past closing doublequote. - (search-forward "\"")) + ;; For literal strings, escape backslashes so our output + ;; shows copy-pasteable literals. + (let* ((start-pos (point)) + (end-pos (progn (forward-char) (search-forward "\"" nil t))) + contents) + (if end-pos + (progn + (setq contents (buffer-substring start-pos end-pos)) + (delete-region start-pos end-pos) + (insert (s-replace "\\" "\\\\" contents))) + (forward-char 1)))) ((looking-at ;; Text of the form \=X (rx "\\=")) diff --git a/test/helpful-unit-test.el b/test/helpful-unit-test.el index 3010708..d4fd835 100644 --- a/test/helpful-unit-test.el +++ b/test/helpful-unit-test.el @@ -71,13 +71,25 @@ (ert-deftest helpful--docstring-strings () "Double-quoted strings should be treated literally." + ;; Ensure backslashes are shown escaped, so the output is a valid string literal. + (let* ((formatted-docstring + (helpful--format-docstring + "hello \"x\\y\" world"))) + ;; This test will fail in a local Emacs instance that has modified + ;; minibuffer keybindings. + (should + (string-equal formatted-docstring "hello \"x\\\\y\" world"))) + ;; Don't crash on unbalanced doublequotes. + (helpful--format-docstring "hello \" world") + ;; Command sequences \\[foo] should be ignored inside doublequotes. (let* ((formatted-docstring (helpful--format-docstring "hello \"\\[foo]\" world"))) ;; This test will fail in a local Emacs instance that has modified ;; minibuffer keybindings. (should - (string-equal formatted-docstring "hello \"\\[foo]\" world")))) + (string-equal formatted-docstring + "hello \"\\\\[foo]\" world")))) (ert-deftest helpful--docstring-keymap () "Handle keymap references in docstrings." |
