2018-01-27 01:07:02 +08:00
|
|
|
|
---
|
2018-05-27 13:05:57 +08:00
|
|
|
|
title: "SpaceVim lsp 模块"
|
2019-01-16 23:34:39 +08:00
|
|
|
|
description: "这一模块为 SpaceVim 提供了 language server protocol 的支持,提供更多语言相关服务。"
|
2018-05-27 13:05:57 +08:00
|
|
|
|
lang: cn
|
2018-01-27 01:07:02 +08:00
|
|
|
|
---
|
|
|
|
|
|
2018-05-27 13:05:57 +08:00
|
|
|
|
# [可用模块](../) >> lsp
|
2018-01-27 01:07:02 +08:00
|
|
|
|
|
|
|
|
|
<!-- vim-markdown-toc GFM -->
|
|
|
|
|
|
2018-05-27 13:05:57 +08:00
|
|
|
|
- [模块描述](#模块描述)
|
|
|
|
|
- [模块特性](#模块特性)
|
|
|
|
|
- [模块安装](#模块安装)
|
|
|
|
|
- [安装语言服务器](#安装语言服务器)
|
|
|
|
|
- [模块配置](#模块配置)
|
|
|
|
|
- [快捷键](#快捷键)
|
2018-01-27 01:07:02 +08:00
|
|
|
|
|
|
|
|
|
<!-- vim-markdown-toc -->
|
|
|
|
|
|
2018-05-27 13:05:57 +08:00
|
|
|
|
## 模块描述
|
2018-01-27 01:07:02 +08:00
|
|
|
|
|
2018-05-27 13:05:57 +08:00
|
|
|
|
lsp 模块提供了 [language-server-protocol](https://microsoft.github.io/language-server-protocol/) 的支持,
|
|
|
|
|
这以模块在 [LanguageClient-neovim](https://github.com/SpaceVim/LanguageClient-neovim) 基础上做了许多定制,
|
|
|
|
|
这一链接为一克隆的版本,需要 `+python` 支持。
|
2018-01-27 01:07:02 +08:00
|
|
|
|
|
2019-01-16 23:34:39 +08:00
|
|
|
|
同时,当你的 Vim 不支持 `+python3` 时,我们采用 [vim-lsp](https://github.com/prabirshrestha/vim-lsp) 这一使用纯 Vim 脚本书写的插件。
|
2018-01-27 01:07:02 +08:00
|
|
|
|
|
2019-01-16 23:34:39 +08:00
|
|
|
|
Neovim 团队也在尝试实现一个内置的 [lsp 框架](https://github.com/neovim/neovim/pull/6856),后续该框架完成后,在 Neovim 内将默认使用该框架。
|
2018-01-27 01:07:02 +08:00
|
|
|
|
|
2018-05-27 13:05:57 +08:00
|
|
|
|
在 SpaceVim 内部,lsp 的框架选择逻辑大致时这样的:
|
2018-01-27 01:07:02 +08:00
|
|
|
|
|
|
|
|
|
```vim
|
|
|
|
|
if has('nvim')
|
|
|
|
|
" use neovim build-in lsp
|
|
|
|
|
elseif has('python3')
|
|
|
|
|
" use LanguageClient-neovim
|
|
|
|
|
else
|
|
|
|
|
" use vim-lsp
|
|
|
|
|
endif
|
|
|
|
|
```
|
|
|
|
|
|
2018-05-27 13:05:57 +08:00
|
|
|
|
## 模块特性
|
2018-01-27 01:07:02 +08:00
|
|
|
|
|
2018-05-27 13:05:57 +08:00
|
|
|
|
- 异步调用,避免卡顿
|
|
|
|
|
- 代码补全,(需要载入 [autocomplet](https://spacevim.org/layers/autocomplete/) 模块)
|
|
|
|
|
- 实时代码语法检查
|
|
|
|
|
- 重命名函数
|
|
|
|
|
- 显示错误信息
|
|
|
|
|
- 跳至定义处
|
|
|
|
|
- 列举所有引用
|
|
|
|
|
- 文档检索
|
|
|
|
|
- 代码格式化
|
|
|
|
|
- 代码自动修复
|
2018-01-27 01:07:02 +08:00
|
|
|
|
|
2019-01-16 23:34:39 +08:00
|
|
|
|
**注:** 以上所有信息依赖于语言服务器所实现的功能,请查阅 [Language Servers](https://microsoft.github.io/language-server-protocol/implementors/servers/) 列表。
|
2018-01-27 01:07:02 +08:00
|
|
|
|
|
2018-05-27 13:05:57 +08:00
|
|
|
|
## 模块安装
|
2018-01-27 01:07:02 +08:00
|
|
|
|
|
2019-01-16 23:34:39 +08:00
|
|
|
|
默认未载入,如需载入该模块,可在配置文件加入:
|
2018-01-27 01:07:02 +08:00
|
|
|
|
|
2018-05-27 13:05:57 +08:00
|
|
|
|
```toml
|
|
|
|
|
[[layers]]
|
|
|
|
|
name = "lsp"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 安装语言服务器
|
2018-01-27 01:07:02 +08:00
|
|
|
|
|
2018-08-15 21:26:38 +08:00
|
|
|
|
**Bash**
|
|
|
|
|
|
|
|
|
|
```sh
|
|
|
|
|
npm i -g bash-language-server
|
|
|
|
|
```
|
|
|
|
|
|
2018-01-27 01:07:02 +08:00
|
|
|
|
**JavaScript:**
|
|
|
|
|
|
|
|
|
|
```sh
|
|
|
|
|
npm install -g javascript-typescript-langserver
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**Python:**
|
|
|
|
|
|
|
|
|
|
```sh
|
|
|
|
|
pip install --user python-language-server
|
|
|
|
|
```
|
|
|
|
|
|
2018-08-15 21:26:38 +08:00
|
|
|
|
**julia:**
|
|
|
|
|
|
|
|
|
|
安装 `LanguageServer` 包需要 Julia(0.6 或更高版本).
|
|
|
|
|
|
|
|
|
|
```sh
|
|
|
|
|
julia> Pkg.clone("https://github.com/JuliaEditorSupport/LanguageServer.jl")
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
在 Julia 0.7 新的包管理系统下,可以在 REPL 模式下进行安装,只需要输入 `]` 即可切入到包管理模式,
|
|
|
|
|
然后执行 `add LanguageServer` 来安装对应的包。
|
|
|
|
|
|
2018-09-09 13:13:55 +08:00
|
|
|
|
**vue:**
|
|
|
|
|
|
|
|
|
|
```sh
|
|
|
|
|
npm install vue-language-server -g
|
|
|
|
|
```
|
|
|
|
|
|
2018-05-27 13:05:57 +08:00
|
|
|
|
## 模块配置
|
2018-01-27 01:07:02 +08:00
|
|
|
|
|
2018-05-27 13:05:57 +08:00
|
|
|
|
为指定模块启用语言服务器支持,需要在载入模块时,指定 `filetypes` 选项:
|
2018-01-27 01:07:02 +08:00
|
|
|
|
|
2018-05-27 13:05:57 +08:00
|
|
|
|
```toml
|
|
|
|
|
[[layers]]
|
|
|
|
|
name = "lsp"
|
|
|
|
|
filetypes = [
|
|
|
|
|
"rust",
|
|
|
|
|
"javascript"
|
|
|
|
|
]
|
2018-01-27 01:07:02 +08:00
|
|
|
|
```
|
|
|
|
|
|
2018-05-27 13:05:57 +08:00
|
|
|
|
默认语言服务器的执行命令列表如下:
|
2018-01-27 01:07:02 +08:00
|
|
|
|
|
2018-08-15 21:26:38 +08:00
|
|
|
|
| 语言 | 命令 |
|
|
|
|
|
| ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
|
|
|
| `javascript` | `['javascript-typescript-stdio']` |
|
2019-01-16 23:34:39 +08:00
|
|
|
|
| `sh` | `['bash-language-server', 'start']` |
|
2018-08-15 21:26:38 +08:00
|
|
|
|
| `typescript` | `['typescript-language-server', '--stdio']` |
|
|
|
|
|
| `haskell` | `['hie', '--lsp']` |
|
|
|
|
|
| `c` | `['clangd']` |
|
|
|
|
|
| `cpp` | `['clangd']` |
|
|
|
|
|
| `html` | `['html-languageserver', '--stdio']` |
|
|
|
|
|
| `objc` | `['clangd']` |
|
|
|
|
|
| `objcpp` | `['clangd']` |
|
|
|
|
|
| `dart` | `['dart_language_server']` |
|
|
|
|
|
| `go` | `['go-langserver', '-mode', 'stdio']` |
|
|
|
|
|
| `rust` | `['rustup', 'run', 'nightly', 'rls']` |
|
|
|
|
|
| `python` | `['pyls']` |
|
|
|
|
|
| `php` | `['php', 'path/to/bin/php-language-server.php']` |
|
|
|
|
|
| `julia` | `['julia', '--startup-file=no', '--history-file=no', '-e', 'using LanguageServer; server = LanguageServer.LanguageServerInstance(STDIN, STDOUT, false); server.runlinter = true; run(server);']` |
|
2018-09-09 13:13:55 +08:00
|
|
|
|
| `vue` | `['vls']` |
|
2018-01-27 01:07:02 +08:00
|
|
|
|
|
2018-05-27 13:05:57 +08:00
|
|
|
|
如果需要修改语言服务器的命令,在载入模块时,需要指定 `override_cmd` 选项:
|
|
|
|
|
|
|
|
|
|
```toml
|
|
|
|
|
[[layers]]
|
|
|
|
|
name = "lsp"
|
|
|
|
|
filetypes = [
|
|
|
|
|
"rust",
|
|
|
|
|
"javascript"
|
|
|
|
|
]
|
|
|
|
|
[layers.override_cmd]
|
|
|
|
|
rust = ["rustup", "run", "nightly", "rls"]
|
2018-01-27 01:07:02 +08:00
|
|
|
|
```
|
|
|
|
|
|
2018-05-27 13:05:57 +08:00
|
|
|
|
## 快捷键
|
2018-01-27 01:07:02 +08:00
|
|
|
|
|
2018-05-27 13:05:57 +08:00
|
|
|
|
| 按键 | 描述 |
|
2018-01-27 01:07:02 +08:00
|
|
|
|
| --------------- | ------------- |
|
2018-05-27 13:05:57 +08:00
|
|
|
|
| `K` / `SPC l d` | 显示文档 |
|
|
|
|
|
| `SPC l e` | 重命名 symbol |
|