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

Commit

Permalink
refactor: rewrite
Browse files Browse the repository at this point in the history
Improved the code and made it easier to pick up and maintain
  • Loading branch information
luckasRanarison committed Nov 30, 2023
1 parent b5d896a commit de3c0f3
Show file tree
Hide file tree
Showing 10 changed files with 293 additions and 205 deletions.
3 changes: 2 additions & 1 deletion .luarc.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"workspace.library": [
"/usr/share/nvim/runtime/lua",
"/usr/local/share/nvim/runtime/lua",
"~/.local/share/nvim/lazy/neodev.nvim/types/stable",
"~/.local/share/nvim/lazy/plenary.nvim/",
"~/.local/share/nvim/lazy/telescope.nvim/",
"~/.local/share/nvim/lazy/nvim-treesitter/",
"${3rd}/luv/library",
"${3rd}/luassert/library"
]
Expand Down
42 changes: 24 additions & 18 deletions lua/nvim-devdocs/build.lua
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
local M = {}

local fs = require("nvim-devdocs.fs")
local notify = require("nvim-devdocs.notify")
local transpiler = require("nvim-devdocs.transpiler")

---@param entry RegisteryEntry
---@param index any
---@param doc_index DocIndex
---@param docs table<string, string>
local function build_docs(entry, index, docs)
M.build_docs = function(entry, doc_index, docs)
local alias = entry.slug:gsub("~", "-")
local current_doc_dir = DOCS_DIR:joinpath(alias)
local sort_lookup = {}
local sort_lookup_last_index = 1

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

if not DOCS_DIR:exists() then DOCS_DIR:mkdir() end
if not INDEX_PATH:exists() then INDEX_PATH:write("{}", "w") end
if not LOCK_PATH:exists() then LOCK_PATH:write("{}", "w") end
if not current_doc_dir:exists() then current_doc_dir:mkdir() end

local index = fs.read_index() or {}
local lockfile = fs.read_lockfile() or {}

--- Used for extracting the markdown headers that will be used as breakpoints when spliting the docs
local section_map = {}
local path_map = {}

for _, index_entry in pairs(index.entries) do
for _, index_entry in pairs(doc_index.entries) do
local splited = vim.split(index_entry.path, "#")
local main = splited[1]
local id = splited[2]
Expand All @@ -29,6 +32,9 @@ local function build_docs(entry, index, docs)
if id then table.insert(section_map[main], id) end
end

-- The entries need to be sorted in order to make spliting work
local sort_lookup = {}
local sort_lookup_last_index = 1
local count = 1

for key, doc in pairs(docs) do
Expand All @@ -42,31 +48,31 @@ local function build_docs(entry, index, docs)
sort_lookup_last_index = sort_lookup_last_index + 1
end

-- Use number as filename instead of the entry name to avoid invalid filenames
path_map[key] = tostring(count)
file_path:write(markdown, "w")
count = count + 1
end

