aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md3
-rw-r--r--helpful.el8
-rw-r--r--test/unit-test.el28
3 files changed, 37 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 56dcb04..6132a9a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,9 @@ Improved wording for functions with no source at all.
Fixed an issue with obsolete aliases without version information.
+Fixed an issue with top-level references where we would show the
+previous form rather than the relevant one.
+
# v0.7
Helpful buffers now start with a summary of what you're looking at,
diff --git a/helpful.el b/helpful.el
index 7a59d5b..e5efd67 100644
--- a/helpful.el
+++ b/helpful.el
@@ -1098,10 +1098,14 @@ from parent keymaps."
(defun helpful--outer-sexp (buf pos)
"Find position POS in BUF, and return the name of the outer sexp,
-along with its position."
+along with its position.
+
+Moves point in BUF."
(with-current-buffer buf
(goto-char pos)
- (beginning-of-defun)
+ (let* ((ppss (syntax-ppss)))
+ (unless (zerop (syntax-ppss-depth ppss))
+ (beginning-of-defun)))
(list (point) (-take 2 (read buf)))))
(defun helpful--count-values (items)
diff --git a/test/unit-test.el b/test/unit-test.el
index fd321d7..985e0e3 100644
--- a/test/unit-test.el
+++ b/test/unit-test.el
@@ -366,3 +366,31 @@ associated a lambda with a keybinding."
(let* ((source (helpful--source #'helpful--source t)))
(should
(s-starts-with-p "(defun " source))))
+
+(ert-deftest helpful--outer-sexp ()
+ ;; If point is in the middle of a form, we should return its position.
+ (with-temp-buffer
+ (insert "(foo bar baz)")
+ (goto-char (point-min))
+ (search-forward "b")
+
+ (-let [(pos subforms)
+ (helpful--outer-sexp (current-buffer) (point))]
+ (should
+ (equal pos (point-min)))
+ (should
+ (equal subforms '(foo bar)))))
+ ;; If point is at the beginning of a form, we should still return its position.
+ (with-temp-buffer
+ (insert "(foo) (bar)")
+ (goto-char (point-min))
+ (search-forward "b")
+ (backward-char 2)
+
+ (-let [(pos subforms)
+ (save-excursion
+ (helpful--outer-sexp (current-buffer) (point)))]
+ (should
+ (equal pos (point)))
+ (should
+ (equal subforms '(bar))))))