summaryrefslogtreecommitdiffstats
path: root/lua/netrw-icons/init.lua
blob: 608d4794bd09d401cec6994eda0d382f82ffd69d (plain)
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
-- 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.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_file_icon(node)
	if not icon_provider then
		return nil, nil
	end

	if icon_provider.type == "devicons" then
		local icon, hl = icon_provider.provider.get_icon(node.node, nil, { strict = true, default = false })
		return icon, hl
	elseif icon_provider.type == "miniicons" then
		local icon, hl = icon_provider.provider.get("file", node.node)
		return icon, hl
	end

	return nil, nil
end

local function get_lsp_diagnostics(bufnr, node)
	local diagnostics = {}
	local filepath = node.dir .. "/" .. node.node

	local diag = vim.diagnostic.get(nil, { path = filepath })

	for _, d in ipairs(diag) do
		if d.severity == vim.diagnostic.severity.ERROR and M.options.lsp.error then
			table.insert(diagnostics, { icon = "E", hl = "DiagnosticError" })
		elseif d.severity == vim.diagnostic.severity.WARN and M.options.lsp.warn then
			table.insert(diagnostics, { icon = "W", hl = "DiagnosticWarn" })
		elseif d.severity == vim.diagnostic.severity.INFO and M.options.lsp.info then
			table.insert(diagnostics, { icon = "I", hl = "DiagnosticInfo" })
		elseif d.severity == vim.diagnostic.severity.HINT and M.options.lsp.hint then
			table.insert(diagnostics, { icon = "H", hl = "DiagnosticHint" })
		end
	end

	return diagnostics
end

local function draw(bufnr)
	local namespace = vim.api.nvim_create_namespace("netrw")
	vim.api.nvim_buf_clear_namespace(bufnr, namespace, 0, -1)

	local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)

	for i, line in ipairs(lines) do
		local node = parse.get_node(line)
		if node then
			-- Draw file/dir icon on the left
			if (node.type == parse.TYPE_FILE and M.options.file) or
				(node.type == parse.TYPE_DIR and M.options.dir) then
				local icon, hl = get_file_icon(node)
				if icon then
					local opts = {
						id = i * 2, -- Unique ID for icon
						virt_text = hl and { { icon .. " ", hl } } or { { icon .. " " } },
						virt_text_pos = "inline",
					}
					vim.api.nvim_buf_set_extmark(bufnr, namespace, i - 1, node.icon_col, opts)
				end
			end

			-- Draw LSP diagnostics on the right
			if node.type == parse.TYPE_FILE then
				local diagnostics = get_lsp_diagnostics(bufnr, node)
				if #diagnostics > 0 then
					local virt_text = {}
					for _, diag in ipairs(diagnostics) do
						table.insert(virt_text, { " " .. diag.icon, diag.hl })
					end

					local opts = {
						id = i * 2 + 1, -- Unique ID for LSP
						virt_text = virt_text,
						virt_text_pos = "inline",
					}
					vim.api.nvim_buf_set_extmark(bufnr, namespace, i - 1, node.lsp_col, opts)
				end
			end
		end
	end
end

--- @class Config
M.options = {}

--- @class Config
local default = {
	-- Preferred icon provider:
	-- nil         -> auto-detect
	-- "miniicons" -> use mini.icons
	-- "devicons"  -> use nvim-web-devicons
	prefer = nil,

	-- Show icons for files
	file = true,

	-- Show icons for directories
	dir = true,

	-- LSP diagnostics integration
	lsp = {
		info = false,
		hint = false,
		warn = true,
		error = true,
	}
}

---@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)
	end

	vim.api.nvim_create_autocmd({ "BufEnter", "DiagnosticChanged" }, {
		pattern = "*",
		group = vim.api.nvim_create_augroup("netrw_icons", { clear = true }),
		callback = function(args)
			if vim.bo[args.buf].filetype ~= "netrw" then
				return
			end
			draw(args.buf)
		end
	})
end

return M