table.sort(index.entries, function(a, b)
table.sort(doc_index.entries, function(a, b)
local index_a = sort_lookup[a.path] or -1
local index_b = sort_lookup[b.path] or -1
return index_a < index_b
end)
for i, index_entry in ipairs(index.entries) do

for i, index_entry in ipairs(doc_index.entries) do
local main = vim.split(index_entry.path, "#")[1]
index.entries[i].link = index.entries[i].path
index.entries[i].path = path_map[index_entry.path] or path_map[main]
doc_index.entries[i].link = doc_index.entries[i].path
doc_index.entries[i].path = path_map[index_entry.path] or path_map[main]
end

local index_parsed = vim.fn.json_decode(INDEX_PATH:read())
index_parsed[alias] = index
INDEX_PATH:write(vim.fn.json_encode(index_parsed), "w")
index[alias] = doc_index
lockfile[alias] = entry

local lock_parsed = vim.fn.json_decode(LOCK_PATH:read())
lock_parsed[alias] = entry
LOCK_PATH:write(vim.fn.json_encode(lock_parsed), "w")
fs.write_index(index)
fs.write_lockfile(lockfile)

notify.log("Build complete!")
end

return build_docs
return M
46 changes: 13 additions & 33 deletions lua/nvim-devdocs/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,29 @@ local M = {}

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

---@param args string[]
---@param arg_lead string
---@return string[]
M.get_non_installed = function(arg_lead)
if not REGISTERY_PATH:exists() then return {} end

local content = REGISTERY_PATH:read()
local parsed = vim.fn.json_decode(content)
local installed = list.get_installed_alias()
local args = {}

for _, entry in pairs(parsed) do
local arg = entry.slug:gsub("~", "-")
local starts_with = string.find(arg, arg_lead, 1, true) == 1
if starts_with and not vim.tbl_contains(installed, arg) then table.insert(args, arg) end
end

return args
local function filter_args(args, arg_lead)
return vim.tbl_filter(function(entry)
local starts_with = string.find(entry, arg_lead, 1, true) == 1
if starts_with then return true end
return false
end, args)
end

---@param arg_lead string
---@return string[]
M.get_installed = function(arg_lead)
local installed = list.get_installed_alias()
local args = vim.tbl_filter(function(entry)
local starts_with = string.find(entry, arg_lead, 1, true) == 1
if starts_with then return true end
return false
end, installed)
return filter_args(installed, arg_lead)
end

return args
M.get_non_installed = function(arg_lead)
local non_installed = list.get_non_installed_alias()
return filter_args(non_installed, arg_lead)
end

---@param arg_lead string
---@return string[]
M.get_updatable = function(arg_lead)
local updatable = list.get_updatable()
local args = vim.tbl_filter(function(entry)
local starts_with = string.find(entry, arg_lead, 1, true) == 1
if starts_with then return true end
return false
end, updatable)

return args
return filter_args(updatable, arg_lead)
end

return M
48 changes: 48 additions & 0 deletions lua/nvim-devdocs/fs.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
local M = {}

---@param registery RegisteryEntry[]
M.write_registery = function(registery)
local encoded = vim.fn.json_encode(registery)
REGISTERY_PATH:write(encoded, "w")
end

---@param index IndexObject
M.write_index = function(index)
local encoded = vim.fn.json_encode(index)
INDEX_PATH:write(encoded, "w")
end

---@param lockfile table<string, RegisteryEntry>
M.write_lockfile = function(lockfile)
local encoded = vim.fn.json_encode(lockfile)
LOCK_PATH:write(encoded, "w")
end

---@return RegisteryEntry[]?
M.read_registery = function()
if not REGISTERY_PATH:exists() then return end
local buf = REGISTERY_PATH:read()
return vim.fn.json_decode(buf)
end

---@return IndexObject?
M.read_index = function()
if not INDEX_PATH:exists() then return end
local buf = INDEX_PATH:read()
return vim.fn.json_decode(buf)
end

---@return table<string, RegisteryEntry>?
M.read_lockfile = function()
if not LOCK_PATH:exists() then return end
local buf = LOCK_PATH:read()
return vim.fn.json_decode(buf)
end

---@param alias string
M.remove_docs = function(alias)
local doc_path = DOCS_DIR:joinpath(alias)
doc_path:rm({ recursive = true })
end

return M
11 changes: 5 additions & 6 deletions lua/nvim-devdocs/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local config = require("nvim-devdocs.config")
local completion = require("nvim-devdocs.completion")
local filetypes = require("nvim-devdocs.filetypes")

M.fetch_registery = function() operations.fetch() end
M.fetch_registery = operations.fetch

M.install_doc = function(args)
if vim.tbl_isempty(args.fargs) then
Expand All @@ -30,7 +30,7 @@ end
M.open_doc = function(args, float)
if vim.tbl_isempty(args.fargs) then
local installed = list.get_installed_alias()
local entries = operations.get_entries(installed)
local entries = list.get_doc_entries(installed)
pickers.open_picker(entries or {}, float)
else
local alias = args.fargs[1]
Expand All @@ -46,10 +46,9 @@ M.open_doc_current_file = function(float)

if type(names) == "string" then names = { names } end

local docs = vim.tbl_flatten(
vim.tbl_map(function(value) return operations.get_doc_variants(value) end, names)
)
local entries = operations.get_entries(docs)
local docs =
vim.tbl_flatten(vim.tbl_map(function(name) return list.get_doc_variants(name) end, names))
local entries = list.get_doc_entries(docs)

if entries and not vim.tbl_isempty(entries) then
pickers.open_picker(entries, float)
Expand Down
Loading

0 comments on commit de3c0f3

Please sign in to comment.