-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrender.lua
More file actions
313 lines (279 loc) · 7.8 KB
/
Copy pathrender.lua
File metadata and controls
313 lines (279 loc) · 7.8 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
-- Render pipeline: turns a cppman page identity + lookup query + width into
-- rendered text lines.
-- Caches both successful renders and failures so re-renders / English-prose K presses
-- never re-spawn cppman. Owns no window state — viewer.lua handles UI.
local M = {}
local util = require("cppman.util")
local index = require("cppman.index")
local plugin_cache_dir = vim.fn.stdpath("cache") .. "/cppman_plugin"
local user_cache_home = vim.env.XDG_CACHE_HOME or (vim.fn.expand("~") .. "/.cache")
local _plugin_cache_dir_ready = false
local PAGE_CACHE_MAX = 64
local RESOLVE_CACHE_MAX = 256
-- LRU cache: { entries = { key = value }, order = { key1, key2, ... } }
-- Newest at the end. Evicts oldest when over max.
local function new_lru()
return { entries = {}, order = {}, max = 0 }
end
local function lru_get(cache, key)
return cache.entries[key]
end
local function lru_set(cache, key, value)
if cache.entries[key] == nil then
cache.order[#cache.order + 1] = key
if #cache.order > cache.max then
local oldest = table.remove(cache.order, 1)
cache.entries[oldest] = nil
end
end
cache.entries[key] = value
end
local page_cache = new_lru()
page_cache.max = PAGE_CACHE_MAX
local resolve_cache = new_lru()
resolve_cache.max = RESOLVE_CACHE_MAX
local _pager_script = nil
local function get_source(source)
return index.get_sources(source)[1]
end
local function ensure_plugin_cache_dir()
if not _plugin_cache_dir_ready then
vim.fn.mkdir(plugin_cache_dir, "p")
_plugin_cache_dir_ready = true
end
end
local function get_config_dir(source)
ensure_plugin_cache_dir()
local dir = plugin_cache_dir .. "/config_" .. source
local cppman_dir = dir .. "/cppman"
if vim.fn.isdirectory(cppman_dir) == 0 then
vim.fn.mkdir(cppman_dir, "p")
local cfg_path = cppman_dir .. "/cppman.cfg"
local cfg_content = "[Settings]\nSource = " .. source .. "\nUpdateManPath = false\nPager = vim\n"
local f, err = io.open(cfg_path, "w")
if f then
f:write(cfg_content)
f:close()
else
vim.notify("[cppman] failed to write " .. cfg_path .. ": " .. tostring(err), vim.log.levels.WARN)
end
end
return dir
end
local function cppman_args(extra)
local args = { "cppman" }
for i = 1, #extra do
args[#args + 1] = extra[i]
end
return args
end
local function run_cppman(source, args, stdin)
local ok, res = pcall(function()
return vim.system(args, {
env = {
XDG_CACHE_HOME = plugin_cache_dir,
XDG_CONFIG_HOME = get_config_dir(get_source(source)),
},
stdin = stdin,
text = true,
}):wait()
end)
if not ok then
return { code = -1, stdout = "", stderr = tostring(res) }
end
return res
end
local function normalize_page_name(name)
return name:gsub("/", "_")
end
local function get_cached_page_path(page, source)
source = get_source(source)
local filename = normalize_page_name(page) .. ".3.gz"
local candidates = {
plugin_cache_dir .. "/cppman/" .. source .. "/" .. filename,
user_cache_home .. "/cppman/" .. source .. "/" .. filename,
}
for i = 1, #candidates do
if vim.fn.filereadable(candidates[i]) == 1 then
return candidates[i]
end
end
return nil
end
local function get_pager_script()
if _pager_script ~= nil then
return _pager_script or nil
end
local pager = index.cppman_paths().pager
if pager then
_pager_script = pager
return _pager_script
end
for _, candidate in ipairs({
"/usr/lib/cppman/lib/pager.sh",
"/usr/local/lib/cppman/lib/pager.sh",
"/usr/share/cppman/pager.sh",
}) do
if vim.fn.filereadable(candidate) == 1 then
_pager_script = candidate
return _pager_script
end
end
_pager_script = false
return nil
end
local function render_cached_page(page_path, width, page)
local pager_script = get_pager_script()
if not pager_script then
return nil
end
local ok, res = pcall(function()
return vim.system({ pager_script, "pipe", page_path, tostring(width), "", page }, { text = true }):wait()
end)
if not ok or res.code ~= 0 or not res.stdout or res.stdout == "" then
return nil
end
return res.stdout
end
-- Single-pass: drop "Source set to" banner + leading blank lines.
local function strip_cppman_banner(lines)
if not (lines[1] and lines[1]:find("Source set to", 1, true) == 1) then
return lines
end
local first = 2
while lines[first] == "" do
first = first + 1
end
if first == 1 then
return lines
end
local out = {}
for i = first, #lines do
out[i - first + 1] = lines[i]
end
return out
end
-- Single-pass: strip disambiguation menu header ("Please enter the selection: [1]").
local function strip_disambiguation(lines)
for i = 1, #lines do
if lines[i]:find("Please enter the selection:", 1, true) then
local out = {}
for j = i + 1, #lines do
out[j - i] = lines[j]
end
return out
end
end
return lines
end
-- Render a cppman page. Cached pages are keyed by canonical page identity,
-- while uncached pages are fetched through cppman using the supplied lookup
-- query. Caches hits AND misses.
-- Returns (lines|nil, timing|nil). timing is nil on in-memory cache hits.
function M.render_page(page, query, width, source)
source = get_source(source)
query = query or page
local key = source .. "\0" .. page .. "\0" .. width
local cached = lru_get(page_cache, key)
if cached ~= nil then
return cached or nil, nil
end
local t0 = util.now_ms()
local stdout = nil
local page_path = get_cached_page_path(page, source)
if page_path then
stdout = render_cached_page(page_path, width, page)
end
if not stdout then
local res = run_cppman(source, cppman_args({ "--force-columns", tostring(width), query }), "1\n")
if res.code ~= 0 or not res.stdout or res.stdout == "" then
lru_set(page_cache, key, false)
return nil, {
cppman_ms = util.now_ms() - t0,
our_ms = 0,
total_ms = util.now_ms() - t0,
}
end
stdout = res.stdout
end
local cppman_ms = util.now_ms() - t0
local internal_t0 = util.now_ms()
local lines = vim.split(stdout, "\n", { plain = true })
if lines[#lines] == "" then
lines[#lines] = nil
end
lines = strip_cppman_banner(lines)
lines = strip_disambiguation(lines)
if #lines == 0 then
lru_set(page_cache, key, false)
return nil,
{
cppman_ms = cppman_ms,
our_ms = util.now_ms() - internal_t0,
total_ms = util.now_ms() - t0,
}
end
local our_ms = util.now_ms() - internal_t0
lru_set(page_cache, key, lines)
return lines, {
cppman_ms = cppman_ms,
our_ms = our_ms,
total_ms = util.now_ms() - t0,
}
end
function M.resolve_page(name, preferred, source)
source = get_source(source)
local key = source .. "\0" .. name .. "\0" .. (preferred or "")
local cached = lru_get(resolve_cache, key)
if cached ~= nil then
return cached or nil
end
local res = run_cppman(source, cppman_args({ "-f", name }))
if res.code ~= 0 then
lru_set(resolve_cache, key, false)
return nil
end
local preferred_lower = preferred and preferred:lower() or nil
local item = nil
for _, line in ipairs(vim.split(res.stdout or "", "\n", { plain = true })) do
local trimmed = vim.trim(line)
if trimmed ~= "" and trimmed:find("Source set to", 1, true) ~= 1 then
local query, page = line:match("^(.-) %- (.+)$")
query = query or line
page = page or line
if query ~= "" and page ~= "" then
local candidate = {
text = page,
page = page,
query = query,
source = source,
}
if not item then
item = candidate
end
if preferred_lower then
local query_lower = query:lower()
local page_lower = page:lower()
if query_lower:find(preferred_lower, 1, true) or page_lower:find(preferred_lower, 1, true) then
item = candidate
break
end
end
end
end
end
if not item then
lru_set(resolve_cache, key, false)
return nil
end
lru_set(resolve_cache, key, item)
return item
end
function M.reset()
page_cache = new_lru()
page_cache.max = PAGE_CACHE_MAX
resolve_cache = new_lru()
resolve_cache.max = RESOLVE_CACHE_MAX
_pager_script = nil
end
return M