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

Commit

Permalink
Browse files Browse the repository at this point in the history
…vdocs into refactor
  • Loading branch information
luckasRanarison committed Sep 15, 2023
2 parents 3603b8e + 33565a0 commit 97f5896
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 121 deletions.
16 changes: 7 additions & 9 deletions lua/nvim-devdocs/build.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
local path = require("plenary.path")

local notify = require("nvim-devdocs.notify")
local plugin_config = require("nvim-devdocs.config").get()
local html_to_md = require("nvim-devdocs.transpiler").html_to_md
local config = require("nvim-devdocs.config")
local transpiler = require("nvim-devdocs.transpiler")

local function build_docs(entry, index, docs)
local alias = entry.slug:gsub("~", "-")

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

local docs_dir = path:new(plugin_config.dir_path, "docs")
local current_doc_dir = path:new(docs_dir, alias)
local index_path = path:new(plugin_config.dir_path, "index.json")
local lock_path = path:new(plugin_config.dir_path, "docs-lock.json")
local docs_dir = config.new_path("docs")
local current_doc_dir = config.new_path("docs", alias)
local index_path = config.new_path("index.json")
local lock_path = config.new_path("docs-lock.json")

if not docs_dir:exists() then docs_dir:mkdir() end
if not current_doc_dir:exists() then current_doc_dir:mkdir() end
Expand All @@ -35,8 +34,7 @@ local function build_docs(entry, index, docs)

for key, doc in pairs(docs) do
local sections = section_map[key]

local markdown, md_sections = html_to_md(doc, sections)
local markdown, md_sections = transpiler.html_to_md(doc, sections)

for id, md_path in pairs(md_sections) do
path_map[key .. "#" .. id] = count .. "," .. md_path
Expand Down
29 changes: 28 additions & 1 deletion lua/nvim-devdocs/config.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
local M = {}

local path = require("plenary.path")

