From 9d7f40b99a34047d41669fa40b7462180ccdc42f Mon Sep 17 00:00:00 2001 From: Arnold Noronha Date: Wed, 26 Apr 2023 14:19:11 -0400 Subject: [PATCH 1/3] Always search for the full package name in existing IMPORT-FROMs .. As opposed to just looking at prefixes. Previously, If you had an existing defpackage like this: (defpakage :foo (:import-from :bar/car :find))) And you imported bar::position, you'll end up with something ugly like this: (defpackage :foo (:import-from :bar :position/car :find)) --- contrib/sly-package-fu.el | 9 +++++-- test/sly-package-fu-tests.el | 47 ++++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/contrib/sly-package-fu.el b/contrib/sly-package-fu.el index 3ac8c05b1..848e73a15 100644 --- a/contrib/sly-package-fu.el +++ b/contrib/sly-package-fu.el @@ -350,10 +350,15 @@ symbol in the Lisp image if possible." ;; (defun sly-package-fu--search-import-from (package) + "Moves point to the end of the package name corresponding to +the (:IMPORT-FROM) for package. If no such import-from exists, +returns nil." (let* ((normalized-package (sly-package-fu--normalize-name package)) - (regexp (format "(:import-from[ \t']*\\(:\\|#:\\)?%s" + (regexp (format "(:import-from[ \t']*\\(:\\|#:\\)?%s[[:space:]]" (regexp-quote normalized-package)))) - (re-search-forward regexp nil t))) + (when (re-search-forward regexp nil t) + (goto-char (- (point) 1)) + (point)))) (defun sly-package-fu--create-new-import-from (package symbol) diff --git a/test/sly-package-fu-tests.el b/test/sly-package-fu-tests.el index 18e20c4db..f37ee20f0 100644 --- a/test/sly-package-fu-tests.el +++ b/test/sly-package-fu-tests.el @@ -15,8 +15,22 @@ :position :find)) (in-package :foo)) - bknr.datastore-dummy::position)) - (let ((file (make-temp-file "sly-package-fu--fixture"))) + bknr.datastore-dummy::position) + (((defpackage :foo + (:import-from :bar/car :find)) + (in-package :foo)) + ((defpackage :foo + (:import-from :bar/car :find) + (:import-from :bar :position)) + (in-package :foo)) + bar::position)) + (let ((file (make-temp-file "sly-package-fu--fixture")) + (sly-export-symbol-representation-auto nil) + (sly-export-symbol-representation-function + ;; For the purpose of these tests, reading back the + ;; #:.. style symbols breaks equality, since they are + ;; uninterned. + (lambda (n) (format ":%s" n)))) (with-temp-buffer (find-file file) (lisp-mode) @@ -25,11 +39,40 @@ ;; FIXME: using internal implementation detail (sly-package-fu--add-or-update-import-from-form (pp-to-string symbol-to-import)) + + (message "%s" final) + (message "%s" (cl-loop initially (goto-char (point-min)) + for f = (ignore-errors (read (current-buffer))) + while f collect f)) (should (equal final (cl-loop initially (goto-char (point-min)) for f = (ignore-errors (read (current-buffer))) while f collect f)))))) +(define-sly-ert-test package-fu-jumps-to-import-from-happy-path () + (with-temp-buffer + (insert "(defpackage :foo (:import-from #:bar #:car))") + ;; Expect to be here:........................^ + (goto-char (point-min)) + (sly-package-fu--search-import-from "bar") + (should (equal 37 (point))))) + +(define-sly-ert-test package-fu-jumps-to-import-from-with-new-line () + (with-temp-buffer + (insert "(defpackage :foo (:import-from #:bar\n#:car))") + ;; Expect to be here:........................^ + (goto-char (point-min)) + (sly-package-fu--search-import-from "bar") + (should (equal 37 (point))))) + +(define-sly-ert-test package-fu-does-not-jump-to-package-prefix () + (with-temp-buffer + (insert "(defpackage :foo (:import-from #:barcelona\n#:car))") + (goto-char (point-min)) + (should + (equal nil + (sly-package-fu--search-import-from "bar"))) + (should (equal 1 (point))))) (provide 'sly-package-fu-tests) From 256f59e88dbc3d74ec912db8e52038effc13d35f Mon Sep 17 00:00:00 2001 From: Arnold Noronha Date: Wed, 26 Apr 2023 14:48:15 -0400 Subject: [PATCH 2/3] Ensure we also handle empty IMPORT-FROMs e.g. (:import-from :foo). --- contrib/sly-package-fu.el | 2 +- test/sly-package-fu-tests.el | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/contrib/sly-package-fu.el b/contrib/sly-package-fu.el index 848e73a15..4518dd865 100644 --- a/contrib/sly-package-fu.el +++ b/contrib/sly-package-fu.el @@ -354,7 +354,7 @@ symbol in the Lisp image if possible." the (:IMPORT-FROM) for package. If no such import-from exists, returns nil." (let* ((normalized-package (sly-package-fu--normalize-name package)) - (regexp (format "(:import-from[ \t']*\\(:\\|#:\\)?%s[[:space:]]" + (regexp (format "(:import-from[ \t']*\\(:\\|#:\\)?%s[)[:space:]]" (regexp-quote normalized-package)))) (when (re-search-forward regexp nil t) (goto-char (- (point) 1)) diff --git a/test/sly-package-fu-tests.el b/test/sly-package-fu-tests.el index f37ee20f0..3bf4bff51 100644 --- a/test/sly-package-fu-tests.el +++ b/test/sly-package-fu-tests.el @@ -23,6 +23,13 @@ (:import-from :bar/car :find) (:import-from :bar :position)) (in-package :foo)) + bar::position) + (((defpackage :foo + (:import-from :bar)) + (in-package :foo)) + ((defpackage :foo + (:import-from :bar :position)) + (in-package :foo)) bar::position)) (let ((file (make-temp-file "sly-package-fu--fixture")) (sly-export-symbol-representation-auto nil) @@ -74,5 +81,13 @@ (sly-package-fu--search-import-from "bar"))) (should (equal 1 (point))))) +(define-sly-ert-test package-fu-jump-to-end-even-when-closing-parenthesis () + (with-temp-buffer + (insert "(defpackage :foo (:import-from #:bar))") + ;; Expect to be here:........................^ + (goto-char (point-min)) + (sly-package-fu--search-import-from "bar") + (should (equal 37 (point))))) + (provide 'sly-package-fu-tests) From ca661e788e0c5f62f93cfd3b032f08b228748399 Mon Sep 17 00:00:00 2001 From: Arnold Noronha Date: Wed, 26 Apr 2023 14:50:47 -0400 Subject: [PATCH 3/3] Remove debugging statements --- test/sly-package-fu-tests.el | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/sly-package-fu-tests.el b/test/sly-package-fu-tests.el index 3bf4bff51..6c4f85abb 100644 --- a/test/sly-package-fu-tests.el +++ b/test/sly-package-fu-tests.el @@ -47,10 +47,6 @@ (sly-package-fu--add-or-update-import-from-form (pp-to-string symbol-to-import)) - (message "%s" final) - (message "%s" (cl-loop initially (goto-char (point-min)) - for f = (ignore-errors (read (current-buffer))) - while f collect f)) (should (equal final (cl-loop initially (goto-char (point-min)) for f = (ignore-errors (read (current-buffer)))