From cf9d7ad51da6ca05cb425e599a3d239c9cd398d8 Mon Sep 17 00:00:00 2001 From: Kento Ogata Date: Wed, 29 Mar 2023 12:14:06 +0900 Subject: [PATCH] Fix: tree-sitter powered uncomment feature doesn't works with `nvim-0.8` --- lua/caw.lua | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/lua/caw.lua b/lua/caw.lua index 19bd9c8..cfbe0e0 100644 --- a/lua/caw.lua +++ b/lua/caw.lua @@ -8,24 +8,36 @@ local languages = { local M = {} function M.has_syntax(lnum, col) - local col = col - 1 local bufnr = vim.api.nvim_get_current_buf() - 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 - return false - end - local query = require"vim.treesitter.query".get_query(lang, "highlights") - local tstree = vim.treesitter.get_parser(bufnr, lang):parse()[1] - local tsnode = tstree:root() + -- vim.treesitter.highlighter.hl_map was removed with Neovim 0.8.0 release. + -- see: https://github.com/neovim/neovim/pull/19931 + if vim.fn.has("nvim-0.5.0") == 1 and vim.fn.has("nvim-0.8.0") == 0 then + local col = col - 1 + 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 + return false + end + local query = require("vim.treesitter.query").get_query(lang, "highlights") + local tstree = vim.treesitter.get_parser(bufnr, lang):parse()[1] + local tsnode = tstree:root() - for _, match in query:iter_matches(tsnode, bufnr, lnum - 1, lnum) do - for id, node in pairs(match) do - local _, start_col, _, end_col = node:range() - local name = query.captures[id] - local highlight = vim.treesitter.highlighter.hl_map[name] or '' + for _, match in query:iter_matches(tsnode, bufnr, lnum - 1, lnum) do + for id, node in pairs(match) do + local _, start_col, _, end_col = node:range() + local name = query.captures[id] + local highlight = vim.treesitter.highlighter.hl_map[name] - if col >= start_col and col < end_col and string.match(highlight, 'Comment') then + if col >= start_col and col < end_col and string.match(highlight, "Comment") then + return true + end + end + end + elseif vim.fn.has("nvim-0.8.0") == 1 then + -- vim.treesitter.get_captures_at_pos using 0-based coordinate + local captures = vim.treesitter.get_captures_at_pos(bufnr, lnum - 1, col - 1) + for _, capture in ipairs(captures) do + if capture.capture == "comment" then return true end end