Skip to content

Commit

Permalink
fix: properly deal with multiline treesitter segments
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Oct 23, 2023
1 parent b680422 commit 48a7367
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
21 changes: 16 additions & 5 deletions lua/trouble/view/text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ function M:append(text, opts)
opts.ts = opts.hl:sub(4)
end

for _, line in Util.lines(text) do
for l, line in Util.lines(text) do
local width = #line
self._col = self._col + width
table.insert(self._lines[#self._lines], {
str = line,
width = width,
hl = opts.hl,
ts = opts.ts,
line = opts.line,
line = opts.line or l,
})
end
return self
Expand Down Expand Up @@ -117,7 +117,7 @@ function M:render(buf)
vim.api.nvim_buf_clear_namespace(buf, M.ns, 0, -1)
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)

local regions = {} ---@type table<string, number[][]>
local regions = {} ---@type table<string, number[][][]>

for l, line in ipairs(self._lines) do
local col = self.opts.padding or 0
Expand All @@ -126,8 +126,19 @@ function M:render(buf)
for _, segment in ipairs(line) do
local width = segment.width
if segment.ts then
regions[segment.ts] = regions[segment.ts] or {}
table.insert(regions[segment.ts], {
regions[segment.ts or ""] = regions[segment.ts] or {}
local ts_regions = regions[segment.ts or ""]
local last_region = ts_regions[#ts_regions]
if not last_region then
last_region = {}
table.insert(ts_regions, last_region)
end
-- combine multiline item segments in one region
if segment.line and #last_region ~= segment.line - 1 then
last_region = {}
table.insert(ts_regions, last_region)
end
table.insert(last_region, {
row,
col,
row,
Expand Down
2 changes: 1 addition & 1 deletion lua/trouble/view/treesitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function M.highlight(buf, lang, regions)
-- lang = "markdown_inline"
local parser = vim.treesitter.get_parser(buf, lang)

parser:set_included_regions({ regions })
parser:set_included_regions(regions)
parser:parse(true)
local ret = {} ---@type Extmark[]

Expand Down

0 comments on commit 48a7367

Please sign in to comment.