Skip to content

Commit 511b4b4

Browse files
author
NgocCuong
committed
refactor: improve blink.cmp integration with official API
Use blink.cmp's Provider Override API instead of internal patching for more reliable and maintainable integration. Changes: - Use official override API for transform_items - Remove auto-integration patching logic - Add comprehensive error handling and nil checks - Fix buffer context detection bug - Improve documentation with clear examples Benefits: - Zero timing issues or race conditions - More reliable and future-proof - Easier to debug and maintain - Better error handling Technical details: - blink.lua: Use helper functions with robust error handling - init.lua: Simplified setup, removed patching code - README.md: Updated with new configuration approach - CLAUDE.md: Updated architecture documentation
1 parent c5095ae commit 511b4b4

File tree

5 files changed

+323
-266
lines changed

5 files changed

+323
-266
lines changed

CHANGELOG.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [Unreleased]
9+
10+
### Changed
11+
12+
- **Improved blink.cmp integration** - Now uses official Provider Override API instead of internal patching
13+
- **Better reliability** - Removed timing-dependent code, zero race conditions
14+
- **Enhanced error handling** - Added comprehensive nil checks and pcall protection
15+
- **Updated documentation** - Clearer examples and configuration guide
16+
17+
### Fixed
18+
19+
- Fixed buffer context detection in transform_items (was checking wrong buffer)
20+
- Added safety checks for nil context/items
21+
- Added error handling for TreeSitter operations
22+
23+
### Technical Improvements
24+
25+
- Simplified blink.lua module to use helper functions only
26+
- Removed auto-integration logic from init.lua
27+
- Better code maintainability and future-proofing
28+
- Uses stable blink.cmp APIs throughout
29+
30+
---
31+
32+
## [1.0.0] - Initial Release
33+
34+
### Added
35+
36+
- TreeSitter injection for CSS syntax highlighting in styled-components
37+
- Native CSS LSP support via cssls
38+
- Custom blink.cmp completion source
39+
- Support for styled.*, css, createGlobalStyle, and keyframes
40+
- Comprehensive documentation
41+
42+
### Features
43+
44+
- Zero-config setup for LazyVim users
45+
- Smart CSS completion filtering
46+
- Performance optimizations (context detection caching)
47+
- Compatible with Neovim 0.10+ and 0.11+
48+
49+
[Unreleased]: https://github.com/crafts69guy/styled-components.nvim/compare/v1.0.0...HEAD
50+
[1.0.0]: https://github.com/crafts69guy/styled-components.nvim/releases/tag/v1.0.0

CLAUDE.md

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -151,65 +151,80 @@ Custom blink.cmp source for CSS completions in styled-component templates:
151151
- Scratch buffer allows cssls to process CSS without file I/O
152152
- Position mapping simplified by using insertText only
153153

154-
#### 6. `lua/styled-components/blink.lua` **Blink.cmp Integration**
154+
#### 6. `lua/styled-components/blink.lua` **Blink.cmp Integration Helpers**
155155

156-
Utilities for integrating with blink.cmp to filter cssls completions outside styled-component context:
156+
Helper functions for integrating with blink.cmp using the **official Provider Override API**:
157157

158158
**Why this module exists:**
159159
- styled-components.nvim configures cssls to attach to TypeScript/JavaScript files (necessary for injection)
160160
- blink.cmp's LSP source forwards ALL completions from ALL LSP clients without context filtering
161161
- Without filtering, users see CSS completions everywhere in TS/JS files (React components, hooks, etc.)
162-
- This module provides filtering to show CSS completions ONLY in styled-component templates
162+
- This module provides helper functions to filter CSS completions ONLY in styled-component templates
163163

164164
**Functions:**
165165

166-
`get_lsp_transform_items()` - Returns a transform_items function for blink.cmp's LSP source:
166+
`enabled()` - Smart filetype detection for source activation:
167+
- Returns `true` for TypeScript/JavaScript filetypes
168+
- Used by blink.cmp to determine when to activate styled-components source
169+
- Simple, efficient, no overhead
170+
171+
`get_lsp_transform_items()` - Returns a transform_items function for blink.cmp's LSP source override:
167172
- Checks if cursor is in styled-component CSS injection region
168173
- Filters out cssls completions when NOT in CSS context
169174
- Allows all completions (including cssls) when IN CSS context
170-
- Compatible with blink.cmp's LSP source API
171-
172-
**Usage patterns:**
175+
- Compatible with blink.cmp's Provider Override API
176+
- Robust item detection (checks multiple fields for different blink.cmp versions)
173177

174-
1. **Automatic Integration** (via `init.lua:setup_blink_integration()`):
175-
- Plugin automatically patches blink.cmp's LSP source on setup
176-
- Preserves existing user `transform_items` if present
177-
- Enabled by default via `config.blink_integration = true`
178-
- Zero user configuration required
178+
**Usage pattern (user's config):**
179179

180-
2. **Manual Integration** (via `get_lsp_transform_items()`):
181-
- User explicitly adds to blink.cmp config:
182-
```lua
183-
providers = {
184-
lsp = {
185-
transform_items = require("styled-components.blink").get_lsp_transform_items(),
186-
},
187-
}
188-
```
189-
- Useful when user wants explicit control or has custom transform logic
180+
Users configure blink.cmp using these helpers:
181+
```lua
182+
local styled = require("styled-components.blink")
183+
require("blink.cmp").setup({
184+
sources = {
185+
default = { "lsp", "path", "snippets", "buffer", "styled-components" },
186+
providers = {
187+
lsp = {
188+
override = {
189+
transform_items = styled.get_lsp_transform_items(),
190+
},
191+
},
192+
["styled-components"] = {
193+
name = "styled-components",
194+
module = "styled-components.completion",
195+
enabled = styled.enabled,
196+
},
197+
},
198+
},
199+
})
200+
```
190201

191-
**Integration timing:**
192-
- Auto-integration defers patching by 100ms to ensure blink.cmp is fully initialized
193-
- Gracefully handles blink.cmp not being installed (silent skip)
194-
- Logs debug messages when `config.debug = true`
202+
**Benefits of this approach:**
203+
- Uses official blink.cmp API (stable, future-proof)
204+
- No internal patching or timing issues
205+
- Transparent and easy to debug
206+
- User has full control
207+
- Zero magic, explicit configuration
195208

196209
### Data Flow
197210

198211
**Initialization:**
199212
```
200-
Plugin loads (lazy=false, priority=1000)
213+
Plugin loads (ft = ["typescript", "typescriptreact", ...])
201214
202215
injection.setup_injection_queries()
203216
→ Adds queries/*.scm to Neovim's runtimepath
204217
205218
injection.setup_cssls_for_injection()
206219
→ Configures cssls filetypes: ['css', 'scss', 'typescript', 'typescriptreact', ...]
207220
208-
setup_blink_integration() (if blink_integration = true, default)
209-
Patches blink.cmp's LSP source to filter cssls completions
210-
Preserves existing transform_items if present
221+
User's blink.cmp config uses helpers from styled-components.blink
222+
→ LSP source override: transform_items = styled.get_lsp_transform_items()
223+
styled-components source: enabled = styled.enabled
211224
212225
blink.cmp registers styled-components completion source
226+
→ Uses official Provider Override API
227+
→ No patching, no timing issues
213228
```
214229

215230
**Runtime (Completion Flow):**

0 commit comments

Comments
 (0)