@@ -2,14 +2,20 @@ local Path = require "plenary.path"
22
33local os_sep = Path .path .sep
44
5+ --- @class PlenaryFiletype
56local filetype = {}
67
8+ --- @class PlenaryFiletypeTable
9+ --- @field file_name ? table<string , string>
10+ --- @field extension ? table<string , string>
11+ --- @field shebang ? table<string , string>
712local filetype_table = {
813 extension = {},
914 file_name = {},
1015 shebang = {},
1116}
1217
18+ --- @param new_filetypes PlenaryFiletypeTable
1319filetype .add_table = function (new_filetypes )
1420 local valid_keys = { " extension" , " file_name" , " shebang" }
1521 local new_keys = {}
@@ -39,6 +45,7 @@ filetype.add_table = function(new_filetypes)
3945 end
4046end
4147
48+ --- @param filename string
4249filetype .add_file = function (filename )
4350 local filetype_files = vim .api .nvim_get_runtime_file (string.format (" data/plenary/filetypes/%s.lua" , filename ), true )
4451
@@ -51,10 +58,15 @@ filetype.add_file = function(filename)
5158end
5259
5360local filename_regex = " [^" .. os_sep .. " ].*"
61+ --- @param filename string
62+ --- @return string[]
5463filetype ._get_extension_parts = function (filename )
64+ --- @type string ?
5565 local current_match = filename :match (filename_regex )
66+ --- @type string[]
5667 local possibilities = {}
5768 while current_match do
69+ --- @type string ?
5870 current_match = current_match :match " [^.]%.(.*)"
5971 if current_match then
6072 table.insert (possibilities , current_match :lower ())
@@ -65,13 +77,17 @@ filetype._get_extension_parts = function(filename)
6577 return possibilities
6678end
6779
80+ --- @param tail string
81+ --- @return string
6882filetype ._parse_modeline = function (tail )
6983 if tail :find " vim:" then
7084 return tail :match " .*:ft=([^: ]*):.*$" or " "
7185 end
7286 return " "
7387end
7488
89+ --- @param head string
90+ --- @return string
7591filetype ._parse_shebang = function (head )
7692 if head :sub (1 , 2 ) == " #!" then
7793 local match = filetype_table .shebang [head :sub (3 , # head )]
@@ -99,6 +115,8 @@ local extend_tbl_with_ext_eq_ft_entries = function()
99115 end
100116end
101117
118+ --- @param filepath string
119+ --- @return string
102120filetype .detect_from_extension = function (filepath )
103121 local exts = filetype ._get_extension_parts (filepath )
104122 for _ , ext in ipairs (exts ) do
@@ -118,6 +136,8 @@ filetype.detect_from_extension = function(filepath)
118136 return " "
119137end
120138
139+ --- @param filepath string
140+ --- @return string
121141filetype .detect_from_name = function (filepath )
122142 if filepath then
123143 filepath = filepath :lower ()
@@ -131,6 +151,8 @@ filetype.detect_from_name = function(filepath)
131151 return " "
132152end
133153
154+ --- @param filepath string
155+ --- @return string ?
134156filetype .detect_from_modeline = function (filepath )
135157 local tail = Path :new (filepath ):readbyterange (- 256 , 256 )
136158 if not tail then
@@ -143,6 +165,8 @@ filetype.detect_from_modeline = function(filepath)
143165 end
144166end
145167
168+ --- @param filepath string
169+ --- @return string
146170filetype .detect_from_shebang = function (filepath )
147171 local head = Path :new (filepath ):readbyterange (0 , 256 )
148172 if not head then
@@ -152,10 +176,12 @@ filetype.detect_from_shebang = function(filepath)
152176 return filetype ._parse_shebang (lines [1 ])
153177end
154178
179+ --- @class PlenaryFiletypeDetectOpts
180+ --- @field fs_access boolean Should check a file if it exists (default : ` true` )
181+
155182--- 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 ?
159185filetype .detect = function (filepath , opts )
160186 opts = opts or {}
161187 opts .fs_access = opts .fs_access or true
@@ -164,6 +190,7 @@ filetype.detect = function(filepath, opts)
164190 filepath = tostring (filepath )
165191 end
166192
193+ --- @type string ?
167194 local match = filetype .detect_from_name (filepath )
168195 if match ~= " " then
169196 return match
0 commit comments