summaryrefslogtreecommitdiffstats
path: root/lua/netrw-icons/parse.lua
blob: c2f245c13eb58a32bfeb75ea0e5b2854b15b352b (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
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
-- 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 = {}

M.TYPE_DIR = 0
M.TYPE_FILE = 1
M.TYPE_SYMLINK = 2

---@alias PWord {dir:string, node:string, link:string|nil, extension:string|nil, type:number, icon_col:number, name_col:number, lsp_col:number}

---@param line string
---@param curdir string
---@return PWord|nil
local parse_liststyle_0 = function(line, curdir)
	local _, _, node, link = string.find(line, "^(.+)@\t%s*%-%->%s*(.+)")
	if node then
		return {
			dir = curdir,
			icon_col = 0,
			name_col = 0,
			lsp_col = #node,
			node = node,
			extension = vim.fn.fnamemodify(node, ":e"),
			link = link,
			type = M.TYPE_SYMLINK,
		}
	end

	local _, _, dir = string.find(line, "^(.*)/")
	if dir then
		return {
			dir = curdir,
			icon_col = 0,
			name_col = 0,
			lsp_col = #dir + 1, -- +1 for the "/"
			node = dir,
			type = M.TYPE_DIR,
		}
	end

	local ext = vim.fn.fnamemodify(line, ":e")
	local clean_line = line
	if string.sub(ext, -1) == "*" then
		ext = string.sub(ext, 1, -2)
		clean_line = string.sub(line, 1, -2)
	end

	return {
		dir = curdir,
		icon_col = 0,
		name_col = 0,
		lsp_col = #clean_line,
		node = clean_line,
		extension = ext,
		type = M.TYPE_FILE,
	}
end

---@param line string
---@param curdir string
---@return PWord|nil
local parse_liststyle_1 = function(line, curdir)
	local _, _, node, link = string.find(line, "^(.+)@%s+")
	if node then
		return {
			dir = curdir,
			icon_col = 0,
			name_col = 0,
			lsp_col = #node,
			node = node,
			extension = vim.fn.fnamemodify(node, ":e"),
			link = link,
			type = M.TYPE_SYMLINK,
		}
	end

	local _, _, dir = string.find(line, "^(.*)/")
	if dir then
		return {
			dir = curdir,
			icon_col = 0,
			name_col = 0,
			lsp_col = #dir + 1,
			node = dir,
			type = M.TYPE_DIR,
		}
	end

	local file = vim.fn.substitute(line, "^\\(\\%(\\S\\+ \\)*\\S\\+\\).\\{-}$", "\\1", "e")
	local ext = vim.fn.fnamemodify(file, ":e")
	if string.sub(ext, -1) == "*" then
		ext = string.sub(ext, 1, -2)
		file = string.sub(file, 1, -2)
	end

	return {
		dir = curdir,
		icon_col = 0,
		name_col = 0,
		lsp_col = #file,
		node = file,
		extension = ext,
		type = M.TYPE_FILE,
	}
end

---@param line string
---@return PWord|nil
local parse_liststyle_3 = function(line)
	local current_dir = vim.b.netrw_curdir;
	local type = M.TYPE_FILE;

	local _, tree_end = string.find(line, "^[|%s]*");

	local content = string.sub(line, tree_end + 1, #line);
	if content == "" then
		return nil;
	end

	local _, _, link, link_target = string.find(content, "^(.+)@\t%s*%-%->%s*(.+)")
	if link then
		type = M.TYPE_SYMLINK;
	end

	local _, _, dir = string.find(content, "^(.*)/")
	if dir then
		type = M.TYPE_DIR;
	end

	return {
		name = link_target or content,
		icon = tree_end,
		type = type,
	}
end




-- local _, to = string.find(line, "^[|%s]*")
-- if to ~= 0 then
-- 	error("to: " .. to);
-- end
-- local pipelessLine = string.sub(line, to + 1, #line)
--
-- if pipelessLine == "" then
-- 	return nil
-- end
--
-- local _, _, node, link = string.find(pipelessLine, "^(.+)@\t%s*%-%->%s*(.+)")
-- if node then
-- 	return {
-- 		dir = curdir,
-- 		icon_col = to,
-- 		name_col = to,
-- 		lsp_col = to + #node,
-- 		node = node,
-- 		extension = vim.fn.fnamemodify(node, ":e"),
-- 		link = link,
-- 		type = M.TYPE_SYMLINK,
-- 	}
-- end
--
-- local _, _, dir = string.find(pipelessLine, "^(.*)/")
-- if dir then
-- 	return {
-- 		dir = curdir,
-- 		icon_col = to,
-- 		name_col = to,
-- 		lsp_col = to + #dir + 1,
-- 		node = dir,
-- 		type = M.TYPE_DIR,
-- 	}
-- end
--
-- local ext = vim.fn.fnamemodify(pipelessLine, ":e")
-- local clean_line = pipelessLine
-- if string.sub(ext, -1) == "*" then
-- 	ext = string.sub(ext, 1, -2)
-- 	clean_line = string.sub(pipelessLine, 1, -2)
-- end
--
-- return {
-- 	current_dir = vim.b.netrw_curdir,
-- 	icon_col = to,
-- 	name_col = to,
-- 	lsp_col = to + #clean_line,
-- 	node = clean_line,
-- 	extension = ext,
-- 	type = M.TYPE_FILE,
-- }

-- return {
-- 	name = "name of the file",
-- 	type = "type of the netrw element (dir, file, symlink)"
-- 	path = "full path of the file",
-- 	icon = "character after the | netrw symbol",
-- 	lsp = "two characters after the filename end",
-- }
-- end

---@param line string
---@return PWord|nil
M.get_node = function(line)
	if string.find(line, '^"') then
		return nil
	end

	if line == "" then
		return nil
	end

	local curdir = vim.b.netrw_curdir
	local liststyle = vim.b.netrw_liststyle

	if liststyle == 0 then
		return parse_liststyle_0(line, curdir)
	elseif liststyle == 1 then
		return parse_liststyle_1(line, curdir)
	elseif liststyle == 3 then
		return parse_liststyle_3(line)
	end

	return nil
end

return M