yasnippet 没有工作,大佬帮看看

贴配置 这样的到处抄的(逃,好像是没工作和companymode。我用的company-lsp

  (use-package yasnippet
    :diminish yas-minor-mode
    :hook (after-init . yas-global-mode)
    :config (use-package yasnippet-snippets))

  (use-package smartparens
    :ensure t
    :config
    (smartparens-global-mode))

 (use-package company
    :diminish company-mode
    :defines (company-dabbrev-ignore-case company-dabbrev-downcase)
    :commands company-abort
    :bind (
            :map company-active-map
            ("C-p" . company-select-previous)
            ("C-n" . company-select-next)
            ("<tab>" . company-complete-common-or-cycle)
            :map company-search-map
            ("C-p" . company-select-previous)
            ("C-n" . company-select-next))
    :hook (after-init . global-company-mode)
    :config
    (setq company-tooltip-align-annotations t
            company-tooltip-limit 12
            company-idle-delay 0
            company-echo-delay (if (display-graphic-p) nil 0)
            company-minimum-prefix-length 2
            company-require-match nil
            company-dabbrev-ignore-case nil
            company-dabbrev-downcase nil)
    (defun company-backend-with-yas (backends)
        "Add :with company-yasnippet to company BACKENDS.
    Taken from https://github.com/syl20bnr/spacemacs/pull/179."
        (if (and (listp backends) (memq 'company-yasnippet backends))
            backends
        (append (if (consp backends)
                    backends
                    (list backends))
                '(:with company-yasnippet))))
  ;; add yasnippet to all backends
  (setq company-backends (mapcar #'company-backend-with-yas company-backends))
  (use-package company-lsp
    :defer t
    :after (lsp-mode company)
    :commands company-lsp
    :init
    (setq company-lsp-async t
          compan-lsp-cache-candidates 'nil)
    )

手动yas-expand有用吗?

手动是用啥 tab?我试了没用啊

最简单的例子,在emacs-lisp-mode中输入 s 然后在后面按一下<tab> 键 (或者M+x yas-expand),看会不会展开为

(setq )

我记得要打开yas-minor-mode才能展开snippet。company-lsp不会自动打开

M-x yas-minor-mode-on

按下tab不会展开 M-x 然后可以选 就出来了

是打开的。我想做到和vim里一样 我输入 meth 然后补全就会列出来 选择就展开了

那你company-lsp-enable-snippet有没有设置为t?

有设置 t,没工作。。

向company-lsp报bug吧 :joy:

问题描述不对,是 company 中想集成 snippet 功能吧?

company-yasnippet

Documentation company-mode backend for yasnippet.

This backend should be used with care, because as long as there are snippets defined for the current major mode, this backend will always shadow backends that come after it. Recommended usages:

  • In a buffer-local value of company-backends, grouped with a backend or several that provide actual text completions.

    (add-hook 'js-mode-hook (lambda () (set (make-local-variable 'company-backends) '((company-dabbrev-code company-yasnippet)))))

  • After keyword :with, grouped with other backends.

    (push '(company-semantic :with company-yasnippet) company-backends)

  • Not in company-backends, just bound to a key.

    (global-set-key (kbd “C-c y”) 'company-yasnippet)

嗯 应该是吧。在vim里我配置是可以做到这样的 输入meth 会有补全。我选中就展开snippet了,emacs里是集成到company里吧?放到company-backend对吧? git

不知道是哪儿的问题啊。我在下面贴了个图 我在vim里是这么用的,怎么在emacs实现差不多的效果

我以为你说lsp的snippet展开失灵了呢。。。

原来是company配合yasnippet啊,给想要的后端加上:with company-yasnippet就行了。比如和company-lsp配合

(with-eval-after-load 'lsp-mode
  (push '(company-lsp :with company-yasnippet) company-backends))

注意禁用lsp-auto-configure

1 个赞

放到company的:config 下面了 没起作用 是要放到哪里呢。我lsp的配置和company是2个文件

首先,最小化运行(运行emacs -q)后,执行:自己添加一点package.el与 use-package 相关的代码和你给的代码,最后手动运行 yas-global-mode 和 company-mode 代码。发现company可以提示关于yasnippet的选项。 附添加的代码

(setq package-archives '(("gnu"   . "http://elpa.emacs-china.org/gnu/")
                         ("melpa" . "http://elpa.emacs-china.org/melpa/")))

(require 'package)

(unless (package-installed-p 'use-package)
  (package-refresh-contents)
  (package-install 'use-package))
(package-initialize)
(eval-and-compile
    (setq use-package-always-ensure t)
    (setq use-package-always-defer t)
    (setq use-package-expand-minimally t)
    (setq use-package-enable-imenu-support t))

(require 'use-package)
;; 之后加上你的代码

PS: 你可以看看 use-package 中关键字 :preface , :init , :config(代码执行的先后顺序)和 :defer , :demand(是否延时执行代码)的介绍 use-package-keywords

那就奇怪了 我一点得不到。是和我vim里的效果一样吗 我输入for 就有补全菜单显示snippet 然后确认选中展开?

对,这是在rust-mode中的 2019-07-31-181735_1366x768_scrot

我只能这样操作才有snippet git

在 github上有关这件事的讨论https://github.com/tigersoldier/company-lsp/issues/119