emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/sweeprolog d974b0fe0a 071/166: ADDED: prepare xref backend


From: ELPA Syncer
Subject: [nongnu] elpa/sweeprolog d974b0fe0a 071/166: ADDED: prepare xref backend
Date: Fri, 30 Sep 2022 04:59:27 -0400 (EDT)

branch: elpa/sweeprolog
commit d974b0fe0a6ba4f2ac033ba870d165d7b23aef45
Author: Eshel Yaron <me@eshelyaron.com>
Commit: Eshel Yaron <me@eshelyaron.com>

    ADDED: prepare xref backend
---
 sweep.el | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 sweep.pl | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 102 insertions(+)

diff --git a/sweep.el b/sweep.el
index 69fdac0eb2..9603c17fdf 100644
--- a/sweep.el
+++ b/sweep.el
@@ -987,6 +987,52 @@ Interactively, a prefix arg means to prompt for BUFFER."
             (string-to-syntax "w")))))
      start end)))
 
+
+(defun sweep-identifier-at-point (&optional point)
+  (let* ((p (or point (point)))
+         (beg (save-mark-and-excursion
+                (goto-char p)
+                (sweep-beginning-of-top-term)
+                (point)))
+         (end (save-mark-and-excursion
+                (goto-char p)
+                (sweep-end-of-top-term)
+                (point)))
+         (contents (buffer-substring-no-properties beg end)))
+    (sweep-open-query "user"
+                      "sweep"
+                      "sweep_identifier_at_point"
+                      (list contents
+                            (buffer-file-name)
+                            (- p beg)))
+    (let ((sol (sweep-next-solution)))
+      (sweep-close-query)
+      (when (sweep-true-p sol)
+        (cdr sol)))))
+
+;;;###autoload
+(defun sweep--xref-backend ()
+  "Hook for `xref-backend-functions'."
+  'sweep)
+
+
+(cl-defmethod xref-backend-identifier-at-point ((_backend (eql 'sweep)))
+  (sweep-identifier-at-point))
+
+(cl-defmethod xref-backend-identifier-completion-table ((_backend (eql 
'sweep)))
+  (sweep-identifier-completion-table))
+
+(cl-defmethod xref-backend-identifier-completion-ignore-case ((_backend (eql 
'sweep)))
+  "Case is always significant for Prolog identifiers, so return nil."
+  nil)
+
+(cl-defmethod xref-backend-definitions ((_backend (eql 'sweep)) symbol))
+
+(cl-defmethod xref-backend-references ((_backend (eql 'sweep)) symbol))
+
+(cl-defmethod xref-backend-apropos ((_backend (eql 'sweep)) pattern))
+
+
 ;;;###autoload
 (define-derived-mode sweep-mode prog-mode "sweep"
   "Major mode for reading and editing Prolog code."
@@ -1007,6 +1053,7 @@ Interactively, a prefix arg means to prompt for BUFFER."
                 (font-lock-fontify-region-function . 
sweep-colourise-some-terms)))
   (sweep-colourise-buffer)
   (sweep--set-buffer-module)
+  (add-hook 'xref-backend-functions #'sweep--xref-backend nil t)
   (add-hook 'completion-at-point-functions 
#'sweep-completion-at-point-function nil t))
 
 ;;;; Testing:
diff --git a/sweep.pl b/sweep.pl
index a9836c6633..61583c6284 100644
--- a/sweep.pl
+++ b/sweep.pl
@@ -34,6 +34,7 @@
           [ sweep_colourise_buffer/2,
             sweep_colourise_some_terms/2,
             sweep_documentation/2,
+            sweep_identifier_at_point/2,
             sweep_expand_file_name/2,
             sweep_path_module/2,
             sweep_predicate_location/2,
@@ -103,6 +104,60 @@ sweep_colourise_buffer_(Path0, Contents, []) :-
     erase(Ref0),
     erase(Ref1).
 
+
+sweep_identifier_at_point([Contents0, Path, Point], Identifier) :-
+    setup_call_cleanup(( new_memory_file(H),
+                         insert_memory_file(H, 0, Contents0),
+                         open_memory_file(H, read, Contents, [encoding(utf8)])
+                       ),
+                       sweep_identifier_at_point_(Path, Point, Contents, 
Identifier),
+                       ( close(Contents),
+                         free_memory_file(H)
+                       )).
+
+:- dynamic sweep_current_identifier_at_point/1.
+
+
+sweep_identifier_at_point_(Path0, Point, Contents, Identifier) :-
+    atom_string(Path, Path0),
+    (   xref_module(Path, M)
+    ->  true
+    ;   M = user
+    ),
+    set_stream(Contents, encoding(utf8)),
+    set_stream(Contents, file_name(Path)),
+    seek(Contents, 0, bof, _),
+    retractall(sweep_current_identifier_at_point(_)),
+    prolog_colourise_term(Contents, Path,
+                          sweep_handle_identifier_at_point(Path, M, Point),
+                          []),
+    sweep_current_identifier_at_point(Identifier0),
+    term_string(Identifier0, Identifier).
+
+
+sweep_handle_identifier_at_point(Path, M, Point, Col, Beg, Len) :-
+    Beg =< Point,
+    Point =< Beg + Len,
+    !,
+    sweep_handle_identifier_at_point_(Path, M, Col).
+sweep_handle_identifier_at_point(_, _, _, _, _, _).
+
+sweep_handle_identifier_at_point_(Path, M0, goal_term(_Kind, Goal)) :-
+    !,
+    pi_head(PI0, Goal),
+    (   PI0 = M:PI
+    ->  true
+    ;   xref_defined(Path, Goal, imported(Other)), xref_module(Other, M)
+    ->  PI = PI0
+    ;   predicate_property(M0:Goal, imported_from(M))
+    ->  PI = PI0
+    ;   '$autoload':library_index(Goal, M, _)
+    ->  PI = PI0
+    ;   M = M0, PI = PI0
+    ),
+    asserta(sweep_current_identifier_at_point(M:PI)).
+sweep_handle_identifier_at_point_(_, _, _).
+
 sweep_colourise_some_terms([String,Path,Offset], Colors) :-
     setup_call_cleanup(( new_memory_file(H),
                          insert_memory_file(H, 0, String),



reply via email to

[Prev in Thread] Current Thread [Next in Thread]