commit e26d35cf2d4f379f59f9f8218d61d8bed6d1b2a8
parent f40e6746b23ec54e1db13af3dabc8f3594a1113f
Author: Lou Woell <lou.woell@posteo.de>
Date:   Thu,  9 Oct 2025 19:58:30 +0200

[haredoc.el] small cleanup

- reduce code duplication by introducing macro for calls to harehelper
- break some lines
- streamline xref setup
- update readme
- update TODOs

Diffstat:
MREADME.md | 4----
Mharedoc.el | 85++++++++++++++++++++++++++++++++++++++++++-------------------------------------
2 files changed, 45 insertions(+), 44 deletions(-)

diff --git a/README.md b/README.md @@ -126,8 +126,6 @@ The following `use-package` declarations include a build step for harehelper. :vc (:url "https://git.repetitions.de/harehelper" :make "all") :hook (hare-mode . haredoc-nav-mode) - :init - (add-to-list 'xref-backend-functions 'haredoc-xref-backend) :config ;;... ) @@ -144,8 +142,6 @@ The following `use-package` declarations include a build step for harehelper. :pre-build (("make")) :files (:defaults "harehelper")) :hook (hare-mode . haredoc-nav-mode) - :init - (add-to-list 'xref-backend-functions 'haredoc-xref-backend) :config ;; ... ) diff --git a/haredoc.el b/haredoc.el @@ -23,7 +23,7 @@ ;;; Commentary: -;; `haredoc.el' provides an emacs interface for `haredoc' using harehelper. The +;; `haredoc.el' provides an Emacs interface for `haredoc' using harehelper. The ;; main entry points are `haredoc/goto-definition', `haredoc' and ;; `haredoc/describe-thing-at-point'. @@ -32,14 +32,11 @@ ;; `(add-hook 'hare-mode-hook #'haredoc-nav-mode)' -;; To use the provided xref-backend: - -;; `(add-to-list 'xref-backend-functions 'haredoc-xref-backend)' - ;;; TODO: ;; - melpa compatible setup ;; - imenu integration +;; - eldoc integration? ;;; Code: @@ -48,11 +45,11 @@ (require 'project) (require 'thingatpt) (require 'ansi-color) +(require 'xref) (defconst haredoc-helperbindir (f-dirname (macroexp-file-name)) - "Default directory for `haredoc-helperbin'. Defaults to directory -containing this file.") - + "Default directory for `haredoc-helperbin'. +Defaults to directory containing this file.") (defcustom haredoc-helperbin (concat haredoc-helperbindir "/harehelper") "Location of the harehelper executable." :type 'file @@ -63,8 +60,12 @@ containing this file.") (defconst haredoc-decl-regex "^\\(fn\\|type\\|def\\|const\\|let\\) ") (defconst haredoc-link-regex "\\[\\[\\(.*?\\)\\]\\]" ) +(defmacro haredoc|harehelper (&rest args) + "Run harehelper with arguments ARGS." + `(process-lines haredoc-helperbin ,@args)) + (defun haredoc/root-dir () - "Return root directory of current project or \".\"" + "Return root directory of current project or \".\"." (interactive) (if-let ((p (project-current nil))) (expand-file-name (project-root p)) @@ -72,7 +73,7 @@ containing this file.") (defun haredoc/get-modules () "Return list of Hare modules." - (process-lines haredoc-helperbin "-p" (haredoc/root-dir) "list-modules")) + (haredoc|harehelper "-p" (haredoc/root-dir) "list-modules")) (defun haredoc/read-module (&optional init) "Ask user to pick from a list of installed hare modules." @@ -87,9 +88,9 @@ containing this file.") (thing-at-point-looking-at haredoc-symbol-regex) (match-string 1)))) ;; resolve identifier - (car (process-lines haredoc-helperbin "-p" (haredoc/root-dir) - "resolve" id - (buffer-file-name))))) + (car (haredoc|harehelper "-p" (haredoc/root-dir) + "resolve" id + (buffer-file-name))))) (define-button-type 'haredoc 'action #'haredoc/button-action @@ -200,9 +201,8 @@ If SIGN is nil, ask user." (ensure-empty-lines 1) (with-environment-variables - (("HAREPATH" (car (process-lines haredoc-helperbin - "-p" (haredoc/root-dir) - "-P")))) + (("HAREPATH" (car (haredoc|harehelper "-p" (haredoc/root-dir) + "-P")))) (call-process "haredoc" nil t nil "-a" "-F" "tty" id)) (ansi-color-apply-on-region (point-min) (point-max)) @@ -213,7 +213,9 @@ If SIGN is nil, ask user." (haredoc/make-button 1 current-ns)) (goto-char (point-max)) - (when (or (length> components 1) haredoc-backward-stack haredoc-forward-stack) + (when (or (length> components 1) + haredoc-backward-stack + haredoc-forward-stack) (ensure-empty-lines 2) (when haredoc-backward-stack (insert-text-button "[back]" :type 'haredoc-back) @@ -240,21 +242,15 @@ If SIGN is nil, ask user." the executable at `haredoc-helperbin'. Return value is a string \"/path/to/file.ha:line:column\"." - (catch 'exit - (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))))) + (when-let* ((loc (car (haredoc|harehelper "-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-definition (&optional name) @@ -312,7 +308,9 @@ declaration at point." (define-minor-mode haredoc-nav-mode "Enable navigation of hare symbols and easy access to haredoc pages." :keymap haredoc-map - :group 'haredoc) + :group 'haredoc + (add-hook 'xref-backend-functions + #'haredoc-xref-backend nil t)) (defvar-keymap haredoc-mode-map "C-c C-h" 'haredoc @@ -327,7 +325,8 @@ declaration at point." (define-derived-mode haredoc-mode help-mode "🐇" "Major mode for navigating hare documentation." :group 'haredoc - (setq header-line-format `((:propertize ,haredoc-current-path face bold)))) + (setq header-line-format + `((:propertize ,haredoc-current-path face bold)))) ;;; xref backend @@ -338,19 +337,25 @@ declaration at point." 'haredoc-xref nil)) -(cl-defmethod xref-backend-identifier-at-point ((_backend (eql 'haredoc-xref))) +(cl-defmethod xref-backend-identifier-at-point + ((_backend (eql 'haredoc-xref))) (haredoc/identifier-at-point)) -(cl-defmethod xref-backend-definitions ((_backend (eql 'haredoc-xref)) symbol) +(cl-defmethod xref-backend-definitions ((_backend (eql 'haredoc-xref)) + symbol) (when-let* ((loc (haredoc/get-location symbol))) (list loc))) -(cl-defmethod xref-backend-identifier-completion-table ((_backend (eql 'haredoc-xref))) +(cl-defmethod xref-backend-identifier-completion-table + ((_backend (eql 'haredoc-xref))) '()) -(cl-defmethod xref-backend-references ((_backend (eql 'haredoc-xref)) identifier) - (let* ((lines (process-lines haredoc-helperbin "-p" (haredoc/root-dir) "find-references" - identifier (haredoc/root-dir)))) +(cl-defmethod xref-backend-references ((_backend (eql 'haredoc-xref)) + identifier) + (let* ((lines (haredoc|harehelper "-p" (haredoc/root-dir) + "find-references" + identifier + (haredoc/root-dir)))) (mapcar (lambda (line) (let* ((loc (string-split line " " t)) (summary (string-join (cdr loc) " "))