diff options
| author | Wilfred Hughes <me@wilfred.me.uk> | 2018-03-02 22:22:06 +0000 |
|---|---|---|
| committer | Wilfred Hughes <me@wilfred.me.uk> | 2018-03-02 22:22:06 +0000 |
| commit | 656ff91eb6094bbd5a78dcd6ef1e9ccb4d257a4d (patch) | |
| tree | f7030b729725c6ca3557efbca11879fc6e20cdc7 | |
| parent | 4abe04870467beb8a5a798773e1f145f65298e59 (diff) | |
Fix references to top-level forms
Previoiusly helpful--outer-sexp assumed point was inside a form, so
returned the wrong sexp for top-level forms.
| -rw-r--r-- | CHANGELOG.md | 3 | ||||
| -rw-r--r-- | helpful.el | 8 | ||||
| -rw-r--r-- | test/unit-test.el | 28 |
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, @@ -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)))))) |
