effective-neovim
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseEffective Neovim
高效开发Neovim Lua插件
Apply best practices from the Neovim community to write clean, idiomatic Lua plugins.
遵循Neovim社区的最佳实践,编写简洁、符合Lua惯用风格的插件。
When to Apply
适用场景
Use this skill automatically when:
- Writing new Neovim plugins in Lua
- Reviewing Neovim plugin code
- Refactoring existing plugin implementations
在以下场景下自动使用本技能:
- 编写全新的基于Lua的Neovim插件
- 评审Neovim插件代码
- 重构现有插件的实现
Tooling
工具链
Key Principles
核心原则
From nvim-best-practices (parts upstreamed to ):
:h lua-plugin- No forced : Plugins should work out of the box. Separate configuration from initialization.
setup() - mappings: Let users define their own keymaps instead of hardcoding bindings.
<Plug> - Subcommands over pollution: Use not
:Rocks install,:RocksInstall, etc.:RocksPrune - Defer : Don't load everything at startup. Require inside command implementations.
require() - LuaCATS annotations: Use type hints; catch bugs in CI with lua-language-server.
- Busted over plenary.nvim: For testing. More powerful, standard in broader Lua community.
- SemVer: Version your plugins properly. Publish to luarocks.org.
- Health checks: Provide for
lua/{plugin}/health.lua.:checkhealth
来自nvim-best-practices(部分内容已被纳入帮助文档):
:h lua-plugin- 不强制要求:插件应开箱即用。将配置与初始化分离。
setup() - 使用映射:让用户自定义快捷键,而非硬编码绑定。
<Plug> - 子命令优于命名污染:使用而非
:Rocks install、:RocksInstall等形式。:RocksPrune - 延迟加载:不要在启动时加载所有模块。在命令实现内部再引入模块。
require() - LuaCATS注解:使用类型提示;在CI流程中通过lua-language-server捕获bug。
- 优先使用Busted而非plenary.nvim:用于测试。功能更强大,在Lua社区更通用。
- 语义化版本(SemVer):为插件正确标记版本。发布至luarocks.org。
- 健康检查:提供文件以支持
lua/{plugin}/health.lua命令。:checkhealth
Style Conventions
风格约定
- Indent: 2 spaces (not tabs)
- Quotes: Single quotes preferred
- Line width: 100 columns
- Naming: for functions and variables,
snake_casefor classes/modulesPascalCase - No semicolons: They're unnecessary in Lua
- Trailing commas: In multi-line tables for cleaner diffs
- Comments: Explain why, not what
- 缩进:2个空格(不使用制表符)
- 引号:优先使用单引号
- 行宽:100列
- 命名:函数和变量使用,类/模块使用
snake_casePascalCase - 不使用分号:Lua中不需要分号
- 多行表使用尾随逗号:让Diff更清晰
- 注释:解释“为什么”,而非“是什么”
Plugin Structure
插件结构
plugin-name/
├── lua/
│ └── plugin-name/
│ ├── init.lua # Entry point, setup function
│ ├── health.lua # :checkhealth integration
│ └── *.lua # Module files
├── plugin/
│ └── plugin-name.lua # Auto-loaded, defines commands/autocommands
├── doc/
│ └── plugin-name.txt # Vimdoc for :h plugin-name
└── tests/
└── *_spec.lua # Busted test filesplugin-name/
├── lua/
│ └── plugin-name/
│ ├── init.lua # 入口文件,包含setup函数
│ ├── health.lua # 集成:checkhealth命令
│ └── *.lua # 模块文件
├── plugin/
│ └── plugin-name.lua # 自动加载,定义命令/自动命令
├── doc/
│ └── plugin-name.txt # 用于:h plugin-name的Vimdoc文档
└── tests/
└── *_spec.lua # Busted测试文件References
参考资料
For detailed guidance with code examples, see .
references/nvim-best-practices.mdExternal sources:
- nvim-best-practices: https://github.com/nvim-neorocks/nvim-best-practices
- Neovim Lua Guide:
:h lua-guide - Plugin Development:
:h lua-plugin - Roblox Style Guide: https://roblox.github.io/lua-style-guide/ (StyLua's basis)
如需包含代码示例的详细指导,请查看。
references/nvim-best-practices.md外部资源:
- nvim-best-practices: https://github.com/nvim-neorocks/nvim-best-practices
- Neovim Lua指南:
:h lua-guide - 插件开发文档:
:h lua-plugin - Roblox风格指南: https://roblox.github.io/lua-style-guide/(StyLua的参考标准)