1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
+++
date = '2026-03-05T11:18:03+08:00'
draft = false
title = 'Neovim 添加新语言的标准操作流程'
tags = ['Neovim','编程']
+++
这篇笔记基于[我的 Neovim 配置](https://src.eltr.ac/eltrac/nvim)进行。
最近学校课程要学 Python,作为 Neovim 用户当然是不会去用老师推荐的 PyCharm 的。要获得比较好的体验就要配置语言服务器(LSP)和 Treesitter,一些语言可能还有其他的辅助工具。为了方便未来配置开发环境,故做此笔记。
## LSP
配置 `lua/plugins/nvim-lspconfig.lua`,找到自己定义的 `lsps` 变量,在这个数组里添加需要的语言服务器名字。可使用 [Telescope](https://github.com/nvim-telescope/telescope.nvim) 的 `help_tags` Picker 搜索 `lspconfig-all`(或者直接 `:help lspconfig-all`)打开所有 LSP 配置的帮助文档,按下 `/` 搜索需要的语言,找到**准确**的语言服务器名称。
例如,Python 的语言服务器可以用 `pylsp`
```lua
local lsps = {
"gopls",
"marksman",
"ts_ls",
"vue_ls",
"cssls",
"html",
"unocss",
"clojure_lsp",
"pylsp"
}
```
运行 `:checkhealth vim.lsp` 检查语言服务器状态,比如,配置 `pylsp` 之后应该能看到这一条。
```
- pylsp:
- before_init: <function @/Users/eltrac/.local/share/nvim/lazy/mason-lspconfig.nvim/lua/mason-lspconfig/lsp/pylsp.lua:5>
- cmd: { "pylsp" }
- filetypes: python
- root_markers: { "pyproject.toml", "setup.py", "setup.cfg", "requirements.txt", "Pipfile", ".git" }
```
## Treesitter
配置 `lua/plugins/nvim-treesitter.lua`,在 `install({})` 方法中添加语言名称即可安装对应语言的 Treesitter。
```lua
require("nvim-treesitter").install({
'javascript',
'html',
'css',
'go',
'clojure',
'gitignore',
'gotmpl',
'markdown',
'lua',
'python'
})
```
成功后应该能在 `:checkhealth` 中找到:
```
Installed languages H L F I J ~
- clojure ✓ ✓ ✓ . ✓
- css ✓ . ✓ ✓ ✓
- ecma
- gitignore ✓ . . . ✓
- go ✓ ✓ ✓ ✓ ✓
- gotmpl ✓ ✓ ✓ . ✓
- html ✓ ✓ ✓ ✓ ✓
- html_tags
- javascript ✓ ✓ ✓ ✓ ✓
- jsx
- lua ✓ ✓ ✓ ✓ ✓
- markdown ✓ . ✓ ✓ ✓
- markdown_inline ✓ . . . ✓
- python ✓ ✓ ✓ ✓ ✓
Legend: [H]ighlights, [L]ocals, [F]olds, [I]ndents, In[J]ections ~
```
### Lisp 方言的额外步骤
为了让 `nvim-paredit` 正常工作,需要手动启动 Lisp 方言的 Treesitter,因为之前的步骤只是安装了 Treesitter,而 `nvim-paredit` 不会自己启动 Treesitter。
在 `init.lua`:
```lua
vim.api.nvim_create_autocmd('FileType', {
pattern = { 'clojure' }, // 加上新增的 Lisp 方言
callback = function() vim.treesitter.start() end,
})
```
应该有更优雅的解决方案,但目前这样也能跑。
## 其他检查
可以在 `:checkhealth` 里查找 `ERROR` 和 `WARNING`,看看有没有其他问题。
一些语言也有专门给 Neovim 的 Provider,这些 Provider 让 Neovim 拥有对应语言专门的一些特性。
```
Python 3 provider (optional) ~
- `g:python3_host_prog` is not set. Searching for pynvim-python in the environment.
- Executable: /Users/eltrac/.local/share/../bin/pynvim-python
- Python version: 3.13.1
- pynvim version: 0.6.0
- ✅ OK Latest pynvim is installed.
```
比如这里的 `pynvim` 就是一个 Provider,一般来说是单独的二进制文件,和 LSP 一样在独立的进程里运行,并与 Neovim 通信。
## 在编辑器里执行代码
最简单的方法是在 Neovim 里打开终端,用 `:term`。比如这是最简单的在 Neovim 里执行当前 Python 文件的命令。
```
:term python %
```
个人比较喜欢的方法是用 [Conjure](https://github.com/Olical/conjure) 插件,可以在编辑器里直接 Eval[^1] 代码。
---
有了 LSP 的语言分析能力和 Treesitter 的语法解析能力,配合本身就已经安装了的、会调用 LSP 和 Treesitter 的插件,比如 `nvim-cmp`(代码补全提示)和 `nvim-paredit`,又有了在编辑器里直接测试和执行代码的方法(其实就算没有配置编辑器,直接在命令行里执行代码也很好用了),基本上就能应对大部分的编码场景了。
**你完全不需要臃肿的 IDE**
[^1]: Evaluate,即计算值的意思。
|