From 02a128bd020d8d41d77c4345c7f8b28abb8a0a11 Mon Sep 17 00:00:00 2001 From: eltrac Date: Thu, 5 Mar 2026 11:57:53 +0800 Subject: docs: new note --- content/notes/nvim-new-language-sop.md | 130 +++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 content/notes/nvim-new-language-sop.md (limited to 'content') diff --git a/content/notes/nvim-new-language-sop.md b/content/notes/nvim-new-language-sop.md new file mode 100644 index 0000000..46ffdb1 --- /dev/null +++ b/content/notes/nvim-new-language-sop.md @@ -0,0 +1,130 @@ ++++ +date = '2026-03-05T11:18:03+08:00' +draft = false +title = '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: + - 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,即计算值的意思。 -- cgit v1.2.3