Skip to content

Commit

Permalink
Fix uncomment bug when nvim-treesitter is used
Browse files Browse the repository at this point in the history
Fixes an issue where trying to uncomment will result in "attempt to
index field 'hl_map' (a nil value)..." when used with treesitter.

Based on the code in [this comment][1] and tyru#185, but tweaked
slightly to avoid messing with indentation of the existing code.

[1]: Shougo/dein.vim#495 (comment)
  • Loading branch information
dshoreman committed Feb 2, 2023
1 parent 3aefcb5 commit c03d19f
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lua/caw.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,35 @@ local M = {}
function M.has_syntax(lnum, col)
local col = col - 1
local bufnr = vim.api.nvim_get_current_buf()

if 1 == vim.fn.has('nvim-0.9.0') then
local items = vim.inspect_pos(bufnr, lnum - 1, col)

if 0 == #items.treesitter then
return false
end

for _, capture in ipairs(items.treesitter) do
if string.match(capture.hl_group, '@comment') then
return true
end
end

return false
end

if 1 == vim.fn.has('nvim-0.8.0') then
local captures = vim.treesitter.get_captures_at_pos(bufnr, lnum - 1, col)

for _, capture in ipairs(captures) do
if string.match(capture.capture, 'comment') then
return true
end
end

return false
end

local filetype = vim.api.nvim_buf_get_option(bufnr, 'ft')
local lang = languages[filetype] or filetype
if not require"vim.treesitter.language".require_language(lang, nil, true) then
Expand Down

0 comments on commit c03d19f

Please sign in to comment.