Skip to content

Commit d6f74a9

Browse files
committed
feat: add type annotations for plenary.filetype
1 parent 24e0407 commit d6f74a9

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

data/plenary/filetypes/base.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
---@type PlenaryFiletypeTable
12
return {
23
extension = {
34
['ncl'] = [[text]],

data/plenary/filetypes/builtin.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ local shebang_fts = {
1010
['zsh'] = 'zsh',
1111
}
1212

13+
---@type table<string, string>
1314
local shebang = {}
1415
for _, prefix in ipairs(shebang_prefixes) do
1516
for k, v in pairs(shebang_fts) do
1617
shebang[prefix .. k] = v
1718
end
1819
end
1920

21+
---@type PlenaryFiletypeTable
2022
return {
2123
extension = {
2224
['_coffee'] = 'coffee',

lua/plenary/filetype.lua

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@ local Path = require "plenary.path"
22

33
local os_sep = Path.path.sep
44

5+
---@class PlenaryFiletype
56
local filetype = {}
67

8+
---@class PlenaryFiletypeTable
9+
---@field file_name? table<string, string>
10+
---@field extension? table<string, string>
11+
---@field shebang? table<string, string>
712
local filetype_table = {
813
extension = {},
914
file_name = {},
1015
shebang = {},
1116
}
1217

18+
---@param new_filetypes PlenaryFiletypeTable
1319
filetype.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
4046
end
4147

48+
---@param filename string
4249
filetype.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)
5158
end
5259

5360
local filename_regex = "[^" .. os_sep .. "].*"
61+
---@param filename string
62+
---@return string[]
5463
filetype._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
6678
end
6779

80+
---@param tail string
81+
---@return string
6882
filetype._parse_modeline = function(tail)
6983
if tail:find "vim:" then
7084
return tail:match ".*:ft=([^: ]*):.*$" or ""
7185
end
7286
return ""
7387
end
7488

89+
---@param head string
90+
---@return string
7591
filetype._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
100116
end
101117

118+
---@param filepath string
119+
---@return string
102120
filetype.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 ""
119137
end
120138

139+
---@param filepath string
140+
---@return string
121141
filetype.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 ""
132152
end
133153

154+
---@param filepath string
155+
---@return string?
134156
filetype.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
144166
end
145167

168+
---@param filepath string
169+
---@return string
146170
filetype.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])
153177
end
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?
159185
filetype.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

lua/plenary/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
---@field context_manager PlenaryContextManager
66
---@field curl PlenaryCurl
77
---@field enum PlenaryEnum
8+
---@field filetype PlenaryFiletype
89
---@field functional PlenaryFunctional
910
---@field job PlenaryJob
1011
---@field json PlenaryJson

0 commit comments

Comments
 (0)