diff --git a/lua/codewindow/highlight.lua b/lua/codewindow/highlight.lua index 535b43d..ef7f75b 100644 --- a/lua/codewindow/highlight.lua +++ b/lua/codewindow/highlight.lua @@ -3,7 +3,6 @@ local M = {} local config = require("codewindow.config").get() local utils = require("codewindow.utils") local highlighter -local ts_utils local hl_namespace local screenbounds_namespace @@ -99,13 +98,22 @@ local function extract_highlighting(buffer, lines) if hl then local c = query._query.captures[capture] if c ~= nil then - local start_row, start_col, end_row, end_col = - ts_utils.get_vim_range({ vim.treesitter.get_node_range(node) }, buffer) + -- vim.treesitter.get_node_range returns 0-indexed rows and columns + -- Rows: 0-indexed inclusive -> 1-indexed inclusive + -- Cols: 0-indexed with end exclusive [start_col, end_col) -> 1-indexed inclusive + local start_row, start_col, end_row, end_col = vim.treesitter.get_node_range(node) + start_row = start_row + 1 + end_row = end_row + 1 + start_col = start_col + 1 + -- end_col is already "one past" in 0-based; in 1-based it's the inclusive end for y = start_row, end_row do for x = start_col, math.min(end_col, minimap_char_width) do local minimap_x, minimap_y = utils.buf_to_minimap(x, y) - highlights[minimap_y][minimap_x][c] = (highlights[minimap_y][minimap_x][c] or 0) + 1 + if minimap_y >= 1 and minimap_y <= minimap_height + and minimap_x >= 1 and minimap_x <= minimap_width then + highlights[minimap_y][minimap_x][c] = (highlights[minimap_y][minimap_x][c] or 0) + 1 + end end end end @@ -124,7 +132,6 @@ end if config.use_treesitter then highlighter = require("vim.treesitter.highlighter") - ts_utils = require("nvim-treesitter.ts_utils") M.extract_highlighting = extract_highlighting else M.extract_highlighting = function() end