emacs python和go的lsp配置
微软在2017年提出lsp后,让emacs和vim之类纯文本编辑器可以摆脱tag的方式,可以直接对语言进行语法、语义分析、代码检查、重构、intelliSense、查看定义、跳转。emacs和vim的神器啊。
python - msplys
python的lsp比以前常用的 elpy 稍微好用点。
写python代码,以前一直用的是elpy,但是elpy在windows上的体验非常差。卡顿、 每次 C-c C-c 后会起一个python进程,执行多了会有一大堆python进程。但是在linux就非常稳定,不会起很多python进程。
lsp有python的和微软的。推荐使用微软的 mspyls 。
pyright
了。
mspyls on windows
- 下载地址
- https://github.com/Microsoft/python-language-server
- .NET Core RID 目录
- https://docs.microsoft.com/zh-cn/dotnet/core/rid-catalog
git clone https://github.com/Microsoft/python-language-server.git
cd src/LanguageServer/Impl
dotnet build
dotnet publish -c Release -r win-x64
mspyls on linux
linux的rid使用 linux-x64 。
[albert@master.k8s /home/albert/mspyls/python-language-server] $git pull -v [albert@master.k8s /home/albert/mspyls/python-language-server] $cd src/LanguageServer/Impl [albert@master.k8s /home/albert/mspyls/python-language-server/src/LanguageServer/Impl] $dotnet build Microsoft (R) Build Engine version 16.1.76+g14b0a930a7 for .NET Core Copyright (C) Microsoft Corporation. All rights reserved. Restore completed in 126.08 ms for /home/albert/mspyls/python-language-server/src/Analysis/Core/Impl/Microsoft.Python.Analysis.Core.csproj. Restore completed in 126.08 ms for /home/albert/mspyls/python-language-server/src/Analysis/Ast/Impl/Microsoft.Python.Analysis.csproj. Restore completed in 1.31 ms for /home/albert/mspyls/python-language-server/src/Core/Impl/Microsoft.Python.Core.csproj. Restore completed in 1.3 ms for /home/albert/mspyls/python-language-server/src/Parsing/Impl/Microsoft.Python.Parsing.csproj. [albert@master.k8s /home/albert/mspyls/python-language-server/src/LanguageServer/Impl] $dotnet publish -c Release -r linux-x64 Microsoft (R) Build Engine version 16.1.76+g14b0a930a7 for .NET Core Copyright (C) Microsoft Corporation. All rights reserved. Restore completed in 58.86 ms for /home/albert/mspyls/python-language-server/src/Analysis/Ast/Impl/Microsoft.Python.Analysis.csproj. Restore completed in 58.87 ms for /home/albert/mspyls/python-language-server/src/Analysis/Core/Impl/Microsoft.Python.Analysis.Core.csproj. Restore completed in 0.65 ms for /home/albert/mspyls/python-language-server/src/Core/Impl/Microsoft.Python.Core.csproj. Restore completed in 0.68 ms for /home/albert/mspyls/python-language-server/src/Parsing/Impl/Microsoft.Python.Parsing.csproj. Restore completed in 1.41 sec for /home/albert/mspyls/python-language-server/src/LanguageServer/Impl/Microsoft.Python.LanguageServer.csproj. Microsoft.Python.Core -> /home/albert/mspyls/python-language-server/output/bin/Release/Microsoft.Python.Core.dll Microsoft.Python.Parsing -> /home/albert/mspyls/python-language-server/output/bin/Release/Microsoft.Python.Parsing.dll Microsoft.Python.Analysis.Core -> /home/albert/mspyls/python-language-server/output/bin/Release/Microsoft.Python.Analysis.Core.dll Microsoft.Python.Analysis -> /home/albert/mspyls/python-language-server/output/bin/Release/Microsoft.Python.Analysis.dll Microsoft.Python.LanguageServer -> /home/albert/mspyls/python-language-server/output/bin/Release/Microsoft.Python.LanguageServer.dll Microsoft.Python.LanguageServer -> /home/albert/mspyls/python-language-server/output/bin/Release/linux-x64/publish/
参考 https://github.com/emacs-lsp/lsp-python-ms
go - bingo and golsp
go的lsp可以用 bingo 和 golsp 。
bingo已经不更新了,转移到go官方的 golsp 。
golsp和bingo在windows上体验很差,看代码一会就emacs就会报错,如 package找不到之类的。
在linux上稳得不得了。建议在linux上写go的代码,emacs不卡顿。
go在windows中的环境变量
set GOPATH=E:\workspace\go set GOROOT=E:\backup\go1.12.windows-amd64\go
go在linux中的环境变量 需要设置
GOPATH
,在PATH
中加入go和gopls的path。export GOPATH=$HOME/go export PATH=$PATH:/usr/local/go/bin:$HOME/go/bin
Install gopls
- gopls安装目录
- ~/go/bin
安装gopls到 $GOPATH/bin 目录,设置了GOPATH后,会装到 $GOPATH/bin 下
cd /drives/e/workspace/go/src/golang.org/x/tools git pull -v cd /drives/e/workspace/go/src/golang.org/x/tools/gopls go install
Install bingo
bingo在windows上速度非常慢。linux上没有试过。
bingo is a go module project, so you need install Go 1.11 or above, to install the bingo, please run
https://github.com/saibing/bingo/wiki/Install
处理install bingo被墙的问题
E:\workspace\bingo\tools\cmd\gopls>go install go: golang.org/x/net@v0.0.0-20190620200207-3b0461eec859: unrecognized import path "golang.org/x/net" (https fetch: Get https://golang.org/x/net?go-get=1: dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.) mkdir -p $GOPATH/src/golang.org/x cd $GOPATH/src/golang.org/x git clone https://github.com/golang/net.git git clone https://github.com/golang/sync.git git clone https://github.com/golang/tools.git
git clone -b bingo https://github.com/saibing/tools.git cd tools/cmd/gopls go install # 不知道为什么 -u 就是不行 go get -u github.com/saibing/bingo [2019-07-04 周四 16:59:20] go get -v github.com/saibing/bingo
emacs配置
lsp-mode
M-x lsp-workspace-folders-add 把目录加到lsp里去。
seagle0128 18年11月
请参考 https://github.com/seagle0128/.emacs.d/blob/master/lisp/init-lsp.el 和 https://github.com/seagle0128/.emacs.d/blob/master/lisp/init-python.el
https://github.com/emacs-lsp/lsp-python-ms
;; Emacs client for the Language Server Protocol ;; https://github.com/emacs-lsp/lsp-mode#supported-languages (use-package lsp-mode ;; :diminish lsp-mode :defer t ;; :hook (prog-mode . lsp) :hook (python-mode . lsp-deferred) (go-mode . lsp-deferred) :bind (:map lsp-mode-map ("C-c C-d" . lsp-describe-thing-at-point)) :init (setq lsp-auto-guess-root t) ; Detect project root ;; disable Yasnippet (setq lsp-enable-snippet nil) (setq lsp-prefer-flymake nil) ; Use lsp-ui and flycheck (setq flymake-fringe-indicator-position 'right-fringe) ) (use-package lsp-ui :defer t :custom-face (lsp-ui-doc-background ((t (:background nil)))) :bind (:map lsp-ui-mode-map ([remap xref-find-definitions] . lsp-ui-peek-find-definitions) ([remap xref-find-references] . lsp-ui-peek-find-references) ("C-c u" . lsp-ui-imenu)) :init (setq lsp-ui-doc-enable t lsp-ui-doc-use-webkit t lsp-ui-doc-include-signature t lsp-ui-doc-position 'top lsp-ui-doc-border (face-foreground 'default) ;; lsp-enable-snippet nil lsp-ui-sideline-enable nil ;; emacs26.2 经常陷入卡顿, set it to nil. lsp-use-native-json nil lsp-ui-sideline-ignore-duplicate t) :config ;; WORKAROUND Hide mode-line of the lsp-ui-imenu buffer ;; https://github.com/emacs-lsp/lsp-ui/issues/243 (defadvice lsp-ui-imenu (after hide-lsp-ui-imenu-mode-line activate) (setq mode-line-format nil))) (use-package company-lsp :defer t :init (setq company-lsp-cache-candidates 'auto)) (use-package lsp-treemacs :defer t :bind (:map lsp-mode-map ("M-9" . lsp-treemacs-errors-list)))
为什么用了mspyls后就找不到其他的package,而用pyls就可以找到? pyls比mspyls会多起不少python进程,可能是jedi起来的,很讨厌。
(use-package lsp-python-ms ;; :ensure nil ;; :defer t :demand :after lsp-mode :hook (python-mode . lsp) :config (setq lsp-python-ms-dir (expand-file-name "e:/workspace/python-language-server/output/bin/Release/")) ;; for executable of language server, if it's not symlinked on your PATH (setq lsp-python-ms-executable "e:/workspace/python-language-server/output/bin/Release/Microsoft.Python.LanguageServer.exe"))
显示行、列号
列号是从0开始的。
(column-number-mode t)
emacs 26.2可以用,显示速度比linum快很多。
(setq display-line-numbers-width-start 5)
(add-hook 'org-mode-hook 'display-line-numbers-mode)
(add-hook 'python-mode-hook 'display-line-numbers-mode)
(add-hook 'c-mode-common-hook 'display-line-numbers-mode)
(add-hook 'emacs-lisp-mode-hook 'display-line-numbers-mode)
(add-hook 'sh-mode-hook 'display-line-numbers-mode)
(add-hook 'go-mode-hook 'display-line-numbers-mode)