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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
-- SPDX-License-Identifier: MIT
--
-- Copyright (c) 2026 Mikołaj Kozłowski
--
-- Portions of this file are derived from:
-- prichrd/netrw.nvim (MIT)
-- https://github.com/prichrd/netrw.nvim
local M = {}
local parse = require("netrw-icons.parse")
local icon_provider = nil
local function get_icon_provider(prefer)
local providers = {}
local has_devicons, devicons = pcall(require, "nvim-web-devicons")
if has_devicons then
providers.devicons = devicons
end
local has_miniicons, miniicons = pcall(require, "mini.icons")
if has_miniicons then
providers.miniicons = miniicons
end
if prefer and providers[prefer] then
return { type = prefer, provider = providers[prefer] }
end
if providers.miniicons then
return { type = "miniicons", provider = providers.miniicons }
elseif providers.devicons then
return { type = "devicons", provider = providers.devicons }
end
return nil
end
local function get_icon_from_provider(name)
if icon_provider then
local provider = icon_provider.provider;
local type = icon_provider.type;
if type == "devicons" then
local symbol, hi = provider.get_icon(name, nil, { strict = true, default = M.options.file_default });
if symbol then
return { symbol = symbol .. " ", hi = hi };
end
elseif type == "miniicons" then
local symbol, hi, is_default = provider.get("file", name)
if symbol then
if is_default and not M.options.file_default then
symbol = "";
end
return { symbol = symbol .. " ", hi = hi };
end
end
end
return nil;
end
local function get_icon(node)
if node.type == parse.TYPE_DIR and M.options.dir then
if M.options.dir then
return { symbol = M.options.dir, hi = "netrwDir" };
end
elseif node.type == parse.TYPE_SYMLINK and M.options.sym then
if M.options.sym then
return { symbol = M.options.sym, hi = "netrwSymlink" };
end
elseif node.type == parse.TYPE_FILE and M.options.file then
local file_type = vim.fn.fnamemodify(node.name, ":e");
if string.sub(file_type, -1) == "*" then
return get_icon_from_provider("executable")
end
return get_icon_from_provider(node.name);
end
return nil;
end
local function path_to_bufnr(path)
path = vim.fn.fnamemodify(path, ":p");
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_is_loaded(bufnr) then -- TODO: Load project buffs
local buf_path = vim.api.nvim_buf_get_name(bufnr);
if buf_path == path then
return bufnr;
end
end
end
return nil
end
local function get_lsp_diagnostics()
local all_diagnostics = vim.diagnostic.get(nil);
local diagnostics_map = {};
for _, diag in ipairs(all_diagnostics) do
local diag_path = vim.api.nvim_buf_get_name(diag.bufnr);
diag_path = vim.fn.fnamemodify(diag_path, ":p");
if not diagnostics_map[diag_path] then
diagnostics_map[diag_path] = {
info = 0,
hint = 0,
warn = 0,
error = 0,
}
end
if diag.severity == vim.diagnostic.severity.ERROR and M.options.lsp.error then
diagnostics_map[diag_path].error = diagnostics_map[diag_path].error + 1
elseif diag.severity == vim.diagnostic.severity.WARN and M.options.lsp.warn then
diagnostics_map[diag_path].warn = diagnostics_map[diag_path].warn + 1
elseif diag.severity == vim.diagnostic.severity.INFO and M.options.lsp.info then
diagnostics_map[diag_path].info = diagnostics_map[diag_path].info + 1
elseif diag.severity == vim.diagnostic.severity.HINT and M.options.lsp.hint then
diagnostics_map[diag_path].hint = diagnostics_map[diag_path].hint + 1
end
end
return diagnostics_map;
end
local function format_diagnostic(count, config, hi)
if not config then
return nil;
end
if count == 0 then
return nil;
end
local prefix = "";
if type(config) == "string" then
prefix = config;
end
return { (prefix .. tostring(count)), hi }
end
local function draw(bufnr)
local namespace_icons = vim.api.nvim_create_namespace("netrw-icons")
local namespace_diagnostics = vim.api.nvim_create_namespace("netrw-diagnostics")
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local diagnostics_map = nil;
if M.options.lsp then
diagnostics_map = get_lsp_diagnostics();
end
for i, line in ipairs(lines) do
local node = parse.get_node(line)
if node then
local icon = get_icon(node);
if icon then
local symbol = icon.symbol;
local virt_text = { symbol };
if icon.hi then
virt_text[2] = icon.hi;
end
vim.api.nvim_buf_set_extmark(bufnr, namespace_icons, i - 1, node.icon, {
id = i,
virt_text_pos = "inline",
virt_text = { virt_text },
});
end
if M.options.lsp then
local diagnostics = diagnostics_map[node.path];
if diagnostics then
local virt_text = { { " " } };
local diag_items = {
{ count = diagnostics.error, config = M.options.lsp.error, hl = "DiagnosticError" },
{ count = diagnostics.warn, config = M.options.lsp.warn, hl = "DiagnosticWarn" },
{ count = diagnostics.hint, config = M.options.lsp.hint, hl = "DiagnosticHint" },
{ count = diagnostics.info, config = M.options.lsp.info, hl = "DiagnosticInfo" },
};
local has_diagnostics = false;
for _, item in ipairs(diag_items) do
local formated = format_diagnostic(item.count, item.config, item.hl);
if formated then
if has_diagnostics then
table.insert(virt_text, { " " });
end
table.insert(virt_text, formated);
has_diagnostics = true;
end
end
if has_diagnostics then
vim.api.nvim_buf_set_extmark(bufnr, namespace_diagnostics, i - 1, node.lsp, {
id = i,
virt_text_pos = "inline",
virt_text = virt_text,
});
end
end
end
end
end
end
--- @class Config
M.options = {}
--- @class Config
local default = {
prefer = nil,
file = true,
file_default = true,
dir = " ",
sym = false,
lsp = {
info = false,
hint = "H-",
warn = true,
error = "E-",
}
}
---@param options Config|nil
function M.setup(options)
M.options = vim.tbl_deep_extend("force", {}, default, options or {})
if M.options.file or M.options.dir then
icon_provider = get_icon_provider(M.options.prefer)
if not icon_provider then
error("No icon provider found");
end
end
vim.api.nvim_create_autocmd("BufModifiedSet", {
pattern = { "*" },
group = vim.api.nvim_create_augroup("netrw_icons", { clear = false }),
callback = function(args)
if not (vim.bo and vim.bo.filetype == "netrw") then
return
end
if vim.b.netrw_liststyle ~= 0 and vim.b.netrw_liststyle ~= 1 and vim.b.netrw_liststyle ~= 3 then
return
end
draw(args.buf)
end
})
end
return M
|