Avoid generating previews for device files

This commit is contained in:
Dmytro Meleshko 2021-11-27 12:53:04 +02:00
parent 81518cf6ae
commit ba1fe4fd51
1 changed files with 8 additions and 11 deletions

View File

@ -100,7 +100,7 @@ local function lines_from(file, count)
local bfile = assert(io.open(file, 'rb'))
local first_k = bfile:read(1024)
if first_k:find('\0') then
return {'binary file'}
return {'binary file'}
end
local lines = {'```'}
for line in first_k:gmatch("[^\r\n]+") do
@ -113,15 +113,6 @@ local function lines_from(file, count)
return lines
end
local function try_get_lines(file, count)
status, ret = pcall(lines_from, file, count)
if status then
return ret
else
return nil
end
end
source._candidates = function(_, params, dirname, offset, callback)
local fs, err = vim.loop.fs_scandir(dirname)
if err then
@ -211,7 +202,13 @@ end
function source:resolve(completion_item, callback)
if completion_item.kind == cmp.lsp.CompletionItemKind.File then
completion_item.documentation = try_get_lines(completion_item.data.path, defaults.max_lines)
local path = completion_item.data.path
if not vim.startswith(path, '/dev') then
local ok, preview_lines = pcall(lines_from, path, defaults.max_lines)
if ok then
completion_item.documentation = preview_lines
end
end
end
callback(completion_item)
end