commit 88513384f42e0f4db4185f8141754cd92541988d
parent 5272930df41002ff9215e7d022af3d895f612884
Author: Lou Woell <lou.woell@posteo.de>
Date: Fri, 10 Oct 2025 06:10:55 +0200
[haredoc.el] work around weird lexer locations
Diffstat:
| M | haredoc.el | | | 39 | ++++++++++++++++++++++++++++++++++++--- |
1 file changed, 36 insertions(+), 3 deletions(-)
diff --git a/haredoc.el b/haredoc.el
@@ -252,7 +252,7 @@ the executable at `haredoc-helperbin'."
(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))))
+ (xref-make sum (xref-make-hare-location file line col))))
;;;###autoload
(defun haredoc/goto-definition (&optional name)
@@ -339,6 +339,39 @@ declaration at point."
'haredoc-xref
nil))
+(cl-defstruct (xref-hare-location
+ (:constructor xref-make-hare-location (file line column))
+ (:include xref-file-location))
+ "Location in a Hare source file.
+
+This specialized class is needed to get around some issues with how the
+hare lexer counts position.")
+
+(cl-defmethod xref-location-marker ((l xref-hare-location))
+ (pcase-let (((cl-struct xref-file-location file line column) l))
+ (with-current-buffer
+ (or (get-file-buffer file)
+ (let ((find-file-suppress-same-file-warnings t))
+ (find-file-noselect file)))
+ (save-restriction
+ (widen)
+ (save-excursion
+ (goto-char (point-min))
+ (ignore-errors
+ ;; xref location may be out of date; it may be past the end of the
+ ;; current file, or the file may have been deleted. Return a
+ ;; reasonable location; the user will figure it out.
+ (beginning-of-line line)
+ ;; the hare lexer counts tabs as 8 columns so we have to use columns
+ ;; instead of characters.
+ (move-to-column column)
+ ;; hack to get around the hare lexers weird location tracking.
+ ;; Should probably be handeled in harehelper at some point.
+ (while (looking-at-p "\n")
+ (forward-line 1)
+ (move-to-column column)))
+ (point-marker))))))
+
(cl-defmethod xref-backend-identifier-at-point
((_backend (eql 'haredoc-xref)))
(haredoc/identifier-at-point))
@@ -360,13 +393,13 @@ declaration at point."
identifier
(haredoc/root-dir))))
(mapcar (lambda (line)
- (let* ((loc (string-split line " " t))
+ (let* ((loc (string-split line ": " t))
(summary (string-join (cdr loc) " "))
(loc (string-split (car loc) ":" t))
(file (nth 0 loc))
(line (string-to-number (nth 1 loc)))
(col (string-to-number (nth 2 loc))))
- (xref-make summary (xref-make-file-location file line col))))
+ (xref-make summary (xref-make-hare-location file line col))))
lines)))
(provide 'haredoc)