Skip to content

Commit

Permalink
fix TS highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
igorlfs committed Dec 8, 2024
1 parent 918344c commit 5271319
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 38 deletions.
5 changes: 2 additions & 3 deletions lua/dap-view/breakpoints/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ local api = vim.api

local M = {}

M.namespace = api.nvim_create_namespace("dap-view")

---@param row integer
---@param len_path integer
Expand Down Expand Up @@ -64,8 +63,8 @@ local populate_buf_with_breakpoints = function()

local col_offset = #relative_path + #tostring(entry.lnum) + 2

treesitter.copy_highlights(buf, entry.lnum - 1, state.bufnr, line_count, col_offset)
extmarks.copy_extmarks(buf, entry.lnum - 1, state.bufnr, line_count, col_offset)
treesitter.copy_highlights(buf, entry.lnum - 1, line_count, col_offset)
extmarks.copy_extmarks(buf, entry.lnum - 1, line_count, col_offset)

highlight_file_name_and_line_number(
line_count,
Expand Down
10 changes: 6 additions & 4 deletions lua/dap-view/extmarks.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
local globals = require("dap-view.globals")
local state = require("dap-view.state")

local M = {}

local api = vim.api

---@param src_bufnr integer
---@param src_row integer
---@param target_bufnr integer
---@param target_row integer
---@param col_offset integer
M.copy_extmarks = function(src_bufnr, src_row, target_bufnr, target_row, col_offset)
M.copy_extmarks = function(src_bufnr, src_row, target_row, col_offset)
local extmarks = api.nvim_buf_get_extmarks(
src_bufnr,
-1,
Expand All @@ -27,8 +29,8 @@ M.copy_extmarks = function(src_bufnr, src_row, target_bufnr, target_row, col_off

if opts then
api.nvim_buf_set_extmark(
target_bufnr,
opts.ns_id or M.namespace,
state.bufnr,
opts.ns_id or globals.NAMESPACE,
target_row,
col + col_offset,
{
Expand Down
90 changes: 59 additions & 31 deletions lua/dap-view/treesitter.lua
Original file line number Diff line number Diff line change
@@ -1,42 +1,70 @@
local globals = require("dap-view.globals")
local state = require("dap-view.state")

local M = {}

local api = vim.api

---@param src_bufnr integer
---@param src_row integer
---@param target_bufnr integer
---@param target_row integer
---@param col_offset integer
M.copy_highlights = function(src_bufnr, src_row, target_bufnr, target_row, col_offset)
local ts_highlighter = vim.treesitter.highlighter.active[src_bufnr]
if ts_highlighter then
ts_highlighter.tree:for_each_tree(function(tstree, ltree)
if not tstree then
return
end
local root = tstree:root()
local start_row, _, end_row, _ = root:range()

if src_row >= start_row and src_row <= end_row then
local query = (ts_highlighter:get_query(ltree:lang())):query()
for id, node, _ in query:iter_captures(root, src_bufnr, src_row, src_row + 1) do
local hl_group = query.captures[id]
if hl_group then
local s_row, s_col, e_row, e_col = node:range()
if s_row == src_row then
api.nvim_buf_add_highlight(
target_bufnr,
-1,
hl_group,
target_row,
s_col + col_offset,
e_row == src_row and e_col + col_offset or -1
)
end
end
end
end
end)
--- See https://github.com/stevearc/quicker.nvim/blob/049d66534d3de5920663ee1b8dd0096d70f55a67/lua/quicker/highlight.lua#L164
M.copy_highlights = function(src_bufnr, src_row, target_row, col_offset)
local filetype = vim.filetype.match({ buf = src_bufnr })

if not filetype then
return
end

local lang = vim.treesitter.language.get_lang(filetype)

if not lang then
return
end

local line = api.nvim_buf_get_lines(src_bufnr, src_row, src_row + 1, false)
local text = table.concat(line, "\n")

local has_parser, parser = pcall(vim.treesitter.get_string_parser, text, lang)

if not has_parser then
return
end

local root = parser:parse(true)[1]:root()
local query = vim.treesitter.query.get(lang, "highlights")

if not query then
return
end

local highlights = {}
for capture, node, metadata in query:iter_captures(root, text) do
if capture == nil then
break
end

local range = vim.treesitter.get_range(node, text, metadata[capture])
local start_row, start_col, _, end_row, end_col, _ = unpack(range)
local capture_name = query.captures[capture]

local hl = string.format("@%s.%s", capture_name, lang)

if end_row > start_row then
end_col = -1
end

table.insert(highlights, { start_col + col_offset, end_col + col_offset, hl })
end

for _, hl in ipairs(highlights) do
local start_col, end_col, hl_group = hl[1], hl[2], hl[3]
api.nvim_buf_set_extmark(state.bufnr, globals.NAMESPACE, target_row, start_col, {
hl_group = hl_group,
end_col = end_col,
strict = false,
})
end
end

Expand Down

0 comments on commit 5271319

Please sign in to comment.