@@ -2,14 +2,20 @@ local Path = require "plenary.path"
2
2
3
3
local os_sep = Path .path .sep
4
4
5
+ --- @class PlenaryFiletype
5
6
local filetype = {}
6
7
8
+ --- @class PlenaryFiletypeTable
9
+ --- @field file_name ? table<string , string>
10
+ --- @field extension ? table<string , string>
11
+ --- @field shebang ? table<string , string>
7
12
local filetype_table = {
8
13
extension = {},
9
14
file_name = {},
10
15
shebang = {},
11
16
}
12
17
18
+ --- @param new_filetypes PlenaryFiletypeTable
13
19
filetype .add_table = function (new_filetypes )
14
20
local valid_keys = { " extension" , " file_name" , " shebang" }
15
21
local new_keys = {}
@@ -39,6 +45,7 @@ filetype.add_table = function(new_filetypes)
39
45
end
40
46
end
41
47
48
+ --- @param filename string
42
49
filetype .add_file = function (filename )
43
50
local filetype_files = vim .api .nvim_get_runtime_file (string.format (" data/plenary/filetypes/%s.lua" , filename ), true )
44
51
@@ -51,10 +58,15 @@ filetype.add_file = function(filename)
51
58
end
52
59
53
60
local filename_regex = " [^" .. os_sep .. " ].*"
61
+ --- @param filename string
62
+ --- @return string[]
54
63
filetype ._get_extension_parts = function (filename )
64
+ --- @type string ?
55
65
local current_match = filename :match (filename_regex )
66
+ --- @type string[]
56
67
local possibilities = {}
57
68
while current_match do
69
+ --- @type string ?
58
70
current_match = current_match :match " [^.]%.(.*)"
59
71
if current_match then
60
72
table.insert (possibilities , current_match :lower ())
@@ -65,13 +77,17 @@ filetype._get_extension_parts = function(filename)
65
77
return possibilities
66
78
end
67
79
80
+ --- @param tail string
81
+ --- @return string
68
82
filetype ._parse_modeline = function (tail )
69
83
if tail :find " vim:" then
70
84
return tail :match " .*:ft=([^: ]*):.*$" or " "
71
85
end
72
86
return " "
73
87
end
74
88
89
+ --- @param head string
90
+ --- @return string
75
91
filetype ._parse_shebang = function (head )
76
92
if head :sub (1 , 2 ) == " #!" then
77
93
local match = filetype_table .shebang [head :sub (3 , # head )]
@@ -99,6 +115,8 @@ local extend_tbl_with_ext_eq_ft_entries = function()
99
115
end
100
116
end
101
117
118
+ --- @param filepath string
119
+ --- @return string
102
120
filetype .detect_from_extension = function (filepath )
103
121
local exts = filetype ._get_extension_parts (filepath )
104
122
for _ , ext in ipairs (exts ) do
@@ -118,6 +136,8 @@ filetype.detect_from_extension = function(filepath)
118
136
return " "
119
137
end
120
138
139
+ --- @param filepath string
140
+ --- @return string
121
141
filetype .detect_from_name = function (filepath )
122
142
if filepath then
123
143
filepath = filepath :lower ()
@@ -131,6 +151,8 @@ filetype.detect_from_name = function(filepath)
131
151
return " "
132
152
end
133
153
154
+ --- @param filepath string
155
+ --- @return string ?
134
156
filetype .detect_from_modeline = function (filepath )
135
157
local tail = Path :new (filepath ):readbyterange (- 256 , 256 )
136
158
if not tail then
@@ -143,6 +165,8 @@ filetype.detect_from_modeline = function(filepath)
143
165
end
144
166
end
145
167
168
+ --- @param filepath string
169
+ --- @return string
146
170
filetype .detect_from_shebang = function (filepath )
147
171
local head = Path :new (filepath ):readbyterange (0 , 256 )
148
172
if not head then
@@ -152,10 +176,12 @@ filetype.detect_from_shebang = function(filepath)
152
176
return filetype ._parse_shebang (lines [1 ])
153
177
end
154
178
179
+ --- @class PlenaryFiletypeDetectOpts
180
+ --- @field fs_access boolean Should check a file if it exists (default : ` true` )
181
+
155
182
--- Detect a filetype from a path.
156
- ---
157
- --- @param opts table : Table with optional keys
158
- --- - fs_access (bool, default=true): Should check a file if it exists
183
+ --- @param opts ? PlenaryFiletypeDetectOpts
184
+ --- @return string ?
159
185
filetype .detect = function (filepath , opts )
160
186
opts = opts or {}
161
187
opts .fs_access = opts .fs_access or true
@@ -164,6 +190,7 @@ filetype.detect = function(filepath, opts)
164
190
filepath = tostring (filepath )
165
191
end
166
192
193
+ --- @type string ?
167
194
local match = filetype .detect_from_name (filepath )
168
195
if match ~= " " then
169
196
return match
0 commit comments