diff --git a/modules/completion/corfu/README.org b/modules/completion/corfu/README.org new file mode 100644 index 000000000..f300bde8c --- /dev/null +++ b/modules/completion/corfu/README.org @@ -0,0 +1,155 @@ +#+title: :completion corfu +#+subtitle: Complete with cap(f), cape and a flying feather +#+created: September 9, 2022 +#+since: 3.0.0 (#7002) + +* Description :unfold: +This module provides code completion, powered by [[doom-package:corfu]]. + +It is recommended to enable either this or [[doom-module::completion company]], in +case you desire pre-configured auto-completion. Corfu is much lighter weight and +focused, plus it's built on native Emacs functionality, whereas company is heavy +and highly non-native, but has some extra features and more maturity. + +** Maintainers +- [[doom-user:][@LuigiPiucco]] + +[[doom-contrib-maintainer:][Become a maintainer?]] + +** Module flags +- +icons :: + Display icons beside completion suggestions. +- +tng :: + Known as Tab'n'Go to Company users, changes behavior to invoke completion on + [[kbd:][TAB]]. When Corfu is active, [[kbd:][TAB]] and [[kbd:][S-TAB]] will navigate the completion + candidates. Arrow keys and evil-style movement are still supported. +- +orderless :: + Pull in [[doom-package:orderless]] if necessary and apply multi-component + completion (still needed if [[doom-module::completion vertico]] is active). + +** Packages +- [[doom-package:corfu]] +- [[doom-package:cape]] +- [[doom-package:nerd-icons-corfu]] if [[doom-module::completion corfu +icons]] +- [[doom-package:orderless]] if [[doom-module::completion corfu +orderless]] +- [[doom-package:corfu-terminal]] if [[doom-module::os tty]] + +** Hacks +/No hacks documented for this module./ + +** TODO Changelog +# This section will be machine generated. Don't edit it by hand. +/This module does not have a changelog yet./ + +* Installation +Enable this module in your ~doom!~ block. + +This module has no direct requirements, but some languages may have their own +requirements to fulfill before you get code completion in them (and some +languages may lack code completion support altogether). Run ~$ doom doctor~ to +find out if you're missing any dependencies. Note that Corfu may have support +for completions in languages that have no development intelligence, since it +supports generic, context insensitive candidates such as file names or recurring +words. + +* TODO Usage +#+begin_quote + 🔨 /This module's usage documentation is incomplete./ [[doom-contrib-module:][Complete it?]] +#+end_quote + +** Code completion +By default, completion gets triggered after typing 2 non-space consecutive +characters, or by means of the [[kbd:][C-SPC]] keybinding at any moment. While the popup +is visible, the following relevant keys are available: + +| Keybind | Description | +|----------+------------------------------------------------------------------| +| [[kbd:][]] | Go to next candidate | +| [[kbd:][]] | Go to previous candidate | +| [[kbd:][C-n]] | Go to next candidate | +| [[kbd:][C-p]] | Go to previous candidate | +| [[kbd:][C-j]] | (evil) Go to next candidate | +| [[kbd:][C-k]] | (evil) Go to previous candidate | +| [[kbd:][C-]] | Go to next doc line | +| [[kbd:][C-]] | Go to previous doc line | +| [[kbd:][C-S-n]] | Go to next doc line | +| [[kbd:][C-S-p]] | Go to previous doc line | +| [[kbd:][C-S-j]] | (evil) Go to next doc line | +| [[kbd:][C-S-k]] | (evil) Go to previous doc line | +| [[kbd:][C-h]] | Toggle documentation (if available) | +| [[kbd:][M-m]] | Export to minibuffer (if [[doom-module::completion vertico]]) | +| [[kbd:][M-S-j]] | (evil) Export to minibuffer (if [[doom-module::completion vertico]]) | +| [[kbd:][RET]] | Insert candidate | +| [[kbd:][SPC]] | (after wildcard) Reset completion | +| [[kbd:][DEL]] | Reset completion | +| [[kbd:][C-SPC]] | Complete (unless [[doom-module::completion corfu +tng]]) | +| [[kbd:][C-SPC]] | (when completing) Insert separator DWIM (see below) | + +If you prefer a [[kbd:][TAB]]-centric completion style, enable the [[doom-module::completion +corfu +tng]] flag so that, instead, you trigger completion with [[kbd:][TAB]], getting the +following additional binds: + +| Keybind | Description | +|---------+-----------------------------------------------| +| [[kbd:][TAB]] | Complete | +| [[kbd:][TAB]] | (when completing) Go to next candidate | +| [[kbd:][S-TAB]] | (when completing) Go to previous candidate | + +** Searching with multiple keywords +If the [[doom-module::completion corfu +orderless]] flag is enabled, users can +perform code completion with multiple search keywords by use of space as the +separator. More information can be found [[https://github.com/oantolin/orderless#company][here]]. Pressing [[kdb:][C-SPC]] again while +completing inserts a space as separator. This allows searching with +space-separated terms; each piece will match individually and in any order, with +smart casing. Pressing just [[kbd:][SPC]] acts as normal and quits completion, so that +when typing sentences it doesn't try to complete the whole sentence instead of +just the word. + +Additionally, for users of evil and regular corfu style, [[kdb:][C-SPC]] is smart +regarding your state. In normal-like states, enter insert then start corfu; in +visual-like states, perform [[help:evil-change][evil-change]] (which leaves you in insert state) then +start corfu; in insert-like states, start corfu immediatelly. + +** Exporting to the minibuffer (requires [[doom-module::completion vertico]]) +When using the [[doom-module::completion vertico]] module, which pulls in the +[[doom-package:consult]] package, the entries shown in the completion popup can be +exported to a consult minibuffer, giving access to all the manipulations the +Vertico suite allows. For instance, one could use this to export with +[[doom-package:embark]] via [[kbd:][C-c C-l]] and get a buffer with all candidates. + +* Configuration +A few variables may be set to change behavior of this module: + +- [[var:corfu-auto-delay]] :: + Number of seconds till completion occurs automatically. Defaults to 0.1. +- [[var:corfu-auto-prefix]] :: + Number of characters till auto-completion starts to happen. Defaults to 2. +- [[var:corfu-on-exact-match]] :: + Configures behavior for exact matches. Its default is nil, and it's + recommended to leave it at that. Otherwise, single matches on snippet keys + expand immediately. + +** Adding CAPFs to a mode +To add other CAPFs on a mode-per-mode basis, put either of the following in your +~config.el~: + +#+begin_src emacs-lisp +(add-hook! some-mode (add-to-list 'completion-at-point-functions #'some-capf)) +;; OR, but note the different call signature +(add-hook 'some-mode-hook (lambda () (add-to-list 'completion-at-point-functions #'some-capf))) +#+end_src + +~DEPTH~ above is an integer between -100, 100, and defaults to 0 of omitted. Also +see ~add-hook!~'s documentation for additional ways to call it. ~add-hook~ only +accepts the quoted arguments form above. + +* Troubleshooting +[[doom-report:][Report an issue?]] + +* Frequently asked questions +/This module has no FAQs yet./ [[doom-suggest-faq:][Ask one?]] + +* TODO Appendix +#+begin_quote + 🔨 This module has no appendix yet. [[doom-contrib-module:][Write one?]] +#+end_quote diff --git a/modules/completion/corfu/autoload.el b/modules/completion/corfu/autoload.el new file mode 100644 index 000000000..c34cb8232 --- /dev/null +++ b/modules/completion/corfu/autoload.el @@ -0,0 +1,10 @@ +;;; completion/corfu/autoload.el -*- lexical-binding: t; -*- + +;;;###autoload +(defun +corfu-move-to-minibuffer () + ;; Taken from corfu's README. + ;; TODO: extend this to other completion front-ends. + (interactive) + (let ((completion-extra-properties corfu--extra) + (completion-cycle-threshold completion-cycling)) + (apply #'consult-completion-in-region completion-in-region--data))) diff --git a/modules/completion/corfu/config.el b/modules/completion/corfu/config.el new file mode 100644 index 000000000..3aea1cf0c --- /dev/null +++ b/modules/completion/corfu/config.el @@ -0,0 +1,114 @@ +;;; completion/corfu/config.el -*- lexical-binding: t; -*- + +;; +;;; Packages +(use-package! corfu + :hook (doom-first-input . global-corfu-mode) + :config + (setq corfu-auto t + corfu-auto-delay 0.1 + corfu-auto-prefix 2 + global-corfu-modes '((not + erc-mode + circe-mode + help-mode + gud-mode + vterm-mode) + t) + corfu-cycle t + corfu-separator (when (modulep! +orderless) ?\s) + corfu-preselect 'valid + corfu-count 16 + corfu-max-width 120 + corfu-preview-current 'insert + corfu-on-exact-match nil + corfu-quit-at-boundary (if (modulep! +orderless) 'separator t) + corfu-quit-no-match (if (modulep! +orderless) 'separator t) + ;; In the case of +tng, TAB should be smart regarding completion; + ;; However, it should otherwise behave like normal, whatever normal was. + tab-always-indent (if (modulep! +tng) 'complete tab-always-indent)) + (add-to-list 'completion-category-overrides `(lsp-capf (styles ,@completion-styles))) + + (map! :map corfu-mode-map + :e "C-M-i" #'completion-at-point + :i "C-SPC" #'completion-at-point + :n "C-SPC" (cmd! (call-interactively #'evil-insert-state) + (call-interactively #'completion-at-point)) + :v "C-SPC" (cmd! (call-interactively #'evil-change) + (call-interactively #'completion-at-point))) + (map! :unless (modulep! :editor evil) + :map corfu-mode-map + "C-M-i" #'completion-at-point) + + (after! evil + (add-hook 'evil-insert-state-exit-hook #'corfu-quit)) + + (when (modulep! +icons) + (add-to-list 'corfu-margin-formatters #'nerd-icons-corfu-formatter)) + + (map! :map corfu-map + [return] #'corfu-insert + "RET" #'corfu-insert) + (when (modulep! +orderless) + (map! :map corfu-map + "C-SPC" #'corfu-insert-separator)) + (when (modulep! +tng) + (map! :map corfu-map + [tab] #'corfu-next + [backtab] #'corfu-previous + "TAB" #'corfu-next + "S-TAB" #'corfu-previous) + (let ((cmds-del (cmds! (and (modulep! +tng) + (> corfu--index -1) + (eq corfu-preview-current 'insert)) + #'corfu-reset))) + (map! :map corfu-map + [backspace] cmds-del + "DEL" cmds-del))) + + (after! vertico + (map! :map corfu-map + "M-m" #'+corfu-move-to-minibuffer + (:when (modulep! :editor evil) + "M-J" #'+corfu-move-to-minibuffer)))) + +(use-package! cape + :defer t + :init + (add-hook! prog-mode + (defun +corfu-add-cape-file-h () + (add-to-list 'completion-at-point-functions #'cape-file))) + (add-hook! (org-mode markdown-mode) + (defun +corfu-add-cape-elisp-block-h () + (add-to-list 'completion-at-point-functions #'cape-elisp-block))) + (advice-add #'lsp-completion-at-point :around #'cape-wrap-noninterruptible)) + +(use-package! corfu-terminal + :when (not (display-graphic-p)) + :hook ((corfu-mode . corfu-terminal-mode))) + +;; +;;; Extensions + +(use-package! corfu-history + :hook ((corfu-mode . corfu-history-mode)) + :config + (after! savehist (add-to-list 'savehist-additional-variables 'corfu-history))) + + +(use-package! corfu-popupinfo + :hook ((corfu-mode . corfu-popupinfo-mode)) + :config + (setq corfu-popupinfo-delay '(0.5 . 1.0)) + (map! :map corfu-map + "C-" #'corfu-popupinfo-scroll-down + "C-" #'corfu-popupinfo-scroll-up + "C-S-p" #'corfu-popupinfo-scroll-down + "C-S-n" #'corfu-popupinfo-scroll-up + "C-h" #'corfu-popupinfo-toggle) + (map! :when (modulep! :editor evil) + :map corfu-popupinfo-map + ;; Reversed because popupinfo assumes opposite of what feels intuitive + ;; with evil. + "C-S-k" #'corfu-popupinfo-scroll-down + "C-S-j" #'corfu-popupinfo-scroll-up)) diff --git a/modules/completion/corfu/packages.el b/modules/completion/corfu/packages.el new file mode 100644 index 000000000..1620fcf99 --- /dev/null +++ b/modules/completion/corfu/packages.el @@ -0,0 +1,11 @@ +;; -*- no-byte-compile: t; -*- +;;; completion/corfu/packages.el + +(package! corfu :pin "24dccafeea114b1aec7118f2a8405b46aa0051e0") +(package! cape :pin "18a30f48bb8754421cb10dad99e0a406173d4551") +(when (modulep! +icons) + (package! nerd-icons-corfu :pin "7077bb76fefc15aed967476406a19dc5c2500b3c")) +(when (modulep! +orderless) + (package! orderless :pin "b24748093b00b37c3a572c4909f61c08fa27504f")) +(when (modulep! :os tty) + (package! corfu-terminal :pin "501548c3d51f926c687e8cd838c5865ec45d03cc")) diff --git a/modules/tools/lsp/+lsp.el b/modules/tools/lsp/+lsp.el index 5ae4a417d..96361aae4 100644 --- a/modules/tools/lsp/+lsp.el +++ b/modules/tools/lsp/+lsp.el @@ -138,8 +138,11 @@ server getting expensively restarted when reverting buffers." " ")) (add-to-list 'global-mode-string '(t (:eval lsp-modeline-icon)) - 'append)))))) + 'append))))) + (when (modulep! :completion corfu) + (setq lsp-completion-provider :none) + (add-hook 'lsp-mode-hook #'lsp-completion-mode))) (use-package! lsp-ui :hook (lsp-mode . lsp-ui-mode)