]> git.eshelyaron.com Git - sweep.git/commitdiff
ADDED: new function sweeprolog-definition-at-point
authorEshel Yaron <me@eshelyaron.com>
Sat, 1 Oct 2022 19:26:07 +0000 (22:26 +0300)
committerEshel Yaron <me@eshelyaron.com>
Sun, 2 Oct 2022 15:39:37 +0000 (18:39 +0300)
sweep.pl
sweeprolog.el

index 41b3618a1d2945afaacd4da437e8f548cd2306a9..763bdd003b3f9bcb6a1e72194c1d2811e54c6535 100644 (file)
--- a/sweep.pl
+++ b/sweep.pl
@@ -37,6 +37,7 @@
             sweep_current_prolog_flags/2,
             sweep_set_prolog_flag/2,
             sweep_documentation/2,
+            sweep_definition_at_point/2,
             sweep_file_at_point/2,
             sweep_identifier_at_point/2,
             sweep_expand_file_name/2,
@@ -142,6 +143,33 @@ sweep_colourise_buffer_(Path0, Contents, []) :-
     erase(Ref0),
     erase(Ref1).
 
+sweep_definition_at_point([Contents|Path0], Result) :-
+    atom_string(Path, Path0),
+    with_buffer_stream(Stream,
+                       Contents,
+                       sweep_definition_at_point_(Stream, Path, Result)).
+
+:- dynamic sweep_current_defintion_at_point/1.
+
+sweep_definition_at_point_(Stream, Path, [Beg,F,N]) :-
+    set_stream(Stream, file_name(Path)),
+    retractall(sweep_current_defintion_at_point(_)),
+    prolog_colourise_term(Stream, Path,
+                          sweep_handle_definition_at_point,
+                          []),
+    sweep_current_defintion_at_point(Beg-Def),
+    (   Def = M:F0/N
+    ->  term_string(M:F0, F)
+    ;   Def = F0/N,
+        term_string(F0, F)
+    ).
+
+sweep_handle_definition_at_point(head_term(_Kind, Goal), Beg, _Len) :-
+    !,
+    pi_head(PI, Goal),
+    asserta(sweep_current_defintion_at_point(Beg-PI)).
+sweep_handle_definition_at_point(_, _, _).
+
 
 sweep_file_at_point([Contents,Path0,Point], Result) :-
     atom_string(Path, Path0),
index 36fe09aaaae6eccf21a728c669d2be94eca3748e..408b871d4df078aa8e02242e54d25e3c140f814b 100644 (file)
@@ -184,23 +184,25 @@ clause."
 (declare-function sweeprolog-cleanup       "sweep-module")
 
 (defun sweeprolog--ensure-module ()
-  (let ((sweep-module-path (car
-                            (save-match-data
-                              (split-string
-                               (shell-command-to-string
-                                (concat
-                                 (or sweeprolog-swipl-path (executable-find "swipl"))
-                                 " -g"
-                                 " write_sweep_module_location"
-                                 " -t"
-                                 " halt"))
-                               "\n")))))
-    (condition-case _
-        (load sweep-module-path)
-      (file-error (user-error
-                   (concat "Failed to locate `sweep-module'. "
-                           "Make sure SWI-Prolog is installed "
-                           "and up to date"))))))
+  "Locate and load `sweep-module', unless already loaded."
+  (unless (featurep 'sweep-module)
+    (let ((sweep-module-path (car
+                              (save-match-data
+                                (split-string
+                                 (shell-command-to-string
+                                  (concat
+                                   (or sweeprolog-swipl-path (executable-find "swipl"))
+                                   " -g"
+                                   " write_sweep_module_location"
+                                   " -t"
+                                   " halt"))
+                                 "\n")))))
+      (condition-case _
+          (load sweep-module-path)
+        (file-error (user-error
+                     (concat "Failed to locate `sweep-module'. "
+                             "Make sure SWI-Prolog is installed "
+                             "and up to date")))))))
 
 (defface sweeprolog-debug-prefix-face
   '((default :inherit shadow))
@@ -2144,6 +2146,28 @@ Interactively, a prefix arg means to prompt for BUFFER."
   (and (looking-at-p (rx bol graph))
        (not (nth 8 (syntax-ppss)))))
 
+(defun sweeprolog-definition-at-point (&optional point)
+  (let* ((p (or point (point)))
+         (beg (save-mark-and-excursion
+                (goto-char p)
+                (unless (sweeprolog-at-beginning-of-top-term-p)
+                  (sweeprolog-beginning-of-top-term))
+                (max (1- (point)) (point-min))))
+         (end (save-mark-and-excursion
+                (goto-char p)
+                (sweeprolog-end-of-top-term)
+                (point)))
+         (contents (buffer-substring-no-properties beg end)))
+    (sweeprolog-open-query "user"
+                           "sweep"
+                           "sweep_definition_at_point"
+                           (cons contents
+                                 (buffer-file-name)))
+    (let ((sol (sweeprolog-next-solution)))
+      (sweeprolog-close-query)
+      (when (sweeprolog-true-p sol)
+        (cons (+ beg (cadr sol)) (cddr sol))))))
+
 (defun sweeprolog-file-at-point (&optional point)
   (let* ((p (or point (point)))
          (beg (save-mark-and-excursion