mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
fix(treesitter): remove virtual text from playground
Implement the range and lang annotations as comments instead
This commit is contained in:
parent
2c9fbe34b2
commit
da56f06037
@ -138,6 +138,19 @@ end
|
||||
|
||||
local decor_ns = api.nvim_create_namespace('ts.playground')
|
||||
|
||||
---@private
|
||||
---@param lnum integer
|
||||
---@param col integer
|
||||
---@param end_lnum integer
|
||||
---@param end_col integer
|
||||
---@return string
|
||||
local function get_range_str(lnum, col, end_col, end_lnum)
|
||||
if lnum == end_lnum then
|
||||
return string.format('[%d:%d-%d]', lnum + 1, col + 1, end_col)
|
||||
end
|
||||
return string.format('[%d:%d-%d:%d]', lnum + 1, col + 1, end_lnum + 1, end_col)
|
||||
end
|
||||
|
||||
--- Write the contents of this Playground into {bufnr}.
|
||||
---
|
||||
---@param bufnr number Buffer number to write into.
|
||||
@ -145,26 +158,31 @@ local decor_ns = api.nvim_create_namespace('ts.playground')
|
||||
function TSPlayground:draw(bufnr)
|
||||
vim.bo[bufnr].modifiable = true
|
||||
local lines = {} ---@type string[]
|
||||
local lang_hl_marks = {} ---@type table[]
|
||||
|
||||
for _, item in self:iter() do
|
||||
lines[#lines + 1] = string.rep(' ', item.depth) .. item.text
|
||||
local range_str = get_range_str(item.lnum, item.col, item.end_lnum, item.end_col)
|
||||
local lang_str = self.opts.lang and string.format(' %s', item.lang) or ''
|
||||
local line = string.rep(' ', item.depth) .. item.text .. '; ' .. range_str .. lang_str
|
||||
|
||||
if self.opts.lang then
|
||||
lang_hl_marks[#lang_hl_marks + 1] = {
|
||||
col = #line - #lang_str,
|
||||
end_col = #line,
|
||||
}
|
||||
end
|
||||
|
||||
lines[#lines + 1] = line
|
||||
end
|
||||
|
||||
api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
|
||||
|
||||
api.nvim_buf_clear_namespace(bufnr, decor_ns, 0, -1)
|
||||
|
||||
for i, item in self:iter() do
|
||||
local range_str
|
||||
if item.lnum == item.end_lnum then
|
||||
range_str = string.format('[%d:%d-%d]', item.lnum + 1, item.col + 1, item.end_col)
|
||||
else
|
||||
range_str =
|
||||
string.format('[%d:%d-%d:%d]', item.lnum + 1, item.col + 1, item.end_lnum + 1, item.end_col)
|
||||
end
|
||||
|
||||
local lang_str = self.opts.lang and string.format(' %s', item.lang) or ''
|
||||
|
||||
api.nvim_buf_set_extmark(bufnr, decor_ns, i - 1, 0, {
|
||||
virt_text = { { range_str, 'Comment' }, { lang_str, 'Title' } },
|
||||
for i, m in ipairs(lang_hl_marks) do
|
||||
api.nvim_buf_set_extmark(bufnr, decor_ns, i - 1, m.col, {
|
||||
hl_group = 'Title',
|
||||
end_col = m.end_col,
|
||||
})
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user