Skip to content

Commit 62d6681

Browse files
feat: automatic source registration for blink.cmp
## Details Follows release of `blink.cmp` 0.14.0, which provides additional APIs for registering sources. Now when the `blink` completion source is enabled in this plugin we'll use the `add_source_provider` & `add_filetype_source` functions to automatically register the completion source on behalf of the user. This is an opt-in feature so out of the box nothing new happens. The in-process LSP source is still the suggested approach for getting completions from this plugin as they are completion engine agnostic. Simplify the instructions for using the `blink.cmp` source in the README.
1 parent 125258a commit 62d6681

File tree

7 files changed

+62
-56
lines changed

7 files changed

+62
-56
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
- enabled flag for link footnote [#362](https://github.com/MeanderingProgrammer/render-markdown.nvim/pull/362)
88
[9721ffe](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/9721ffe230ec90e49c49ee33b5ca44c3fc689214)
9+
- blink.cmp source registration [b8d93e8](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/b8d93e83a02dadddc6a566b1f60dab87190c1296)
10+
11+
### Bug Fixes
12+
13+
- account for indent when right aligning code language [125258a](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/125258ac5bccd21651505d78dbd6120906243749)
914

1015
### Collaborator Shoutouts
1116

README.md

+2-11
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,8 @@ cmp.setup({
148148
## blink.cmp
149149

150150
```lua
151-
require('blink.cmp').setup({
152-
sources = {
153-
default = { 'lsp', 'path', 'snippets', 'buffer', 'markdown' },
154-
providers = {
155-
markdown = {
156-
name = 'RenderMarkdown',
157-
module = 'render-markdown.integ.blink',
158-
fallbacks = { 'lsp' },
159-
},
160-
},
161-
},
151+
require('render-markdown').setup({
152+
completions = { blink = { enabled = true } },
162153
})
163154
```
164155

doc/render-markdown.txt

+3-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2025 March 18
1+
*render-markdown.txt* For 0.10.0 Last change: 2025 March 19
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -210,17 +210,8 @@ NVIM-CMP *render-markdown-completions-nvim-cmp*
210210
BLINK.CMP *render-markdown-completions-blink.cmp*
211211

212212
>lua
213-
require('blink.cmp').setup({
214-
sources = {
215-
default = { 'lsp', 'path', 'snippets', 'buffer', 'markdown' },
216-
providers = {
217-
markdown = {
218-
name = 'RenderMarkdown',
219-
module = 'render-markdown.integ.blink',
220-
fallbacks = { 'lsp' },
221-
},
222-
},
223-
},
213+
require('render-markdown').setup({
214+
completions = { blink = { enabled = true } },
224215
})
225216
<
226217

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 = '8.1.5'
8+
M.version = '8.1.6'
99

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

lua/render-markdown/integ/blink.lua

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---@module 'blink.cmp'
22

33
local source = require('render-markdown.integ.source')
4+
local state = require('render-markdown.state')
45

56
---@class render.md.blink.Source: blink.cmp.Source
67
local Source = {}
@@ -39,8 +40,10 @@ function Source:get_completions(context, callback)
3940
end
4041

4142
---@class render.md.integ.Blink
43+
---@field private id string
4244
---@field private registered boolean
4345
local M = {
46+
id = 'markdown',
4447
registered = false,
4548
}
4649

@@ -50,8 +53,17 @@ function Source.setup()
5053
return
5154
end
5255
M.registered = true
53-
-- TODO(blink.cmp): need add_source_provider to be released
54-
vim.notify('render-markdown.nvim blink.cmp source registeration requires add_source_provider', 3)
56+
local has_blink, blink = pcall(require, 'blink.cmp')
57+
if not has_blink or blink == nil then
58+
return
59+
end
60+
pcall(blink.add_source_provider, M.id, {
61+
name = 'RenderMarkdown',
62+
module = 'render-markdown.integ.blink',
63+
})
64+
for _, file_type in ipairs(state.file_types) do
65+
pcall(blink.add_filetype_source, file_type, M.id)
66+
end
5567
end
5668

5769
return Source

lua/render-markdown/integ/cmp.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function M.setup()
4242
end
4343
M.registered = true
4444
local has_cmp, cmp = pcall(require, 'cmp')
45-
if not has_cmp then
45+
if not has_cmp or cmp == nil then
4646
return
4747
end
4848
pcall(cmp.register_source, Source:get_debug_name(), Source)

lua/render-markdown/integ/source.lua

+36-29
Original file line numberDiff line numberDiff line change
@@ -19,58 +19,65 @@ end
1919

2020
---@return string[]
2121
function M.trigger_characters()
22-
return { '-', '*', '+', '>', ' ' }
22+
local characters = vim.tbl_values(list_markers)
23+
return vim.list_extend(characters, { '>', ' ' })
2324
end
2425

2526
---@param buf integer 0 for current buffer
2627
---@param row integer 0-indexed
2728
---@param col integer 0-indexed
2829
---@return lsp.CompletionItem[]?
2930
function M.items(buf, row, col)
30-
if buf == 0 then
31-
buf = util.current('buf')
32-
end
33-
34-
local has_parser, parser = pcall(vim.treesitter.get_parser, buf)
35-
if not has_parser or parser == nil then
36-
return nil
37-
end
38-
39-
-- Parse current row to get up to date node
40-
parser:parse({ row, row })
41-
local node = vim.treesitter.get_node({ bufnr = buf, pos = { row, col } })
31+
buf = buf == 0 and util.current('buf') or buf
32+
local node = M.get_node(buf, row, col)
4233
if node == nil then
4334
return nil
4435
end
45-
46-
local children = { 'block_quote_marker', 'block_continuation' }
47-
if vim.tbl_contains(children, node:type()) or list_markers[node:type()] ~= nil then
48-
node = node:parent()
49-
if node == nil then
50-
return nil
51-
end
52-
end
53-
54-
local items = {}
55-
local config = state.get(buf)
36+
local result, config = {}, state.get(buf)
5637
if node:type() == 'block_quote' then
5738
local quote_row = node:range()
5839
if quote_row == row then
5940
local prefix = M.space_prefix(buf, node)
6041
for _, component in pairs(config.callout) do
61-
table.insert(items, M.item(prefix .. component.raw, component.rendered, nil))
42+
table.insert(result, M.item(prefix .. component.raw, component.rendered, nil))
6243
end
6344
end
6445
elseif node:type() == 'list_item' then
6546
local checkbox = config.checkbox
6647
local prefix = M.list_prefix(buf, row, node)
67-
table.insert(items, M.item(prefix .. '[ ] ', checkbox.unchecked.icon, 'unchecked'))
68-
table.insert(items, M.item(prefix .. '[x] ', checkbox.checked.icon, 'checked'))
48+
table.insert(result, M.item(prefix .. '[ ] ', checkbox.unchecked.icon, 'unchecked'))
49+
table.insert(result, M.item(prefix .. '[x] ', checkbox.checked.icon, 'checked'))
6950
for name, component in pairs(checkbox.custom) do
70-
table.insert(items, M.item(prefix .. component.raw .. ' ', component.rendered, name))
51+
table.insert(result, M.item(prefix .. component.raw .. ' ', component.rendered, name))
7152
end
7253
end
73-
return items
54+
return result
55+
end
56+
57+
---@private
58+
---@param buf integer
59+
---@param row integer
60+
---@param col integer
61+
---@return TSNode?
62+
function M.get_node(buf, row, col)
63+
-- Parse current row to get up to date node
64+
local has_parser, parser = pcall(vim.treesitter.get_parser, buf)
65+
if not has_parser or parser == nil then
66+
return nil
67+
end
68+
parser:parse({ row, row })
69+
70+
local node = vim.treesitter.get_node({
71+
bufnr = buf,
72+
lang = 'markdown',
73+
pos = { row, col },
74+
})
75+
local children = vim.tbl_keys(list_markers)
76+
vim.list_extend(children, { 'block_quote_marker', 'block_continuation' })
77+
if node ~= nil and vim.tbl_contains(children, node:type()) then
78+
node = node:parent()
79+
end
80+
return node
7481
end
7582

7683
---@private

0 commit comments

Comments
 (0)