summaryrefslogtreecommitdiffstats
path: root/lua/netrw-icons/parse.lua
blob: fbd9da1267de49cca08d6c702f2702ed5c0e0139 (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
-- 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_EXE = 2
M.TYPE_SYMLINK = 3

---@alias rwNode{name:string, icon:number, type:number}

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

	local name = line;

	if name:sub(-1) == "*" then
		type = M.TYPE_EXE;
	end

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

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

	return {
		name = name,
		icon = 0,
		type = type,
	}
end

---@param line string
---@return rwNode|nil
local parse_liststyle_1 = function(line)
	local current_dir = vim.b.netrw_curdier;
	local type = M.TYPE_FILE;

	local name = line:match("^(%S+)")

	if name:sub(-1) == "*" then
		type = M.TYPE_EXE;
	end

	local _, _, link, link_target = string.find(line, "^(.+)@%s+")
	if link then
		type = M.TYPE_SYMLINK;
		name = link_target;
	end

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

	return {
		name = name,
		icon = 0,
		type = type,
	}
end

---@param line string
---@return rwNode|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 name = content;

	if name:sub(-1) == "*" then
		type = M.TYPE_EXE;
	end

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

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

	return {
		name = name,
		icon = tree_end,
		type = type,
	}
end

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

	local liststyle = vim.b.netrw_liststyle

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

	return nil
end

return M