使用EGLOT时如何显示Yasnippets的建议

发布于 2025-02-06 20:17:04 字数 2965 浏览 1 评论 0原文

我目前正在使用EGLOT(最小LSP客户端)的Emacs。但是,当我一起使用摘要库(例如,Yasnippet,Tempel)时,没有出现摘要的完成。 现在,我可以指定摘要名称并将其扩展在代码中。但是我希望emacs像键入摘要的名称一样工作,它显示了摘要的建议。

如果有一种方法在使用EGLOT时有一种建议,我将不胜感激。 我的Emacs配置如下所示。

  • EGLOT
(use-package eglot
  :ensure t
  :config
  (add-to-list 'eglot-server-programs '(c-mode . ("clangd")))
  (add-to-list 'eglot-server-programs '(c++-mode . ("clangd")))
  (add-to-list 'eglot-server-programs '(go-mode . ("gopls")))
  (add-to-list 'eglot-server-programs '(rustic-mode . ("rust-analyzer")))
  (add-to-list 'eglot-server-programs '(python-mode . ("pyls")))
  (add-to-list 'eglot-server-programs '(LaTeX-mode . ("digestif")))
  (add-hook 'c-mode-hook 'eglot-ensure)
  (add-hook 'c++-mode-hook 'eglot-ensure)
  (add-hook 'go-mode-hook 'eglot-ensure)
  (add-hook 'rustic-mode-hook 'eglot-ensure)
  (add-hook 'python-mode-hook 'eglot-ensure)
  (add-hook 'LaTeX-mode-hook 'eglot-ensure)
  ;; format on save
  (add-hook 'c-mode-hook '(lambda() (add-hook 'before-save-hook 'eglot-format-buffer nil t)))
  (add-hook 'c++-mode-hook '(lambda() (add-hook 'before-save-hook 'eglot-format-buffer nil t)))
  (add-hook 'python-mode-hook '(lambda() (add-hook 'before-save-hook 'eglot-format-buffer nil t)))
  (define-key eglot-mode-map (kbd "C-c r") 'eglot-rename))
  • YASNIPPET
(use-package yasnippet
  :ensure t
  :hook
  (prog-mode . yas-minor-mode)
  :bind
  (("C-c y n" . yas-new-snippet)
   ("C-c y v" . yas-visit-snippet-file)
   ("C-c y i" . yas-insert-snippet))
  :config
  (yas-reload-all)
  (setq yas-snippet-dirs
        '("~/.emacs.d/snippets")))

公司(我现在不使用公司,转到Corfu / Cape。)

(use-package company
   :ensure t
   :diminish company-mode
   :bind
   (("C-M-i" . company-complete)
    :map company-active-map
    ("M-n" . nil)
    ("M-p" . nil)
    ("C-h" . nil)
    ("C-n" . company-select-next)
    ("C-p" . company-select-previous)
    ("C-s" . company-filter-candidates)
    ("C-i" . company-complete-selection)
    ([tab] . company-complete-selection))
   :hook
   (after-init . global-company-mode)
   :config
   (setq company-backends '((company-capf :with company-yasnippet)))
   (setq company-idle-delay 0
        company-minimum-prefix-length 2
        company-selection-wrap-around t
        completion-ignore-case t
        company-show-quick-access t))
  • Corfu / Cape
(use-package corfu
  :ensure t
  :custom
  (corfu-cycle t)
  (corfu-auto t)
  (corfu-quit-at-boundary nil)
  (corfu-scroll-margin 5)
  (corfu-echo-documentation t)
  :bind
  (:map corfu-map
        ("TAB" . corfu-insert)
        ([tab] . corfu-insert)
        ("C-n" . corfu-next)
        ("C-p" . corfu-previous))
  :init
  (global-corfu-mode))


;;;;; cape ;;;;;
(use-package cape
  :ensure t
  :config
  (add-to-list 'completion-at-point-functions (cape-company-to-capf #'company-yasnippet)))

I'm currently using Emacs with eglot (minimal lsp client). However, no completions for snippets have appeared when I use the snippet library (e.g., YASnippet, tempel) together.
Now I can specify the snippet name and expand it in the code. But I want emacs to work like when I type the snippet's name, it shows suggestions of the snippet.

I'll appreciate it if there's a way to show suggestions for snippets when using eglot.
My emacs configuration is shown below.

  • eglot
(use-package eglot
  :ensure t
  :config
  (add-to-list 'eglot-server-programs '(c-mode . ("clangd")))
  (add-to-list 'eglot-server-programs '(c++-mode . ("clangd")))
  (add-to-list 'eglot-server-programs '(go-mode . ("gopls")))
  (add-to-list 'eglot-server-programs '(rustic-mode . ("rust-analyzer")))
  (add-to-list 'eglot-server-programs '(python-mode . ("pyls")))
  (add-to-list 'eglot-server-programs '(LaTeX-mode . ("digestif")))
  (add-hook 'c-mode-hook 'eglot-ensure)
  (add-hook 'c++-mode-hook 'eglot-ensure)
  (add-hook 'go-mode-hook 'eglot-ensure)
  (add-hook 'rustic-mode-hook 'eglot-ensure)
  (add-hook 'python-mode-hook 'eglot-ensure)
  (add-hook 'LaTeX-mode-hook 'eglot-ensure)
  ;; format on save
  (add-hook 'c-mode-hook '(lambda() (add-hook 'before-save-hook 'eglot-format-buffer nil t)))
  (add-hook 'c++-mode-hook '(lambda() (add-hook 'before-save-hook 'eglot-format-buffer nil t)))
  (add-hook 'python-mode-hook '(lambda() (add-hook 'before-save-hook 'eglot-format-buffer nil t)))
  (define-key eglot-mode-map (kbd "C-c r") 'eglot-rename))
  • YASnippet
(use-package yasnippet
  :ensure t
  :hook
  (prog-mode . yas-minor-mode)
  :bind
  (("C-c y n" . yas-new-snippet)
   ("C-c y v" . yas-visit-snippet-file)
   ("C-c y i" . yas-insert-snippet))
  :config
  (yas-reload-all)
  (setq yas-snippet-dirs
        '("~/.emacs.d/snippets")))

Company (I do not use company right now, transferred to corfu/cape instead.)

(use-package company
   :ensure t
   :diminish company-mode
   :bind
   (("C-M-i" . company-complete)
    :map company-active-map
    ("M-n" . nil)
    ("M-p" . nil)
    ("C-h" . nil)
    ("C-n" . company-select-next)
    ("C-p" . company-select-previous)
    ("C-s" . company-filter-candidates)
    ("C-i" . company-complete-selection)
    ([tab] . company-complete-selection))
   :hook
   (after-init . global-company-mode)
   :config
   (setq company-backends '((company-capf :with company-yasnippet)))
   (setq company-idle-delay 0
        company-minimum-prefix-length 2
        company-selection-wrap-around t
        completion-ignore-case t
        company-show-quick-access t))
  • Corfu / Cape
(use-package corfu
  :ensure t
  :custom
  (corfu-cycle t)
  (corfu-auto t)
  (corfu-quit-at-boundary nil)
  (corfu-scroll-margin 5)
  (corfu-echo-documentation t)
  :bind
  (:map corfu-map
        ("TAB" . corfu-insert)
        ([tab] . corfu-insert)
        ("C-n" . corfu-next)
        ("C-p" . corfu-previous))
  :init
  (global-corfu-mode))


;;;;; cape ;;;;;
(use-package cape
  :ensure t
  :config
  (add-to-list 'completion-at-point-functions (cape-company-to-capf #'company-yasnippet)))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弥繁 2025-02-13 20:17:04

我终于从Emacs-JP Slack获得了解决方案。我将重印它们,希望他们会为同样困扰的其他人提供帮助。

;; for company

(add-hook 'eglot-managed-mode-hook (lambda ()
                                     (add-to-list 'company-backends
                                                  '(company-capf :with company-yasnippet))))


;; for corfu
(straight-use-package 'cape)

(defun my/eglot-capf ()
  (setq-local completion-at-point-functions
              (list (cape-super-capf
                     #'eglot-completion-at-point
                     (cape-company-to-capf #'company-yasnippet)))))

(add-hook 'eglot-managed-mode-hook #'my/eglot-capf)

I finally got the solution from emacs-jp slack. I will reprint them in the hope that they will be of help to others who are similarly troubled.

;; for company

(add-hook 'eglot-managed-mode-hook (lambda ()
                                     (add-to-list 'company-backends
                                                  '(company-capf :with company-yasnippet))))


;; for corfu
(straight-use-package 'cape)

(defun my/eglot-capf ()
  (setq-local completion-at-point-functions
              (list (cape-super-capf
                     #'eglot-completion-at-point
                     (cape-company-to-capf #'company-yasnippet)))))

(add-hook 'eglot-managed-mode-hook #'my/eglot-capf)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文