Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions doc/recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,51 @@ sources = {
### Dynamically picking providers by treesitter node/filetype

```lua
sources.default = function(ctx)
local success, node = pcall(vim.treesitter.get_node)
-- check if current line is inside a comment block
local function inside_comment_block()
if vim.api.nvim_get_mode().mode ~= 'i' then
return false
end
local node_under_cursor = vim.treesitter.get_node()
local parser = vim.treesitter.get_parser(nil, nil, { error = false })
if not parser or not node_under_cursor then
return false
end
local query = vim.treesitter.query.get(parser:lang(), 'highlights')
if not query then
return false
end
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
row = row - 1
for id, node, _ in query:iter_captures(node_under_cursor, 0, row, row + 1) do
if query.captures[id]:find('comment') then
local start_row, start_col, end_row, end_col = node:range()
if start_row <= row and row <= end_row then
if start_row == row and end_row == row then
if start_col <= col and col <= end_col then
return true
end
elseif start_row == row then
if start_col <= col then
return true
end
elseif end_row == row then
if col <= end_col then
return true
end
else
return true
end
end
end
end
return false
end

sources.default = function()
if vim.bo.filetype == 'lua' then
return { 'lsp', 'path' }
elseif success and node and vim.tbl_contains({ 'comment', 'line_comment', 'block_comment' }, node:type()) then
elseif inside_comment_block() then
return { 'buffer' }
else
return { 'lsp', 'path', 'snippets', 'buffer' }
Expand Down