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

Commit 30a20c6

Browse files
refactor: cleanup
* refactor: code cleanup Simplified pickers, some functions and made some cleanup. * update: additional changes Reordered symbols in `transpiler.lua`. * refactor: make paths global * refactor: remplace all paths Use the global paths to construct all paths.
1 parent 3603b8e commit 30a20c6

File tree

9 files changed

+136
-170
lines changed

9 files changed

+136
-170
lines changed

lua/nvim-devdocs/build.lua

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
1-
local path = require("plenary.path")
2-
31
local notify = require("nvim-devdocs.notify")
4-
local plugin_config = require("nvim-devdocs.config").get()
5-
local html_to_md = require("nvim-devdocs.transpiler").html_to_md
2+
local transpiler = require("nvim-devdocs.transpiler")
63

74
local function build_docs(entry, index, docs)
85
local alias = entry.slug:gsub("~", "-")
6+
local current_doc_dir = DOCS_DIR:joinpath(alias)
97

108
notify.log("Building " .. alias .. " documentation...")
119

12-
local docs_dir = path:new(plugin_config.dir_path, "docs")
13-
local current_doc_dir = path:new(docs_dir, alias)
14-
local index_path = path:new(plugin_config.dir_path, "index.json")
15-
local lock_path = path:new(plugin_config.dir_path, "docs-lock.json")
16-
17-
if not docs_dir:exists() then docs_dir:mkdir() end
10+
if not DOCS_DIR:exists() then DOCS_DIR:mkdir() end
11+
if not INDEX_PATH:exists() then INDEX_PATH:write("{}", "w") end
12+
if not LOCK_PATH:exists() then LOCK_PATH:write("{}", "w") end
1813
if not current_doc_dir:exists() then current_doc_dir:mkdir() end
19-
if not index_path:exists() then index_path:write("{}", "w") end
20-
if not lock_path:exists() then lock_path:write("{}", "w") end
2114

2215
local section_map = {}
2316
local path_map = {}
@@ -35,17 +28,14 @@ local function build_docs(entry, index, docs)
3528

3629
for key, doc in pairs(docs) do
3730
local sections = section_map[key]
38-
39-
local markdown, md_sections = html_to_md(doc, sections)
31+
local markdown, md_sections = transpiler.html_to_md(doc, sections)
32+
local file_path = current_doc_dir:joinpath(tostring(count) .. ".md")
4033

4134
for id, md_path in pairs(md_sections) do
4235
path_map[key .. "#" .. id] = count .. "," .. md_path
4336
end
4437

4538
path_map[key] = tostring(count)
46-
47-
local file_path = path:new(current_doc_dir, tostring(count) .. ".md")
48-
4939
file_path:write(markdown, "w")
5040
count = count + 1
5141
end
@@ -56,13 +46,13 @@ local function build_docs(entry, index, docs)
5646
index.entries[i].path = path_map[index_entry.path] or path_map[main]
5747
end
5848

59-
local index_parsed = vim.fn.json_decode(index_path:read())
49+
local index_parsed = vim.fn.json_decode(INDEX_PATH:read())
6050
index_parsed[alias] = index
61-
index_path:write(vim.fn.json_encode(index_parsed), "w")
51+
INDEX_PATH:write(vim.fn.json_encode(index_parsed), "w")
6252

63-
local lock_parsed = vim.fn.json_decode(lock_path:read())
53+
local lock_parsed = vim.fn.json_decode(LOCK_PATH:read())
6454
lock_parsed[alias] = entry
65-
lock_path:write(vim.fn.json_encode(lock_parsed), "w")
55+
LOCK_PATH:write(vim.fn.json_encode(lock_parsed), "w")
6656

6757
notify.log("Build complete!")
6858
end

lua/nvim-devdocs/completion.lua

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

3-
local path = require("plenary.path")
4-
53
local list = require("nvim-devdocs.list")
6-
local plugin_config = require("nvim-devdocs.config").get()
74

85
M.get_non_installed = function(arg_lead)
9-
local registery_path = path:new(plugin_config.dir_path, "registery.json")
10-
11-
if not registery_path:exists() then return {} end
6+
if not REGISTERY_PATH:exists() then return {} end
127

