关于 lsp-mode / go-mode / company-lsp 怎么配置(已解决)

真的不想多发贴子,但这个东西已经弄了一天了。对 elisp 的了解有限,但真的很想用大佬们那样的 company. 可我就是配不成功。。

$emacs -version
GNU Emacs 26.3
Copyright (C) 2019 Free Software Foundation, Inc.
GNU Emacs comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of GNU Emacs
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.

init.el

;; file path: ~/.emacs.d/init.el
(use-package exec-path-from-shell
  :demand t
  :config
  (when (memq window-system '(mac ns x))
    (exec-path-from-shell-initialize)))
;; more ...
(use-package company
  :ensure t
  :config
  (global-company-mode t)
  (setq company-idle-delay 0)
  (setq company-minimum-prefix-length 3)
  (setq company-backends
        `((:separate company-files company-capf company-keywords )
          (:separate company-abbrev company-dabbrev)))
  )

(use-package company-box
  :after company
  :diminish
  :config
  :hook (company-mode . company-box-mode))

go.el

;; file path ~/.emacs.d/custom/go.el
(use-package go-mode
  :ensure t
  :mode (("\\.go'\\'" . go-mode))
  :hook ((before-save . gofmt-before-save))
  :config
  (setq gofmt-command "goimports")
  (use-package company-go
    :ensure t
    :config
      (setq company-tooltip-limit 20)                      ; bigger popup window
      (setq company-idle-delay .3)                         ; decrease delay before autocompletion popup shows
      (setq company-echo-delay 0)                          ; remove annoying blinking
      (setq company-begin-commands '(self-insert-command)) ; start autocompletion only after typing
      (add-hook 'go-mode-hook (lambda ()
          (set (make-local-variable 'company-backends) '(company-lsp company-go))
          (company-mode)))
    )
  )

(use-package lsp-mode
    :config
    (setq lsp-prefer-flymake nil)
    :commands lsp)
(use-package lsp-ui
  :commands lsp-ui-mode)
(use-package company-lsp
  :commands company-lsp)
(push 'company-lsp company-backends)
;; (add-hook 'go-mode-hook #'lsp-deferred)
(add-hook 'go-mode-hook #'lsp)

最后显示的错误是:

Debugger entered--Lisp error: (file-missing "Cannot open load file" "No such file or directory" "lsp-mode")
  lsp()
  run-hooks(change-major-mode-after-body-hook prog-mode-hook go-mode-hook)
  apply(run-hooks (change-major-mode-after-body-hook prog-mode-hook go-mode-hook))
  run-mode-hooks(go-mode-hook)
  go-mode()
  set-auto-mode-0(go-mode nil)
  set-auto-mode()
  normal-mode(t)
  after-find-file(nil t)
  find-file-noselect-1(#<buffer main.go> "~/gProjects/test/main.go" nil nil "~/gProjects/test/main.go" (8611632160 16777220))
  find-file-noselect("/Users/byron/gProjects/test/main.go" nil nil nil)
  find-file("/Users/byron/gProjects/test/main.go")
  counsel-find-file-action("/Users/byron/gProjects/test/main.go")
  ivy-call()
  ivy-read("Find file: " read-file-name-internal :matcher counsel--find-file-matcher :initial-input nil :action counsel-find-file-action :preselect nil :require-match confirm-after-completion :history file-name-history :keymap (keymap (96 lambda (&optional arg) "nil (`nil')" (interactive "p") (if (string= "" ivy-text) (execute-kbd-macro (kbd "M-o b")) (self-insert-command arg))) (C-backspace . counsel-up-directory) (67108991 . counsel-up-directory)) :caller counsel-find-file)
  counsel--find-file-1("Find file: " nil counsel-find-file-action counsel-find-file)
  counsel-find-file()
  funcall-interactively(counsel-find-file)
  call-interactively(counsel-find-file nil nil)
  command-execute(counsel-find-file)

需要帮助。。迈过难关。万分感谢。

1 个赞

lsp-mode没装好,你没看到你lsp-mode和company-box的配置里没有:ensure t

lsp-mode 加上 ensure t 会报这样的错。

Debugger entered--Lisp error: (wrong-type-argument stringp nil)
  string-match("\\`https?:" nil nil)
  package-install-from-archive(#s(package-desc :name spinner :version (1 7 3) :summary "Add spinners and progress-bars to the mode-line for ongoing operations" :reqs nil :kind single :archive "gnu" :dir nil :extras ((:keywords "processes" "mode-line") (:authors ("Artur Malabarba" . "[email protected]")) (:maintainer "Artur Malabarba" . "[email protected]") (:url . "https://github.com/Malabarba/spinner.el")) :signed nil))
  mapc(package-install-from-archive (#s(package-desc :name spinner :version (1 7 3) :summary "Add spinners and progress-bars to the mode-line for ongoing operations" :reqs nil :kind single :archive "gnu" :dir nil :extras ((:keywords "processes" "mode-line") (:authors ("Artur Malabarba" . "[email protected]")) (:maintainer "Artur Malabarba" . "[email protected]") (:url . "https://github.com/Malabarba/spinner.el")) :signed nil) #s(package-desc :name ht :version (20190924 704) :summary "The missing hash table library for Emacs" :reqs ((dash (2 12 0))) :kind single :archive "melpa" :dir nil :extras ((:commit . "66c5f9131242697fabaede5566d87ecda4c14b1f") (:keywords "hash table" "hash map" "hash") (:authors ("Wilfred Hughes" . "[email protected]")) (:maintainer "Wilfred Hughes" . "[email protected]")) :signed nil) #s(package-desc :name f :version (20190109 906) :summary "Modern API for working with files and directories" :reqs ((s (1 7 0)) (dash (2 2 0))) :kind single :archive "melpa" :dir nil :extras ((:commit . "8191672377816a1975414cc1f116fd3b94b30bd0") (:keywords "files" "directories") (:authors ("Johan Andersson" . "[email protected]")) (:maintainer "Johan Andersson" . "[email protected]") (:url . "http://github.com/rejeep/f.el")) :signed nil) #s(package-desc :name lsp-mode :version (20191013 1943) :summary "LSP mode" :reqs ((emacs (25 1)) (dash (2 14 1)) (dash-functional (2 14 1)) (f (0 20 0)) (ht (2 0)) (spinner (1 7 3)) (markdown-mode (2 3))) :kind tar :archive "melpa" :dir nil :extras ((:commit . "d26209de5ed555c5dc14f8c6d9cb1322c3682c88") (:keywords "languages") (:authors ("Vibhav Pant, Fangrui Song, Ivan Yonchovski")) (:maintainer "Vibhav Pant, Fangrui Song, Ivan Yonchovski") (:url . "https://github.com/emacs-lsp/lsp-mode")) :signed nil)))
  package-download-transaction((#s(package-desc :name spinner :version (1 7 3) :summary "Add spinners and progress-bars to the mode-line for ongoing operations" :reqs nil :kind single :archive "gnu" :dir nil :extras ((:keywords "processes" "mode-line") (:authors ("Artur Malabarba" . "[email protected]")) (:maintainer "Artur Malabarba" . "[email protected]") (:url . "https://github.com/Malabarba/spinner.el")) :signed nil) #s(package-desc :name ht :version (20190924 704) :summary "The missing hash table library for Emacs" :reqs ((dash (2 12 0))) :kind single :archive "melpa" :dir nil :extras ((:commit . "66c5f9131242697fabaede5566d87ecda4c14b1f") (:keywords "hash table" "hash map" "hash") (:authors ("Wilfred Hughes" . "[email protected]")) (:maintainer "Wilfred Hughes" . "[email protected]")) :signed nil) #s(package-desc :name f :version (20190109 906) :summary "Modern API for working with files and directories" :reqs ((s (1 7 0)) (dash (2 2 0))) :kind single :archive "melpa" :dir nil :extras ((:commit . "8191672377816a1975414cc1f116fd3b94b30bd0") (:keywords "files" "directories") (:authors ("Johan Andersson" . "[email protected]")) (:maintainer "Johan Andersson" . "[email protected]") (:url . "http://github.com/rejeep/f.el")) :signed nil) #s(package-desc :name lsp-mode :version (20191013 1943) :summary "LSP mode" :reqs ((emacs (25 1)) (dash (2 14 1)) (dash-functional (2 14 1)) (f (0 20 0)) (ht (2 0)) (spinner (1 7 3)) (markdown-mode (2 3))) :kind tar :archive "melpa" :dir nil :extras ((:commit . "d26209de5ed555c5dc14f8c6d9cb1322c3682c88") (:keywords "languages") (:authors ("Vibhav Pant, Fangrui Song, Ivan Yonchovski")) (:maintainer "Vibhav Pant, Fangrui Song, Ivan Yonchovski") (:url . "https://github.com/emacs-lsp/lsp-mode")) :signed nil)))
  package-install(lsp-mode)
  use-package-ensure-elpa(lsp-mode (t) nil)
  eval-buffer(#<buffer  *load*-466402> nil "/Users/byron/.emacs.d/custom/go.el" nil t)  ; Reading at buffer position 898
  load-with-code-conversion("/Users/byron/.emacs.d/custom/go.el" "/Users/byron/.emacs.d/custom/go.el" nil nil)
  load("~/.emacs.d/custom/go.el")
  eval-buffer(#<buffer  *load*> nil "/Users/byron/.emacs.d/init.el" nil t)  ; Reading at buffer position 4665
  load-with-code-conversion("/Users/byron/.emacs.d/init.el" "/Users/byron/.emacs.d/init.el" t t)
  load("/Users/byron/.emacs.d/init" t t)
  #f(compiled-function () #<bytecode 0x400d1a6d>)()
  command-line()
  normal-top-level()

C-h v package-archives RET

看一下值

elpa 总是连不上出错,所以我只设了 melpa.

package-archives is a variable defined in ‘package.el’.
Its value is (("melpa" . "https://melpa.org/packages/"))
Original value was 
(("gnu" . "https://elpa.gnu.org/packages/"))

  This variable may be risky if used as a file-local variable.
...

建议使用

(use-package company
  :hook (after-init . global-company-mode))

否则company-mode无法加载(这得看你是否设置全局defer),如果使用lsp-mode,company-go就可以先disable了,还有lsp-mode, lsp-ui, company-lsp这些应该都要加上:ensure t

然而lsp-mode有个依赖spinner是只有GNU ELPA才有的。连不上为什么不用镜像看看呢

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

加了,可是还是,似乎并不支持ensure t的关键参数

Debugger entered--Lisp error: (wrong-type-argument stringp nil)
  string-match("\\`https?:" nil nil)
  package-install-from-archive(#s(package-desc :name spinner :version (1 7 3) :summary "Add spinners and progress-bars to the mode-line for ongoing operations" :reqs nil :kind single :archive "gnu" :dir nil :extras ((:keywords "processes" "mode-line") (:authors ("Artur Malabarba" . "[email protected]")) (:maintainer "Artur Malabarba" . "[email protected]") (:url . "https://github.com/Malabarba/spinner.el")) :signed nil))
  mapc(package-install-from-archive (#s(package-desc :name spinner :version (1 7 3) :summary "Add spinners and progress-bars to the mode-line for ongoing operations" :reqs nil :kind single :archive "gnu" :dir nil :extras ((:keywords "processes" "mode-line") (:authors ("Artur Malabarba" . "[email protected]")) (:maintainer "Artur Malabarba" . "[email protected]") (:url . "https://github.com/Malabarba/spinner.el")) :signed nil) #s(package-desc :name ht :version (20190924 704) :summary "The missing hash table library for Emacs" :reqs ((dash (2 12 0))) :kind single :archive "melpa" :dir nil :extras ((:commit . "66c5f9131242697fabaede5566d87ecda4c14b1f") (:keywords "hash table" "hash map" "hash") (:authors ("Wilfred Hughes" . "[email protected]")) (:maintainer "Wilfred Hughes" . "[email protected]")) :signed nil) #s(package-desc :name s :version (20180406 808) :summary "The long lost Emacs string manipulation library." :reqs nil :kind single :archive "melpa" :dir nil :extras ((:commit . "03410e6a7a2b11e47e1fea3b7d9899c7df26435e") (:keywords "strings") (:authors ("Magnar Sveen" . "[email protected]")) (:maintainer "Magnar Sveen" . "[email protected]")) :signed nil) #s(package-desc :name f :version (20190109 906) :summary "Modern API for working with files and directories" :reqs ((s (1 7 0)) (dash (2 2 0))) :kind single :archive "melpa" :dir nil :extras ((:commit . "8191672377816a1975414cc1f116fd3b94b30bd0") (:keywords "files" "directories") (:authors ("Johan Andersson" . "[email protected]")) (:maintainer "Johan Andersson" . "[email protected]") (:url . "http://github.com/rejeep/f.el")) :signed nil) #s(package-desc :name lsp-mode :version (20191013 1943) :summary "LSP mode" :reqs ((emacs (25 1)) (dash (2 14 1)) (dash-functional (2 14 1)) (f (0 20 0)) (ht (2 0)) (spinner (1 7 3)) (markdown-mode (2 3))) :kind tar :archive "melpa" :dir nil :extras ((:commit . "d26209de5ed555c5dc14f8c6d9cb1322c3682c88") (:keywords "languages") (:authors ("Vibhav Pant, Fangrui Song, Ivan Yonchovski")) (:maintainer "Vibhav Pant, Fangrui Song, Ivan Yonchovski") (:url . "https://github.com/emacs-lsp/lsp-mode")) :signed nil)))
  package-download-transaction((#s(package-desc :name spinner :version (1 7 3) :summary "Add spinners and progress-bars to the mode-line for ongoing operations" :reqs nil :kind single :archive "gnu" :dir nil :extras ((:keywords "processes" "mode-line") (:authors ("Artur Malabarba" . "[email protected]")) (:maintainer "Artur Malabarba" . "[email protected]") (:url . "https://github.com/Malabarba/spinner.el")) :signed nil) #s(package-desc :name ht :version (20190924 704) :summary "The missing hash table library for Emacs" :reqs ((dash (2 12 0))) :kind single :archive "melpa" :dir nil :extras ((:commit . "66c5f9131242697fabaede5566d87ecda4c14b1f") (:keywords "hash table" "hash map" "hash") (:authors ("Wilfred Hughes" . "[email protected]")) (:maintainer "Wilfred Hughes" . "[email protected]")) :signed nil) #s(package-desc :name s :version (20180406 808) :summary "The long lost Emacs string manipulation library." :reqs nil :kind single :archive "melpa" :dir nil :extras ((:commit . "03410e6a7a2b11e47e1fea3b7d9899c7df26435e") (:keywords "strings") (:authors ("Magnar Sveen" . "[email protected]")) (:maintainer "Magnar Sveen" . "[email protected]")) :signed nil) #s(package-desc :name f :version (20190109 906) :summary "Modern API for working with files and directories" :reqs ((s (1 7 0)) (dash (2 2 0))) :kind single :archive "melpa" :dir nil :extras ((:commit . "8191672377816a1975414cc1f116fd3b94b30bd0") (:keywords "files" "directories") (:authors ("Johan Andersson" . "[email protected]")) (:maintainer "Johan Andersson" . "[email protected]") (:url . "http://github.com/rejeep/f.el")) :signed nil) #s(package-desc :name lsp-mode :version (20191013 1943) :summary "LSP mode" :reqs ((emacs (25 1)) (dash (2 14 1)) (dash-functional (2 14 1)) (f (0 20 0)) (ht (2 0)) (spinner (1 7 3)) (markdown-mode (2 3))) :kind tar :archive "melpa" :dir nil :extras ((:commit . "d26209de5ed555c5dc14f8c6d9cb1322c3682c88") (:keywords "languages") (:authors ("Vibhav Pant, Fangrui Song, Ivan Yonchovski")) (:maintainer "Vibhav Pant, Fangrui Song, Ivan Yonchovski") (:url . "https://github.com/emacs-lsp/lsp-mode")) :signed nil)))
  package-install(lsp-mode)
  use-package-ensure-elpa(lsp-mode (t) nil)
  eval-buffer(#<buffer  *load*-932606> nil "/Users/byron/.emacs.d/custom/go.el" nil t)  ; Reading at buffer position 890
  load-with-code-conversion("/Users/byron/.emacs.d/custom/go.el" "/Users/byron/.emacs.d/custom/go.el" nil nil)
  load("~/.emacs.d/custom/go.el")
  eval-buffer(#<buffer  *load*> nil "/Users/byron/.emacs.d/init.el" nil t)  ; Reading at buffer position 4950
  load-with-code-conversion("/Users/byron/.emacs.d/init.el" "/Users/byron/.emacs.d/init.el" t t)
  load("/Users/byron/.emacs.d/init" t t)
  #f(compiled-function () #<bytecode 0x400d1a6d>)()
  command-line()
  normal-top-level()

你这个emacs感觉有问题啊,重装一下看看

我是用 brew cask 安装的。MacOS 也升级到最新的 Catalina 了。。。

brew cask install emacs 

爱莫能助……

你能说是我emacs的问题,已经是我这两天最大的安慰了。配到怀疑人生。

设为

(use-package company
  :ensure t
  :config
  (global-company-mode t)
  (setq company-idle-delay 0)
  (setq company-minimum-prefix-length 3)
  (setq company-backends
        `((:separate company-files company-capf company-keywords )
          (:separate company-abbrev company-dabbrev)))
  :hook (after-init . global-company-mode)
  )

结果错误是这个:

Warning (initialization): An error occurred while loading ‘/Users/byron/.emacs.d/init.el’:

Symbol's value as variable is void: company-backends

To ensure normal operation, you should investigate and remove the
cause of the error in your initialization file.  Start Emacs with
the ‘--debug-init’ option to view a complete error backtrace.

:config 里的 global-company-mode 就不要用了,这个错误是由(push 'company-lsp company-backends)引起的,修改

(use-package company-lsp
  :after company
   :init (push 'company-lsp company-backends))

其实相关的配置直接clone一份spacemacs或者doom-emacs到本地,需要哪些mode的配置就直接去这些公有配置里找就行,如果你要从头配置一份自己的emacs,还是需要先了解一下elisp的

我觉得你可以从最简单的手动安装一个包开始

或是使用别人配置好的 如doom space

use-package有点复杂 隐藏了底层的东西 不建议新人使用

谢谢各位的帮助,问题已经解决,在此把问题原因一一列出,以分享。

1.我在init.el中增加了自定义变量文件,并且把package-archives写死了。所以一直更新不了 spinner,导致 lsp-mode 中 enusre t 一直出错。

(setq custom-file "~/.emacs.d/custom-variables.el")
(load custom-file)

正如 @ cireu 所说,谢谢提醒。

  1. go.el文件中,我胡乱抄了一个 push
;; (push 'company-lsp company-backends)   ;小心使用
;; (add-hook 'go-mode-hook #'lsp-deferred)  ;小心使用
(add-hook 'go-mode-hook #'lsp)
  1. 在外网查到一个“重新安装” emacs 的方法就是直接删除 .emacs.d/elpa 目录,用use-pakcage 重新下载。

  2. 我使用了清华的镜像。

(setq package-archives '(("gnu"   . "http://mirrors.tuna.tsinghua.edu.cn/elpa/gnu/")
                         ("melpa" . "http://mirrors.tuna.tsinghua.edu.cn/elpa/melpa/")))

再次感谢 emacser 的帮助。这个圈子小,有人互助真好。

1 个赞

company-lsp 已经被 deprecated 了,lsp 里面现在采用 capf 的方式来做补全了,需要把这个放在 company-backends 靠前位置,或者不设置,lsp 会自动 configure 是会 push 进去。

  '(company-dabbrev-code
    (company-gtags company-etags
                   company-keywords)
    company-files
    ;; company-capf
    company-dabbrev
    )

我之前这样设置的,没法补全,注释掉 capf 就好了