Skip to content

Commit cf9d7ad

Browse files
committed
Fix: tree-sitter powered uncomment feature doesn't works with nvim-0.8
1 parent 748f15c commit cf9d7ad

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

lua/caw.lua

+27-15
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,36 @@ local languages = {
88
local M = {}
99

1010
function M.has_syntax(lnum, col)
11-
local col = col - 1
1211
local bufnr = vim.api.nvim_get_current_buf()
13-
local filetype = vim.api.nvim_buf_get_option(bufnr, 'ft')
14-
local lang = languages[filetype] or filetype
15-
if not require"vim.treesitter.language".require_language(lang, nil, true) then
16-
return false
17-
end
18-
local query = require"vim.treesitter.query".get_query(lang, "highlights")
19-
local tstree = vim.treesitter.get_parser(bufnr, lang):parse()[1]
20-
local tsnode = tstree:root()
12+
-- vim.treesitter.highlighter.hl_map was removed with Neovim 0.8.0 release.
13+
-- see: https://github.com/neovim/neovim/pull/19931
14+
if vim.fn.has("nvim-0.5.0") == 1 and vim.fn.has("nvim-0.8.0") == 0 then
15+
local col = col - 1
16+
local filetype = vim.api.nvim_buf_get_option(bufnr, "ft")
17+
local lang = languages[filetype] or filetype
18+
if not require("vim.treesitter.language").require_language(lang, nil, true) then
19+
return false
20+
end
21+
local query = require("vim.treesitter.query").get_query(lang, "highlights")
22+
local tstree = vim.treesitter.get_parser(bufnr, lang):parse()[1]
23+
local tsnode = tstree:root()
2124

22-
for _, match in query:iter_matches(tsnode, bufnr, lnum - 1, lnum) do
23-
for id, node in pairs(match) do
24-
local _, start_col, _, end_col = node:range()
25-
local name = query.captures[id]
26-
local highlight = vim.treesitter.highlighter.hl_map[name] or ''
25+
for _, match in query:iter_matches(tsnode, bufnr, lnum - 1, lnum) do
26+
for id, node in pairs(match) do
27+
local _, start_col, _, end_col = node:range()
28+
local name = query.captures[id]
29+
local highlight = vim.treesitter.highlighter.hl_map[name]
2730

28-
if col >= start_col and col < end_col and string.match(highlight, 'Comment') then
31+
if col >= start_col and col < end_col and string.match(highlight, "Comment") then
32+
return true
33+
end
34+
end
35+
end
36+
elseif vim.fn.has("nvim-0.8.0") == 1 then
37+
-- vim.treesitter.get_captures_at_pos using 0-based coordinate
38+
local captures = vim.treesitter.get_captures_at_pos(bufnr, lnum - 1, col - 1)
39+
for _, capture in ipairs(captures) do
40+
if capture.capture == "comment" then
2941
return true
3042
end
3143
end

0 commit comments

Comments
 (0)