From 65c153796e6b1f68d7283e3232dcf5d73cb09311 Mon Sep 17 00:00:00 2001 From: Jethro Kuan Date: Sat, 16 Jan 2021 20:21:50 +0800 Subject: [PATCH] Add selectrum project search --- .../selectrum/autoload/selectrum.el | 63 +++++++++++++++++++ modules/completion/selectrum/config.el | 27 ++++++-- modules/completion/selectrum/packages.el | 6 +- modules/config/default/autoload/search.el | 10 +-- 4 files changed, 97 insertions(+), 9 deletions(-) create mode 100644 modules/completion/selectrum/autoload/selectrum.el diff --git a/modules/completion/selectrum/autoload/selectrum.el b/modules/completion/selectrum/autoload/selectrum.el new file mode 100644 index 000000000..d8135b4e2 --- /dev/null +++ b/modules/completion/selectrum/autoload/selectrum.el @@ -0,0 +1,63 @@ +;;;###autoload +(cl-defun +selectrum-file-search (&key query in all-files (recursive t) prompt args) + "Conduct a file search using ripgrep. + +:query STRING + Determines the initial input to search for. +:in PATH + Sets what directory to base the search out of. Defaults to the current project's root. +:recursive BOOL + Whether or not to search files recursively from the base directory." + (declare (indent defun)) + (unless (executable-find "rg") + (user-error "Couldn't find ripgrep in your PATH")) + (require 'consult) + (setq deactivate-mark t) + (let* ((this-command 'consult--grep) + (project-root (or (doom-project-root) default-directory)) + (directory (or in project-root)) + (args (split-string + (string-trim + (concat (if all-files "-uu") + (unless recursive "--maxdepth 1") + "--null --line-buffered --color=always --max-columns=500 --no-heading --line-number" + " --hidden -g!.git " + (mapconcat #'shell-quote-argument args " "))) + " ")) + (prompt (or prompt + (format "rg [%s]: " + (cond ((equal directory default-directory) + "./") + ((equal directory project-root) + (projectile-project-name)) + ((file-relative-name directory project-root)))))) + (query (or query + (when (doom-region-active-p) + (replace-regexp-in-string + "[! |]" (lambda (substr) + (cond ((and (string= substr " ") + (not (featurep! +fuzzy))) + " ") + ((string= substr "|") + "\\\\\\\\|") + ((concat "\\\\" substr)))) + (rxt-quote-pcre (doom-thing-at-point-or-region)))))) + (ripgrep-command `("rg" ,@args "." "-e"))) + (consult--grep prompt ripgrep-command directory query))) + +;;;###autoload +(defun +selectrum/project-search (&optional arg initial-query directory) + "Peforms a live project search from the project root using ripgrep. + +If ARG (universal argument), include all files, even hidden or compressed ones, +in the search." + (interactive "P") + (+selectrum-file-search :query initial-query :in directory :all-files arg)) + +;;;###autoload +(defun +selectrum/project-search-from-cwd (&optional arg initial-query) + "Performs a live project search from the current directory. + +If ARG (universal argument), include all files, even hidden or compressed ones." + (interactive "P") + (+selectrum/project-search arg initial-query default-directory)) diff --git a/modules/completion/selectrum/config.el b/modules/completion/selectrum/config.el index e838d3254..b6f1c24db 100644 --- a/modules/completion/selectrum/config.el +++ b/modules/completion/selectrum/config.el @@ -1,7 +1,12 @@ ;;; completion/selectrum/config.el -*- lexical-binding: t; -*- (use-package! selectrum - :hook (doom-first-input . selectrum-mode)) + :hook (doom-first-input . selectrum-mode) + :config + (setq selectrum-extend-current-candidate-highlight t + selectrum-fix-minibuffer-height t) + (unless (featurep! +orderless) + (setq completion-styles '(substring partial-completion)))) (when (featurep! +prescient) (use-package! selectrum-prescient @@ -9,9 +14,15 @@ :hook ((doom-first-input . selectrum-prescient-mode) (doom-first-input . prescient-persist-mode)))) +(use-package! orderless + :when (featurep! +orderless) + :config + (setq completion-styles '(orderless))) + (use-package! consult :defer t :init + (fset 'multi-occur #'consult-multi-occur) (define-key! [remap apropos] #'consult-apropos [remap goto-line] #'consult-goto-line @@ -22,14 +33,22 @@ [remap man] #'consult-man [remap yank-pop] #'consult-yank-pop [remap locate] #'consult-locate - [remap load-theme] #'consult-theme)) + [remap load-theme] #'consult-theme + [remap recentf-open-files] #'consult-recent-file) + :config + (setq consult-project-root-function #'projectile-project-root)) -(use-package! consult-flycheck) +(use-package! consult-flycheck + :when (featurep! :checkers syntax) + :after (consult flycheck)) (use-package! embark :init (define-key! - "C-S-a" #'embark-act)) + "C-S-a" #'embark-act) + (define-key! selectrum-minibuffer-map + "C-c C-o" #'embark-export + "C-c C-c" #'embark-act-noexit)) (use-package! marginalia :after selectrum diff --git a/modules/completion/selectrum/packages.el b/modules/completion/selectrum/packages.el index 7b569cef7..7c0853154 100644 --- a/modules/completion/selectrum/packages.el +++ b/modules/completion/selectrum/packages.el @@ -6,8 +6,12 @@ (when (featurep! +prescient) (package! selectrum-prescient)) +(when (featurep! +orderless) + (package! orderless)) + (package! consult) -(package! consult-flycheck) +(when (featurep! :checkers syntax) + (package! consult-flycheck)) (package! embark) (package! embark-consult) diff --git a/modules/config/default/autoload/search.el b/modules/config/default/autoload/search.el index 41bd8489c..766d2b744 100644 --- a/modules/config/default/autoload/search.el +++ b/modules/config/default/autoload/search.el @@ -10,8 +10,9 @@ If prefix ARG is set, prompt for a directory to search from." (read-directory-name "Search directory: ") default-directory))) (call-interactively - (cond ((featurep! :completion ivy) #'+ivy/project-search-from-cwd) - ((featurep! :completion helm) #'+helm/project-search-from-cwd) + (cond ((featurep! :completion ivy) #'+ivy/project-search-from-cwd) + ((featurep! :completion helm) #'+helm/project-search-from-cwd) + ((featurep! :completion selectrum) #'+selectrum/project-search-from-cwd) (#'rgrep))))) ;;;###autoload @@ -45,8 +46,9 @@ If prefix ARG is set, include ignored/hidden files." (user-error "There are no known projects")) default-directory))) (call-interactively - (cond ((featurep! :completion ivy) #'+ivy/project-search) - ((featurep! :completion helm) #'+helm/project-search) + (cond ((featurep! :completion ivy) #'+ivy/project-search) + ((featurep! :completion helm) #'+helm/project-search) + ((featurep! :completion selectrum) #'+selectrum/project-search) (#'projectile-ripgrep))))) ;;;###autoload