-
-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathfiletype.lua
More file actions
196 lines (168 loc) · 4.74 KB
/
filetype.lua
File metadata and controls
196 lines (168 loc) · 4.74 KB
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
local Path = require "plenary.path"
local os_sep = Path.path.sep
local filetype = {}
local filetype_table = {
extension = {},
file_name = {},
shebang = {},
}
filetype.add_table = function(new_filetypes)
local valid_keys = { "extension", "file_name", "shebang" }
local new_keys = {}
-- Validate keys
for k, _ in pairs(new_filetypes) do
new_keys[k] = true
end
for _, k in ipairs(valid_keys) do
new_keys[k] = nil
end
for k, v in pairs(new_keys) do
error(debug.traceback("Invalid key / value:" .. tostring(k) .. " / " .. tostring(v)))
end
if new_filetypes.extension then
filetype_table.extension = vim.tbl_extend("force", filetype_table.extension, new_filetypes.extension)
end
if new_filetypes.file_name then
filetype_table.file_name = vim.tbl_extend("force", filetype_table.file_name, new_filetypes.file_name)
end
if new_filetypes.shebang then
filetype_table.shebang = vim.tbl_extend("force", filetype_table.shebang, new_filetypes.shebang)
end
end
filetype.add_file = function(filename)
local filetype_files = vim.api.nvim_get_runtime_file(string.format("data/plenary/filetypes/%s.lua", filename), true)
for _, file in ipairs(filetype_files) do
local ok, msg = pcall(filetype.add_table, dofile(file))
if not ok then
error("Unable to add file " .. file .. ":\n" .. msg)
end
end
end
local filename_regex = "[^" .. os_sep .. "].*"
filetype._get_extension_parts = function(filename)
local current_match = filename:match(filename_regex)
local possibilities = {}
while current_match do
current_match = current_match:match "[^.]%.(.*)"
if current_match then
table.insert(possibilities, current_match:lower())
else
return possibilities
end
end
return possibilities
end
filetype._parse_modeline = function(tail)
if tail:find "vim:" then
return tail:match ".*:ft=([^: ]*):.*$" or ""
end
return ""
end
filetype._parse_shebang = function(head)
if head:sub(1, 2) == "#!" then
local match = filetype_table.shebang[head:sub(3, #head)]
if match then
return match
end
end
return ""
end
local done_adding = false
local extend_tbl_with_ext_eq_ft_entries = function()
if not done_adding then
if vim.in_fast_event() then
return
end
local all_valid_filetypes = vim.fn.getcompletion("", "filetype")
for _, v in ipairs(all_valid_filetypes) do
if not filetype_table.extension[v] then
filetype_table.extension[v] = v
end
end
done_adding = true
return true
end
end
filetype.detect_from_extension = function(filepath)
local exts = filetype._get_extension_parts(filepath)
for _, ext in ipairs(exts) do
local match = ext and filetype_table.extension[ext]
if match then
return match
end
end
if extend_tbl_with_ext_eq_ft_entries() then
for _, ext in ipairs(exts) do
local match = ext and filetype_table.extension[ext]
if match then
return match
end
end
end
return ""
end
filetype.detect_from_name = function(filepath)
if filepath then
filepath = filepath:lower()
local split_path = vim.split(filepath, os_sep, true)
local fname = split_path[#split_path]
local match = filetype_table.file_name[fname]
if match then
return match
end
end
return ""
end
filetype.detect_from_modeline = function(filepath)
local tail = Path:new(filepath):readbyterange(-256, 256)
if not tail then
return ""
end
local lines = vim.split(tail, "\n")
local idx = lines[#lines] ~= "" and #lines or #lines - 1
if idx >= 1 then
return filetype._parse_modeline(lines[idx])
end
end
filetype.detect_from_shebang = function(filepath)
local head = Path:new(filepath):readbyterange(0, 256)
if not head then
return ""
end
local lines = vim.split(head, "\n")
return filetype._parse_shebang(lines[1])
end
--- Detect a filetype from a path.
---
---@param opts table: Table with optional keys
--- - fs_access (bool, default=true): Should check a file if it exists
filetype.detect = function(filepath, opts)
opts = opts or {}
opts.fs_access = opts.fs_access or true
if type(filepath) ~= string then
filepath = tostring(filepath)
end
local match = filetype.detect_from_name(filepath)
if match ~= "" then
return match
end
match = filetype.detect_from_extension(filepath)
if opts.fs_access and Path:new(filepath):exists() then
if match == "" then
match = filetype.detect_from_shebang(filepath)
if match ~= "" then
return match
end
end
if match == "text" or match == "" then
match = filetype.detect_from_modeline(filepath)
if match ~= "" then
return match
end
end
end
return match
end
filetype.add_file "base"
filetype.add_file "builtin"
return filetype