-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathrepo_builtin.lua
148 lines (129 loc) · 4.21 KB
/
repo_builtin.lua
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
local actions = require'telescope.actions'
local actions_set = require'telescope.actions.set'
local actions_state = require'telescope.actions.state'
local conf = require'telescope.config'.values
local entry_display = require'telescope.pickers.entry_display'
local finders = require'telescope.finders'
local from_entry = require'telescope.from_entry'
local pickers = require'telescope.pickers'
local previewers = require'telescope.previewers'
local utils = require'telescope.utils'
local Path = require'plenary.path'
local os_home = vim.loop.os_homedir()
local M = {}
local function search_readme(dir)
for _, name in pairs{
'README', 'README.md', 'README.markdown', 'README.mkd',
} do
local file = dir / name
if file:is_file() then return file end
end
return nil
end
local function search_doc(dir)
local doc_path = Path:new(dir, 'doc', '**', '*.txt')
local maybe_doc = vim.split(vim.fn.glob(doc_path.filename), '\n')
for _, filepath in pairs(maybe_doc) do
local file = Path:new(filepath)
if file:is_file() then return file end
end
return nil
end
local function gen_from_ghq(opts)
local displayer = entry_display.create{
items = {{}},
}
local function make_display(entry)
local dir = (function(path)
if path == Path.path.root() then return path end
local p = Path:new(path)
if opts.tail_path then
local parts = p:_split()
return parts[#parts]
end
if opts.shorten_path then return p:shorten() end
if vim.startswith(path, opts.cwd) and path ~= opts.cwd then
return Path:new(p):make_relative(opts.cwd)
end
if vim.startswith(path, os_home) then
return (Path:new'~' / p:make_relative(os_home)).filename
end
return path
end)(entry.path)
return displayer{dir}
end
return function(line)
return {
value = line,
ordinal = line,
path = line,
display = make_display,
}
end
end
local function project_files(opts)
local ok = pcall(require'telescope.builtin'.git_files, opts)
if not ok then require'telescope.builtin'.find_files(opts) end
end
M.list = function(opts)
opts = opts or {}
opts.bin = opts.bin and vim.fn.expand(opts.bin) or 'fd'
opts.cwd = vim.env.HOME
opts.entry_maker = utils.get_lazy_default(opts.entry_maker, gen_from_ghq, opts)
local bin = vim.fn.expand(opts.bin)
local fd_command = {bin}
local repo_pattern = opts.pattern or [[^\.git$]]
-- Don’t filter only on directories with fd as git worktrees actually have a
-- .git file in them.
local find_repo_opts = {'--hidden', '--case-sensitive', '--absolute-path'}
local find_user_opts = opts.fd_opts or {}
local find_exec_opts = {'--exec', 'echo', [[{//}]], ';'}
local find_pattern_opts = {repo_pattern}
table.insert(fd_command, find_repo_opts)
table.insert(fd_command, find_user_opts)
table.insert(fd_command, find_exec_opts)
table.insert(fd_command, find_pattern_opts)
fd_command = vim.tbl_flatten(fd_command)
pickers.new(opts, {
prompt_title = 'Git repositories',
finder = finders.new_oneshot_job(
fd_command,
opts
),
previewer = previewers.new_termopen_previewer{
get_command = function(entry)
local dir = Path:new(from_entry.path(entry))
local doc = search_readme(dir)
local is_mardown
if doc then
is_mardown = true
else
-- TODO: doc may be previewed in a plain text. Can I use syntax highlight?
doc = search_doc(dir)
end
if doc then
if is_mardown and vim.fn.executable'glow' == 1 then
return {'glow', doc.filename}
elseif vim.fn.executable'bat' == 1 then
return {'bat', '--style', 'header,grid', doc.filename}
end
return {'cat', doc.filename}
end
return {'echo', ''}
end,
},
sorter = conf.file_sorter(opts),
attach_mappings = function(prompt_bufnr)
actions_set.select:replace(function(_, type)
local entry = actions_state.get_selected_entry()
local dir = from_entry.path(entry)
if type == 'default' then
actions._close(prompt_bufnr, true)
project_files{cwd = dir}
end
end)
return true
end,
}):find()
end
return M