local config = {
dir_path = vim.fn.stdpath("data") .. "/devdocs",
telescope = {},
Expand All @@ -19,7 +21,8 @@ local config = {
mappings = {
open_in_browser = "",
},
after_open = function() end,
---@diagnostic disable-next-line: unused-local
after_open = function(bufnr) end,
}

M.get = function() return config end
Expand All @@ -34,4 +37,28 @@ M.setup = function(new_config)
return config
end

M.new_path = function(...)
if ... then
return path:new(config.dir_path, ...)
else
return path:new(config.dir_path)
end
end

M.set_keymaps = function(bufnr, entry)
local slug = entry.alias:gsub("-", "~")
local keymaps = config.mappings
local set_buf_keymap = function(key, action, description)
vim.keymap.set("n", key, action, { buffer = bufnr, desc = description })
end

if type(keymaps.open_in_browser) == "string" and keymaps.open_in_browser ~= "" then
set_buf_keymap(
keymaps.open_in_browser,
function() vim.ui.open("https://devdocs.io/" .. slug .. "/" .. entry.link) end,
"Open in the browser"
)
end
end

return M
18 changes: 6 additions & 12 deletions lua/nvim-devdocs/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,22 @@ M.uninstall_doc = function(args)
end
end

M.open_doc = function(args)
M.open_doc = function(args, float)
if vim.tbl_isempty(args.fargs) then
pickers.global_search_picker(false)
local entries = operations.get_all_entries()
pickers.open_picker(entries, float)
else
local alias = args.fargs[1]
pickers.open_picker(alias, false)
pickers.open_picker_alias(alias, float)
end
end

M.open_doc_float = function(args)
if vim.tbl_isempty(args.fargs) then
pickers.global_search_picker(true)
else
local alias = args.fargs[1]
pickers.open_picker(alias, true)
end
end
M.open_doc_float = function(args) M.open_doc(args, true) end

M.open_doc_current_file = function(float)
local filetype = vim.bo.filetype
local alias = filetypes[filetype] or filetype
pickers.open_picker(alias, float)
pickers.open_picker_alias(alias, float)
end

M.update = function(args)
Expand Down
49 changes: 16 additions & 33 deletions lua/nvim-devdocs/operations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,32 @@ local M = {}

local job = require("plenary.job")
local curl = require("plenary.curl")
local path = require("plenary.path")
local state = require("nvim-devdocs.state")

local list = require("nvim-devdocs.list")
local state = require("nvim-devdocs.state")
local notify = require("nvim-devdocs.notify")
local plugin_config = require("nvim-devdocs.config").get()
local config = require("nvim-devdocs.config")
local build_docs = require("nvim-devdocs.build")

local devdocs_site_url = "https://devdocs.io"
local devdocs_cdn_url = "https://documents.devdocs.io"
local docs_dir = path:new(plugin_config.dir_path, "docs")
local lock_path = path:new(plugin_config.dir_path, "docs-lock.json")
local registery_path = path:new(plugin_config.dir_path, "registery.json")
local index_path = path:new(plugin_config.dir_path, "index.json")

local plugin_config = config.get()
local data_dir = config.new_path()
local index_path = config.new_path("index.json")
local lock_path = config.new_path("docs-lock.json")
local registery_path = config.new_path("registery.json")

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

curl.get(devdocs_site_url .. "/docs.json", {
headers = {
["User-agent"] = "chrome",
["User-agent"] = "chrome", -- fake user agent, see #25
},
callback = function(response)
local dir_path = path:new(plugin_config.dir_path)
local file_path = path:new(plugin_config.dir_path, "registery.json")

if not dir_path:exists() then dir_path:mkdir() end

file_path:write(response.body, "w", 438)

if not data_dir:exists() then data_dir:mkdir() end
registery_path:write(response.body, "w", 438)
notify.log("DevDocs registery has been written to the disk")
end,
on_error = function(error)
Expand Down Expand Up @@ -138,7 +134,7 @@ M.uninstall = function(alias)
else
local index = vim.fn.json_decode(index_path:read())
local lockfile = vim.fn.json_decode(lock_path:read())
local doc_path = path:new(docs_dir, alias)
local doc_path = config.new_path("docs", alias)

index[alias] = nil
lockfile[alias] = nil
Expand Down Expand Up @@ -215,7 +211,7 @@ M.filter_doc = function(lines, pattern)
end

M.render_cmd = function(bufnr, is_picker)
vim.bo[bufnr].ft = "glow"
vim.bo[bufnr].ft = plugin_config.previewer_cmd

local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local chan = vim.api.nvim_open_term(bufnr, {})
Expand Down Expand Up @@ -247,8 +243,8 @@ M.open = function(entry, bufnr, float)
local col = (ui.width - plugin_config.float_win.width) * 0.5
local float_opts = plugin_config.float_win

if not plugin_config.row then float_opts.row = row end
if not plugin_config.col then float_opts.col = col end
float_opts.row = plugin_config.row or row
float_opts.col = plugin_config.col or col

local win = nil
local last_win = state.get("last_win")
Expand All @@ -275,20 +271,7 @@ M.open = function(entry, bufnr, float)
vim.bo[bufnr].ft = "markdown"
end

local slug = entry.alias:gsub("-", "~")
local keymaps = plugin_config.mappings
local set_buf_keymap = function(key, action, description)
vim.keymap.set("n", key, action, { buffer = bufnr, desc = description })
end

if type(keymaps.open_in_browser) == "string" and keymaps.open_in_browser ~= "" then
set_buf_keymap(
keymaps.open_in_browser,
function() vim.ui.open("https://devdocs.io/" .. slug .. "/" .. entry.link) end,
"Open in the browser"
)
end

config.set_keymaps(bufnr, entry)
plugin_config.after_open(bufnr)
end

Expand Down
42 changes: 8 additions & 34 deletions lua/nvim-devdocs/pickers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,7 @@ M.update_picker = function()
picker:find()
end

M.open_picker = function(alias, float)
local entries = operations.get_entries(alias)

if not entries then
notify.log_err(alias .. " documentation is not installed")
return
end

M.open_picker = function(entries, float)
local picker = pickers.new(plugin_config.telescope, {
prompt_title = "Select an entry",
finder = finders.new_table({
Expand All @@ -182,33 +175,14 @@ M.open_picker = function(alias, float)
picker:find()
end

M.global_search_picker = function(float)
local entries = operations.get_all_entries()
local picker = pickers.new(plugin_config.telescope, {
prompt_title = "Select an entry",
finder = finders.new_table({
results = entries,
entry_maker = function(entry)
return {
value = entry,
display = entry.name,
ordinal = entry.name,
}
end,
}),
sorter = config.generic_sorter(plugin_config.telescope),
previewer = doc_previewer,
attach_mappings = function()
actions.select_default:replace(function(prompt_bufnr)
actions.close(prompt_bufnr)
open_doc(float)
end)

return true
end,
})
M.open_picker_alias = function(alias, float)
local entries = operations.get_entries(alias)

picker:find()
if not entries then
notify.log_err(alias .. " documentation is not installed")
else
M.open_picker(entries, float)
end
end

return M
1 change: 1 addition & 0 deletions lua/nvim-devdocs/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local state = {
last_win = nil,
}

---@return any
M.get = function(key) return state[key] end

M.set = function(key, value) state[key] = value end
Expand Down
Loading

0 comments on commit 97f5896

Please sign in to comment.