company-lsp是不维护了?补全列表里没有yasnippet。

用了最小的配置,也没法在补全列表里找到yasnippet。感觉是company-lsp的问题。。好像不维护了?你们有能正常工作的吗? image 我用的最小的测试配置

(package-initialize)
(setq use-package-always-ensure t)
(defun hardcore/init-use-package ()
(setq
 package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
                    ("org" . "http://orgmode.org/elpa/")
                    ("melpa" . "http://melpa.org/packages/")))
  (unless (package-installed-p 'use-package)
    (package-refresh-contents)
    (package-install 'use-package))
  (eval-when-compile
  (require 'use-package))
  (setq use-package-always-ensure t)
  (use-package use-package-ensure-system-package))

(hardcore/init-use-package)

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

(use-package lsp-mode
     :diminish lsp-mode
     :hook (go-mode . lsp-deferred)
     :commands (lsp lsp-deferred))

(use-package go-mode
  :mode "\\.go\\'"
  :custom (gofmt-command "goimports")
  :bind (:map go-mode-map
         ("C-c C-n" . go-run)
         ("C-c ."   . go-test-current-test)
         ("C-c f"   . go-test-current-file)
         ("C-c a"   . go-test-current-project))
  :config
  (add-hook 'before-save-hook #'gofmt-before-save)
  (use-package gotest)
  (use-package go-tag
    :config (setq go-tag-args (list "-transform" "camelcase"))))

 (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))

能工作的都是用的哪个版本的company-lsp呢?

  1. 查看当前buffer 的 company-backends 变量是否为
((company-lsp :with company-yasnippet)
  ....)

还是 (company-lsp ...)

  1. 如果是前者在补全会显示 yasnippet, 如果是后者试试将关于 company-lsp 的配置添加一句
  (use-package company-lsp
    :defer t
    :after (lsp-mode company)
    :commands company-lsp
    :init
    (setq company-lsp-async t
          compan-lsp-cache-candidates 'nil)
    :config 
    (setq company-backends (mapcar #'company-backend-with-yas company-backends)))
  1. 再次查看 company-backends 值

是nil。。C-h v 但是我能补全啊。。代码片段还是没有

写错了 :sob: :sob:,是 company-backends

image

因为 company 会优先使用 company-backends 变量最前面的项作为补全的后端。而启用 lsp-mode 后 company-lsp 添加到 company-backends 的最前面,所以如果要使其它的补全后端在lsp-mode 启动后生效, 需要重新配置 company-backends 的值。

所以要在company-lsp后面加那个配置?实际上 我的lsp和company是2个文件。也能用吗

可以用,但有可能报如下错误: 类似与没有 company-backend-with-yas 函数,

那只是一个例子,事实上你可以使用更简单的方法如:

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

具体代码你可以看看你之前发的帖子的回答。

有点看不懂啊 刚从vim转过来。

用了下你那个with-after的。。就乱套了

但是 company-yasnippet 工作了,这也是为什么company-yasnippet没有默认放入 company-bankends 中的原因, company-yasnippet单独工作时,非常的好。和某些后端一起工作会感觉补全非常乱。

很多时候推荐将 company-yasnippet 绑定在特定的快捷键上。

不习惯啊。vim里这2个互不干涉的。工作的很好。emacs里这么麻烦吗。。。