2019年08月09日

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

[2021-09-07 Tue 00:28] updated: 在用 pyright 了。

mspyls on windows

[2019-07-04 Thu 09:58]

下载地址
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

[2019-08-07 Wed 09:21]

[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可以用 bingogolsp

bingo已经不更新了,转移到go官方的 golsp

golsp和bingo在windows上体验很差,看代码一会就emacs就会报错,如 package找不到之类的。

在linux上稳得不得了。建议在linux上写go的代码,emacs不卡顿。

[2019-07-04 Thu 16:09]

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

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

[2019-06-14 Fri 21:30]

M-x lsp-workspace-folders-add 把目录加到lsp里去。

seagle0128 18年11月

请参考 https://github.com/seagle0128/.emacs.d/blob/master/lisp/init-lsp.elhttps://github.com/seagle0128/.emacs.d/blob/master/lisp/init-python.el

https://vxlabs.com/2018/11/19/configuring-emacs-lsp-mode-and-microsofts-visual-studio-code-python-language-server/

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

[2019-06-15 Sat 22:19] 为什么用了mspyls后就找不到其他的package,而用pyls就可以找到? pyls比mspyls会多起不少python进程,可能是jedi起来的,很讨厌。

[2019-06-15 Sat 23:57]

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

[2019-05-15 Wed 15:33] 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)