🌙 Cap 06: Neovim Moderno com Lua: Lazy.nvim, Mason, Treesitter e LSP
🗺️ A Arquitetura Modular do Neovim Moderno em Lua
flowchart TD
INIT["~/.config/nvim/init.lua (Ponto de Entrada)"]
--> LAZY["Gerenciador de Plugins: Lazy.nvim (Carregamento Preguiçoso)"]
LAZY --> SYNTAX["Treesitter (Parsing AST Incremental e Destaque Semântico)"]
LAZY --> MASON["Mason.nvim (Gerenciador de Binários de Servidores LSP, DAP e Linters)"]
LAZY --> LSPCONFIG["nvim-lspconfig (Configuração dos Servidores de Linguagem)"]
LAZY --> CMP["nvim-cmp + LuaSnip (Autocompletion Inteligente e Snippets)"]
LAZY --> TELESCOPE["Telescope.nvim (Fuzzy Finder de Arquivos, Símbolos e Git) ⚡"]
style INIT fill:#ede7f6,stroke:#512da8,stroke-width:2px
style LAZY fill:#00897b,stroke:#004d40,stroke-width:2px,color:#ffffff
style SYNTAX fill:#e1f5fe,stroke:#03a9f4,stroke-width:1.5px
style MASON fill:#fff3e0,stroke:#ff9800,stroke-width:1.5px
style LSPCONFIG fill:#e8f5e9,stroke:#4caf50,stroke-width:1.5px
style CMP fill:#fff9c4,stroke:#fbc02d,stroke-width:1.5px
style TELESCOPE fill:#ede7f6,stroke:#512da8,stroke-width:1.5px
📖 Fundamentos e Aplicação
O Neovim revolucionou a edição modal ao adotar a linguagem LuaJIT como cidadã de primeira classe e integrar nativamente o Language Server Protocol (LSP) da Microsoft e o parser sintático Tree-sitter.
1. Estrutura Modular Canônica em Lua:
~/.config/nvim/
├── init.lua # Carrega opções e plugins
└── lua/
├── config/
│ ├── keymaps.lua # Mapeamento de teclas (Leader key)
│ ├── options.lua # Configurações do editor (números, tabs)
│ └── lazy.lua # Bootstrap do Lazy.nvim
└── plugins/ # Arquivos modulares de plugins
├── lsp.lua # Mason, nvim-lspconfig
├── treesitter.lua # Destaque sintático
├── telescope.lua # Busca difusa
└── completion.lua # nvim-cmp
2. O Papel dos Componentes Centrais:
- Lazy.nvim: Gerenciador moderno com suporte a carregamento sob demanda (lazy-loading por eventos como
BufReadPost, comandos ou teclas). - Tree-sitter: Constrói uma árvore sintática abstrata (AST) do código em tempo real, permitindo destaque de sintaxe contextual, seleção incremental de escopos e dobras inteligentes.
- LSP Nativo: Fornece Go to Definition (
gd), Find References (gr), Hover Documentation (K) e Rename Symbol (<leader>rn) sem engasgar o editor.