joaotavora / yasnippet

A template system for Emacs
http://joaotavora.github.io/yasnippet/
2.79k stars 311 forks source link

Is there a tutorial use yasnippet with auto-complete.el? #336

Open feicunrain opened 11 years ago

feicunrain commented 11 years ago

I do many googles, and spent many time to read the src but my elisp level is sucks, I can not figure out how to do it

there is no tutorial on the internet,maybe some,but they are all incomplete and out of date.I have to put this in my .emacs:

(add-to-list 'load-path "xxxxxxxxxxxxxxx") (require 'auto-complete) (global-auto-complete-mode t) (add-to-list 'ac-dictionary-directories 'xxxxxxxx") (require 'auto-complete-config) (ac-config-default)

(add-to-list 'load-path "xxxxxxxxxxxxxxxxx") (require 'yasnippet) (yas-load-directory "xxxxxxxxxxxxxxx") (yas--initialize)

sorry I type this manually, maybe some error

and I have yas minor mode and auto-complete minor mode in the same time, the auto-complete can invoke yas'api to work, but this is chaos.Many people do the same,but this is really chaos : ( and the keymap maybe conflict. I think

joaotavora commented 11 years ago

I patched yasnippet (and auto-complete) some months ago but apparently failed miserably. I understand this is an issue bothering many people, and I have marked this "high priority", will do some testing and try to provide that simple tutorial, and maybe some patches.

wyuenho commented 11 years ago

Would be nice to include a tutorial on how to unbind/rebind the keys too. tab, return seem to conflict with many many major and minor modes.

zw963 commented 11 years ago

Give you a workaround is:

  1. First not add ac-yasnippet into ac-source, it buggy. only wait @capitaomorte
    have time to fix it.
  2. Because not ac-yasippet, it always auto pop ac-dictionary word defined in AC, so, if you want active a ab key, you must add ab in you ac-dictionary manually.
marcinant commented 11 years ago

@zw963 Could you explain more? I use ac-yasnippet as ac-source and currently don't see any better method.

Problem with ac is that it cannot complete keywords with yasnippet if there is opening bracket in front of the keyword. This is a pretty annoying in lisp and other languages where brackets are almost everywhere, but for example in PHP ac+yas is pretty useful.

What could we do to improve situation?

zw963 commented 11 years ago

Marcin Antczak notifications@github.com writes:

hi, what i do maybe not same as you situation?

I primary to resolve when no ac-yasnippet, ac always auto complete a word, (it can not automatic stop when snippet key is satisfied.)

And your's opening braket problem is not happen in my emacs.

@zw963 Could you explain more? I use ac-yasnippet as ac-source and currently don't see any better method.

Problem with ac is that it cannot complete keywords with yasnippet if there is opening bracket in front of the keyword. This is a pretty annoying in lisp and other languages where brackets are almost everywhere, but for example in PHP ac+yas is pretty useful.

What could we do to improve situation?


Reply to this email directly or view it on GitHub: https://github.com/capitaomorte/yasnippet/issues/336#issuecomment-11142727

Good luck!

marcinant commented 11 years ago

I don't know what's yours configuration.

I got latest available Emacs 24 + auto-complete + yasnippet.

And I use this with PHP mode: (setq ac-sources (append '(ac-source-yasnippet) ac-sources))

Sometimes auto-complete doesn't want to work when there is opening bracket and no space. But I cannot see any other bugs.

You wrote: "First not add ac-yasnippet into ac-source, it buggy. only wait"

This is why I asked what could you say more... "buggy" but why and where?

zw963 commented 11 years ago

Marcin Antczak notifications@github.com writes:

sorry for my pool english.

my means buggy is Yasnppet is not automatic register all have defined key into AC dictionary, so, when you input a key, e.g. aaa, try expand a snippet. if you current file content include string aaaaaa, when press tab, it just auto-complete aaaaaa'. it because the candidate pop-menu is notaaa` unless you add a aaa into ac-dictionary, otherwise, ac-sources-yasnippet is useless.

if early version (0.6), snippet key will auto insert into dictionary, if you ac-source-yasnippet is in ac-source, and prior others source, will first expand snippet. this is what i want to do.

you problem, I guess it php-mode problem, it relate with syntax table. perhaps you can change you syntax table.

I don't know what's yours configuration.

I got latest available Emacs 24 + auto-complete + yasnippet.

And I use this with PHP mode: (setq ac-sources (append '(ac-source-yasnippet) ac-sources))

Sometimes auto-complete doesn't want to work when there is opening bracket and no space. But I cannot see any other bugs.

You wrote: "First not add ac-yasnippet into ac-source, it buggy. only wait"

This is why I asked what could you say more... "buggy" but why and where?


Reply to this email directly or view it on GitHub: https://github.com/capitaomorte/yasnippet/issues/336#issuecomment-11165033

Good luck!

marcinant commented 11 years ago

@zw963 Cannot duplicate your case. Could you please provide exact sample of config+mode+file and string to input to duplicate this?

I also would like to mention that in current version of auto-complete and yasnippet the problem I described before with opening bracket is gone. And everything behaves properly.

joaotavora commented 11 years ago

I just mailed the auto-complete author trying to get a feel for exactly what is happening in the present between the two packages. Reading this thread only brings more confusion and I have no time to investigate. Can someone help me?

You only need to help me complete, in this format, this 3 part idiot-proof recipe:

launching emacs

cd ~/Source/Emacs
git clone https://github.com/auto-complete/auto-complete
git clone https://github.com/capitaomorte/yasnippet
cd auto-complete
git submodule init
git submodule update
cd ../
emacs -Q -L yasnippet\
         -L auto-complete\
         -L auto-complete/lib/popup\
         -L auto-complete/lib/fuzzy\

emacs-lisp setup

The emacs-lisp part of the recipe, I think should start with, but what else should I put in? I just put in the yasnippet parts for now.

(require 'auto-complete)
;; ... what else?
(require 'yasnippet)
(yas-global-mode t)

what the user should do

The user-actions part of the recipe is empty for now, I have very little experience with auto-complete and very little time.

zw963 commented 11 years ago

@capitaomorte , Hi~

here is my step, it almost same as yours steps, and it work OK.

launching emacs

cd ~/Source/Emacs
git clone https://github.com/auto-complete/auto-complete
git clone https://github.com/capitaomorte/yasnippet
cd auto-complete
git submodule init
git submodule update
cd ../
emacs -Q -L yasnippet\
         -L auto-complete\
         -L auto-complete/lib/popup\
         -L auto-complete/lib/fuzzy

emacs-lisp setup

;; load auto-complete and its helpers, such as the `ac-source-yasnippet' completion source
(require 'auto-complete-config)

;; setting dictionary directory.
(add-to-list 'ac-dictionary-directories "~/Source/Emacs/auto-complete/dict")

;; setup and turn on `auto-complete-mode` for various major modes
(ac-config-default)

;; resetting ac-sources
(setq-default ac-sources '(
                           ac-source-yasnippet
                           ac-source-abbrev
                           ac-source-dictionary
                           ac-source-words-in-same-mode-buffers
                           ))

(require 'yasnippet)
(setq yas-snippet-dirs "~/Source/Emacs/yasnippet/snippets")
(yas-global-mode t)

what the user should do

inc            a
int             d
inline        d

it work OK.

problems

But when I setting use my own snippet, it failed, I realize the problem is about my snippet. But when not add ac-source-yasnippet into ac-sources, Yasnipipet is work OK. I investigate it, find it maybe cause by yasnippet condition, when current(mode) snippet directory exist a snippet file, it content include: #condition: (some-not-exist-condition), auto-complete is not work.

when a non-exist condition is provided, I get following message:

[yas] Error in condition evaluation: Symbol's function definition is void: rails/controller-spec\?
[yas] Error in condition evaluation: Symbol's function definition is void: rails/migration\? 

I have resolve this problem, (remove non-exist condition), but it still have a error message exist.

[yas] Error in condition evaluation: Symbol's function definition is void: nil [55 times]

I'm still not not found it come from. when I found it, I will tell you.

zw963 commented 11 years ago

@capitaomorte , error message is occur when key is matched, and input a addtion char,

e.g. re is a snippet key, when press rea, reb, rec ... re?? ... , it have a error message.

zw963 commented 11 years ago

@capitaomorte , I have found the root of the problem...

When ac-sources add ac-source-yasnippet

  1. non-existed expand condition, will cause auto-complete can not active.
  2. if not provide a # key: ..., will always send a error message all time. ( I have some snippet, only # binding: ..., not # key: ....)
joaotavora commented 11 years ago

Hi @zw963 and all,

I have different things to report:

  1. Your recipe is impossible to duplicate because:

    • Switching to a buffer with a .rb extension does not call ruby-mode
    • Emacs, when launched the way you describe it, does not have a ruby-mode.

    But it is reproduced in c-mode. Also you don't need the final yas-reload-all. I have updated your recipe accordingly.

  2. Your snippets with invalid conditions don't prevent yas-active-keys from working correctly. The error message warns you that something is not right that you should fix. If you would rather not see the error message you can set yas-verbosity to 1 or less.
  3. When a snippets condition evaluation fails, it is not returned by yas-active-keys and ac-source-yasnippet also doesn't provide any snippets. This is why issue #347, which I fixed in d9f966, was preventing any snippets from being returned by yas-active-keys. So please try that version.
  4. Snippets without # key: directives are not supposed to be returned by yas-active-keys since they don't have an external representation. This is by design

I will try to enhace and simplify this functionality in the future.

Thanks

zw963 commented 11 years ago

João Távora notifications@github.com writes:

  • Switching to a buffer with a .rb extension does not call ruby-mode
  • Emacs, when launched the way you describe it, does not have a ruby-mode.

sorry, I forget to clarify M-x ruby-mode to open ruby-mode

  1. Your snippets with invalid conditions don't prevent yas-active-keys from working correctly. The error message warns you that something is not right that you should fix. If you would rather not see the error message you can set yas-verbosity` to 1 or less.

invalid conditions don't prevent yas-active-keys from working correctly. but it prevent auto-complete show pop-menu correctly.

  1. When a snippets condition evaluation fails, it is not returned by yas-active-keysandac-source-yasnippetalso doesn't provide any snippets. This is why issue #347, which I fixed in d9f966, was preventing any snippets from being returned byyas-active-keys`. So please try that version.

sorry. I just update my yasnippet to lastest version. I see the #347 just now, i will test and feedback soon.

but my [yas] Error in condition evaluation: Symbol's function definition is void: nil error is caused by missing key: ... definition. it seem irrelevant with invalid conditions.

if invalid conditions, auto-complete pop-menu can not be active. but if no key, it just give a error message frequently, but pop-menu can be active.

  1. Snippets without # key: directives are not supposed to be returned by yas-active-keys` since they don't have an external representation. This is by design

Thanks. I already add all keys in all snippets. it work seem fine now, this is a very large increase for me, maybe I could delete a big lump auto-complete dictionary define (used to active snippets key)

Good luck!

zw963 commented 11 years ago

@capitaomorte , Hi, I find I fix all key missing, name missing error, it still complain ```[yas] Error in condition evaluation: Symbol's function definition is void: nil

And, only the first times use auto-complete is work OK, when reinvoke ac, it failed.

I one by one to test, what problem cause this... I find a VERY SERIOUS problem.

When one mode exist a snippet file, this file content include # binding: ..... , it can cause auto-complete can not work! this is intolerable.

e.g.

# -*- mode: snippet -*-
# name: a
# key: a
# --
aaa

it work OK.

# -*- mode: snippet -*-
# name: a
# key: a
# binding: a
# --
aaa

OR

# -*- mode: snippet -*-
# name: a
# binding: a
# --
aaa

all cause current mode auto-complete can not worked.

I think this is the real reason ... !

zw963 commented 11 years ago

just describe is not exactly.

when auto-complete not trigger any snippet, it can worked (first time) once invoke a snippet, auto-complete is broken.

joaotavora commented 11 years ago

I see now @zw963 your report is a duplicate of #315, which I just fixed.

Please refrain from using ALLCAPS it only makes your report harder to understand. Also if you think yasnippet is intolerable, you don't have to use it.

zw963 commented 11 years ago

@capitaomorte , sorry!

you misunderstood me... my english is very pool. I can't find a perfect word to express my feeling... I just too too exciting... because I find the source of this problem finally! so my tone is somewhat radical. you know, this problem is Troubled me for a long long time.. I have mention this problem several times... But nobody think this is a problem, actually ... other snippet user use it very Good. no problem...

Say sorry to you once again, thanks for you busy work while still maintaining this project... And Christmas is still time to fix this error.

You know, snippet is my favorite... it is my all to use Emacs almost... I can't leave it at all.. and I have do so many work for use snippet in Emacs better, I will spend time to refine it and contribute to snippet near future.

zw963 commented 11 years ago

@capitaomorte , about auto-complete and yasnippet.

315 is make it can work together, but I still find some little problem.

  1. it can not completely support condition system. here is a example.

When no condition, pop-menu first item is a key named describe, when press TAB, AC can expand snippet directly.

# -*- mode: snippet -*-
# name: describe
# key: describe
# binding: M-c d
# --cl
describe do
 $0
end

But when feed some special condition, the first item is turn into a dictionary word, (not key, just a word) , when i press tab, it auto-complete describe word .

# -*- mode: snippet -*-
# name: describe
# key: describe
# binding: M-c d
# condition: (yas-expand-from-key-p)
# --
describe do
 $0
end

here is the function define.

(defun expand-from-key-p ()
  "snippet must expand from normal trigger key."
  (eq (symbol-function this-command) 'yas-expand-from-trigger-key))

(yas-define-condition-cache yas-expand-from-key-p (expand-from-key-p))

Can you tell me where is the problem ?

joaotavora commented 11 years ago

@zw963

EricTill commented 10 years ago

Well here's my solution to the overlap problem. I'm not 100% happy with it as it is kinda glitchy (I'll get into that afterwards), but here's my basic setup.

;;Yasnippet
(add-to-list 'load-path "~/.emacs.d/fromgit/yasnippet")
(require 'yasnippet)
(yas-global-mode 1)

(define-key yas-minor-mode-map (kbd "C-RET") 'yas-next-field)
(define-key yas-minor-mode-map (kbd "<C-return>") 'yas-next-field)
(define-key yas-minor-mode-map [(control return)] 'yas-next-field)

;;Autocomplete
(add-to-list 'load-path "~/.emacs.d/")
(require 'auto-complete-config)
;;(add-to-list 'ac-dictionary-directories "~/.emacs.d/ac-dict")
(ac-config-default)
(ac-set-trigger-key "TAB")
(ac-set-trigger-key "<tab>")

What this does is lets auto-complete try to hog the tab key most of the time, but if you enter a keyword (like fn in javascript mode) and press tab more than once it will let you expand your snippet. You can't do this if the beginning of your snippet auto-completes to anything else, though. Then if, inside of your expanded snippet, you auto-complete something you'll go to the next section or if not, you can press C-Ret to skip to the next section.

Overall, a really, really messy hack. There has to be a better way!

This is definitely my top want from yasnippet. I'm not sure how regularly auto-complete is updated, though... so I can see why this is such a pain in the butt. I will keep trying to find a better solution and will post here or create a pull request if I can figure one out.

-PS, if you're using CUA mode, you better rebind C-Ret with something like (setq cua-rectangle-mark-key (kbd "C-S-")) BEFORE you turn on CUA mode. It's a pain to rebind after CUA mode is flipped on.

weavejester commented 10 years ago

This sounds like a difficult problem, but perhaps it should at least be mentioned on the README that there is no good solution for this yet. Perhaps something like: "yasnippet does not play well with autocomplete. Currently the only solution to this is to use assign autocomplete and yasnippet to different key bindings."

marcomansilla commented 10 years ago

Hi, found this[0] while looking for a good solution to the yasnippet/auto-complete integration, and seems to be a very good and simple solution.

[0] http://bit.ly/PuVRTp

ghost commented 10 years ago

That script did not work for me...