]> git.eshelyaron.com Git - sweep.git/commitdiff
Use built-in file_autoload_directives/3 to update deps V9.0.1 V9.0.2 V9.1.1
authorEshel Yaron <me@eshelyaron.com>
Sun, 4 Dec 2022 17:09:30 +0000 (19:09 +0200)
committerEshel Yaron <me@eshelyaron.com>
Sun, 4 Dec 2022 17:09:30 +0000 (19:09 +0200)
* sweep.pl (sweep_file_missing_dependencies/2): new predicate.
* sweeprolog.el (sweeprolog-update-dependencies): use it.
* sweeprolog-tests.el: add another test case for
sweeprolog-update-dependencies.

sweep.pl
sweeprolog-tests.el
sweeprolog.el

index 360d9333af79a0bf974a91a1896ead8c064427e3..4c0c12ea1dc020607b49e38e0cbf49b574294a22 100644 (file)
--- a/sweep.pl
+++ b/sweep.pl
@@ -69,7 +69,8 @@
             sweep_exportable_predicates/2,
             sweep_interrupt/0,
             sweep_string_to_atom/2,
-            sweep_file_path_in_library/2
+            sweep_file_path_in_library/2,
+            sweep_file_missing_dependencies/2
           ]).
 
 :- use_module(library(pldoc)).
@@ -86,6 +87,7 @@
 :- use_module(library(http/html_write)).
 :- use_module(library(prolog_pack)).
 :- use_module(library(prolog_deps)).
+:- use_module(library(dcg/high_order)).
 
 :- if(exists_source(library(help))).
 :- use_module(library(help)).
@@ -908,3 +910,35 @@ strip_mode_and_type(S, N) => strip_type(S, N).
 
 strip_type(N:_, N) :- !.
 strip_type(N, N).
+
+sweep_file_missing_dependencies(File0, Deps) :-
+    atom_string(File, File0),
+    file_autoload_directives(File, Directives, [missing(true)]),
+    phrase(dep_directives(Directives), Deps).
+
+dep_directives(Directives) --> sequence(dep_directive, Directives).
+
+dep_directive(:- use_module(Spec)) -->
+    !,
+    {      absolute_file_name(Spec, Path0,
+                           [ file_type(prolog),
+                             access(read)
+                           ]),
+           atom_string(Path0, Path)
+    },
+    [[Path, [], "use_module"]].
+dep_directive(:- Directive) -->
+    {   compound_name_arguments(Directive, Kind0, [Spec, ImportList]),
+        atom_string(Kind0, Kind),
+        absolute_file_name(Spec, Path0,
+                           [ file_type(prolog),
+                             access(read)
+                           ]),
+        atom_string(Path0, Path)
+    },
+    sequence(dep_import(Path, Kind), ImportList).
+
+dep_import(Path, Kind, PI0) -->
+    {   term_string(PI0, PI)
+    },
+    [[Path, PI, Kind]].
index 7124aee268836b3cb6048c99546497d8ed036643..46a4c612a45932db85e2a20dff0998e0ea69fa23 100644 (file)
@@ -691,6 +691,38 @@ foo :- bar.
 foo :- Body.
 "))))
 
