From 65b43c00d065639d53c6fbff63727d0b98cfb940 Mon Sep 17 00:00:00 2001 From: Eshel Yaron Date: Wed, 21 Dec 2022 22:41:59 +0200 Subject: [PATCH] ENHANCED: use variable names from docs when inserting next clause * sweep.pl (sweep_format_head/2): new predicate. * sweeprolog.el (sweeprolog-insert-clause): use it. --- sweep.pl | 10 +++++++++- sweeprolog-tests.el | 24 ++++++++++++++++++++++++ sweeprolog.el | 33 ++++++++++++++++++++++++--------- 3 files changed, 57 insertions(+), 10 deletions(-) diff --git a/sweep.pl b/sweep.pl index 95f1f2d..91a4e69 100644 --- 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). diff --git a/sweeprolog-tests.el b/sweeprolog-tests.el index c62a031..1210a5b 100644 --- a/sweeprolog-tests.el +++ b/sweeprolog-tests.el @@ -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" diff --git a/sweeprolog.el b/sweeprolog.el index 7422eab..057b164 100644 --- a/sweeprolog.el +++ b/sweeprolog.el @@ -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))) -- 2.39.2