From f70491107a0855485980eca5caf1eda0f5cd63d7 Mon Sep 17 00:00:00 2001 From: Kaiser-Yang <624626089@qq.com> Date: Fri, 28 Feb 2025 15:46:54 +0800 Subject: [PATCH 1/3] doc: use the correct way to check comment block Using node:type() do not work as expected, sometimes, the type will go with a "chunk" or even empty string, we must iterate the node of current line to check if current line is inside a comment block. Besides, in the code, there is no `ctx` passed to the default function, just remove the parameter in the doc. --- doc/recipes.md | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/doc/recipes.md b/doc/recipes.md index b12a4f177..da7a83a3f 100644 --- a/doc/recipes.md +++ b/doc/recipes.md @@ -235,11 +235,48 @@ 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 }) + local query = vim.treesitter.query.get(vim.bo.filetype, 'highlights') + if not parser or not node_under_cursor or 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' } From 0e9c94f654b130b112ee39f5b0623d054d39c12b Mon Sep 17 00:00:00 2001 From: Kaiser-Yang <624626089@qq.com> Date: Fri, 9 May 2025 20:39:57 +0800 Subject: [PATCH 2/3] fix: use parser:lang() instead of vim.bo.filetype --- doc/recipes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/recipes.md b/doc/recipes.md index da7a83a3f..ef9f92c22 100644 --- a/doc/recipes.md +++ b/doc/recipes.md @@ -242,7 +242,7 @@ local function inside_comment_block() end local node_under_cursor = vim.treesitter.get_node() local parser = vim.treesitter.get_parser(nil, nil, { error = false }) - local query = vim.treesitter.query.get(vim.bo.filetype, 'highlights') + local query = vim.treesitter.query.get(parser:lang(), 'highlights') if not parser or not node_under_cursor or not query then return false end From 9d6d5ae5f01f377349728ef9b47a276e0db63bdd Mon Sep 17 00:00:00 2001 From: Kaiser-Yang <624626089@qq.com> Date: Thu, 22 May 2025 11:20:04 +0800 Subject: [PATCH 3/3] fix: check nil before using parser --- doc/recipes.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/doc/recipes.md b/doc/recipes.md index ef9f92c22..f875c4836 100644 --- a/doc/recipes.md +++ b/doc/recipes.md @@ -242,8 +242,11 @@ local function inside_comment_block() 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 parser or not node_under_cursor or not query then + if not query then return false end local row, col = unpack(vim.api.nvim_win_get_cursor(0))