13-
local content = registery_path:read()
8+
local content = REGISTERY_PATH:read()
149
local parsed = vim.fn.json_decode(content)
1510
local installed = list.get_installed_alias()
1611
local args = {}

lua/nvim-devdocs/config.lua

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
local M = {}
22

3+
local path = require("plenary.path")
4+
35
local config = {
46
dir_path = vim.fn.stdpath("data") .. "/devdocs",
57
telescope = {},
@@ -19,7 +21,8 @@ local config = {
1921
mappings = {
2022
open_in_browser = "",
2123
},
22-
after_open = function() end,
24+
---@diagnostic disable-next-line: unused-local
25+
after_open = function(bufnr) end,
2326
}
2427

2528
M.get = function() return config end
@@ -31,7 +34,29 @@ M.setup = function(new_config)
3134
end
3235
end
3336

37+
DATA_DIR = path:new(config.dir_path)
38+
DOCS_DIR = DATA_DIR:joinpath("docs")
39+
INDEX_PATH = DATA_DIR:joinpath("index.json")
40+
LOCK_PATH = DATA_DIR:joinpath("docs-lock.json")
41+
REGISTERY_PATH = DATA_DIR:joinpath("registery.json")
42+
3443
return config
3544
end
3645

46+
M.set_keymaps = function(bufnr, entry)
47+
local slug = entry.alias:gsub("-", "~")
48+
local keymaps = config.mappings
49+
local set_buf_keymap = function(key, action, description)
50+
vim.keymap.set("n", key, action, { buffer = bufnr, desc = description })
51+
end
52+
53+
if type(keymaps.open_in_browser) == "string" and keymaps.open_in_browser ~= "" then
54+
set_buf_keymap(
55+
keymaps.open_in_browser,
56+
function() vim.ui.open("https://devdocs.io/" .. slug .. "/" .. entry.link) end,
57+
"Open in the browser"
58+
)
59+
end
60+
end
61+
3762
return M

lua/nvim-devdocs/init.lua

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,22 @@ M.uninstall_doc = function(args)
2626
end
2727
end
2828

29-
M.open_doc = function(args)
29+
M.open_doc = function(args, float)
3030
if vim.tbl_isempty(args.fargs) then
31-
pickers.global_search_picker(false)
31+
local entries = operations.get_all_entries()
32+
pickers.open_picker(entries, float)
3233
else
3334
local alias = args.fargs[1]
34-
pickers.open_picker(alias, false)
35+
pickers.open_picker_alias(alias, float)
3536
end
3637
end
3738

38-
M.open_doc_float = function(args)
39-
if vim.tbl_isempty(args.fargs) then
40-
pickers.global_search_picker(true)
41-
else
42-
local alias = args.fargs[1]
43-
pickers.open_picker(alias, true)
44-
end
45-
end
39+
M.open_doc_float = function(args) M.open_doc(args, true) end
4640

4741
M.open_doc_current_file = function(float)
4842
local filetype = vim.bo.filetype
4943
local alias = filetypes[filetype] or filetype
50-
pickers.open_picker(alias, float)
44+
pickers.open_picker_alias(alias, float)
5145
end
5246

5347
M.update = function(args)

lua/nvim-devdocs/list.lua

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
local M = {}
22

3-
local path = require("plenary.path")
4-
53
local notify = require("nvim-devdocs.notify")
6-
local plugin_config = require("nvim-devdocs.config").get()
7-
8-
local lock_path = path:new(plugin_config.dir_path, "docs-lock.json")
9-
local registery_path = path:new(plugin_config.dir_path, "registery.json")
104

115
M.get_installed_alias = function()
12-
if not lock_path:exists() then return {} end
6+
if not LOCK_PATH:exists() then return {} end
137

14-
local lockfile = lock_path:read()
8+
local lockfile = LOCK_PATH:read()
159
local lock_parsed = vim.fn.json_decode(lockfile)
1610
local installed = vim.tbl_keys(lock_parsed)
1711

1812
return installed
1913
end
2014

2115
M.get_installed_entry = function()
22-
if not registery_path:exists() then
16+
if not REGISTERY_PATH:exists() then
2317
notify.log_err("Devdocs registery not found, please run :DevdocsFetch")
2418
return
2519
end
2620

27-
local content = registery_path:read()
21+
local content = REGISTERY_PATH:read()
2822
local parsed = vim.fn.json_decode(content)
2923
local installed = M.get_installed_alias()
3024

@@ -39,12 +33,12 @@ M.get_installed_entry = function()
3933
end
4034

4135
M.get_updatable = function()
42-
if not registery_path:exists() or not lock_path:exists() then return {} end
36+
if not REGISTERY_PATH:exists() or not LOCK_PATH:exists() then return {} end
4337

4438
local results = {}
45-
local registery = registery_path:read()
39+
local registery = REGISTERY_PATH:read()
4640
local registery_parsed = vim.fn.json_decode(registery)
47-
local lockfile = lock_path:read()
41+
local lockfile = LOCK_PATH:read()
4842
local lock_parsed = vim.fn.json_decode(lockfile)
4943

5044
for alias, value in pairs(lock_parsed) do

lua/nvim-devdocs/operations.lua

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,28 @@ local M = {}
22

33
local job = require("plenary.job")
44
local curl = require("plenary.curl")
5-
local path = require("plenary.path")
6-
local state = require("nvim-devdocs.state")
75

86
local list = require("nvim-devdocs.list")
7+
local state = require("nvim-devdocs.state")
98
local notify = require("nvim-devdocs.notify")
10-
local plugin_config = require("nvim-devdocs.config").get()
9+
local config = require("nvim-devdocs.config")
1110
local build_docs = require("nvim-devdocs.build")
1211

1312
local devdocs_site_url = "https://devdocs.io"
1413
local devdocs_cdn_url = "https://documents.devdocs.io"
15-
local docs_dir = path:new(plugin_config.dir_path, "docs")
16-
local lock_path = path:new(plugin_config.dir_path, "docs-lock.json")
17-
local registery_path = path:new(plugin_config.dir_path, "registery.json")
18-
local index_path = path:new(plugin_config.dir_path, "index.json")
14+
15+
local plugin_config = config.get()
1916

2017
M.fetch = function()
2118
notify.log("Fetching DevDocs registery...")
2219

2320
curl.get(devdocs_site_url .. "/docs.json", {
2421
headers = {
25-
["User-agent"] = "chrome",
22+
["User-agent"] = "chrome", -- fake user agent, see #25
2623
},
2724
callback = function(response)
28-
local dir_path = path:new(plugin_config.dir_path)
29-
local file_path = path:new(plugin_config.dir_path, "registery.json")
30-
31-
if not dir_path:exists() then dir_path:mkdir() end
32-
33-
file_path:write(response.body, "w", 438)
34-
25+
if not DATA_DIR:exists() then DATA_DIR:mkdir() end
26+
REGISTERY_PATH:write(response.body, "w", 438)
3527
notify.log("DevDocs registery has been written to the disk")
3628
end,
3729
on_error = function(error)
@@ -41,7 +33,7 @@ M.fetch = function()
4133
end
4234

4335
M.install = function(entry, verbose, is_update)
44-
if not registery_path:exists() then
36+
if not REGISTERY_PATH:exists() then
4537
if verbose then notify.log_err("DevDocs registery not found, please run :DevdocsFetch") end
4638
return
4739
end
@@ -98,13 +90,13 @@ M.install = function(entry, verbose, is_update)
9890
end
9991

10092
M.install_args = function(args, verbose, is_update)
101-
if not registery_path:exists() then
93+
if not REGISTERY_PATH:exists() then
10294
if verbose then notify.log_err("DevDocs registery not found, please run :DevdocsFetch") end
10395
return
10496
end
10597

10698
local updatable = list.get_updatable()
107-
local content = registery_path:read()
99+
local content = REGISTERY_PATH:read()
108100
local parsed = vim.fn.json_decode(content)
109101

110102
for _, arg in ipairs(args) do
@@ -136,15 +128,15 @@ M.uninstall = function(alias)
136128
if not vim.tbl_contains(installed, alias) then
137129
notify.log(alias .. " documentation is already uninstalled")
138130
else
139-
local index = vim.fn.json_decode(index_path:read())
140-
local lockfile = vim.fn.json_decode(lock_path:read())
141-
local doc_path = path:new(docs_dir, alias)
131+
local index = vim.fn.json_decode(INDEX_PATH:read())
132+
local lockfile = vim.fn.json_decode(LOCK_PATH:read())
133+
local doc_path = DOCS_DIR:joinpath(alias)
142134

143135
index[alias] = nil
144136
lockfile[alias] = nil
145137

146-
index_path:write(vim.fn.json_encode(index), "w")
147-
lock_path:write(vim.fn.json_encode(lockfile), "w")
138+
INDEX_PATH:write(vim.fn.json_encode(index), "w")
139+
LOCK_PATH:write(vim.fn.json_encode(lockfile), "w")
148140
doc_path:rm({ recursive = true })
149141

150142
notify.log(alias .. " documentation has been uninstalled")
@@ -155,9 +147,9 @@ M.get_entries = function(alias)
155147
local installed = list.get_installed_alias()
156148
local is_installed = vim.tbl_contains(installed, alias)
157149

158-
if not index_path:exists() or not is_installed then return end
150+
if not INDEX_PATH:exists() or not is_installed then return end
159151

160-
local index_parsed = vim.fn.json_decode(index_path:read())
152+
local index_parsed = vim.fn.json_decode(INDEX_PATH:read())
161153
local entries = index_parsed[alias].entries
162154

163155
for key, _ in ipairs(entries) do
@@ -168,10 +160,10 @@ M.get_entries = function(alias)
168160
end
169161

170162
M.get_all_entries = function()
171-
if not index_path:exists() then return {} end
163+
if not INDEX_PATH:exists() then return {} end
172164

173165
local entries = {}
174-
local index_parsed = vim.fn.json_decode(index_path:read())
166+
local index_parsed = vim.fn.json_decode(INDEX_PATH:read())
175167

176168
for alias, index in pairs(index_parsed) do
177169
for _, doc_entry in ipairs(index.entries) do
@@ -215,7 +207,7 @@ M.filter_doc = function(lines, pattern)
215207
end
216208

217209
M.render_cmd = function(bufnr, is_picker)
218-
vim.bo[bufnr].ft = "glow"
210+
vim.bo[bufnr].ft = plugin_config.previewer_cmd
219211

220212
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
221213
local chan = vim.api.nvim_open_term(bufnr, {})
@@ -247,8 +239,8 @@ M.open = function(entry, bufnr, float)
247239
local col = (ui.width - plugin_config.float_win.width) * 0.5
248240
local float_opts = plugin_config.float_win
249241

250-
if not plugin_config.row then float_opts.row = row end
251-
if not plugin_config.col then float_opts.col = col end
242+
float_opts.row = plugin_config.row or row
243+
float_opts.col = plugin_config.col or col
252244

253245
local win = nil
254246
local last_win = state.get("last_win")
@@ -275,20 +267,7 @@ M.open = function(entry, bufnr, float)
275267
vim.bo[bufnr].ft = "markdown"
276268
end
277269

278-
local slug = entry.alias:gsub("-", "~")
279-
local keymaps = plugin_config.mappings
280-
local set_buf_keymap = function(key, action, description)
281-
vim.keymap.set("n", key, action, { buffer = bufnr, desc = description })
282-
end
283-
284-
if type(keymaps.open_in_browser) == "string" and keymaps.open_in_browser ~= "" then
285-
set_buf_keymap(
286-
keymaps.open_in_browser,
287-
function() vim.ui.open("https://devdocs.io/" .. slug .. "/" .. entry.link) end,
288-
"Open in the browser"
289-
)
290-
end
291-
270+
config.set_keymaps(bufnr, entry)
292271
plugin_config.after_open(bufnr)
293272
end
294273

0 commit comments

Comments
 (0)