About auto complete and yasnippet in emacs
Asked Answered
E

1

8

I'm using auto-complete and yasnippet in Emacs and I am confused by their settings. I placed the following code in my .emacs:

(add-to-list 'load-path "~/.emacs.d/plugins/yasnippet")
(require 'yasnippet)
(yas/global-mode 1)
(global-set-key (kbd "C-i") 'yas/expand)
(setq yas/also-auto-indent-first-line t)

(add-to-list 'load-path "~/.emacs.d/plugins/autocomplete/")
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "~/.emacs.d/plugins/autocomplete/ac-dict")
(ac-config-default)
(setq ac-use-menu-map t)
(define-key ac-menu-map "\C-n" 'ac-next)
(define-key ac-menu-map "\C-p" 'ac-previous)

(defun ac-js-mode()
(setq ac-sources '(ac-source-yasnippet
                 ac-source-symbols
                 ac-source-words-in-buffer
                 ac-source-words-in-same-mode-buffers
                 ac-source-files-in-current-dir
                 )))
(add-hook 'js-mode-hook 'ac-js-mode)

I am trying to set yasnippet as the first candidate in the auto-complete popup menu. However, as the example below shows, this doesn't work with my current settings: when I type the word for, formatItem is in first position and for in second. formatItem is just a local function in current buffer.

 for_____________
|formatItem      |
|for            a|
|for            s|
|force          s|
|foreachv       s|
 ----------------

So my question is: how can I make yasnippet the first candidate in auto-complete? And is there something missing in my .emacs config?

Any help is appreciated.

Esteban answered 26/3, 2012 at 8:57 Comment(1)
I don't know the answer, but auto complete has a builtin learning system, so the more often you select a candidate for a completion the higher it will be next time. So you may only have to use it for a while, and all your frequently used snippets will float to the top automatically.Tamandua
I
5

ac-config-default installs hooks to setup sensible default ac-sources values. These hooks (especially ac-common-setup) might interfere with your settings.

You can check this by looking at the actual value of ac-sources (C-h vac-sources) in a js-mode buffer to see if it has been modified by comparison to your custom value.

If this is the case, I see two solutions:

  • stop using ac-config-default and specifiy all autocomplete settings,
  • advise the faulty hook to put ac-source-yasnippet at the beginning of ac-sources after it has run. Assuming ac-common-setup is indeed the troublemaker, this would look like:
(defadvice ac-common-setup (after give-yasnippet-highest-priority activate)
  (setq ac-sources (delq 'ac-source-yasnippet ac-sources))
  (add-to-list 'ac-sources 'ac-source-yasnippet))
Impress answered 26/3, 2012 at 10:19 Comment(2)
Thanks for you answer.It is the case as you described. But I am not clearly understanding the method 2 given above. Can you tell me something more about it ?Thanks.Esteban
Hi, after I place your code in my .emacs, the actual value of ac-sources seems to be right where C-h v in a js file outputs: Its value is (ac-source-yasnippet ac-source-filename ac-source-symbols ac-source-words-in-buffer ac-source-words-in-same-mode-buffers ac-source-files-in-current-dir).Local in buffer set.js; But when I type the word 'for', the popup menu still gives the graph which I given in the question.I have no idea about that.Esteban

© 2022 - 2024 — McMap. All rights reserved.