From cb6c219c9fd7ac8f020dc4b69a655d265413fad2 Mon Sep 17 00:00:00 2001
From: "Jacob I. Komissar" <18540209+jirassimok@users.noreply.github.com>
Date: Fri, 17 Nov 2023 09:47:56 -0500
Subject: [PATCH 1/2] Fix parameter to lsp--make-reference-params

The definition of that function was changed in
emacs-lsp/lsp-mode@2c6a0e92 to reverse the meaning of the parameter,
so here we have to reverse the value we pass in.

This restores the previous behavior of not including declarations in
lsp-ui-peek-find-references by default.

For lsp-ui-find-{next,prev}-reference, reverse the parameters instead,
assuming that when cycling through references, the definition should
be included.
---
 lsp-ui-peek.el |  4 +++-
 lsp-ui.el      | 12 ++++++------
 2 files changed, 9 insertions(+), 7 deletions(-)

diff --git a/lsp-ui-peek.el b/lsp-ui-peek.el
index 6c8d74dc..de097e50 100644
--- a/lsp-ui-peek.el
+++ b/lsp-ui-peek.el
@@ -604,7 +604,9 @@ PARAM is the request params."
   "Find references to the IDENTIFIER at point."
   (interactive)
   (lsp-ui-peek--find-xrefs (symbol-at-point) "textDocument/references"
-                           (append extra (lsp--make-reference-params nil include-declaration))))
+                           (append extra (lsp--make-reference-params
+                                          nil
+                                          (not include-declaration)))))
 
 (defun lsp-ui-peek-find-definitions (&optional extra)
   "Find definitions to the IDENTIFIER at point."
diff --git a/lsp-ui.el b/lsp-ui.el
index b7f3fc4d..78b7bb32 100644
--- a/lsp-ui.el
+++ b/lsp-ui.el
@@ -127,10 +127,10 @@ Both should have the form (FILENAME LINE COLUMN)."
         (< (cadr x) (cadr y))
       (< (caddr x) (caddr y)))))
 
-(defun lsp-ui--reference-triples (include-declaration)
+(defun lsp-ui--reference-triples (exclude-declaration)
   "Return references as a list of (FILENAME LINE COLUMN) triples given EXTRA."
   (let ((refs (lsp-request "textDocument/references"
-                           (lsp--make-reference-params nil include-declaration))))
+                           (lsp--make-reference-params nil exclude-declaration))))
     (sort
      (mapcar
       (-lambda ((&Location :uri :range (&Range :start (&Position :line :character))))
@@ -139,11 +139,11 @@ Both should have the form (FILENAME LINE COLUMN)."
      #'lsp-ui--location<)))
 
 ;; TODO Make it efficient
-(defun lsp-ui-find-next-reference (&optional include-declaration)
+(defun lsp-ui-find-next-reference (&optional exclude-declaration)
   "Find next reference of the symbol at point."
   (interactive)
   (let* ((cur (list buffer-file-name (1- (line-number-at-pos)) (- (point) (line-beginning-position))))
-         (refs (lsp-ui--reference-triples include-declaration))
+         (refs (lsp-ui--reference-triples exclude-declaration))
          (idx -1)
          (res (-first (lambda (ref) (cl-incf idx) (lsp-ui--location< cur ref)) refs)))
     (if res
@@ -156,11 +156,11 @@ Both should have the form (FILENAME LINE COLUMN)."
       (cons 0 0))))
 
 ;; TODO Make it efficient
-(defun lsp-ui-find-prev-reference (&optional include-declaration)
+(defun lsp-ui-find-prev-reference (&optional exclude-declaration)
   "Find previous reference of the symbol at point."
   (interactive)
   (let* ((cur (list buffer-file-name (1- (line-number-at-pos)) (- (point) (line-beginning-position))))
-         (refs (lsp-ui--reference-triples include-declaration))
+         (refs (lsp-ui--reference-triples exclude-declaration))
          (idx -1)
          (res (-last (lambda (ref) (and (lsp-ui--location< ref cur) (cl-incf idx))) refs)))
     (if res

From 29687b92095870ab58c164273b828c334aa07d1e Mon Sep 17 00:00:00 2001
From: "Jacob I. Komissar" <18540209+jirassimok@users.noreply.github.com>
Date: Fri, 17 Nov 2023 10:11:34 -0500
Subject: [PATCH 2/2] Allow prefix arg to include declaration when finding
 references

---
 lsp-ui-peek.el | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/lsp-ui-peek.el b/lsp-ui-peek.el
index de097e50..fd1a65c2 100644
--- a/lsp-ui-peek.el
+++ b/lsp-ui-peek.el
@@ -601,8 +601,11 @@ PARAM is the request params."
       (lsp-ui-peek--show xrefs))))
 
 (defun lsp-ui-peek-find-references (&optional include-declaration extra)
-  "Find references to the IDENTIFIER at point."
-  (interactive)
+  "Find references to the IDENTIFIER at point.
+
+With a prefix argument, or if INCLUDE-DECLARATION is non-nil,
+includes the declaration of the identifier in the results."
+  (interactive "P")
   (lsp-ui-peek--find-xrefs (symbol-at-point) "textDocument/references"
                            (append extra (lsp--make-reference-params
                                           nil