Skip to content

Commit 2d32d79

Browse files
authored
Merge branch 'main' into partial-matching
2 parents 7fc1c9d + de22f53 commit 2d32d79

File tree

3 files changed

+60
-26
lines changed

3 files changed

+60
-26
lines changed

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,20 @@ return {
4444
'nvim-telescope/telescope.nvim', -- or, 'folke/snacks.nvim'
4545
},
4646
cmd = { 'ApidocsSearch', 'ApidocsInstall', 'ApidocsOpen', 'ApidocsSelect', 'ApidocsUninstall' },
47+
-- If using ensure_installed (see below), uncomment the next line to trigger the
48+
-- installation when Neovim is opened
49+
-- lazy = false
4750
config = function()
48-
require('apidocs').setup()
49-
-- Picker will be auto-detected. To select a picker of your choice explicitly you can set picker by the configuration option 'picker':
50-
-- require('apidocs').setup({picker = "snacks"})
51-
-- Possible options are 'ui_select', 'telescope', and 'snacks'
51+
local opts = {
52+
-- Picker will be auto-detected. To select a picker of your choice explicitly you can set picker by the configuration option 'picker':
53+
-- Possible options are 'ui_select', 'telescope', and 'snacks'
54+
picker = "telescope",
55+
-- Use the below to make sure documentations for your languages are installed
56+
-- Note: best used with `lazy = false` (see above)
57+
-- ensure_installed = { "lua~5.4" }
58+
59+
}
60+
require('apidocs').setup(opts)
5261
end,
5362
keys = {
5463
{ '<leader>sad', '<cmd>ApidocsOpen<cr>', desc = 'Search Api Doc' },

lua/apidocs.lua

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,50 @@ local install = require("apidocs.install")
33

44
Config = {}
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+
650
local 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
@@ -135,6 +158,7 @@ end
135158

136159
local 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, {})

lua/apidocs/telescope.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ local function apidocs_search(opts)
105105
end
106106
local search_dirs = get_data_dirs(opts)
107107

108+
108109
local default_entry_maker = make_entry.gen_from_vimgrep()
109110
local function entry_maker(entry)
110111
local r = default_entry_maker(entry)

0 commit comments

Comments
 (0)