@@ -3,6 +3,50 @@ local install = require("apidocs.install")
3
3
4
4
Config = {}
5
5
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
+
6
50
local function set_picker (opts )
7
51
if opts and (opts .picker == " snacks" or opts .picker == " telescope" or opts .picker == " ui_select" ) then
8
52
return opts
@@ -31,28 +75,7 @@ local function apidocs_open(opts)
31
75
local docs_path = common .data_folder ()
32
76
local fs = vim .uv .fs_scandir (docs_path )
33
77
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 )
56
79
57
80
if opts and opts .ensure_installed then
58
81
for _ , source in ipairs (opts .ensure_installed ) do
135
158
136
159
local function setup (conf )
137
160
set_config (conf )
161
+ ensure_installed (conf )
138
162
vim .api .nvim_create_user_command (" ApidocsInstall" , install .apidocs_install , {})
139
163
vim .api .nvim_create_user_command (" ApidocsOpen" , apidocs_open , {})
140
164
vim .api .nvim_create_user_command (" ApidocsSearch" , apidocs_search , {})
0 commit comments