2

Company doesn't show snippets in some modes e.g. python or sh. Yet yas/describe-tables displays all snippets. In other modes e.g. org,emacs-lisp, snippets are shown as expected.

Looking at company-diag I don't see company-yasnippets loaded anywhere in python-mode.

Used backend: company-capf

While in the working org mode it returns

Used backend: (company-capf :with company-yasnippet)

Testing this variable (as proposed in spacemac docu) didn't change anything:

spacemacs-default-company-backends '(company-yasnippet)

Environment:

yas-snippet-dirs:

("/home/user/.emacs.d/private/snippets/" yasnippet-snippets-dir)

yas-global-mode t

 (auto-completion :variables
                  auto-completion-enable-snippets-in-popup t
                  auto-completion-enable-help-tooltip t)
 (python :variables
         python-backend 'lsp
         python-lsp-server 'pyright
         python-test-runner 'pytest
         python-enable-yapf-format-on-save t
         )

Where should I start looking?


Update:

Custom created snippets e.g. /private/snippets/python-mode/foo.snippet appear in a second list. In a different color and out-of-line position. It looks like multiple completion systems are interfering.

Gif: Python after enabling auto-complete-mode alongside company-mode

screenshot

Gif: Expected behavior (as in org mode and company-mode only): expected_gif

3
  • 2
    Do you have any snippet files stored in the folders (within the snippets folder) labeled python-mode and/or sh-mode, or do you have a snippet inherit file .yas-parents in either of those folders so that snippets from other modes are inherited? Have you enabled yasnippet globally as described in the README.md file?: github.com/joaotavora/yasnippet
    – lawlist
    Aug 6, 2021 at 7:52
  • @lawlist No parents in neither directories. Does my question update help?
    – jjk
    Aug 6, 2021 at 10:28
  • 1
    Do the snippets work correctly if you revert to the default spacemacs settings of auto-completion-enable-snippets-in-popup having a nil value? Yasnippet comes with its own mechanism for triggering snippets when the user presses the tab key, and if more than one snippet matches the key at point, the available snippets to choose from are displayed. See the section "Multiples snippet with the same key" github.com/joaotavora/yasnippet/blob/master/doc/… See also the function yas-insert-snippet ... "Prompts you for possible snippet expansion ..."
    – lawlist
    Aug 6, 2021 at 17:39

2 Answers 2

2

[Solved] Auto-completion support

Disclaimer: The configuration and setup mentioned in this guide are specific to Spacemacs only. The instructions provided may not be applicable or compatible with other Emacs setups or configurations.

I've successfully set up various completions in Spacemacs after trying several combinations. I'm sharing this solution here in case anyone else encounters the same dilemma.

Yasnippet Auto-completion

The backend for yasnippet is company-yasnippet. You can integrate it into your Spacemacs configuration. There are two methods to get it working; choose only one. I used the second option in my spacemacs config file.

  1. At your dotspacemacs-configuration-layers add
     (auto-completion :variables
                      auto-completion-enable-snippets-in-popup t
                      auto-completion-enable-help-tooltip t
                      auto-completion-return-key-behavior 'complete
                      auto-completion-tab-key-behavior 'cycle ;;'complete 'cycle
                      auto-completion-enable-sort-by-usage t
                      auto-completion-idle-delay 0.0
                      :config
                      add-to-list 'company-backends '(company-yasnippet company-capf company-anaconda))

  1. You can add to dotspacemacs/user-config the following
  ;; -------------------------------------------------------------
  ;;            Auto-completion - Back End Support
  ;; -------------------------------------------------------------
  (require 'company-yasnippet)
    (add-to-list 'company-backends '(company-yasnippet  company-capf company-anaconda))

[Optional][bonus] Tabnine Auto-completion support

I like also to add company-tabnine to my back-ends completions. Here is how I did it.

  1. Ensure you already installed the tabnine in dotspacemacs-additional-packages as:
   dotspacemacs-additional-packages '(
                (company-tabnine) ;; for using tab-nine AI auto-assistance
)
  1. Put in your dotspacemacs/user-config (similar to before)
  ;; -------------------------------------------------------------
  ;;            Auto-completion - Back End Support
  ;; -------------------------------------------------------------
  (require 'company-yasnippet)
  (require 'company-tabnine)
  (add-to-list 'company-backends '(company-yasnippet company-tabnine  company-capf company-anaconda))

  1. Install tabnine binary using M-x: company-tabnine-install-binary ( twice in spacemacs then write this command). It will be installed in your home directory (e.g. for my machine M1 macOS, it will be downloaded here ~/.TabNine/4.4.321/aarch64-apple-darwin. Make sure it is executable by running the command below in this directory (read more here).
chmod +x ~/.TabNine/4.4.321/aarch64-apple-darwin/*

It works now as you can see auto-completion support in action

NOTE:

  • [I think] changing the order of the company backends in the company-backends list will affect their order in the popup menu. Make sure to set auto-completion-enable-snippets-in-popup t (read more here)

  • To investigate your back-end company support, you can use <space-space> and type (M-x: describe-variable) then search for company-backends and you will see a doc popup showing all back-ends supported. For me they are

Value:
((company-capf company-dabbrev-code . #1=(:with company-yasnippet))
 (company-semantic company-dabbrev-code company-gtags company-etags company-keywords . #1#)
 (company-files . #1#)
 (company-dabbrev . #1#))
Original value was 
(company-bbdb company-semantic company-cmake company-capf company-clang company-files
              (company-dabbrev-code company-gtags company-etags company-keywords)
              company-oddmuse company-dabbrev)
Local in buffer .spacemacs; global value is 
((company-yasnippet company-tabnine company-lsp company-capf company-anaconda)
 company-bbdb company-semantic company-cmake company-capf company-clang company-files
 (company-dabbrev-code company-gtags company-etags company-keywords)
 company-oddmuse company-dabbrev)

  • company-lsp is no longer supported read the overview here
4
  • 1
    In item 1 there is an extra parenthesis that should be removed on the line "auto-completion-idle-delay 0.0)"
    – Niles
    Oct 2, 2023 at 11:49
  • Thank you for your suggestion, I have removed it already.
    – Dr Neo
    Oct 3, 2023 at 3:11
  • 1
    Awesome, thank you so much, huge improvement for my daily workflow :) Note company-lsp is not supported by lsp-mode anymore. You might remove it from your answer.
    – jjk
    Oct 4, 2023 at 19:35
  • I have updated the post, thank you for your feedback,
    – Dr Neo
    Oct 5, 2023 at 5:22
2

Remove the extension .snippet from the name of any new snippet created . No snippets have such extension.

Assuming you have some python snippets, take into account the execution order - if your python mode is enabled after yasnippets-snippets package, then at the end of your python configuration add the expression (yas-reload-all) to reload all snippets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.