8
8
--- @field start_col number
9
9
--- @field end_col number
10
10
---
11
- --- @class blink.cmp.TriggerContext
11
+ --- @class blink.cmp.Context
12
12
--- @field id number
13
13
--- @field bounds blink.cmp.TriggerBounds
14
+ --- @field bufnr number
14
15
--- @field treesitter_node table | nil
15
- ---
16
- --- @class blink.cmp.ShowContext : blink.cmp.TriggerContext
17
- --- @field trigger_character string | nil
16
+ --- @field trigger { kind : number , character : string | nil }
18
17
---
19
18
--- @class blink.cmp.TriggerEventTargets
20
- --- @field on_show fun ( context : blink.cmp.ShowContext )
19
+ --- @field on_show fun ( context : blink.cmp.Context )
21
20
--- @field on_hide fun ()
22
21
---
23
22
--- @class blink.cmp.Trigger
24
- --- @field context blink.cmp.TriggerContext | nil
25
- --- @field context_last_id number
23
+ --- @field context blink.cmp.Context | nil
24
+ --- @field current_context_id number
26
25
--- @field context_regex string
27
26
--- @field event_targets blink.cmp.TriggerEventTargets
28
27
29
28
local sources = require (' blink.cmp.sources.lib' )
30
29
31
30
--- @class blink.cmp.Trigger
32
31
local trigger = {
33
- context_last_id = - 1 ,
32
+ current_context_id = - 1 ,
34
33
context = nil ,
35
34
context_regex = ' [%w_\\ -]' ,
36
35
@@ -108,15 +107,23 @@ end
108
107
function trigger .show (opts )
109
108
opts = opts or {}
110
109
111
- -- update context (to update bounds and treesitter node)
112
- -- todo: this behavior isn't obvious
113
- trigger .context = trigger .get_context ()
114
-
115
- trigger .event_targets .on_show ({
116
- id = trigger .context .id ,
117
- bounds = trigger .context .bounds ,
118
- trigger_character = opts .trigger_character ,
119
- })
110
+ -- update context
111
+ local cursor = vim .api .nvim_win_get_cursor (0 )
112
+ if trigger .context == nil then trigger .current_context_id = trigger .current_context_id + 1 end
113
+ trigger .context = {
114
+ id = trigger .current_context_id ,
115
+ bufnr = vim .api .nvim_get_current_buf (),
116
+ cursor = cursor ,
117
+ line = vim .api .nvim_buf_get_lines (0 , cursor [1 ] - 1 , cursor [1 ], false )[1 ],
118
+ bounds = helpers .get_context_bounds (trigger .context_regex ),
119
+ trigger = {
120
+ kind = opts .trigger_character and vim .lsp .protocol .CompletionTriggerKind .TriggerCharacter
121
+ or vim .lsp .protocol .CompletionTriggerKind .Invoked ,
122
+ character = opts .trigger_character ,
123
+ },
124
+ }
125
+
126
+ trigger .event_targets .on_show (trigger .context )
120
127
end
121
128
122
129
function trigger .listen_on_show (callback ) trigger .event_targets .on_show = callback end
130
137
131
138
function trigger .listen_on_hide (callback ) trigger .event_targets .on_hide = callback end
132
139
133
- ---- -- Context ------
134
- -- Gets the current context, always updating the bounds to the current position
135
- --- @return blink.cmp.TriggerContext
136
- function trigger .get_context ()
137
- local bounds = helpers .get_query (trigger .context_regex )
138
- -- local treesitter_node = helpers.get_treesitter_node_at_cursor()
139
- if not trigger .context then
140
- trigger .context_last_id = trigger .context_last_id + 1
141
- trigger .context = { id = trigger .context_last_id , bounds = bounds }
142
- end
143
- return { id = trigger .context .id , bounds = bounds }
144
- end
145
-
140
+ --- @param context blink.cmp.ShowContext | nil
146
141
--- @param cursor number[]
147
142
--- @return boolean
148
143
function trigger .within_query_bounds (cursor )
@@ -163,12 +158,11 @@ end
163
158
-- Moves forward and backwards around the cursor looking for word boundaries
164
159
--- @param regex string
165
160
--- @return blink.cmp.TriggerBounds
166
- function helpers .get_query (regex )
167
- local bufnr = vim .api .nvim_get_current_buf ()
161
+ function helpers .get_context_bounds (regex )
168
162
local cursor_line = vim .api .nvim_win_get_cursor (0 )[1 ]
169
163
local cursor_col = vim .api .nvim_win_get_cursor (0 )[2 ]
170
164
171
- local line = vim .api .nvim_buf_get_lines (bufnr , cursor_line - 1 , cursor_line , false )[1 ]
165
+ local line = vim .api .nvim_buf_get_lines (0 , cursor_line - 1 , cursor_line , false )[1 ]
172
166
local start_col = cursor_col
173
167
while start_col > 1 do
174
168
local char = line :sub (start_col , start_col )
@@ -186,7 +180,7 @@ function helpers.get_query(regex)
186
180
end_col = end_col + 1
187
181
end
188
182
189
- return { line = line , line_number = cursor_line , start_col = start_col , end_col = end_col }
183
+ return { line_number = cursor_line , start_col = start_col , end_col = end_col }
190
184
end
191
185
192
186
--- @return TSNode | nil
0 commit comments