lazy.nvim Lua Treesitter

现在Neovim已经集成了lua treesitter parser,我在最近的 Debian环境编译neovim 源代码是最新版本

编译nvim的debian安装包
# 如果已经安装过neovim,则先卸载
# sudo apt remove neovim
# sudo apt install ninja-build gettext cmake unzip curl

git clone git@github.com:neovim/neovim.git
cd neovim
make CMAKE_BUILD_TYPE=RelWithDebInfo
cd build
cpack -G DEB
sudo dpkg -i nvim-linux64.deb

# sudo dpkg -i nvim-linux64.deb
# sudo apt remove neovim
# 这里使用 --force-overwrite 可以覆盖之前移除neovim 0.7.x 遗留的文件
# sudo dpkg -i --force-overwrite  nvim-linux64.deb

遇到如下报错:

使用自己编译的 nvim 配置 NeoVim IDE ,在进行 plugins.lua 部分时,使用:

~/.config/nvim/lua/plugins.lua 管理插件
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "https://github.com/folke/lazy.nvim.git",
    "--branch=stable", -- latest stable release
    lazypath,
  })
end
vim.opt.rtp:prepend(lazypath)

require("lazy").setup({})

一旦 init.lua 应用了这个 plugins.lua ,则使用 nvim 编辑任何 .lua 文件都会出现如下报错:

不能正确处理 .lua 文件类型的 treesitter-parsers 报错
Error detected while processing BufReadPost Autocommands for "*":                                                             
Error executing lua callback: /usr/local/share/nvim/runtime/filetype.lua:36:
 BufReadPost Autocommands for "*"..FileType Autocommands for "*"..function <SNR>1_LoadFTPlugin[20]..script 
 /usr/local/share/nvim/runtime/ftplugin/lua.lua: Vim(runtime):E5113: 
 Error while calling lua chunk: /usr/share/nvim/runtime/lua/vim/treesitter/language.lua:107: 
 no parser for 'lua' language, see :help treesitter-parsers                               
stack traceback:                                                                                                              
        [C]: in function 'error'                                                                                              
        /usr/share/nvim/runtime/lua/vim/treesitter/language.lua:107: in function 'add'
        /usr/share/nvim/runtime/lua/vim/treesitter/languagetree.lua:111: in function 'new'
        /usr/share/nvim/runtime/lua/vim/treesitter.lua:41: in function '_create_parser'
        /usr/share/nvim/runtime/lua/vim/treesitter.lua:108: in function 'get_parser'
        /usr/share/nvim/runtime/lua/vim/treesitter.lua:422: in function 'start'
        /usr/share/nvim/runtime/ftplugin/lua.lua:2: in main chunk
        [C]: in function 'nvim_cmd'
        /usr/share/nvim/runtime/filetype.lua:36: in function </usr/share/nvim/runtime/filetype.lua:35>
        [C]: in function 'pcall'
        vim/shared.lua: in function <vim/shared.lua:0>
        [C]: in function '_with'
        /usr/share/nvim/runtime/filetype.lua:35: in function </usr/share/nvim/runtime/filetype.lua:10>
stack traceback:
        [C]: in function '_with'
        /usr/share/nvim/runtime/filetype.lua:35: in function </usr/share/nvim/runtime/filetype.lua:10>
Press ENTER or type command to continue                                                                                       

nvim-treesitter 是在Neovim中提供使用 tree-sitter 的简单易用方式,并且提供一些基于 tree-sitter 基本功能,如高亮。

这个报错在 bug: Encountered Error detected while processing BufReadPost Autocommands for "*": while loading a Lua file failing in Neovim nightly #1343 和发行版有关(例如fedora)。所以我修改了编译安装方法,参考Neovim官方安装文档 neovim/INSTALL.md 重新安装:

编译nvim安装到用户目录
git clone [email protected]:neovim/neovim.git
cd neovim
rm -r build/  # clear the CMake cache
make CMAKE_EXTRA_FLAGS="-DCMAKE_INSTALL_PREFIX=$HOME/neovim"
make install

cat << 'EOF' >> $HOME/.bashrc
export PATH="$HOME/neovim/bin:$PATH"
EOF

参考