emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] master 294ec7f 4/5: `counsel-git-grep' can now handle huge git re


From: Oleh Krehel
Subject: [elpa] master 294ec7f 4/5: `counsel-git-grep' can now handle huge git repos
Date: Mon, 20 Apr 2015 17:33:00 +0000

branch: master
commit 294ec7f480908268055b273da96674382b84e198
Author: Oleh Krehel <address@hidden>
Commit: Oleh Krehel <address@hidden>

    `counsel-git-grep' can now handle huge git repos
    
    * counsel.el (counsel-git-grep-count): Return a number instead of a
      string. Apply `ivy--regex' on the input.
    (counsel--git-grep-count): New defvar.
    (counsel-git-grep-function): If the repo has >20000 lines, use `head' 5000
    instead, but still display the true amount of matches.
    (counsel-git-grep): Set `ivy--dynamic-function'.
    (counsel-locate-function): Update.
    
    * ivy.el (ivy--dynamic-function): New defvar. When this isn't nil, it
      will be called to get a new batch of candidates in case the `ivy-text'
      was changed.
    (ivy--full-length): The true candidates length in case of `head' 
shenanigans.
    (ivy--old-text): Store the old text for when `ivy--dynamic-function' was
    called last.
    (ivy--insert-prompt): Update.
    (ivy--exhibit): Don't do filtering for non-nil `ivy--dynamic-function' -
    let the caller (usually a shell tool) do the filtering.
    (ivy--insert-minibuffer): New defun, created from a part of
    `ivy--exhibit'.
---
 counsel.el |   51 +++++++++++++++++++++++++++++++++------------------
 ivy.el     |   58 ++++++++++++++++++++++++++++++++++++----------------------
 2 files changed, 69 insertions(+), 40 deletions(-)

diff --git a/counsel.el b/counsel.el
index 85ab584..03c0b29 100644
--- a/counsel.el
+++ b/counsel.el
@@ -177,24 +177,42 @@
 
 (defun counsel-git-grep-count (str)
   "Quickly count the amount of git grep STR matches."
-  (shell-command-to-string
-   (format "git grep -c '%s' | sed 's/.*:\\(.*\\)/\\1/g' | awk '{s+=$1} END 
{print s}'" str)))
+  (let ((out (shell-command-to-string
+              (format "git grep -c '%s' | sed 's/.*:\\(.*\\)/\\1/g' | awk 
'{s+=$1} END {print s}'"
+                      (ivy--regex str)))))
+    (string-to-number out)))
+
+(defvar counsel--git-grep-count nil
+  "Store the line count in current repository.")
 
 (defun counsel-git-grep-function (string &optional _pred &rest _unused)
   "Grep in the current git repository for STRING."
-  (split-string
-   (shell-command-to-string
-    (format "git --no-pager grep --full-name -n --no-color -i -e \"%s\"" 
string))
-   "\n"
-   t))
+  (if (and (> counsel--git-grep-count 20000)
+           (< (length string) 3))
+      (progn
+        (setq ivy--full-length counsel--git-grep-count)
+        (list ""
+              (format "%d chars more" (- 3 (length ivy-text)))))
+    (let ((cmd-t "git --no-pager grep --full-name -n --no-color -i -e \"%s\"")
+          res)
+      (if (<= counsel--git-grep-count 20000)
+          (progn
+            (setq res (shell-command-to-string (format cmd-t string)))
+            (setq ivy--full-length nil))
+        (setq res (shell-command-to-string (concat (format cmd-t (ivy--regex 
string)) " | head -n 5000")))
+        (setq ivy--full-length (counsel-git-grep-count ivy-text)))
+      (split-string res "\n" t))))
 
 (defun counsel-git-grep ()
   "Grep for a string in the current git repository."
   (interactive)
-  (let ((default-directory (locate-dominating-file
+  (let* ((counsel--git-grep-count (counsel-git-grep-count ""))
+         (ivy--dynamic-function (when (> counsel--git-grep-count 20000)
+                                  'counsel-git-grep-function))
+         (default-directory (locate-dominating-file
                              default-directory ".git"))
-        (val (ivy-read "pattern: " 'counsel-git-grep-function))
-        lst)
+         (val (ivy-read "pattern: " 'counsel-git-grep-function))
+         lst)
     (when val
       (setq lst (split-string val ":"))
       (find-file (car lst))
@@ -202,14 +220,11 @@
       (forward-line (1- (string-to-number (cadr lst)))))))
 
 (defun counsel-locate-function (str &rest _u)
-  (if (string= str "")
-      '("no" "matches")
-    (setq ivy--all-candidates
-          (split-string
-           (shell-command-to-string (concat "locate -l 20 " str)) "\n" t))
-    (setq ivy--old-re nil)
-    (setq ivy--old-cands nil)
-    (ivy--exhibit)))
+  (if (< (length str) 3)
+      (list ""
+            (format "%d chars more" (- 3 (length ivy-text))))
+    (split-string
+     (shell-command-to-string (concat "locate -i -l 20 --regex " (ivy--regex 
str))) "\n" t)))
 
 (defun counsel-locate ()
   "Call locate."
diff --git a/ivy.el b/ivy.el
index f6e4c1f..88825aa 100644
--- a/ivy.el
+++ b/ivy.el
@@ -524,6 +524,15 @@ Turning on Ivy mode will set `completing-read-function' to
     (goto-char (minibuffer-prompt-end))
     (delete-region (line-end-position) (point-max))))
 
