Skip to content

Commit

Permalink
Minor counsel-git-grep cleanup
Browse files Browse the repository at this point in the history
* counsel.el (counsel-git-grep-function): Simplify.
(counsel--git-grep-file-and-line-number): Rename...
(counsel--git-grep-file-and-line): ...to this, splitting long lines.
(counsel--git-grep-go-to-location): Rename...
(counsel--git-grep-visit): ...to this, incorporating more DRY.
(counsel-git-grep-action, counsel-git-grep-action-other-window):
Adapt accordingly (#3044).
  • Loading branch information
basil-conto committed May 12, 2024
1 parent b316b8d commit 058cb3a
Showing 1 changed file with 30 additions and 35 deletions.
65 changes: 30 additions & 35 deletions counsel.el
Original file line number Diff line number Diff line change
Expand Up @@ -1429,49 +1429,44 @@ This function should set `ivy--old-re'."
"Grep in the current Git repository for STRING."
(or
(ivy-more-chars)
(progn
(counsel--async-command
(concat
(funcall counsel-git-grep-cmd-function string)
(if (ivy--case-fold-p string) " -i" "")))
nil)))
(ignore
(counsel--async-command
(concat
(funcall counsel-git-grep-cmd-function string)
(and (ivy--case-fold-p string) " -i"))))))

(defun counsel-git-grep-action (x)
"Go to occurrence X in current Git repository."
(let ((file-and-line-number (counsel--git-grep-file-and-line-number x)))
(when file-and-line-number
(find-file (expand-file-name
(car file-and-line-number)
(ivy-state-directory ivy-last)))
(counsel--git-grep-go-to-location (cdr file-and-line-number)))))
(counsel--git-grep-visit x))

(defun counsel-git-grep-action-other-window (x)
"Go to occurrence X in current Git repository in another window."
(let ((file-and-line-number (counsel--git-grep-file-and-line-number x)))
(when file-and-line-number
(find-file-other-window (expand-file-name
(car file-and-line-number)
(ivy-state-directory ivy-last)))
(counsel--git-grep-go-to-location (cdr file-and-line-number)))))

(defun counsel--git-grep-file-and-line-number (x)
(counsel--git-grep-visit x t))

(defun counsel--git-grep-file-and-line (x)
"Extract file name and line number from `counsel-git-grep' line X.
Return a pair (FILE . LINE) on success; nil otherwise."
(when (string-match "\\`\\(.*?\\):\\([0-9]+\\):\\(.*\\)\\'" x)
(cons (match-string-no-properties 1 x) (string-to-number (match-string-no-properties 2 x)))))

(defun counsel--git-grep-go-to-location (line-number)
"Go to LINE-NUMBER within current buffer."
(goto-char (point-min))
(forward-line (1- line-number))
(when (re-search-forward (ivy--regex ivy-text t) (line-end-position) t)
(when swiper-goto-start-of-match
(goto-char (match-beginning 0))))
(swiper--ensure-visible)
(run-hooks 'counsel-grep-post-action-hook)
(unless (eq ivy-exit 'done)
(swiper--cleanup)
(swiper--add-overlays (ivy--regex ivy-text))))
(and (string-match "\\`\\(.*?\\):\\([0-9]+\\):\\(.*\\)\\'" x)
(cons (match-string-no-properties 1 x)
(string-to-number (match-string-no-properties 2 x)))))

(defun counsel--git-grep-visit (cand &optional other-window)
"Visit `counsel-git-grep' CAND, optionally in OTHER-WINDOW."
(let ((file-and-line (counsel--git-grep-file-and-line cand)))
(when file-and-line
(funcall (if other-window #'find-file-other-window #'find-file)
(expand-file-name (car file-and-line)
(ivy-state-directory ivy-last)))
(goto-char (point-min))
(forward-line (1- (cdr file-and-line)))
(when (re-search-forward (ivy--regex ivy-text t) (line-end-position) t)
(when swiper-goto-start-of-match
(goto-char (match-beginning 0))))
(swiper--ensure-visible)
(run-hooks 'counsel-grep-post-action-hook)
(unless (eq ivy-exit 'done)
(swiper--cleanup)
(swiper--add-overlays (ivy--regex ivy-text))))))

(ivy-set-actions
'counsel-git-grep
Expand Down

0 comments on commit 058cb3a

Please sign in to comment.