lazy.nvim/lua/lazy/view/text.lua

172 lines
3.8 KiB
Lua
Raw Permalink Normal View History

local Config = require("lazy.core.config")
local Util = require("lazy.util")
2022-11-20 22:34:35 +00:00
---@alias TextSegment {str: string, hl?:string|Extmark}
---@alias Extmark {hl_group?:string, col?:number, end_col?:number}
2022-11-20 22:25:21 +00:00
---@class Text
2022-11-20 22:34:35 +00:00
---@field _lines TextSegment[][]
---@field padding number
---@field wrap number
2022-11-20 22:25:21 +00:00
local Text = {}
function Text.new()
local self = setmetatable({}, {
__index = Text,
})
self._lines = {}
return self
end
---@param str string
---@param hl? string|Extmark
---@param opts? {indent?: number, prefix?: string, wrap?: boolean}
function Text:append(str, hl, opts)
opts = opts or {}
2022-11-20 22:25:21 +00:00
if #self._lines == 0 then
self:nl()
end
local lines = vim.split(str, "\n")
for l, line in ipairs(lines) do
if opts.prefix then
line = opts.prefix .. line
end
if opts.indent then
line = string.rep(" ", opts.indent) .. line
end
if l > 1 then
self:nl()
end
if
Config.options.ui.wrap
and opts.wrap
and str ~= ""
and self:col() > 0
and self:col() + vim.fn.strwidth(line) + self.padding > self.wrap
then
self:nl()
end
table.insert(self._lines[#self._lines], {
str = line,
hl = hl,
})
end
2022-11-20 22:25:21 +00:00
return self
end
function Text:nl()
table.insert(self._lines, {})
return self
end
function Text:render(buf)
2022-11-20 22:25:21 +00:00
local lines = {}
for _, line in ipairs(self._lines) do
local str = (" "):rep(self.padding)
2023-05-27 15:11:01 +01:00
local has_extmark = false
2022-11-20 22:25:21 +00:00
for _, segment in ipairs(line) do
str = str .. segment.str
2023-05-27 15:11:01 +01:00
if type(segment.hl) == "table" then
has_extmark = true
end
2022-11-20 22:25:21 +00:00
end
2023-05-27 15:11:01 +01:00
if str:match("^%s*$") and not has_extmark then
2023-05-02 20:30:28 +01:00
str = ""
end
2022-11-20 22:25:21 +00:00
table.insert(lines, str)
end
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
vim.api.nvim_buf_clear_namespace(buf, Config.ns, 0, -1)
2022-11-20 22:25:21 +00:00
for l, line in ipairs(self._lines) do
if lines[l] ~= "" then
local col = self.padding
for _, segment in ipairs(line) do
local width = vim.fn.strlen(segment.str)
local extmark = segment.hl
if extmark then
if type(extmark) == "string" then
extmark = { hl_group = extmark, end_col = col + width }
end
---@cast extmark Extmark
local extmark_col = extmark.col or col
extmark.col = nil
2023-05-27 15:11:01 +01:00
local ok, err = pcall(vim.api.nvim_buf_set_extmark, buf, Config.ns, l - 1, extmark_col, extmark)
if not ok then
Util.error(
"Failed to set extmark. Please report a bug with this info:\n"
2023-05-27 15:11:01 +01:00
.. vim.inspect({ segment = segment, line = line, error = err })
)
end
end
2022-11-20 22:25:21 +00:00
col = col + width
end
2022-11-20 22:25:21 +00:00
end
end
end
---@param patterns table<string,string>
function Text:highlight(patterns)
local col = self.padding
local last = self._lines[#self._lines]
---@type TextSegment?
local text
for s, segment in ipairs(last) do
if s == #last then
text = segment
break
end
col = col + vim.fn.strlen(segment.str)
end
if text then
for pattern, hl in pairs(patterns) do
local from, to, match = text.str:find(pattern)
while from do
if match then
from, to = text.str:find(match, from, true)
end
self:append("", {
col = col + from - 1,
end_col = col + to,
hl_group = hl,
})
from, to = text.str:find(pattern, to + 1)
end
end
end
end
2022-11-20 22:25:21 +00:00
function Text:trim()
while #self._lines > 0 and #self._lines[#self._lines] == 0 do
table.remove(self._lines)
end
end
2022-11-22 20:12:50 +00:00
function Text:row()
return #self._lines == 0 and 1 or #self._lines
end
function Text:col()
if #self._lines == 0 then
return 0
end
local width = 0
for _, segment in ipairs(self._lines[#self._lines]) do
width = width + vim.fn.strlen(segment.str)
end
return width
end
2022-11-20 22:25:21 +00:00
return Text