Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add completion documentation for directories #54

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lua/cmp_path/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ end

source.resolve = function(self, completion_item, callback)
local data = completion_item.data
if data.stat and data.stat.type == 'file' then
if data.stat and (data.stat.type == "file" or data.stat.type == "directory") then
local ok, documentation = pcall(function()
return self:_get_documentation(data.path, constants.max_lines)
return self:_get_documentation(data.path, constants.max_lines, data.stat.type)
end)
if ok then
completion_item.documentation = documentation
Expand Down Expand Up @@ -202,8 +202,13 @@ source._validate_option = function(_, params)
return option
end

source._get_documentation = function(_, filename, count)
local binary = assert(io.open(filename, 'rb'))
source._get_documentation = function(_, path, count, data_stat_type)
local binary
if data_stat_type == "file" then
binary = assert(io.open(path, 'rb'))
elseif data_stat_type == "directory" then
binary = assert(io.popen('ls '..path))
end
local first_kb = binary:read(1024)
if first_kb:find('\0') then
return { kind = cmp.lsp.MarkupKind.PlainText, value = 'binary file' }
Expand All @@ -217,7 +222,7 @@ source._get_documentation = function(_, filename, count)
end
end

local filetype = vim.filetype.match({ filename = filename })
local filetype = vim.filetype.match({ filename = path })
if not filetype then
return { kind = cmp.lsp.MarkupKind.PlainText, value = table.concat(contents, '\n') }
end
Expand Down