branch: externals/xref-union
commit 40baff9b100dc7e6879d2cedd199df5db3534398
Author: fap <[email protected]>
Commit: Philip Kaludercic <[email protected]>

    Match references on line in file (#2)
    
    The previous implementation only matched references to the exact same
    POINT. If the backends find the same reference but reference them on
    different columns of the line then this method will see them as
    distinct.
    By matching on lime and file those references get recognized as the
    same reference now.
    
    ---
    
    Example from a case where the previous method didn't recognize that the 
reference pointed to the same definition:
    
    ```elisp
    ;; l1 location:
    #s(xref-etags-location (#("function enrol_get_instances($courseid, 
$enabled) {" 0 51 (fontified nil)) 174 . 4995) 
"/home/fap/git/viaMINT/moodle-docker/moodle/lib/enrollib.php")
    
    ;; l2 location:
    #s(xref-file-location 
"/home/fap/git/viaMINT/moodle-docker/moodle/lib/enrollib.php" 174 9)
    
    ;; l1 full:
    #s(xref-item #("function enrol_get_instances($courseid, $enabled) {" 0 51 
(fontified nil)) #s(xref-etags-location (#("function 
enrol_get_instances($courseid, $enabled) {" 0 51 (fontified nil)) 174 . 4995) 
"/home/fap/git/viaMINT/moodle-docker/moodle/lib/enrollib.php"))
    
    ;; l2 full:
    #s(xref-match-item #("function enrol_get_instances($courseid, $enabled) {" 
0 8 (face font-lock-keyword-face fontified t) 8 9 (fontified t) 9 28 (face 
(font-lock-function-name-face xref-match) fontified t) 28 29 (face 
(rainbow-delimiters-depth-1-face) fontified t) 29 30 (fontified t) 30 38 (face 
font-lock-variable-name-face fontified t) 38 41 (fontified t) 41 48 (face 
font-lock-variable-name-face fontified t) 48 49 (face 
(rainbow-delimiters-depth-1-face) fontified t) 49 50 (fontified t [...]
    ```
    
    See https://github.com/joaotavora/eglot/issues/420#issuecomment-3926523346 
for additional reference.
    
    Reviewed-on: https://codeberg.org/pkal/xref-union.el/pulls/2
    Co-authored-by: fap <[email protected]>
    Co-committed-by: fap <[email protected]>
---
 xref-union.el | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/xref-union.el b/xref-union.el
index 6b6d724e61..fef5227330 100644
--- a/xref-union.el
+++ b/xref-union.el
@@ -58,13 +58,23 @@ Consult `add-hook' for the interpretation of DEPTH."
   :type 'number)
 
 
+;; Helper functions
+(defun xref-union--bol-marker (item)
+  "Return a marker for `xref-item' ITEM.
+The marker points to the beginning of the line in which ITEM is located."
+  (let* ((loc (xref-item-location item))
+         (mark (xref-location-marker loc)))
+    (with-current-buffer (marker-buffer mark)
+      (copy-marker (line-beginning-position)))))
+
 ;;;; Xref interface
 
-(defun xref-union-same-p (l1 l2)
-  "Check if the locations L1 and L2 are the same.
-Same in this context means they reference the same object."
-  (= (xref-location-marker (xref-item-location l1))
-     (xref-location-marker (xref-item-location l2))))
+(defun xref-union-same-p (item1 item2)
+  "Check if the items ITEM1 and ITEM2 are the same.
+Same in this context means they reference the same line in the same
+buffer."
+  (= (xref-union--bol-marker item1)
+     (xref-union--bol-marker item2)))
 
 (cl-defmethod xref-backend-identifier-at-point ((backends (head union)))
   "Collect the results of multiple Xref BACKENDS."

Reply via email to