(progn
(defun exec/lsp-which-function(file line column)
(with-current-buffer
(find-file-noselect file)
(goto-line line)
(move-to-column column)
(which-function)))
(defun get-function-name-and-overlay (file line column)
"Find the function name at a specific line and column in a file and put an overlay."
(let* ((function-name (exec/lsp-which-function file line column))
(ov (make-overlay (line-beginning-position) (1+ (line-beginning-position)) nil t))
(text (format "%30s │" (if function-name function-name "")))
)
;; (delete-all-overlays (current-buffer))
(overlay-put ov 'before-string
(propertize text 'face 'font-lock-string-face)
)
(overlay-put ov 'evaporate t)
))
(defun parse-buffer-and-overlay-function-name (&optional a b c)
"Parse the buffer content to get file, line number, column and make an overlay of function name."
(interactive)
(save-excursion
(goto-char (point-min))
(let ((current-file nil))
(while (not (eobp)) ; while not end of buffer
;; check if current line is a file path
(if (looking-at "^/.+?$")
;; update the current file
(setq current-file (buffer-substring-no-properties
(line-beginning-position) (line-end-position)))
(progn
;; else check if it's a line:col
(when (and current-file (looking-at "^\\([0-9]+\\):\\([0-9]+\\):"))
;; call your function with the captured groups as arguments
(get-function-name-and-overlay
current-file
(string-to-number (match-string 1))
(string-to-number (match-string 2))))))
(forward-line 1)))))
(advice-add 'lsp-bridge-references--popup :after 'parse-buffer-and-overlay-function-name))
(defun mp/xref-which-function (file pos)
"Get function name from a marker in a file."
(with-current-buffer
(find-file-noselect file)
(xref--goto-char pos)
(which-function)))
(defun mp/xref-put-function-name-work ()
"Put function name before all items."
(while (not (eobp)) ; while not end of buffer
(forward-line 1)
(when-let ((item (xref--item-at-point)))
(let* ((location (xref-item-location item))
(file (xref-location-group location))
(marker (xref-location-marker location))
(function-name (mp/xref-which-function file marker))
(ov (make-overlay (line-beginning-position) (1+ (line-beginning-position)) nil t))
(text (format "%30s │" (or function-name ""))))
(overlay-put ov 'before-string
(propertize text 'face 'font-lock-string-face))
(overlay-put ov 'evaporate t)))))
(defun mp/xref-put-function-name (&optional arg)
"Put function name before items in current group. If called with
`universal-argument', apply to the entire buffer."
(interactive "P")
(save-excursion
(if arg
(progn
(goto-char (point-min))
(mp/xref-put-function-name-work))
(or (xref--search-property 'xref-group)
(goto-char (point-max)))
(let ((max (point)))
(xref--search-property 'xref-group t)
(with-restriction (point) max
(mp/xref-put-function-name-work))))))