feat(ai): refactor path tag helpers and add absolute version

This commit is contained in:
2026-03-23 15:39:10 +08:00
parent ec119361e5
commit 20a2f69501
+25 -16
View File
@@ -172,30 +172,39 @@
(gptel-rewrite-region (region-beginning) (region-end) (gptel-rewrite-region (region-beginning) (region-end)
"Fix spelling, grammar, and improve clarity. Keep the same tone and style."))) "Fix spelling, grammar, and improve clarity. Keep the same tone and style.")))
(defun my--get-region-as-path-tag (path-fn)
"Helper to get path tag using PATH-FN to determine the file path."
(let* ((filename (funcall path-fn))
(start-line (line-number-at-pos (if (use-region-p) (region-beginning) (point))))
(tag (if (use-region-p)
(let* ((end-pos (1- (region-end)))
(end-line (line-number-at-pos (max (region-beginning) end-pos))))
(format "@%s:%d-%d" filename start-line end-line))
(format "@%s:%d" filename start-line))))
tag))
(defun my/copy-region-as-path-tag () (defun my/copy-region-as-path-tag ()
"Copy the current region as a path tag: @path/to/file.ext:start-end" "Copy the current region as a relative path tag: @path/to/file.ext:start-end"
(interactive) (interactive)
(if (use-region-p) (let ((tag (my--get-region-as-path-tag
(let* ((filename (file-relative-name (buffer-file-name) (doom-project-root))) (lambda () (file-relative-name (buffer-file-name) (doom-project-root))))))
(start-line (line-number-at-pos (region-beginning))) (kill-new tag)
;; If region ends at beginning of a line, we want the previous line (message "Copied: %s" tag)))
(end-pos (1- (region-end)))
(end-line (line-number-at-pos (max (region-beginning) end-pos))) (defun my/copy-region-as-path-tag-absolute ()
(tag (format "@%s:%d-%d" filename start-line end-line))) "Copy the current region as an absolute path tag: @/abs/path/to/file.ext:start-end"
(kill-new tag) (interactive)
(message "Copied: %s" tag)) (let ((tag (my--get-region-as-path-tag #'buffer-file-name)))
(let* ((filename (file-relative-name (buffer-file-name) (doom-project-root))) (kill-new tag)
(line (line-number-at-pos)) (message "Copied: %s" tag)))
(tag (format "@%s:%d" filename line)))
(kill-new tag)
(message "Copied: %s" tag))))
;; Add keybindings for helper functions ;; Add keybindings for helper functions
(map! :leader (map! :leader
(:prefix "A" (:prefix "A"
:desc "Quick explain" "e" #'my/gptel-quick-explain-region :desc "Quick explain" "e" #'my/gptel-quick-explain-region
:desc "Proofread text" "p" #'my/gptel-proofread-region :desc "Proofread text" "p" #'my/gptel-proofread-region
:desc "Copy path tag" "t" #'my/copy-region-as-path-tag)) :desc "Copy path tag (relative)" "t" #'my/copy-region-as-path-tag
:desc "Copy path tag (absolute)" "T" #'my/copy-region-as-path-tag-absolute))
;;; Claude Code Configuration ;;; Claude Code Configuration