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
(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.
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)
(let* ((this-command 'consult-ripgrep)
(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 " ")))
" "))
(ripgrep-command (seq-remove 'null
(append (butlast consult-ripgrep-command)
(list
(when all-files "-uu")
(unless recursive " --maxdepth 1")
"--hidden"
"-g!.git")
args
'("-e"))))
(prompt (or prompt
(format "rg [%s]: "
(cond ((equal directory default-directory)
"./")
((equal directory project-root)
(projectile-project-name))
(format "%s [%s]: "
(mapconcat #'identity ripgrep-command " ")
(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 "|")
"\\\\\\\\|")
(cond ((string= substr " ") " ")
((string= substr "|") "\\\\\\\\|")
((concat "\\\\" substr))))
(rxt-quote-pcre (doom-thing-at-point-or-region))))))
(ripgrep-command `("rg" ,@args "." "-e")))
(rxt-quote-pcre (doom-thing-at-point-or-region))))
" ")))
;; (setq deactivate-mark t)
(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.
"Performs 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))
(+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
(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."
(interactive "P")
(+selectrum/project-search arg initial-query default-directory))

View file

@ -2,22 +2,55 @@
(use-package! selectrum
:hook (doom-first-input . selectrum-mode)
:config
(setq selectrum-extend-current-candidate-highlight t
:init
(setq selectrum-display-action nil
selectrum-num-candidates-displayed 15
selectrum-extend-current-candidate-highlight t
selectrum-fix-minibuffer-height t)
(unless (featurep! +orderless)
(setq completion-styles '(substring partial-completion))))
(when (featurep! +orderless)
(when (featurep! +prescient)
(use-package! selectrum-prescient
:after selectrum
:hook ((selectrum-mode . selectrum-prescient-mode)
(selectrum-mode . prescient-persist-mode))))
(setq completion-styles '(substring partial-completion)
selectrum-refine-candidates-function #'orderless-filter
selectrum-highlight-candidates-function #'orderless-highlight-matches))
(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
:when (featurep! +orderless)
:defer t
:init
(setq orderless-component-separator "[ &]"
orderless-matching-styles '(orderless-prefixes
orderless-initialism
orderless-regexp))
: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
:defer t
@ -36,7 +69,12 @@
[remap load-theme] #'consult-theme
[remap recentf-open-files] #'consult-recent-file)
: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
:when (featurep! :checkers syntax)
@ -45,12 +83,22 @@
(use-package! embark
:defer t
:init
(define-key!
"C-S-a" #'embark-act)
;; TODO need to figure out what keybindings to put here
(define-key! selectrum-minibuffer-map
(map!
:g "C-s-e" #'embark-act
(:map minibuffer-local-completion-map
"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
:after selectrum