Skip to content

Commit c85d682

Browse files
fix: handle difference in nvim-cmp and blink.cmp cursor context
## Details Issue: #310 `nvim-cmp` passes the cursor position as a (1,1)-indexed value by adjusting the column output of `nvim_win_get_cursor`. `blink.cmp` does not make this adjustment and directly passes the (1,0)-indexed value from `nvim_win_get_cursor`. Previously we did not handle this difference and would subtract 1 from the column resulting in an exception due to negative values. The fix is to account for this difference when adjusting the values to treesitter's (0,0)-indexed APIs. I'm not 100% sure why this did not crop up before while testing but likely related to this note from the `0.11.0` release: > [!IMPORTANT] > Blink.cmp now fetches the completion items immediately upon entering > insert mode by default. More ideas for prefetching are being explored! So we still had the off by 1 column issue but since items were not being pre-fetched and due to the trigger characters configuration the column would always be > 0, so it would not error.
1 parent 8004461 commit c85d682

File tree

4 files changed

+10
-5
lines changed

4 files changed

+10
-5
lines changed

lua/render-markdown/health.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local state = require('render-markdown.state')
55
local M = {}
66

77
---@private
8-
M.version = '7.8.10'
8+
M.version = '7.8.11'
99

1010
function M.check()
1111
M.start('version')

lua/render-markdown/integ/blink.lua

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ end
2424
---@param context blink.cmp.Context
2525
---@param callback fun(response?: blink.cmp.CompletionResponse)
2626
function Source:get_completions(context, callback)
27-
local items = source.items(context.bufnr, context.cursor[1] - 1, context.cursor[2] - 1)
27+
-- nvim_win_get_cursor: (1,0)-indexed
28+
local cursor = context.cursor
29+
local items = source.items(context.bufnr, cursor[1] - 1, cursor[2])
2830
if items == nil then
2931
callback(nil)
3032
else

lua/render-markdown/integ/cmp.lua

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ end
2222
---@param callback fun(response?: lsp.CompletionItem[])
2323
function Source:complete(params, callback)
2424
local context = params.context
25-
local items = source.items(context.bufnr, context.cursor.row - 1, context.cursor.col - 1)
25+
-- nvim_win_get_cursor: (1,0)-indexed
26+
-- nvim-cmp col + 1 : (1,1)-indexed
27+
local cursor = context.cursor
28+
local items = source.items(context.bufnr, cursor.row - 1, cursor.col - 1)
2629
callback(items)
2730
end
2831

lua/render-markdown/integ/source.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ function M.trigger_characters()
2323
end
2424

2525
---@param buf integer
26-
---@param row integer
27-
---@param col integer
26+
---@param row integer 0-indexed
27+
---@param col integer 0-indexed
2828
---@return lsp.CompletionItem[]?
2929
function M.items(buf, row, col)
3030
local has_parser, parser = pcall(vim.treesitter.get_parser, buf)

0 commit comments

Comments
 (0)