/
renderer.lua
150 lines (123 loc) · 3.8 KB
/
renderer.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
local providers = require("trouble.providers")
local util = require("trouble.util")
local config = require("trouble.config")
local Text = require("trouble.text")
local folds = require("trouble.folds")
---@class Renderer
local renderer = {}
renderer.signs = {}
local function get_icon(file)
local ok, icons = pcall(require, "nvim-web-devicons")
if not ok then
util.warn(
"'nvim-web-devicons' is not installed. Install it, or set icons=false in your configuration to disable this message"
)
return ""
end
local fname = vim.fn.fnamemodify(file, ":t")
local ext = vim.fn.fnamemodify(file, ":e")
return icons.get_icon(fname, ext, { default = true })
end
local function update_signs()
renderer.signs = config.options.signs
if config.options.use_diagnostic_signs then
local lsp_signs = require("trouble.providers.diagnostic").get_signs()
renderer.signs = vim.tbl_deep_extend("force", {}, renderer.signs, lsp_signs)
end
end
---@param view TroubleView
function renderer.render(view, opts)
opts = opts or {}
local buf = vim.api.nvim_win_get_buf(view.parent)
providers.get(view.parent, buf, function(items)
local grouped = providers.group(items)
local count = util.count(grouped)
-- check for auto close
if opts.auto and config.options.auto_close then
if count == 0 then
view:close()
return
end
end
-- Update lsp signs
update_signs()
local text = Text:new()
view.items = {}
if config.options.padding then
text:nl()
end
-- render file groups
for _, group in ipairs(grouped) do
if opts.open_folds then
folds.open(group.filename)
end
if opts.close_folds then
folds.close(group.filename)
end
renderer.render_file(view, text, group.filename, group.items)
end
view:render(text)
if opts.focus then
view:focus()
end
end, config.options)
end
---@param view TroubleView
---@param text Text
---@param items Item[]
---@param filename string
function renderer.render_file(view, text, filename, items)
view.items[text.lineNr + 1] = { filename = filename, is_file = true }
if view.group == true then
local count = util.count(items)
text:render(" ")
if folds.is_folded(filename) then
text:render(config.options.fold_closed, "FoldIcon", " ")
else
text:render(config.options.fold_open, "FoldIcon", " ")
end
if config.options.icons then
local icon, icon_hl = get_icon(filename)
text:render(icon, icon_hl, { exact = true, append = " " })
end
text:render(vim.fn.fnamemodify(filename, ":p:."), "File", " ")
text:render(" " .. count .. " ", "Count")
text:nl()
end
if not folds.is_folded(filename) then
renderer.render_diagnostics(view, text, items)
end
end
---@param view TroubleView
---@param text Text
---@param items Item[]
function renderer.render_diagnostics(view, text, items)
for _, diag in ipairs(items) do
view.items[text.lineNr + 1] = diag
local sign = diag.sign or renderer.signs[string.lower(diag.type)]
if not sign then
sign = diag.type
end
local indent = " "
if config.options.indent_lines then
indent = " │ "
elseif config.options.group == false then
indent = " "
end
local sign_hl = diag.sign_hl or ("TroubleSign" .. diag.type)
text:render(indent, "Indent")
text:render(sign .. " ", sign_hl, { exact = true })
text:render(diag.text, "Text" .. diag.type, " ")
-- text:render(diag.type, diag.type, " ")
if diag.source then
text:render(diag.source, "Source")
end
if diag.code and diag.code ~= vim.NIL then
text:render(" (" .. diag.code .. ")", "Code")
end
text:render(" ")
text:render("[" .. diag.lnum .. ", " .. diag.col .. "]", "Location")
text:nl()
end
end
return renderer