joaotavora / yasnippet

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

Support LSP tab stops like ${1} #1012

Open muirdm opened 5 years ago

muirdm commented 5 years ago

The LSP spec ("Snippet Syntax") says tab stops can optionally have curlies: tabstop ::= '$' int | '${' int '}'. Currently yasnippet seems to support $1 but not ${1} (expanding ${1} inserts a literal "1"). Can ${1} be supported in a backwards compatible way, or is the current behavior intended?

npostavs commented 5 years ago

yasnippet doesn't implement the LSP Snippet Syntax, but the current handling of ${1} doesn't seem to make much sense (it creates a tab stop with default contents of 1), so I'd say changing it is not out of the question.

Related: #979 "Handle curly braces with LSP-style escaping"

xendk commented 9 months ago

The PHPactor language server, for one, insists on using ${0} on a lot of completions, so it turns annoying fast.

A workaround until it's decided if yasnippet or eglot should handle it:

(define-advice eglot--snippet-expansion-fn (:override (&rest args) xen-eglot--snippet-expansion-fn)
    (and (fboundp 'yas-minor-mode)
         (lambda (&rest args)
           (with-no-warnings
             (unless (bound-and-true-p yas-minor-mode) (yas-minor-mode 1))
             (apply #'yas-expand-snippet (list (replace-regexp-in-string "\\${\\([[:digit:]]+\\)}" "$\\1" (car args))))))))
monnier commented 9 months ago

I just pushed to master a patch which seems to make YASnippet accept this. I'm not familiar with this part of the code at all, so knock on wood. If you can try it (e.g. via GNU-devel ELPA where it should appear this afternoon) and confirm that it works for you, that would be great.

xendk commented 9 months ago

I'm using elpaca, so I just pulled and commented my advice. Seems to work fine here. :+1: