commit 9b8dbbacbe30b84c94f49f1a682f71b699007f9d
parent 6d2b873f128b92e616abfdb2c1934bf210bca65c
Author: Lou Woell <lou.woell@posteo.de>
Date:   Tue,  7 Oct 2025 02:30:07 +0200

[haredoc.el] use xref internally

Diffstat:
Mharedoc.el | 68++++++++++++++++++++++++++++++--------------------------------------
1 file changed, 30 insertions(+), 38 deletions(-)

diff --git a/haredoc.el b/haredoc.el @@ -108,19 +108,17 @@ containing this file.") 'help-echo (purecopy "go forward")) (defun haredoc/button-action (button) - (let* ((name (button-get button :args)) - (module (button-get button :module)) + (haredoc (button-get button :target))) + +(defun haredoc/make-button (match-number module) + (let* ((name (match-string match-number)) (namespace? (string-match-p ".*::.*" name)) (target (if namespace? name (string-join (list module name) "::")))) - (haredoc target))) - -(defun haredoc/make-button (match-number module) - (make-text-button (match-beginning match-number) - (match-end match-number) - :type 'haredoc - :module module - :args (match-string match-number))) + (make-text-button (match-beginning match-number) + (match-end match-number) + :type 'haredoc + :target target))) ;; State variables for haredoc history. (defvar-local haredoc-backward-stack '() @@ -179,9 +177,8 @@ If SIGN is nil, ask user." (interactive) (let* ((id (or sign (haredoc/read-module))) (components (string-split id "::" t)) - (location (haredoc/get-location id)) - (location (string-split location ":" t)) - (path (nth 0 location)) + (loc (haredoc/get-location id)) + (path (xref-file-location-file (xref-item-location loc))) (parent-ns (if (length> components 1) (string-join (butlast components) "::") (car components))) @@ -244,31 +241,30 @@ the executable at `haredoc-helperbin'. Return value is a string \"/path/to/file.ha:line:column\"." (catch 'exit - (car (process-lines-handling-status - haredoc-helperbin - (lambda (x) (unless (= x 0) - (message "Could not find %s" name) - (throw 'exit nil))) - "-p" (haredoc/root-dir) - "locate" name)))) + (when-let* ((loc (car (process-lines-handling-status + haredoc-helperbin + (lambda (x) (unless (= x 0) + (message "Could not find %s" name) + (throw 'exit nil))) + "-p" (haredoc/root-dir) + "locate" name))) + (loc (split-string loc ":")) + (file (nth 0 loc)) + (line (string-to-number (nth 1 loc))) + (col (string-to-number (nth 2 loc))) + (sum (or (nth 3 loc) + name))) + (xref-make sum (xref-make-file-location file line col))))) ;;;###autoload -(defun haredoc/goto-defintion (&optional name) +(defun haredoc/goto-definition (&optional name) "Go to definition of hare identifier NAME. See `haredoc/identifier-at-point' and `haredoc/get-location'." (interactive) (when-let* ((name (or name (haredoc/identifier-at-point))) - (loc (haredoc/get-location name)) - (loc (split-string loc ":")) - (file (nth 0 loc)) - (line (string-to-number (nth 1 loc))) - (col (string-to-number (nth 2 loc)))) - (with-current-buffer (find-file file) - (goto-char (point-min)) - (forward-line (1- line)) - (forward-char col) - (forward-to-word)))) + (loc (haredoc/get-location name))) + (xref-pop-to-location loc))) (defun haredoc/next-decl () "In `haredoc-mode' buffer, jump to next declaration." @@ -306,7 +302,7 @@ declaration at point." "In `haredoc-mode' buffer, jump to source code of declaration at point." (interactive) (when (derived-mode-p 'haredoc-mode) - (haredoc/goto-defintion (haredoc/get-decl-apt)))) + (haredoc/goto-definition (haredoc/get-decl-apt)))) (defvar-keymap haredoc-map "C-c C-h" 'haredoc @@ -345,12 +341,8 @@ declaration at point." (haredoc/identifier-at-point)) (cl-defmethod xref-backend-definitions ((_backend (eql 'haredoc-xref)) symbol) - (let* ((loc (haredoc/get-location symbol)) - (loc (split-string loc ":" t)) - (file (nth 0 loc)) - (line (string-to-number (nth 1 loc))) - (col (string-to-number (nth 2 loc)))) - (list (xref-make symbol (xref-make-file-location file line col))))) + (when-let* ((loc (haredoc/get-location symbol))) + (list loc))) (cl-defmethod xref-backend-identifier-completion-table ((_backend (eql 'haredoc-xref))) '())