Moving to Doom Emacs!

This commit is contained in:
Derek Taylor
2019-12-16 20:21:19 -06:00
parent d9f2f456f1
commit d4b4c33550
683 changed files with 51877 additions and 100 deletions

View File

@@ -0,0 +1,131 @@
#+TITLE: completion/company
#+DATE: February 19, 2017
#+SINCE: v2.0
#+STARTUP: inlineimages
* Table of Contents :TOC_3:noexport:
- [[#description][Description]]
- [[#module-flags][Module Flags]]
- [[#plugins][Plugins]]
- [[#prerequisites][Prerequisites]]
- [[#features][Features]]
- [[#code-completion][Code completion]]
- [[#vim-esque-omni-completion-prefix-c-x][Vim-esque omni-completion prefix (C-x)]]
- [[#configuration][Configuration]]
- [[#enable-company-backends-in-certain-modes][Enable company backend(s) in certain modes]]
- [[#troubleshooting][Troubleshooting]]
- [[#x-mode-doesnt-have-code-completion-support-or-requires-extra-setup][X-mode doesn't have code completion support or requires extra setup.]]
- [[#no-backends-or-the-incorrect-ones-have-been-registered-for-x-mode][No backends (or the incorrect ones) have been registered for X-mode.]]
* Description
This module provides code completion, powered by [[https://github.com/company-mode/company-mode][company-mode]]. It is required
for code completion in many of Doom's :lang modules.
https://assets.doomemacs.org/completion/company/overlay.png
** Module Flags
+ =+childframe= Enables displaying completion candidates in a child frame,
rather than an overlay or tooltip (among with other UI enhancements). *This
requires GUI Emacs 26.1+ and is incompatible with the =+tng= flag*
+ =+tng= Enables completion using only ~TAB~. Pressing ~TAB~ will select the
next completion suggestion, while ~S-TAB~ will select the previous one. *This
is incompatible with the =+childframe= flag*
** Plugins
+ [[https://github.com/company-mode/company-mode][company-mode]]
+ [[https://github.com/hlissner/emacs-company-dict][company-dict]]
+ [[https://github.com/raxod502/prescient.el][company-prescient]]
+ [[https://github.com/sebastiencs/company-box][company-box]]* (=+childframe=)
* Prerequisites
This module has no direct prerequisites.
However, some major modes may require additional setup for code completion to
work in them. Some major modes may have no completion support at all. Check that
major mode's module's documentation for details.
* Features
** Code completion
By default, completion is triggered after a short idle period or with the
=C-SPC= key. While the popup is visible, the following keys are available:
| Keybind | Description |
|---------+------------------------------------------|
| =C-n= | Go to next candidate |
| =C-p= | Go to previous candidate |
| =C-j= | (evil) Go to next candidate |
| =C-k= | (evil) Go to previous candidate |
| =C-h= | Display documentation (if available) |
| =C-u= | Move to previous page of candidates |
| =C-d= | Move to next page of candidates |
| =C-s= | Filter candidates |
| =C-S-s= | Search candidates with helm/ivy |
| =C-SPC= | Complete common |
| =TAB= | Complete common or select next candidate |
| =S-TAB= | Select previous candidate |
** Vim-esque omni-completion prefix (C-x)
In the spirit of Vim's omni-completion, the following insert mode keybinds are
available to evil users to access specific company backends:
| Keybind | Description |
|-----------+-----------------------------------|
| =C-x C-]= | Complete etags |
| =C-x C-f= | Complete file path |
| =C-x C-k= | Complete from dictionary/keyword |
| =C-x C-l= | Complete full line |
| =C-x C-o= | Invoke complete-at-point function |
| =C-x C-n= | Complete next symbol at point |
| =C-x C-p= | Complete previous symbol at point |
| =C-x C-s= | Complete snippet |
| =C-x s= | Complete spelling suggestions |
* Configuration
** Enable company backend(s) in certain modes
The ~set-company-backend!~ function exists for setting ~company-backends~
buffer-locally in MODES, which is either a major-mode symbol, a minor-mode
symbol, or a list of either. BACKENDS are prepended to ~company-backends~ for
those modes.
#+BEGIN_SRC emacs-lisp
(after! js2-mode
(set-company-backend! 'js2-mode 'company-tide 'company-yasnippet))
(after! sh-script
(set-company-backend! 'sh-mode
'(company-shell :with company-yasnippet)))
(after! cc-mode
(set-company-backend! 'c-mode
'(:separate company-irony-c-headers company-irony)))
#+END_SRC
To unset the backends for a particular mode, pass ~nil~ to it:
#+BEGIN_SRC emacs-lisp
(after! sh-script
(set-company-backend! 'sh-mode nil))
#+END_SRC
* Troubleshooting
If code completion isn't working for you, consider the following common causes
before you file a bug report:
** X-mode doesn't have code completion support or requires extra setup.
There is no guarantee your language mode will have completion support.
Some, like ~lua-mode~, don't have completion support in Emacs at all. Others may
requires additional setup to get code completion working. For instance,
~go-mode~ requires ~guru~ to be installed on your system, and ~enh-ruby-mode~
requires that you have a Robe server running (~M-x robe-start~).
Check the relevant module's documentation for this kind of information.
** No backends (or the incorrect ones) have been registered for X-mode.
Doom expects every mode to have an explicit list of company-backends (and as
short a list as possible). This may mean you aren't getting all the completion
you want or any at all.
Check the value of ~company-backends~ (=SPC h v company-backends=) from that
mode to see what backends are available. Check the [[*Assigning company backend(s) to modes][Configuration section]] for
details on changing what backends are available for that mode.

View File

@@ -0,0 +1,156 @@
;;; completion/company/autoload.el -*- lexical-binding: t; -*-
;;;###autoload
(defvar +company-backend-alist
'((text-mode company-dabbrev company-yasnippet company-ispell)
(prog-mode company-capf company-yasnippet)
(conf-mode company-capf company-dabbrev-code company-yasnippet))
"An alist matching modes to company backends. The backends for any mode is
built from this.")
;;;###autodef
(defun set-company-backend! (modes &rest backends)
"Prepends BACKENDS (in order) to `company-backends' in MODES.
MODES should be one symbol or a list of them, representing major or minor modes.
This will overwrite backends for MODES on consecutive uses.
If the car of BACKENDS is nil, unset the backends for MODES.
Examples:
(set-company-backend! 'js2-mode
'company-tide 'company-yasnippet)
(set-company-backend! 'sh-mode
'(company-shell :with company-yasnippet))
(set-company-backend! '(c-mode c++-mode)
'(:separate company-irony-c-headers company-irony))
(set-company-backend! 'sh-mode nil) ; unsets backends for sh-mode"
(declare (indent defun))
(dolist (mode (doom-enlist modes))
(if (null (car backends))
(setq +company-backend-alist
(delq (assq mode +company-backend-alist)
+company-backend-alist))
(setf (alist-get mode +company-backend-alist)
backends))))
;;
;;; Library
(defun +company--backends ()
(let (backends)
(let ((mode major-mode)
(modes (list major-mode)))
(while (setq mode (get mode 'derived-mode-parent))
(push mode modes))
(dolist (mode modes)
(dolist (backend (append (cdr (assq mode +company-backend-alist))
(default-value 'company-backends)))
(push backend backends)))
(delete-dups
(append (cl-loop for (mode . backends) in +company-backend-alist
if (or (eq major-mode mode) ; major modes
(and (boundp mode)
(symbol-value mode))) ; minor modes
append backends)
(nreverse backends))))))
;;
;;; Hooks
;;;###autoload
(defun +company-init-backends-h ()
"Set `company-backends' for the current buffer."
(if (not company-mode)
(remove-hook 'change-major-mode-after-body-hook #'+company-init-backends-h 'local)
(unless (eq major-mode 'fundamental-mode)
(setq-local company-backends (+company--backends)))
(add-hook 'change-major-mode-after-body-hook #'+company-init-backends-h nil 'local)))
(put '+company-init-backends-h 'permanent-local-hook t)
;;
;;; Commands
;;;###autoload
(defun +company-has-completion-p ()
"Return non-nil if a completion candidate exists at point."
(and (company-manual-begin)
(= company-candidates-length 1)))
;;;###autoload
(defun +company/toggle-auto-completion ()
"Toggle as-you-type code completion."
(interactive)
(require 'company)
(setq company-idle-delay (unless company-idle-delay 0.2))
(message "Auto completion %s"
(if company-idle-delay "enabled" "disabled")))
;;;###autoload
(defun +company/complete ()
"Bring up the completion popup. If only one result, complete it."
(interactive)
(require 'company)
(when (ignore-errors
(/= (point)
(cdr (bounds-of-thing-at-point 'symbol))))
(save-excursion (insert " ")))
(when (and (company-manual-begin)
(= company-candidates-length 1))
(company-complete-common)))
;;;###autoload
(defun +company/dabbrev ()
"Invokes `company-dabbrev-code' in prog-mode buffers and `company-dabbrev'
everywhere else."
(interactive)
(call-interactively
(if (derived-mode-p 'prog-mode)
#'company-dabbrev-code
#'company-dabbrev)))
;;;###autoload
(defun +company/whole-lines (command &optional arg &rest ignored)
"`company-mode' completion backend that completes whole-lines, akin to vim's
C-x C-l."
(interactive (list 'interactive))
(require 'company)
(pcase command
(`interactive (company-begin-backend '+company/whole-lines))
(`prefix (company-grab-line "^[\t\s]*\\(.+\\)" 1))
(`candidates
(all-completions
arg
(delete-dups
(split-string
(replace-regexp-in-string
"^[\t\s]+" ""
(concat (buffer-substring-no-properties (point-min) (line-beginning-position))
(buffer-substring-no-properties (line-end-position) (point-max))))
"\\(\r\n\\|[\n\r]\\)" t))))))
;;;###autoload
(defun +company/dict-or-keywords ()
"`company-mode' completion combining `company-dict' and `company-keywords'."
(interactive)
(require 'company-dict)
(require 'company-keywords)
(let ((company-backends '((company-keywords company-dict))))
(call-interactively #'company-complete)))
;;;###autoload
(defun +company/dabbrev-code-previous ()
"TODO"
(interactive)
(require 'company-dabbrev)
(let ((company-selection-wrap-around t))
(call-interactively #'+company/dabbrev)
(company-select-previous-or-abort)))

View File

@@ -0,0 +1,133 @@
;;; completion/company/config.el -*- lexical-binding: t; -*-
(use-package! company
:commands company-complete-common company-manual-begin company-grab-line
:after-call pre-command-hook after-find-file
:init
(setq company-minimum-prefix-length 2
company-tooltip-limit 14
company-dabbrev-downcase nil
company-dabbrev-ignore-case nil
company-dabbrev-code-other-buffers t
company-tooltip-align-annotations t
company-require-match 'never
company-global-modes
'(not erc-mode message-mode help-mode gud-mode eshell-mode)
company-backends '(company-capf)
company-frontends
'(company-pseudo-tooltip-frontend
company-echo-metadata-frontend))
:config
(when (featurep! :editor evil)
(add-hook 'company-mode-hook #'evil-normalize-keymaps)
;; Allow users to switch between backends on the fly. E.g. C-x C-s followed
;; by C-x C-n, will switch from `company-yasnippet' to
;; `company-dabbrev-code'.
(defadvice! +company--abort-previous-a (&rest _)
:before #'company-begin-backend
(company-abort)))
(add-hook 'company-mode-hook #'+company-init-backends-h)
(global-company-mode +1))
(use-package! company-tng
:when (featurep! +tng)
:after-call post-self-insert-hook
:config
(add-to-list 'company-frontends 'company-tng-frontend)
(define-key! company-active-map
"RET" nil
[return] nil
"TAB" #'company-select-next
[tab] #'company-select-next
[backtab] #'company-select-previous))
;;
;; Packages
(after! company-files
(pushnew! company-files--regexps
"file:\\(\\(?:\\.\\{1,2\\}/\\|~/\\|/\\)[^\]\n]*\\)"))
(use-package! company-prescient
:hook (company-mode . company-prescient-mode)
:config
;; NOTE prescient config duplicated with `ivy'
(setq prescient-save-file (concat doom-cache-dir "prescient-save.el"))
(prescient-persist-mode +1))
(use-package! company-box
:when (featurep! +childframe)
:hook (company-mode . company-box-mode)
:config
(setq company-box-show-single-candidate t
company-box-backends-colors nil
company-box-max-candidates 50
company-box-icons-alist 'company-box-icons-all-the-icons
company-box-icons-functions
'(+company-box-icons--yasnippet-fn
company-box-icons--lsp
+company-box-icons--elisp-fn
company-box-icons--acphp)
company-box-icons-all-the-icons
`((Unknown . ,(all-the-icons-material "find_in_page" :height 0.8 :face 'all-the-icons-purple))
(Text . ,(all-the-icons-material "text_fields" :height 0.8 :face 'all-the-icons-green))
(Method . ,(all-the-icons-material "functions" :height 0.8 :face 'all-the-icons-red))
(Function . ,(all-the-icons-material "functions" :height 0.8 :face 'all-the-icons-red))
(Constructor . ,(all-the-icons-material "functions" :height 0.8 :face 'all-the-icons-red))
(Field . ,(all-the-icons-material "functions" :height 0.8 :face 'all-the-icons-red))
(Variable . ,(all-the-icons-material "adjust" :height 0.8 :face 'all-the-icons-blue))
(Class . ,(all-the-icons-material "class" :height 0.8 :face 'all-the-icons-red))
(Interface . ,(all-the-icons-material "settings_input_component" :height 0.8 :face 'all-the-icons-red))
(Module . ,(all-the-icons-material "view_module" :height 0.8 :face 'all-the-icons-red))
(Property . ,(all-the-icons-material "settings" :height 0.8 :face 'all-the-icons-red))
(Unit . ,(all-the-icons-material "straighten" :height 0.8 :face 'all-the-icons-red))
(Value . ,(all-the-icons-material "filter_1" :height 0.8 :face 'all-the-icons-red))
(Enum . ,(all-the-icons-material "plus_one" :height 0.8 :face 'all-the-icons-red))
(Keyword . ,(all-the-icons-material "filter_center_focus" :height 0.8 :face 'all-the-icons-red))
(Snippet . ,(all-the-icons-material "short_text" :height 0.8 :face 'all-the-icons-red))
(Color . ,(all-the-icons-material "color_lens" :height 0.8 :face 'all-the-icons-red))
(File . ,(all-the-icons-material "insert_drive_file" :height 0.8 :face 'all-the-icons-red))
(Reference . ,(all-the-icons-material "collections_bookmark" :height 0.8 :face 'all-the-icons-red))
(Folder . ,(all-the-icons-material "folder" :height 0.8 :face 'all-the-icons-red))
(EnumMember . ,(all-the-icons-material "people" :height 0.8 :face 'all-the-icons-red))
(Constant . ,(all-the-icons-material "pause_circle_filled" :height 0.8 :face 'all-the-icons-red))
(Struct . ,(all-the-icons-material "streetview" :height 0.8 :face 'all-the-icons-red))
(Event . ,(all-the-icons-material "event" :height 0.8 :face 'all-the-icons-red))
(Operator . ,(all-the-icons-material "control_point" :height 0.8 :face 'all-the-icons-red))
(TypeParameter . ,(all-the-icons-material "class" :height 0.8 :face 'all-the-icons-red))
;; (Template . ,(company-box-icons-image "Template.png"))))
(Yasnippet . ,(all-the-icons-material "short_text" :height 0.8 :face 'all-the-icons-green))
(ElispFunction . ,(all-the-icons-material "functions" :height 0.8 :face 'all-the-icons-red))
(ElispVariable . ,(all-the-icons-material "check_circle" :height 0.8 :face 'all-the-icons-blue))
(ElispFeature . ,(all-the-icons-material "stars" :height 0.8 :face 'all-the-icons-orange))
(ElispFace . ,(all-the-icons-material "format_paint" :height 0.8 :face 'all-the-icons-pink))))
(defun +company-box-icons--yasnippet-fn (candidate)
(when (get-text-property 0 'yas-annotation candidate)
'Yasnippet))
(defun +company-box-icons--elisp-fn (candidate)
(when (derived-mode-p 'emacs-lisp-mode)
(let ((sym (intern candidate)))
(cond ((fboundp sym) 'ElispFunction)
((boundp sym) 'ElispVariable)
((featurep sym) 'ElispFeature)
((facep sym) 'ElispFace))))))
(use-package! company-dict
:defer t
:config
(setq company-dict-dir (expand-file-name "dicts" doom-private-dir))
(add-hook! 'doom-project-hook
(defun +company-enable-project-dicts-h (mode &rest _)
"Enable per-project dictionaries."
(if (symbol-value mode)
(add-to-list 'company-dict-minor-mode-list mode nil #'eq)
(setq company-dict-minor-mode-list (delq mode company-dict-minor-mode-list))))))

View File

@@ -0,0 +1,8 @@
;; -*- no-byte-compile: t; -*-
;;; completion/company/packages.el
(package! company)
(package! company-dict)
(package! company-prescient)
(when (featurep! +childframe)
(package! company-box))

View File

@@ -0,0 +1,75 @@
;; -*- lexical-binding: t; no-byte-compile: t; -*-
;;; completion/company/test/test-company.el
(describe "completion/company"
(before-all
(load! "../autoload"))
(describe ":company-backend"
:var (a +company-backend-alist backends)
(before-each
(setq-default company-backends '(t))
(setq +company-backend-alist nil
a (get-buffer-create "x"))
(fset 'backends
(lambda (mode)
(let ((major-mode mode))
(+company--backends))))
(set-buffer a)
(spy-on 'require))
(after-each
(kill-buffer a))
;;
(it "sets backends for a major mode"
(set-company-backend! 'text-mode 'a)
(expect (backends 'text-mode) :to-equal '(a t)))
(it "sets backends for a derived-mode"
(set-company-backend! 'prog-mode 'a)
(expect (backends 'prog-mode) :to-equal '(a t))
(expect (backends 'emacs-lisp-mode) :to-equal '(a t)))
(it "sets multiple backends for exact major modes"
(set-company-backend! '(text-mode emacs-lisp-mode) 'a 'b)
(expect (backends 'text-mode) :to-equal (backends 'emacs-lisp-mode)))
(it "sets cumulative backends"
(set-company-backend! 'prog-mode '(a b c))
(set-company-backend! 'emacs-lisp-mode 'd 'e)
(expect (backends 'emacs-lisp-mode) :to-equal '(d e (a b c) t)))
(it "sets cumulative backends with a minor mode"
(set-company-backend! 'prog-mode '(a b c))
(set-company-backend! 'emacs-lisp-mode 'd 'e)
(set-company-backend! 'some-minor-mode 'x 'y)
(setq-local some-minor-mode t)
(expect (backends 'emacs-lisp-mode) :to-equal '(x y d e (a b c) t)))
(it "overwrites past backends"
(set-company-backend! 'text-mode 'old 'backends)
(set-company-backend! 'text-mode 'new 'backends)
(expect (backends 'text-mode) :to-equal '(new backends t)))
(it "unsets past backends"
(set-company-backend! 'text-mode 'old)
(set-company-backend! 'text-mode nil)
(expect (backends 'text-mode) :to-equal (default-value 'company-backends)))
(it "unsets past parent backends"
(set-company-backend! 'prog-mode 'old)
(set-company-backend! 'emacs-lisp-mode 'child)
(set-company-backend! 'prog-mode nil)
(expect (backends 'emacs-lisp-mode) :to-equal '(child t)))
(it "overwrites past cumulative backends"
(set-company-backend! 'prog-mode 'base)
(set-company-backend! 'emacs-lisp-mode 'old)
(set-company-backend! 'emacs-lisp-mode 'new)
(expect (backends 'emacs-lisp-mode) :to-equal '(new base t)))
(it "overwrites past parent backends"
(set-company-backend! 'prog-mode 'base)
(set-company-backend! 'emacs-lisp-mode 'child)
(set-company-backend! 'prog-mode 'new)
(expect (backends 'emacs-lisp-mode) :to-equal '(child new t)))))