+(ert-deftest update-dependencies-no-autoload ()
+  "Tests making adding a use_module/1 directive."
+  (let ((temp (make-temp-file "sweeprolog-test"
+                              nil
+                              "pl"
+                              "
+:- module(foo, [bar/1]).
+
+/** <module> Foo
+
+*/
+
+bar(X) :- arithmetic_function(X).
+"
+                              )))
+    (find-file-literally temp)
+    (sweeprolog-mode)
+    (call-interactively #'sweeprolog-update-dependencies)
+    (should (string= (buffer-string)
+                              "
+:- module(foo, [bar/1]).
+
+/** <module> Foo
+
+*/
+
+:- use_module(library(arithmetic), [ arithmetic_function/1
+                                   ]).
+
+bar(X) :- arithmetic_function(X).
+"))))
+
 (ert-deftest append-dependencies ()
   "Tests making implicit autoloads explicit with existing directive."
   (let ((temp (make-temp-file "sweeprolog-test"
index 514dd3acbbc96c6d2cc7b6dd763d18e0947b84a9..88fde91240ab29e1dbdfdc8123efd82d6a9de446 100644 (file)
@@ -4110,7 +4110,6 @@ valid Prolog atom."
   "Add explicit dependencies for implicitly autoaloaded predicates."
   (interactive "" sweeprolog-mode)
   (let ((existing nil)
-        (missing nil)
         (current-directive-beg nil)
         (current-directive-end nil)
         (current-directive-file nil))
@@ -4137,57 +4136,67 @@ valid Prolog atom."
                          current-directive-end))
             (push
              (cons current-directive-file (copy-marker (1- end) t))
-             existing)))
-         (`("goal" ("autoload" . ,file) ,functor ,arity)
-          (let ((autoloaded (list file functor arity)))
-            (unless (member autoloaded missing)
-              (push autoloaded missing)))))))
-    (if missing
+             existing))))))
+    (if-let ((missing
+              (sweeprolog--query-once "sweep" "sweep_file_missing_dependencies"
+                                      (buffer-file-name))))
         (progn
           (dolist (autoloaded missing)
             (let* ((file    (nth 0 autoloaded))
-                   (functor (nth 1 autoloaded))
-                   (arity   (nth 2 autoloaded))
-                   (pred    (concat (sweeprolog-format-string-as-atom functor)
-                                    "/" (number-to-string arity))))
-              (message "Adding explicit dependency on %s from %s."
-                       pred file)
-              (if-let ((marker (cdr (assoc-string file existing))))
+                   (pred    (nth 1 autoloaded))
+                   (kind    (nth 2 autoloaded)))
+              (if (not pred)
                   (save-mark-and-excursion
-                    (goto-char marker)
-                    (pcase (sweeprolog-last-token-boundaries)
-                      (`(open ,_ ,oend)
-                       (goto-char oend)
-                       (insert " " pred "\n"))
-                      (`(symbol ,_ ,oend)
-                       (let ((point (point)))
+                    (goto-char (point-min))
+                    (sweeprolog-end-of-top-term)
+                    (while (forward-comment 1))
+                    (insert ":- " kind "("
+                            (sweeprolog--query-once "sweep"
+                                                    "sweep_file_path_in_library"
+                                                    file)
+                            ").\n\n")
+                    (indent-region-line-by-line (save-excursion
+                                                  (sweeprolog-beginning-of-top-term)
+                                                  (point))
+                                                (point)))
+                (message "Adding explicit dependency on %s from %s."
+                         pred file)
+                (if-let ((marker (cdr (assoc-string file existing))))
+                    (save-mark-and-excursion
+                      (goto-char marker)
+                      (pcase (sweeprolog-last-token-boundaries)
+                        (`(open ,_ ,oend)
                          (goto-char oend)
-                         (insert ",")
-                         (goto-char (1+ point))
-                         (insert pred "\n")))
-                      (tok
-                       (user-error "Unexpected token %s while looking for import list"
-                                   tok)))
-                    (indent-region-line-by-line  (save-excursion
-                                                   (sweeprolog-beginning-of-top-term)
-                                                   (point))
-                                                 (save-excursion
-                                                   (sweeprolog-end-of-top-term)
-                                                   (point))))
-                (save-mark-and-excursion
-                  (goto-char (point-min))
-                  (sweeprolog-end-of-top-term)
-                  (while (forward-comment 1))
-                  (insert ":- autoload("
-                          (sweeprolog--query-once "sweep"
-                                                  "sweep_file_path_in_library"
-                                                  file)
-                          ", [ " pred "\n]).\n\n")
-                  (indent-region-line-by-line (save-excursion
-                                                (sweeprolog-beginning-of-top-term)
+                         (insert " " pred "\n"))
+                        (`(symbol ,_ ,oend)
+                         (let ((point (point)))
+                           (goto-char oend)
+                           (insert ",")
+                           (goto-char (1+ point))
+                           (insert pred "\n")))
+                        (tok
+                         (user-error "Unexpected token %s while looking for import list"
+                                     tok)))
+                      (indent-region-line-by-line  (save-excursion
+                                                     (sweeprolog-beginning-of-top-term)
+                                                     (point))
+                                                   (save-excursion
+                                                     (sweeprolog-end-of-top-term)
+                                                     (point))))
+                  (save-mark-and-excursion
+                    (goto-char (point-min))
+                    (sweeprolog-end-of-top-term)
+                    (while (forward-comment 1))
+                    (insert ":- " kind "("
+                            (sweeprolog--query-once "sweep"
+                                                    "sweep_file_path_in_library"
+                                                    file)
+                            ", [ " pred "\n]).\n\n")
+                    (indent-region-line-by-line (save-excursion
+                                                  (sweeprolog-beginning-of-top-term)
+                                                  (point))
                                                 (point))
-                                              (point))
-                  (push (cons file (copy-marker (- (point) 5) t)) existing)))))
+                    (push (cons file (copy-marker (- (point) 5) t)) existing))))))
           (sweeprolog-analyze-buffer t))
       (message "No implicit autoloads found."))))