+(defvar ivy--dynamic-function nil
+  "When this is non-nil, call it for each input change to get new candidates.")
+
+(defvar ivy--full-length nil
+  "When `ivy--dynamic-function' is non-nil, this can be the total amount of 
candidates.")
+
+(defvar ivy--old-text nil
+  "Store old `ivy-text' for dynamic completion.")
+
 (defun ivy--insert-prompt ()
   "Update the prompt according to `ivy--prompt'."
   (when ivy--prompt
@@ -532,7 +541,10 @@ Turning on Ivy mode will set `completing-read-function' to
            (format
             (if ivy--directory
                 (concat ivy--prompt (abbreviate-file-name ivy--directory))
-              ivy--prompt) ivy--length)))
+              ivy--prompt)
+            (or (and ivy--dynamic-function
+                     ivy--full-length)
+                ivy--length))))
       (save-excursion
         (goto-char (point-min))
         (delete-region (point-min) (minibuffer-prompt-end))
@@ -544,32 +556,34 @@ Turning on Ivy mode will set `completing-read-function' to
       ;; get out of the prompt area
       (constrain-to-field nil (point-max)))))
 
-(defvar ivy--dynamic-function nil
-  "When this is non-nil, call it for each input change to get new candidates.")
-
 (defun ivy--exhibit ()
   "Insert Ivy completions display.
 Should be run via minibuffer `post-command-hook'."
   (setq ivy-text (ivy--input))
+  (if ivy--dynamic-function
+      ;; while-no-input would cause annoying
+      ;; "Waiting for process to die...done" message interruptions
+      (progn
+        (unless (equal ivy--old-text ivy-text)
+          (let ((store ivy--dynamic-function)
+                (ivy--dynamic-function nil))
+            (setq ivy--all-candidates (funcall store ivy-text)))
+          (setq ivy--old-text ivy-text))
+        (ivy--insert-minibuffer (ivy--format ivy--all-candidates)))
+    (when ivy--directory
+      (if (string-match "/$" ivy-text)
+          (if (member ivy-text ivy--all-candidates)
+              (ivy--cd (expand-file-name ivy-text ivy--directory))
+            (ivy--cd "/"))
+        (if (string-match "~$" ivy-text)
+            (ivy--cd (expand-file-name "~/")))))
+    (ivy--insert-minibuffer
+     (ivy--format
+      (ivy--filter ivy-text ivy--all-candidates)))))
+
+(defun ivy--insert-minibuffer (text)
   (ivy--cleanup)
-  (when (and ivy--dynamic-function
-             (not (equal (ivy--regex ivy-text)
-                         ivy--old-re)))
-    (setq ivy--old-re nil)
-    (setq ivy--old-cands nil)
-    (let ((store ivy--dynamic-function)
-          (ivy--dynamic-function nil))
-      (setq ivy--all-candidates (funcall store ivy-text))))
-  (when ivy--directory
-    (if (string-match "/$" ivy-text)
-        (if (member ivy-text ivy--all-candidates)
-            (ivy--cd (expand-file-name ivy-text ivy--directory))
-          (ivy--cd "/"))
-      (if (string-match "~$" ivy-text)
-          (ivy--cd (expand-file-name "~/")))))
-  (let ((text (ivy--format
-               (ivy--filter ivy-text ivy--all-candidates)))
-        (buffer-undo-list t)
+  (let ((buffer-undo-list t)
         deactivate-mark)
     (when ivy--update-fn
       (funcall ivy--update-fn))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]