@@ -12,7 +12,8 @@ local nvim = require('blink.lib.nvim')
1212--- @field mode blink.cmp.Mode
1313--- @field id integer
1414--- @field bufnr integer
15- --- @field cursor blink.cmp.CursorPos
15+ --- @field cursor { [1] : integer , [2] : integer } Deprecated , use ` pos` intead
16+ --- @field pos vim.Pos
1617--- @field line string
1718--- @field term blink.cmp.ContextTerm
1819--- @field bounds blink.cmp.ContextBounds
@@ -23,11 +24,12 @@ local nvim = require('blink.lib.nvim')
2324---
2425--- @field new fun ( opts : blink.cmp.ContextOpts ): blink.cmp.Context
2526--- @field get_keyword fun (): string
26- --- @field within_query_bounds fun ( self : blink.cmp.Context , cursor : blink.cmp.CursorPos , include_start_bound ?: boolean ): boolean
27+ --- @field within_query_bounds fun ( self : blink.cmp.Context , include_start_bound ?: boolean ): boolean
2728---
2829--- @field get_mode fun (): blink.cmp.Mode
29- --- @field get_cursor fun (): blink.cmp.CursorPos
30- --- @field set_cursor fun ( cursor : blink.cmp.CursorPos )
30+ --- @field get_pos fun (): vim.Pos
31+ --- @field get_cursor fun (): { [ 1]: integer , [ 2]: integer } Deprecated , use ` get_pos` intead
32+ --- @field set_cursor fun ( pos : vim.Pos )
3133--- @field get_line fun ( num ?: integer ): string
3234--- @field get_bounds fun ( range : blink.cmp.CompletionKeywordRange ): blink.cmp.ContextBounds
3335--- @field get_term_command fun (): blink.cmp.ContextTermCommand ?
@@ -60,15 +62,15 @@ local nvim = require('blink.lib.nvim')
6062local context = {}
6163
6264function context .new (opts )
63- local cursor = context .get_cursor ()
64- local line = context .get_line ()
65+ local pos = context .get_pos ()
6566
6667 return setmetatable ({
6768 mode = context .get_mode (),
6869 id = opts .id ,
6970 bufnr = nvim .get_current_buf (),
70- cursor = cursor ,
71- line = line ,
71+ pos = pos ,
72+ cursor = pos :to_cursor (),
73+ line = context .get_line (),
7274 term = { command = context .get_term_command () },
7375 bounds = context .get_bounds (' full' ),
7476 trigger = {
@@ -89,18 +91,19 @@ function context.get_keyword()
8991 return string.sub (context .get_line (), range .start_col , range .start_col + range .length - 1 )
9092end
9193
92- --- @param cursor blink.cmp.CursorPos
9394--- @param include_start_bound ? boolean Whether to include the start boundary as inside of the query. E.g. start_col = 1 (one indexed ), cursor[2] = 0 (zero indexed ) would be considered within the query bounds with this flag enabled.
9495--- @return boolean
95- function context :within_query_bounds (cursor , include_start_bound )
96- local row , col = cursor [ 1 ], cursor [ 2 ]
97- col = col + 1 -- Convert from 0-indexed to 1-indexed
96+ function context :within_query_bounds (include_start_bound )
97+ local pos , bounds = self . pos , self . bounds
98+ if pos . row + 1 ~= bounds . line_number then return false end
9899
99- local bounds = self .bounds
100- if include_start_bound then
101- return row == bounds .line_number and col >= bounds .start_col and col <= (bounds .start_col + bounds .length )
102- end
103- return row == bounds .line_number and col > bounds .start_col and col <= (bounds .start_col + bounds .length )
100+ local cursor_col = pos .col + 1
101+ local end_col = bounds .start_col + bounds .length
102+ if cursor_col > end_col then return false end
103+
104+ if include_start_bound then return cursor_col >= bounds .start_col end
105+
106+ return cursor_col > bounds .start_col
104107end
105108
106109function context .get_mode ()
@@ -117,20 +120,25 @@ function context.get_mode()
117120 or ' default'
118121end
119122
120- function context .get_cursor ()
121- return context .get_mode () == ' cmdline' and { 1 , vim .fn .getcmdpos () - 1 } or nvim .win_get_cursor (0 )
123+ function context .get_pos ()
124+ local bufnr = context .bufnr or 0
125+ if context .get_mode () == ' cmdline' then return vim .pos (bufnr , 0 , vim .fn .getcmdpos () - 1 ) end
126+
127+ return vim .pos .cursor (bufnr )
122128end
123129
124- function context .set_cursor (cursor )
130+ function context .get_cursor () return context .get_pos ():to_cursor () end
131+
132+ function context .set_cursor (pos )
125133 local mode = context .get_mode ()
126134 if vim .tbl_contains ({ ' default' , ' term' , ' cmdwin' }, mode ) then
127- nvim .win_set_cursor (0 , cursor )
135+ nvim .win_set_cursor (0 , pos : to_cursor () )
128136 return
129137 end
130138
131139 assert (mode == ' cmdline' , ' Unsupported mode for setting cursor: ' .. mode )
132- assert (cursor [ 1 ] == 1 , ' Cursor must be on the first line in cmdline mode' )
133- vim .fn .setcmdpos (cursor [ 2 ] )
140+ assert (pos . row == 0 , ' Cursor must be on the first line in cmdline mode' )
141+ vim .fn .setcmdpos (pos . col + 1 )
134142end
135143
136144function context .get_line (num )
@@ -143,16 +151,18 @@ function context.get_line(num)
143151 end
144152
145153 -- This method works for normal buffers and the terminal prompt
146- if num == nil then num = context .get_cursor ()[1 ] - 1 end
154+ if not num then num = context .get_pos ().row end
155+
147156 return nvim .buf_get_lines (0 , num , num + 1 , false )[1 ] or ' '
148157end
149158
150159--- Gets characters around the cursor and returns the range, 0-indexed
151160function context .get_bounds (range )
152161 local line = context .get_line ()
153- local cursor = context .get_cursor ()
154- local start_col , end_col = require (' blink.cmp.fuzzy' ).get_keyword_range (line , cursor [2 ], range )
155- return { line = line , line_number = cursor [1 ], start_col = start_col + 1 , length = end_col - start_col }
162+ local pos = context .get_pos ()
163+ local start_col , end_col = require (' blink.cmp.fuzzy' ).get_keyword_range (line , pos .col , range )
164+
165+ return { line = line , line_number = pos .row + 1 , start_col = start_col + 1 , length = end_col - start_col }
156166end
157167
158168--- Get the terminal command in the current line without the shell prompt.
@@ -168,18 +178,10 @@ end
168178function context .get_term_command ()
169179 if context .get_mode () ~= ' term' then return end
170180
171- local cursor = context .get_cursor ()
172- local cursor_row = cursor [1 ]
173- local cursor_col = cursor [2 ] + 1
174- local line = string.sub (context .get_line (), 1 , cursor_col - 1 )
175-
176- local extmarks = nvim .buf_get_extmarks (
177- 0 ,
178- nvim .create_namespace (' blink_cmp_term_command_start' ),
179- { cursor_row - 1 , cursor_col - 1 },
180- { cursor_row - 1 , 0 },
181- { limit = 1 }
182- )
181+ local pos = context .get_pos ()
182+ local line = string.sub (context .get_line (), 1 , pos .col )
183+ local ns = nvim .create_namespace (' blink_cmp_term_command_start' )
184+ local extmarks = nvim .buf_get_extmarks (0 , ns , { pos .row , pos .col }, { pos .row , 0 }, { limit = 1 })
183185
184186 --- If we find no mark for the start of the terminal command the terminal or shell
185187 --- probably does not support the FTCS_COMMAND_START escape sequence. The best effort
0 commit comments