Skip to content

Commit 3cd63d9

Browse files
committed
feat(utils): use vim.filetype.match
1 parent 89ad53d commit 3cd63d9

2 files changed

Lines changed: 2 additions & 92 deletions

File tree

lua/neoplen/path.lua

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ end
126126

127127
-- TODO: Asyncify this and use vim.wait in the meantime.
128128
-- This will allow other events to happen while we're waiting!
129-
function Path:_read()
129+
function Path:read()
130130
self = check_self(self)
131131

132132
local fd = assert(uv.fs_open(self:_fs_filename(), "r", 438)) -- for some reason test won't pass with absolute
@@ -184,13 +184,6 @@ function Path:_read_async(callback)
184184
end)
185185
end
186186

187-
function Path:read(callback)
188-
if callback then
189-
return self:_read_async(callback)
190-
end
191-
return self:_read()
192-
end
193-
194187
function Path:readlines()
195188
self = check_self(self)
196189

@@ -200,41 +193,4 @@ function Path:readlines()
200193
return vim.split(data, "\n")
201194
end
202195

203-
function Path:readbyterange(offset, length)
204-
self = check_self(self)
205-
206-
local fd = uv.fs_open(self:_fs_filename(), "r", 438)
207-
if not fd then
208-
return
209-
end
210-
local stat = assert(uv.fs_fstat(fd))
211-
if stat.type ~= "file" then
212-
uv.fs_close(fd)
213-
return nil
214-
end
215-
216-
if offset < 0 then
217-
offset = stat.size + offset
218-
-- Windows fails if offset is < 0 even though offset is defined as signed
219-
-- http://docs.libuv.org/en/v1.x/fs.html#c.uv_fs_read
220-
if offset < 0 then
221-
offset = 0
222-
end
223-
end
224-
225-
local data = ""
226-
while #data < length do
227-
local read_chunk = assert(uv.fs_read(fd, length - #data, offset))
228-
if #read_chunk == 0 then
229-
break
230-
end
231-
data = data .. read_chunk
232-
offset = offset + #read_chunk
233-
end
234-
235-
assert(uv.fs_close(fd))
236-
237-
return data
238-
end
239-
240196
return Path

lua/telescope/previewers/utils.lua

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,11 @@ local strings = require "neoplen.strings"
55
local conf = require("telescope.config").values
66

77
local Job = require "neoplen.job"
8-
local Path = require "neoplen.path"
98

109
local utils = {}
1110

12-
local detect_from_shebang = function(p)
13-
local s = p:readbyterange(0, 256)
14-
if s then
15-
local lines = ts_utils.split_lines(s)
16-
return vim.filetype.match { contents = lines }
17-
end
18-
end
19-
20-
local parse_modeline = function(tail)
21-
if tail:find "vim:" then
22-
return tail:match ".*:ft=([^: ]*):.*$" or ""
23-
end
24-
end
25-
26-
local detect_from_modeline = function(p)
27-
local s = p:readbyterange(-256, 256)
28-
if s then
29-
local lines = ts_utils.split_lines(s)
30-
local idx = lines[#lines] ~= "" and #lines or #lines - 1
31-
if idx >= 1 then
32-
return parse_modeline(lines[idx])
33-
end
34-
end
35-
end
36-
3711
utils.filetype_detect = function(filepath)
38-
if type(filepath) ~= string then
39-
filepath = tostring(filepath)
40-
end
41-
42-
local match = vim.filetype.match { filename = filepath }
43-
if match and match ~= "" then
44-
return match
45-
end
46-
47-
local p = Path:new(filepath)
48-
if p and p:is_file() then
49-
match = detect_from_shebang(p)
50-
if match and match ~= "" then
51-
return match
52-
end
53-
54-
match = detect_from_modeline(p)
55-
if match and match ~= "" then
56-
return match
57-
end
58-
end
12+
return vim.filetype.match { filename = filepath }
5913
end
6014

6115
-- API helper functions for buffer previewer

0 commit comments

Comments
 (0)