Skip to content
This repository was archived by the owner on May 16, 2024. It is now read-only.

Commit a9ece8d

Browse files
refactor: add docs & type annotations
Added code documentation and some explanations.
1 parent 26895a8 commit a9ece8d

File tree

9 files changed

+117
-44
lines changed

9 files changed

+117
-44
lines changed

lua/nvim-devdocs/build.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
local notify = require("nvim-devdocs.notify")
22
local transpiler = require("nvim-devdocs.transpiler")
33

4+
---@param entry RegisteryEntry
5+
---@param index any
6+
---@param docs table<string, string>
47
local function build_docs(entry, index, docs)
58
local alias = entry.slug:gsub("~", "-")
69
local current_doc_dir = DOCS_DIR:joinpath(alias)

lua/nvim-devdocs/completion.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ local M = {}
22

33
local list = require("nvim-devdocs.list")
44

5+
---@param arg_lead string
6+
---@return string[]
57
M.get_non_installed = function(arg_lead)
68
if not REGISTERY_PATH:exists() then return {} end
79

@@ -19,6 +21,8 @@ M.get_non_installed = function(arg_lead)
1921
return args
2022
end
2123

24+
---@param arg_lead string
25+
---@return string[]
2226
M.get_installed = function(arg_lead)
2327
local installed = list.get_installed_alias()
2428
local args = vim.tbl_filter(function(entry)
@@ -30,6 +34,8 @@ M.get_installed = function(arg_lead)
3034
return args
3135
end
3236

37+
---@param arg_lead string
38+
---@return string[]
3339
M.get_updatable = function(arg_lead)
3440
local updatable = list.get_updatable()
3541
local args = vim.tbl_filter(function(entry)

lua/nvim-devdocs/config.lua

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ local M = {}
22

33
local path = require("plenary.path")
44

5-
local config = {
5+
---@class nvim_devdocs.Config
6+
local default = {
67
dir_path = vim.fn.stdpath("data") .. "/devdocs",
78
telescope = {},
89
float_win = {
@@ -25,27 +26,26 @@ local config = {
2526
after_open = function(bufnr) end,
2627
}
2728

28-
M.get = function() return config end
29+
---@class nvim_devdocs.Config
30+
M.options = {}
2931

3032
M.setup = function(new_config)
31-
if new_config ~= nil then
32-
for key, value in pairs(new_config) do
33-
config[key] = value
34-
end
35-
end
33+
M.options = vim.tbl_deep_extend("force", default, new_config or {})
3634

37-
DATA_DIR = path:new(config.dir_path)
35+
DATA_DIR = path:new(default.dir_path)
3836
DOCS_DIR = DATA_DIR:joinpath("docs")
3937
INDEX_PATH = DATA_DIR:joinpath("index.json")
4038
LOCK_PATH = DATA_DIR:joinpath("docs-lock.json")
4139
REGISTERY_PATH = DATA_DIR:joinpath("registery.json")
4240

43-
return config
41+
return default
4442
end
4543

44+
---@param bufnr number
45+
---@param entry DocEntry
4646
M.set_keymaps = function(bufnr, entry)
4747
local slug = entry.alias:gsub("-", "~")
48-
local keymaps = config.mappings
48+
local keymaps = M.options.mappings
4949
local set_buf_keymap = function(key, action, description)
5050
vim.keymap.set("n", key, action, { buffer = bufnr, desc = description })
5151
end

lua/nvim-devdocs/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ end
7575
M.setup = function(opts)
7676
config.setup(opts)
7777

78-
local ensure_installed = config.get().ensure_installed
78+
local ensure_installed = config.options.ensure_installed
7979

8080
vim.defer_fn(function() operations.install_args(ensure_installed) end, 3000)
8181

lua/nvim-devdocs/list.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local M = {}
22

33
local notify = require("nvim-devdocs.notify")
44

5+
---@return string[]
56
M.get_installed_alias = function()
67
if not LOCK_PATH:exists() then return {} end
78

@@ -12,6 +13,7 @@ M.get_installed_alias = function()
1213
return installed
1314
end
1415

16+
---@return RegisteryEntry[] | nil
1517
M.get_installed_entry = function()
1618
if not REGISTERY_PATH:exists() then
1719
notify.log_err("Devdocs registery not found, please run :DevdocsFetch")
@@ -32,6 +34,7 @@ M.get_installed_entry = function()
3234
return results
3335
end
3436

37+
---@return string[]
3538
M.get_updatable = function()
3639
if not REGISTERY_PATH:exists() or not LOCK_PATH:exists() then return {} end
3740

lua/nvim-devdocs/operations.lua

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ local build_docs = require("nvim-devdocs.build")
1212
local devdocs_site_url = "https://devdocs.io"
1313
local devdocs_cdn_url = "https://documents.devdocs.io"
1414

15-
local plugin_config = config.get()
16-
1715
M.fetch = function()
1816
notify.log("Fetching DevDocs registery...")
1917

@@ -32,6 +30,9 @@ M.fetch = function()
3230
})
3331
end
3432

33+
---@param entry RegisteryEntry
34+
---@param verbose? boolean
35+
---@param is_update? boolean
3536
M.install = function(entry, verbose, is_update)
3637
if not REGISTERY_PATH:exists() then
3738
if verbose then notify.log_err("DevDocs registery not found, please run :DevdocsFetch") end
@@ -89,6 +90,9 @@ M.install = function(entry, verbose, is_update)
8990
end
9091
end
9192

93+
---@param args string[]
94+
---@param verbose? boolean
95+
---@param is_update? boolean
9296
M.install_args = function(args, verbose, is_update)
9397
if not REGISTERY_PATH:exists() then
9498
if verbose then notify.log_err("DevDocs registery not found, please run :DevdocsFetch") end
@@ -122,6 +126,7 @@ M.install_args = function(args, verbose, is_update)
122126
end
123127
end
124128

129+
---@param alias string
125130
M.uninstall = function(alias)
126131
local installed = list.get_installed_alias()
127132

@@ -143,6 +148,8 @@ M.uninstall = function(alias)
143148
end
144149
end
145150

151+
---@param alias string
152+
---@return DocEntry[] | nil
146153
M.get_entries = function(alias)
147154
local installed = list.get_installed_alias()
148155
local is_installed = vim.tbl_contains(installed, alias)
@@ -159,6 +166,7 @@ M.get_entries = function(alias)
159166
return entries
160167
end
161168

169+
---@return DocEntry[]
162170
M.get_all_entries = function()
163171
if not INDEX_PATH:exists() then return {} end
164172

@@ -180,6 +188,8 @@ M.get_all_entries = function()
180188
return entries
181189
end
182190

191+
---@param entry DocEntry
192+
---@param callback function
183193
M.read_entry = function(entry, callback)
184194
local splited_path = vim.split(entry.path, ",")
185195
local file = splited_path[1]
@@ -194,7 +204,10 @@ M.read_entry = function(entry, callback)
194204
end))
195205
end
196206

197-
-- if we have a pattern to search for, only consider lines after the pattern
207+
---if we have a pattern to search for, only consider lines after the pattern
208+
---@param lines string[]
209+
---@param pattern? string
210+
---@return string[]
198211
M.filter_doc = function(lines, pattern)
199212
if not pattern then return lines end
200213

@@ -224,14 +237,16 @@ M.filter_doc = function(lines, pattern)
224237
return filtered_lines
225238
end
226239

240+
---@param bufnr number
241+
---@param is_picker? boolean
227242
M.render_cmd = function(bufnr, is_picker)
228-
vim.bo[bufnr].ft = plugin_config.previewer_cmd
243+
vim.bo[bufnr].ft = config.options.previewer_cmd
229244

230245
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
231246
local chan = vim.api.nvim_open_term(bufnr, {})
232-
local args = is_picker and plugin_config.picker_cmd_args or plugin_config.cmd_args
247+
local args = is_picker and config.options.picker_cmd_args or config.options.cmd_args
233248
local previewer = job:new({
234-
command = plugin_config.previewer_cmd,
249+
command = config.options.previewer_cmd,
235250
args = args,
236251
on_stdout = vim.schedule_wrap(function(_, data)
237252
if not data then return end
@@ -246,19 +261,22 @@ M.render_cmd = function(bufnr, is_picker)
246261
previewer:start()
247262
end
248263

264+
---@param entry DocEntry
265+
---@param bufnr number
266+
---@param float boolean
249267
M.open = function(entry, bufnr, float)
250268
vim.api.nvim_buf_set_option(bufnr, "modifiable", false)
251269

252270
if not float then
253271
vim.api.nvim_set_current_buf(bufnr)
254272
else
255273
local ui = vim.api.nvim_list_uis()[1]
256-
local row = (ui.height - plugin_config.float_win.height) * 0.5
257-
local col = (ui.width - plugin_config.float_win.width) * 0.5
258-
local float_opts = plugin_config.float_win
274+
local row = (ui.height - config.options.float_win.height) * 0.5
275+
local col = (ui.width - config.options.float_win.width) * 0.5
276+
local float_opts = config.options.float_win
259277

260-
float_opts.row = plugin_config.row or row
261-
float_opts.col = plugin_config.col or col
278+
float_opts.row = config.options.float_win.row or row
279+
float_opts.col = config.options.float_win.col or col
262280
float_opts.zindex = 10
263281

264282
local win = nil
@@ -272,15 +290,15 @@ M.open = function(entry, bufnr, float)
272290
state.set("last_win", win)
273291
end
274292

275-
vim.wo[win].wrap = plugin_config.wrap
276-
vim.wo[win].linebreak = plugin_config.wrap
293+
vim.wo[win].wrap = config.options.wrap
294+
vim.wo[win].linebreak = config.options.wrap
277295
vim.wo[win].nu = false
278296
vim.wo[win].relativenumber = false
279297
end
280298

281-
local ignore = vim.tbl_contains(plugin_config.cmd_ignore, entry.alias)
299+
local ignore = vim.tbl_contains(config.options.cmd_ignore, entry.alias)
282300

283-
if plugin_config.previewer_cmd and not ignore then
301+
if config.options.previewer_cmd and not ignore then
284302
M.render_cmd(bufnr)
285303
else
286304
vim.bo[bufnr].ft = "markdown"
@@ -289,9 +307,10 @@ M.open = function(entry, bufnr, float)
289307
vim.bo[bufnr].keywordprg = ":DevdocsKeywordprg"
290308

291309
config.set_keymaps(bufnr, entry)
292-
plugin_config.after_open(bufnr)
310+
config.options.after_open(bufnr)
293311
end
294312

313+
---@param keyword string
295314
M.keywordprg = function(keyword)
296315
local alias = state.get("current_doc")
297316
local float = state.get("last_mode") == "float"

lua/nvim-devdocs/pickers.lua

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ local notify = require("nvim-devdocs.notify")
1313
local operations = require("nvim-devdocs.operations")
1414
local transpiler = require("nvim-devdocs.transpiler")
1515
local plugin_state = require("nvim-devdocs.state")
16-
local plugin_config = require("nvim-devdocs.config").get()
16+
local plugin_config = require("nvim-devdocs.config")
1717

18-
local new_docs_picker = function(prompt, entries, previwer, attach)
19-
return pickers.new(plugin_config.telescope, {
18+
local new_docs_picker = function(prompt, entries, previewer, attach)
19+
return pickers.new(plugin_config.options.telescope, {
2020
prompt_title = prompt,
2121
finder = finders.new_table({
2222
results = entries,
@@ -28,8 +28,8 @@ local new_docs_picker = function(prompt, entries, previwer, attach)
2828
}
2929
end,
3030
}),
31-
sorter = config.generic_sorter(plugin_config.telescope),
32-
previewer = previwer,
31+
sorter = config.generic_sorter(plugin_config.options.telescope),
32+
previewer = previewer,
3333
attach_mappings = attach,
3434
})
3535
end
@@ -55,7 +55,7 @@ local doc_previewer = previewers.new_buffer_previewer({
5555
operations.read_entry(entry.value, function(filtered_lines)
5656
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, filtered_lines)
5757

58-
if plugin_config.previewer_cmd and plugin_config.picker_cmd then
58+
if plugin_config.options.previewer_cmd and plugin_config.options.picker_cmd then
5959
plugin_state.set("preview_lines", filtered_lines)
6060
operations.render_cmd(bufnr, true)
6161
else
@@ -68,7 +68,7 @@ local doc_previewer = previewers.new_buffer_previewer({
6868
local open_doc = function(selection, float)
6969
local bufnr = nil
7070

71-
if plugin_config.picker_cmd then
71+
if plugin_config.options.picker_cmd then
7272
bufnr = vim.api.nvim_create_buf(false, true)
7373
local lines = plugin_state.get("preview_lines")
7474
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
@@ -138,8 +138,10 @@ M.update_picker = function()
138138
picker:find()
139139
end
140140

141+
---@param entries DocEntry[]
142+
---@param float? boolean
141143
M.open_picker = function(entries, float)
142-
local picker = pickers.new(plugin_config.telescope, {
144+
local picker = pickers.new(plugin_config.options.telescope, {
143145
prompt_title = "Select an entry",
144146
finder = finders.new_table({
145147
results = entries,
@@ -151,7 +153,7 @@ M.open_picker = function(entries, float)
151153
}
152154
end,
153155
}),
154-
sorter = config.generic_sorter(plugin_config.telescope),
156+
sorter = config.generic_sorter(plugin_config.options.telescope),
155157
previewer = doc_previewer,
156158
attach_mappings = function()
157159
actions.select_default:replace(function(prompt_bufnr)
@@ -172,6 +174,8 @@ M.open_picker = function(entries, float)
172174
picker:find()
173175
end
174176

177+
---@param alias string
178+
---@param float? boolean
175179
M.open_picker_alias = function(alias, float)
176180
local entries = operations.get_entries(alias)
177181

0 commit comments

Comments
 (0)