@@ -3,6 +3,50 @@ local install = require("apidocs.install")
33
44Config = {}
55
6+ local function get_installed_docs (fs , opts )
7+ local installed_docs = {}
8+ if fs == nil then
9+ return installed_docs
10+ end
11+ while true do
12+ local name , type = vim .uv .fs_scandir_next (fs )
13+ if not name then
14+ break
15+ end
16+ if type == " directory" then
17+ if opts and opts .restrict_sources then
18+ for _ , source in ipairs (opts .restrict_sources ) do
19+ if name :match (" ^" .. source ) then
20+ table.insert (installed_docs , name )
21+ end
22+ end
23+ else
24+ table.insert (installed_docs , name )
25+ end
26+ end
27+ end
28+ return installed_docs
29+ end
30+
31+ local function ensure_installed (opts )
32+ local docs_path = common .data_folder ()
33+ local fs = vim .uv .fs_scandir (docs_path )
34+ local installed_docs = get_installed_docs (fs , opts )
35+ if opts and opts .ensure_installed then
36+ for _ , source in ipairs (opts .ensure_installed ) do
37+ if not vim .tbl_contains (installed_docs , source ) then
38+ if slugs_to_mtimes == nil then
39+ install .fetch_slugs_and_mtimes_and_then (function (slugs_to_mtimes )
40+ install .apidoc_install (source , slugs_to_mtimes )
41+ end )
42+ else
43+ install .apidoc_install (source , slugs_to_mtimes )
44+ end
45+ end
46+ end
47+ end
48+ end
49+
650local function set_picker (opts )
751 if opts and (opts .picker == " snacks" or opts .picker == " telescope" or opts .picker == " ui_select" ) then
852 return opts
@@ -31,28 +75,7 @@ local function apidocs_open(opts)
3175 local docs_path = common .data_folder ()
3276 local fs = vim .uv .fs_scandir (docs_path )
3377 local candidates = {}
34- local installed_docs = {}
35- while true do
36- local name , type = vim .uv .fs_scandir_next (fs )
37- if not name then
38- break
39- end
40- if type == " directory" then
41- if opts and opts .restrict_sources then
42- for _ , source in ipairs (opts .restrict_sources ) do
43- if vim .tbl_contains (opts .restrict_sources , name ) then
44- table.insert (installed_docs , name )
45- -- no exact match, check for partial match
46- -- e.g. "python" -> "python~3.12"
47- elseif name :match (" ^" .. source .. " ~%d+(%.%d+)?$" ) then
48- table.insert (installed_docs , name )
49- end
50- end
51- else
52- table.insert (installed_docs , name )
53- end
54- end
55- end
78+ local installed_docs = get_installed_docs (fs , opts )
5679
5780 if opts and opts .ensure_installed then
5881 for _ , source in ipairs (opts .ensure_installed ) do
135158
136159local function setup (conf )
137160 set_config (conf )
161+ ensure_installed (conf )
138162 vim .api .nvim_create_user_command (" ApidocsInstall" , install .apidocs_install , {})
139163 vim .api .nvim_create_user_command (" ApidocsOpen" , apidocs_open , {})
140164 vim .api .nvim_create_user_command (" ApidocsSearch" , apidocs_search , {})
0 commit comments