feat(selectrum): Add config from Townk's private config

Co-authored-by: Townk <thiago@rapinialves.com>
This commit is contained in:
Edmund Miller 2021-02-16 16:46:35 -06:00 committed by Itai Y. Efrat
parent a0ff8d42c1
commit 42b3184eae
2 changed files with 154 additions and 45 deletions

View file

@ -1,63 +1,124 @@
;;; completion/selectrum/autoload/selectrum.el -*- lexical-binding: t; -*-
;;;###autoload
(defadvice! +selectrum--company-capf--candidates-a (fn &rest args)
"Function to help company to highlight all candidates with just
one face."
:around 'company-capf--candidates
(let ((orderless-match-faces [completions-common-part]))
(apply fn args)))
;;;###autoload
(defadvice! +selectrum--+default/yank-pop-a (&rest _)
"Interactively select what text to insert from the kill ring."
:override '+default/yank-pop
(interactive "P")
(call-interactively
(cond ((fboundp 'counsel-yank-pop) #'counsel-yank-pop)
((fboundp 'consult-yank-pop) #'consult-yank-pop)
((fboundp 'helm-show-kill-ring) #'helm-show-kill-ring)
((error "No kill-ring search backend available. Enable ivy or helm!")))))
;;;###autoload
(defadvice! +selectrum--+default/search-project-a (&optional arg)
"Conduct a text search in the current project root.
If prefix ARG is set, include ignored/hidden files."
:override '+default/search-project
(interactive "P")
(let* ((projectile-project-root nil)
(disabled-command-function nil)
(current-prefix-arg (unless (eq arg 'other) arg))
(default-directory
(if (eq arg 'other)
(if-let (projects (projectile-relevant-known-projects))
(completing-read "Search project: " projects nil t)
(user-error "There are no known projects"))
default-directory)))
(call-interactively
(cond ((featurep! :completion ivy) #'+ivy/project-search)
((featurep! :completion helm) #'+helm/project-search)
((fboundp 'consult--grep) #'+selectrum/project-search)
(#'projectile-ripgrep)))))
;;;###autoload
(defadvice! +selectrum--+default/search-cwd-a (&optional arg)
"Conduct a text search in files under the current folder.
If prefix ARG is set, prompt for a directory to search from."
:override '+default/search-cwd
(interactive "P")
(let ((default-directory
(if arg
(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)
((fboundp 'consult--grep) #'+selectrum/project-search-from-cwd)
(#'rgrep)))))
;;;###autoload ;;;###autoload
(cl-defun +selectrum-file-search (&key query in all-files (recursive t) prompt args) (cl-defun +selectrum-file-search (&key query in all-files (recursive t) prompt args)
"Conduct a file search using ripgrep. "Conduct a file search using ripgrep.
:query STRING :query STRING
Determines the initial input to search for. Determines the initial input to search for.
:in PATH :in PATH
Sets what directory to base the search out of. Defaults to the current project's root. Sets what directory to base the search out of. Defaults to the current
project's root.
:recursive BOOL :recursive BOOL
Whether or not to search files recursively from the base directory." Whether or not to search files recursively from the base directory."
(declare (indent defun)) (declare (indent defun))
(unless (executable-find "rg") (unless (executable-find "rg")
(user-error "Couldn't find ripgrep in your PATH")) (user-error "Couldn't find ripgrep in your PATH"))
(require 'consult) (require 'consult)
(setq deactivate-mark t) (let* ((this-command 'consult-ripgrep)
(let* ((this-command 'consult--grep)
(project-root (or (doom-project-root) default-directory)) (project-root (or (doom-project-root) default-directory))
(directory (or in project-root)) (directory (or in project-root))
(args (split-string (ripgrep-command (seq-remove 'null
(string-trim (append (butlast consult-ripgrep-command)
(concat (if all-files "-uu") (list
(unless recursive "--maxdepth 1") (when all-files "-uu")
"--null --line-buffered --color=always --max-columns=500 --no-heading --line-number" (unless recursive " --maxdepth 1")
" --hidden -g!.git " "--hidden"
(mapconcat #'shell-quote-argument args " "))) "-g!.git")
" ")) args
'("-e"))))
(prompt (or prompt (prompt (or prompt
(format "rg [%s]: " (format "%s [%s]: "
(cond ((equal directory default-directory) (mapconcat #'identity ripgrep-command " ")
"./") (cond ((equal directory default-directory) "./")
((equal directory project-root) ((equal directory project-root) (projectile-project-name))
(projectile-project-name))
((file-relative-name directory project-root)))))) ((file-relative-name directory project-root))))))
(query (or query (query (or query
(when (doom-region-active-p) (when (doom-region-active-p)
(replace-regexp-in-string (replace-regexp-in-string
"[! |]" (lambda (substr) "[! |]" (lambda (substr)
(cond ((and (string= substr " ") (cond ((string= substr " ") " ")
(not (featurep! +fuzzy))) ((string= substr "|") "\\\\\\\\|")
" ")
((string= substr "|")
"\\\\\\\\|")
((concat "\\\\" substr)))) ((concat "\\\\" substr))))
(rxt-quote-pcre (doom-thing-at-point-or-region)))))) (rxt-quote-pcre (doom-thing-at-point-or-region))))
(ripgrep-command `("rg" ,@args "." "-e"))) " ")))
;; (setq deactivate-mark t)
(consult--grep prompt ripgrep-command directory query))) (consult--grep prompt ripgrep-command directory query)))
;;;###autoload ;;;###autoload
(defun +selectrum/project-search (&optional arg initial-query directory) (defun +selectrum/project-search (&optional arg initial-query directory)
"Peforms a live project search from the project root using ripgrep. "Performs a live project search from the project root using ripgrep.
If ARG (universal argument), include all files, even hidden or compressed ones, If ARG (universal argument), include all files, even hidden or compressed ones,
in the search." in the search."
(interactive "P") (interactive "P")
(+selectrum-file-search :query initial-query :in directory :all-files arg)) (+selectrum-file-search
:prompt (format "Find text on project files \[%s\]"
(if (or (and (not directory) (doom-project-root))
(and directory (equal directory (doom-project-root))))
(projectile-project-name)
(file-relative-name (or directory (doom-project-root) default-directory))))
:query initial-query
:in directory
:all-files arg))
;;;###autoload ;;;###autoload
(defun +selectrum/project-search-from-cwd (&optional arg initial-query) (defun +selectrum/project-search-from-cwd (&optional arg initial-query)
"Performs a live project search from the current directory. "Performs a project search recursively from the current directory.
If ARG (universal argument), include all files, even hidden or compressed ones." If ARG (universal argument), include all files, even hidden or compressed ones."
(interactive "P") (interactive "P")
(+selectrum/project-search arg initial-query default-directory)) (+selectrum/project-search arg initial-query default-directory))

View file

@ -2,22 +2,55 @@
(use-package! selectrum (use-package! selectrum
:hook (doom-first-input . selectrum-mode) :hook (doom-first-input . selectrum-mode)
:config :init
(setq selectrum-extend-current-candidate-highlight t (setq selectrum-display-action nil
selectrum-num-candidates-displayed 15
selectrum-extend-current-candidate-highlight t
selectrum-fix-minibuffer-height t) selectrum-fix-minibuffer-height t)
(unless (featurep! +orderless) (when (featurep! +orderless)
(setq completion-styles '(substring partial-completion))))
(when (featurep! +prescient) (setq completion-styles '(substring partial-completion)
(use-package! selectrum-prescient selectrum-refine-candidates-function #'orderless-filter
:after selectrum selectrum-highlight-candidates-function #'orderless-highlight-matches))
:hook ((selectrum-mode . selectrum-prescient-mode)
(selectrum-mode . prescient-persist-mode)))) (when (featurep! +prescient)
(use-package! selectrum-prescient
:after selectrum
:hook ((selectrum-mode . selectrum-prescient-mode)
(selectrum-mode . prescient-persist-mode))
:config
(setq selectrum-preprocess-candidates-function #'selectrum-prescient--preprocess)
(add-hook 'selectrum-candidate-selected-hook #'selectrum-prescient--remember)
(add-hook 'selectrum-candidate-inserted-hook #'selectrum-prescient--remember)))
:config
(defadvice! +selectrum-refresh-on-cycle (&rest _)
:after 'marginalia-cycle
(when (bound-and-true-p selectrum-mode) (selectrum-exhibit)))
(map!
:g "C-s-r" #'selectrum-repeat
(:map selectrum-minibuffer-map
:geni "M-RET" #'selectrum-submit-exact-input
:geni "C-j" #'selectrum-next-candidate
:geni "C-S-j" #'selectrum-next-page
:geni "C-s-j" #'selectrum-goto-end
:geni "C-k" #'selectrum-previous-candidate
:geni "C-S-k" #'selectrum-previous-page
:geni "C-s-k" #'selectrum-goto-beginning)))
(use-package! orderless (use-package! orderless
:when (featurep! +orderless) :when (featurep! +orderless)
:defer t
:init
(setq orderless-component-separator "[ &]"
orderless-matching-styles '(orderless-prefixes
orderless-initialism
orderless-regexp))
:config :config
(setq completion-styles '(orderless))) (setq completion-styles '(orderless))
(setq orderless-skip-highlighting (lambda () selectrum-active-p))
(setq selectrum-highlight-candidates-function #'orderless-highlight-matches))
(use-package! consult (use-package! consult
:defer t :defer t
@ -36,7 +69,12 @@
[remap load-theme] #'consult-theme [remap load-theme] #'consult-theme
[remap recentf-open-files] #'consult-recent-file) [remap recentf-open-files] #'consult-recent-file)
:config :config
(setq consult-project-root-function #'projectile-project-root)) (setq consult-project-root-function #'doom-project-root)
(setq completion-in-region-function #'consult-completion-in-region)
(setq consult-narrow-key "<")
(setq consult-line-numbers-widen t)
(setq consult-async-input-debounce 0.5)
(setq consult-async-input-throttle 0.8))
(use-package! consult-flycheck (use-package! consult-flycheck
:when (featurep! :checkers syntax) :when (featurep! :checkers syntax)
@ -45,12 +83,22 @@
(use-package! embark (use-package! embark
:defer t :defer t
:init :init
(define-key! (map!
"C-S-a" #'embark-act) :g "C-s-e" #'embark-act
;; TODO need to figure out what keybindings to put here (:map minibuffer-local-completion-map
(define-key! selectrum-minibuffer-map
"C-c C-o" #'embark-export "C-c C-o" #'embark-export
"C-c C-c" #'embark-act-noexit)) "C-c C-c" #'embark-act-noexit)
(:map embark-file-map
:desc "Open Dired on target" :g "j" #'ffap-dired
:desc "Open target with sudo" :g "s" #'sudo-edit
:desc "Open target with vlf" :g "l" #'vlf)
(:map embark-file-map
:desc "Cycle marginalia views" :g "A" #'marginalia-cycle))
(setq embark-action-indicator
(lambda (map)
(which-key--show-keymap "Embark" map nil nil 'no-paging)
#'which-key--hide-popup-ignore-command)
embark-become-indicator embark-action-indicator))
(use-package! marginalia (use-package! marginalia
:after selectrum :after selectrum