This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
spacemacs/layers/+completion/auto-completion/README.org

254 lines
11 KiB
Org Mode
Raw Normal View History

#+TITLE: Auto-completion layer
2016-03-31 02:59:55 +00:00
2019-05-02 21:49:30 +00:00
#+TAGS: completion|layer
2019-05-07 20:05:06 +00:00
* Table of Contents :TOC_5_gh:noexport:
2017-05-22 14:16:12 +00:00
- [[#description][Description]]
- [[#features][Features:]]
2017-05-22 14:16:12 +00:00
- [[#install][Install]]
- [[#configuration][Configuration]]
- [[#key-bindings][Key bindings]]
- [[#snippets-directories][Snippets directories]]
- [[#show-snippets-in-auto-completion-popup][Show snippets in auto-completion popup]]
- [[#tooltips][Tooltips]]
- [[#sort-results-by-usage][Sort results by usage]]
- [[#enable-company-or-auto-complete-globally][Enable company or auto-complete globally]]
- [[#replacing-company-by-auto-complete][Replacing company by auto-complete]]
- [[#add-auto-completion-in-a-layer][Add auto-completion in a layer]]
- [[#completion-back-ends][Completion back ends]]
- [[#improved-faces][Improved faces]]
- [[#key-bindings-1][Key bindings]]
2017-05-22 14:16:12 +00:00
- [[#company][Company]]
- [[#auto-complete][Auto-complete]]
- [[#yasnippet][Yasnippet]]
- [[#auto-yasnippet][Auto-yasnippet]]
2015-06-10 16:44:30 +00:00
* Description
This layer adds auto-completion to all supported language layers.
2015-06-10 16:44:30 +00:00
** Features:
- Support for code completion with [[http://company-mode.github.io/][company]] or [[https://github.com/auto-complete/auto-complete][auto-complete]] for various language layers
- Frequency-based suggestions via [[https://github.com/company-mode/company-statistics][company-statistics]] for =company=
- Integration with [[https://github.com/capitaomorte/yasnippet][yasnippet]] and [[https://github.com/abo-abo/auto-yasnippet][auto-yasnippet]]
- Automatic configuration of [[https://www.emacswiki.org/emacs/HippieExpand][hippie-expand]]
2015-06-10 16:44:30 +00:00
* Install
To use this configuration layer, add it to your =~/.spacemacs=. You will need to
add =auto-completion= to the existing =dotspacemacs-configuration-layers= list in this
file.
2015-06-10 16:44:30 +00:00
As this is a support layer you will also have to install at least one supported language
layer for it to have any effect.
2015-06-10 16:44:30 +00:00
* Configuration
** Key bindings
You can customize the user experience of auto-completion with the following
layer variables:
1. =auto-completion-return-key-behavior= set the action to perform when the
~RET~ key is pressed, the possible values are:
- =complete= completes with the current selection
- =nil= does nothing
2. =auto-completion-tab-key-behavior= set the action to perform when the ~TAB~
key is pressed, the possible values are:
- =complete= completes with the current selection
- =cycle= completes the common prefix and cycle between candidates
- =nil= does nothing
3. =auto-completion-complete-with-key-sequence= is a string of two characters
denoting a key sequence that will perform a =complete action= if the sequence
has been entered quickly enough. If its value is =nil= then the feature is
disabled.
4. =auto-completion-complete-with-key-sequence-delay= is the number of seconds
to wait for the auto-completion key sequence to be entered. The default value
is 0.1 seconds.
5. =auto-completion-idle-delay= is the number of seconds to wait before suggesting
completions. The default value is 0.2 seconds. Set to =nil= to disable
automatic suggestions (the ~TAB~ key will still perform completion).
2015-06-10 16:44:30 +00:00
The default configuration of the layer is:
#+BEGIN_SRC emacs-lisp
2018-09-19 03:54:47 +00:00
(setq-default dotspacemacs-configuration-layers '(
(auto-completion :variables
auto-completion-return-key-behavior 'complete
auto-completion-tab-key-behavior 'cycle
auto-completion-complete-with-key-sequence nil
auto-completion-complete-with-key-sequence-delay 0.1
auto-completion-idle-delay 0.2
auto-completion-private-snippets-directory nil
auto-completion-enable-snippets-in-popup nil
auto-completion-enable-help-tooltip nil
auto-completion-enable-sort-by-usage nil)))
2015-06-10 16:44:30 +00:00
#+END_SRC
~jk~ is a good candidate for =auto-completion-complete-with-key-sequence= if
you don't use it already.
2015-06-10 16:44:30 +00:00
** Snippets directories
The following directories are added by default:
- =~/.emacs.d/elpa/yasnippet-xxxxx/snippets=
- =~/.emacs.d/layers/auto-completion/snippets=
- =~/.emacs.d/private/snippets= (conditional to the value of =auto-completion-private-snippets-directory=)
- =~/.spacemacs.d/snippets= (conditional to the existence of =~/.spacemacs.d= directory)
You can provide additional directories by setting the variable =auto-completion-private-snippets-directory=
which can either take a single path as string or a list of paths.
If its value is =nil= then the path =~/.emacs.d/private/snippets= is used.
** Show snippets in auto-completion popup
By default, snippets are not shown in the auto-completion popup. To show them in
the popup, set the variable =auto-completion-enable-snippets-in-popup= to =t=.
#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers
'((auto-completion :variables
auto-completion-enable-snippets-in-popup t)))
#+END_SRC
2015-06-10 16:44:30 +00:00
** Tooltips
To enable automatic docstring tooltips set =auto-completion-enable-help-tooltip=
to =t=.
2015-06-10 16:44:30 +00:00
#+BEGIN_SRC emacs-lisp
2018-09-19 03:54:47 +00:00
(setq-default dotspacemacs-configuration-layers
'((auto-completion :variables
auto-completion-enable-help-tooltip t)))
2015-06-10 16:44:30 +00:00
#+END_SRC
To enable manual non-automatic invocation of docstring tooltips, set it to
=manual=. The tooltip can be invoked manually by pressing ~M-h~.
#+BEGIN_SRC emacs-lisp
2018-09-19 03:54:47 +00:00
(setq-default dotspacemacs-configuration-layers
'((auto-completion :variables
auto-completion-enable-help-tooltip 'manual)))
#+END_SRC
2015-06-10 16:44:30 +00:00
** Sort results by usage
To enable sorting auto-completion results by their usage frequency set
=auto-completion-enable-sort-by-usage= to =t=.
This feature is provided by the [[https://github.com/company-mode/company-statistics][company-statistics]] package when =company=
is used. The variable has no effect when =auto-complete= is used.
2015-06-10 16:44:30 +00:00
#+BEGIN_SRC emacs-lisp
2018-09-19 03:54:47 +00:00
(setq-default dotspacemacs-configuration-layers
'((auto-completion :variables
auto-completion-enable-sort-by-usage t)))
2015-06-10 16:44:30 +00:00
#+END_SRC
** Enable company or auto-complete globally
By default Spacemacs enables auto-completion explicitly for each supported
major-mode, it means that =company= and =auto-complete= are not enabled
globally, it allows more flexibility to choose an auto-completion engine
for a given mode.
You may want to enable company globally to get auto-completion
everywhere even in the modes which are not configured by Spacemacs. To do
so, you just have to add =(global-company-mode)= in the
=dotspacemacs/user-config= function of your dotfile.
2015-06-10 16:44:30 +00:00
Note that if you want to enable =auto-complete= globally you will have to
disable =company= first, see the next section to do so.
** Replacing company by auto-complete
You can disable =company= by adding it to the =dotspacemacs-excluded-packages=
variable, then you are free to enable =auto-complete= globally.
** Add auto-completion in a layer
Refactor and simplify company backends declaration Enabling a company backend for a specific mode was a tedious tasks with code scattered at different locations, one for local variable definitions, one for company hook function definitions and another where the backends were pushed to the local variables (which was problematic, since we ended up pushing the same backends over and over again with `SPC f e R`, pushes have been replaced by add-to-list calls in the new macro). All these steps are now put together at one place with the new macro spacemacs|add-company-backends, check its docstring for more info on its arguments. This macro also allows to define arbitrary buffer local variables to tune company for specific modes (similar to layer variables via a keyword :variables) The code related to company backends management has been moved to the auto-completion layer in the funcs.el file. A nice side effect of this move is that it enforces correct encapsulation of company backends related code. We can now easily detect if there is some configuration leakage when the auto-completion layer is not used. But we loose macro expansion at file loading time (not sue it is a big concern though). The function spacemacs|enable-auto-complete was never used so it has been deleted which led to the deletion of the now empty file core-auto-completion.el. The example in LAYERS.org regarding auto-completion is now out of date and has been deleted. An example to setup auto-completion is provided in the README.org file of the auto-completion layer.
2017-01-02 05:39:04 +00:00
Here is an example to add =company= auto-completion to python buffers via the
package =company-anaconda=.
2015-06-10 16:44:30 +00:00
Refactor and simplify company backends declaration Enabling a company backend for a specific mode was a tedious tasks with code scattered at different locations, one for local variable definitions, one for company hook function definitions and another where the backends were pushed to the local variables (which was problematic, since we ended up pushing the same backends over and over again with `SPC f e R`, pushes have been replaced by add-to-list calls in the new macro). All these steps are now put together at one place with the new macro spacemacs|add-company-backends, check its docstring for more info on its arguments. This macro also allows to define arbitrary buffer local variables to tune company for specific modes (similar to layer variables via a keyword :variables) The code related to company backends management has been moved to the auto-completion layer in the funcs.el file. A nice side effect of this move is that it enforces correct encapsulation of company backends related code. We can now easily detect if there is some configuration leakage when the auto-completion layer is not used. But we loose macro expansion at file loading time (not sue it is a big concern though). The function spacemacs|enable-auto-complete was never used so it has been deleted which led to the deletion of the now empty file core-auto-completion.el. The example in LAYERS.org regarding auto-completion is now out of date and has been deleted. An example to setup auto-completion is provided in the README.org file of the auto-completion layer.
2017-01-02 05:39:04 +00:00
In the file =packages.el= of the python layer:
2015-06-10 16:44:30 +00:00
#+BEGIN_SRC emacs-lisp
;; Add the relevant packages to the layer
Refactor and simplify company backends declaration Enabling a company backend for a specific mode was a tedious tasks with code scattered at different locations, one for local variable definitions, one for company hook function definitions and another where the backends were pushed to the local variables (which was problematic, since we ended up pushing the same backends over and over again with `SPC f e R`, pushes have been replaced by add-to-list calls in the new macro). All these steps are now put together at one place with the new macro spacemacs|add-company-backends, check its docstring for more info on its arguments. This macro also allows to define arbitrary buffer local variables to tune company for specific modes (similar to layer variables via a keyword :variables) The code related to company backends management has been moved to the auto-completion layer in the funcs.el file. A nice side effect of this move is that it enforces correct encapsulation of company backends related code. We can now easily detect if there is some configuration leakage when the auto-completion layer is not used. But we loose macro expansion at file loading time (not sue it is a big concern though). The function spacemacs|enable-auto-complete was never used so it has been deleted which led to the deletion of the now empty file core-auto-completion.el. The example in LAYERS.org regarding auto-completion is now out of date and has been deleted. An example to setup auto-completion is provided in the README.org file of the auto-completion layer.
2017-01-02 05:39:04 +00:00
;; here it is `company-anaconda'
2015-06-10 16:44:30 +00:00
(setq python-packages
'(...
(company-anaconda :toggle (configuration-layer/package-used-p 'company))
2015-06-10 16:44:30 +00:00
...))
(defun python/init-company-anaconda ()
(use-package company-anaconda
:defer t
Refactor and simplify company backends declaration Enabling a company backend for a specific mode was a tedious tasks with code scattered at different locations, one for local variable definitions, one for company hook function definitions and another where the backends were pushed to the local variables (which was problematic, since we ended up pushing the same backends over and over again with `SPC f e R`, pushes have been replaced by add-to-list calls in the new macro). All these steps are now put together at one place with the new macro spacemacs|add-company-backends, check its docstring for more info on its arguments. This macro also allows to define arbitrary buffer local variables to tune company for specific modes (similar to layer variables via a keyword :variables) The code related to company backends management has been moved to the auto-completion layer in the funcs.el file. A nice side effect of this move is that it enforces correct encapsulation of company backends related code. We can now easily detect if there is some configuration leakage when the auto-completion layer is not used. But we loose macro expansion at file loading time (not sue it is a big concern though). The function spacemacs|enable-auto-complete was never used so it has been deleted which led to the deletion of the now empty file core-auto-completion.el. The example in LAYERS.org regarding auto-completion is now out of date and has been deleted. An example to setup auto-completion is provided in the README.org file of the auto-completion layer.
2017-01-02 05:39:04 +00:00
:init
(spacemacs|add-company-backends
:backends company-anaconda
Refactor and simplify company backends declaration Enabling a company backend for a specific mode was a tedious tasks with code scattered at different locations, one for local variable definitions, one for company hook function definitions and another where the backends were pushed to the local variables (which was problematic, since we ended up pushing the same backends over and over again with `SPC f e R`, pushes have been replaced by add-to-list calls in the new macro). All these steps are now put together at one place with the new macro spacemacs|add-company-backends, check its docstring for more info on its arguments. This macro also allows to define arbitrary buffer local variables to tune company for specific modes (similar to layer variables via a keyword :variables) The code related to company backends management has been moved to the auto-completion layer in the funcs.el file. A nice side effect of this move is that it enforces correct encapsulation of company backends related code. We can now easily detect if there is some configuration leakage when the auto-completion layer is not used. But we loose macro expansion at file loading time (not sue it is a big concern though). The function spacemacs|enable-auto-complete was never used so it has been deleted which led to the deletion of the now empty file core-auto-completion.el. The example in LAYERS.org regarding auto-completion is now out of date and has been deleted. An example to setup auto-completion is provided in the README.org file of the auto-completion layer.
2017-01-02 05:39:04 +00:00
:modes python-mode)))
2015-06-10 16:44:30 +00:00
#+END_SRC
** Completion back ends
Many spacemacs layers (e.g., python, html, haskell) configure company mode
backends to provide mode-specific completion. These modes will include
completion backends specified in the =spacemacs-default-company-backends=
variable. The defaults should work well, but you can configure this variable in
your =.spacemacs= file with (e.g.)
2018-07-10 10:57:25 +00:00
#+BEGIN_SRC emacs-lisp
(setq-default
dotspacemacs-configuration-layers
'((auto-completion :variables
spacemacs-default-company-backends '(company-files company-capf))))
#+END_SRC
** Improved faces
For nicer-looking faces, try adding the following to `custom-set-faces` in your dotspacemacs file.
#+BEGIN_SRC emacs-lisp
2018-09-19 03:54:47 +00:00
(custom-set-faces
'(company-tooltip-common
((t (:inherit company-tooltip :weight bold :underline nil))))
'(company-tooltip-common-selection
((t (:inherit company-tooltip-selection :weight bold :underline nil)))))
#+END_SRC
2016-06-10 04:09:42 +00:00
* Key bindings
2015-06-10 16:44:30 +00:00
** Company
| Key binding | Description |
|-------------+------------------------------------------------------------------------------------------------------|
| ~C-d~ | open minibuffer with documentation of thing at point in company dropdown |
| ~C-/~ | show candidates in Helm or Ivy (for fuzzy searching) |
| ~C-M-/~ | filter the company dropdown menu |
| ~M-h~ | show current candidate's documentation in a tooltip (requires =auto-completion-enable-help-tooltip=) |
Vim Style:
| Key binding | Description |
2016-04-22 02:25:51 +00:00
|-------------+----------------------------------------------|
| ~C-j~ | (vim style) go down in company dropdown menu |
| ~C-k~ | (vim style) go up in company dropdown menu |
| ~C-l~ | (vim style) complete selection |
Emacs style:
| Key binding | Description |
2016-04-22 02:25:51 +00:00
|-------------+------------------------------------------------|
| ~C-n~ | (emacs style) go down in company dropdown menu |
| ~C-p~ | (emacs style) go up in company dropdown menu |
2015-06-10 16:44:30 +00:00
** Auto-complete
| Key binding | Description |
2015-06-10 16:44:30 +00:00
|-------------+----------------------------------------------------------------------|
| ~C-j~ | select next candidate |
| ~C-k~ | select previous candidate |
| ~TAB~ | expand selection or select next candidate |
| ~S-TAB~ | select previous candidate |
| ~RET~ | complete word, if word is already completed insert a carriage return |
2015-06-10 16:44:30 +00:00
** Yasnippet
| Key binding | Description |
2015-06-10 16:44:30 +00:00
|-------------+----------------------------------------------------------------|
| ~M-/~ | Expand a snippet if text before point is a prefix of a snippet |
| ~SPC i s~ | List all current yasnippets for inserting |
** Auto-yasnippet
| Key binding | Description |
2015-06-10 16:44:30 +00:00
|-------------+---------------------------------------------------------------------------|
| ~SPC i S c~ | create a snippet from an active region |
| ~SPC i S e~ | Expand the snippet just created with ~SPC i y~ |
| ~SPC i S w~ | Write the snippet inside =private/snippets= directory for future sessions |