]> git.eshelyaron.com Git - sweep.git/commitdiff
ENHANCED: use variable names from docs when inserting next clause
authorEshel Yaron <me@eshelyaron.com>
Wed, 21 Dec 2022 20:41:59 +0000 (22:41 +0200)
committerEshel Yaron <me@eshelyaron.com>
Wed, 21 Dec 2022 20:41:59 +0000 (22:41 +0200)
* sweep.pl (sweep_format_head/2): new predicate.
* sweeprolog.el (sweeprolog-insert-clause): use it.

sweep.pl
sweeprolog-tests.el
sweeprolog.el

index 95f1f2d01b47521fb93729b3eb5a33ae4fa4467c..91a4e69c5e2c999416479b40afd6e591600ac4fd 100644 (file)
--- a/sweep.pl
+++ b/sweep.pl
@@ -70,7 +70,8 @@
             sweep_interrupt/0,
             sweep_string_to_atom/2,
             sweep_file_path_in_library/2,
-            sweep_file_missing_dependencies/2
+            sweep_file_missing_dependencies/2,
+            sweep_format_head/2
           ]).
 
 :- use_module(library(pldoc)).
@@ -937,3 +938,10 @@ dep_import(Path, Kind, PI0) -->
     {   term_string(PI0, PI)
     },
     [[Path, PI, Kind]].
+
+
+sweep_format_head([F0|A], R) :-
+    atom_string(F, F0),
+    pi_head(F/A, H),
+    sweep_current_module(M),
+    sweep_format_predicate(M, 0, H, R).
index c62a031bc7ec0c2768d66ee97fff152a24c09d8e..1210a5b26fc6a4dc0b891c314b5c9cd1a51c33e0 100644 (file)
@@ -612,6 +612,30 @@ spam:foo --> Body.
 "
                      ))))
 
+(ert-deftest dwim-next-clause-args ()
+  "Tests inserting new clause with arguments."
+  (let ((temp (make-temp-file "sweeprolog-test"
+                              nil
+                              "pl"
+                              "
+%!  foo(+Bar) is det.
+
+foo(bar) :- bar.
+"
+                              )))
+    (find-file-literally temp)
+    (sweeprolog-mode)
+    (goto-char (point-max))
+    (sweeprolog-insert-term-dwim)
+    (should (string= (buffer-string)
+                              "
+%!  foo(+Bar) is det.
+
+foo(bar) :- bar.
+foo(Bar) :- Body.
+
+"))))
+
 (ert-deftest dwim-next-clause-module-qualified ()
   "Tests inserting new module-qualified clause."
   (let ((temp (make-temp-file "sweeprolog-test"
index 7422eab87f3c8f182d606a0b2be3e09bc8328361..057b164edeeb197b0f0caddce3a169f811f89eee 100644 (file)
@@ -2739,20 +2739,35 @@ instead."
                                 font-lock-face)))
 
 (defun sweeprolog-insert-clause (functor arity &optional neck module)
-  (let ((point nil)
-        (neck (or neck ":-")))
+  (let ((point (point))
+        (neck (or neck ":-"))
+        (head-format (sweeprolog--query-once "sweep" "sweep_format_head"
+                                             (cons functor arity))))
     (combine-after-change-calls
       (insert "\n"
               (if module
                   (concat module ":")
                 "")
-              functor)
-      (setq point (point))
-      (when (< 0 arity)
-        (insert "(")
-        (dotimes (_ (1- arity))
-          (insert (sweeprolog--hole) ", "))
-        (insert (sweeprolog--hole) ")"))
+              (car head-format))
+      (pcase (cdr head-format)
+        (`(compound
+           "term_position"
+           0 ,length
+           ,_fbeg ,_fend
+           ,holes)
+         (with-silent-modifications
+           (dolist (hole holes)
+             (pcase hole
+               (`(compound "-" ,hbeg ,hend)
+                (add-text-properties
+                 (- (point) length (- hbeg))
+                 (- (point) length (- hend))
+                 (list
+                  'sweeprolog-hole t
+                  'font-lock-face (list (sweeprolog-hole-face))
+                  'rear-nonsticky '(sweeprolog-hole
+                                    cursor-sensor-functions
+                                    font-lock-face)))))))))
       (insert " " neck " " (sweeprolog--hole "Body") ".\n"))
     (goto-char point)
     (sweeprolog-forward-hole)))