Skip to content

Commit

Permalink
♻️ refactor: More elegant solution for #1. The new util `sanitize_con…
Browse files Browse the repository at this point in the history
…fig()` ensures this bug will never happen again.
  • Loading branch information
Zeioth committed Nov 12, 2023
1 parent 8cb6b52 commit 4c2398b
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lua/dooku/backends/doxygen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
local M = {}
local uv = vim.uv or vim.loop
local utils = require "dooku.utils"
local config = require "dooku.config"
local config = vim.g.dooku_config

local job

Expand Down
2 changes: 1 addition & 1 deletion lua/dooku/backends/godoc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local augroup = vim.api.nvim_create_augroup
local autocmd = vim.api.nvim_create_autocmd
local uv = vim.uv or vim.loop
local utils = require "dooku.utils"
local config = require "dooku.config"
local config = vim.g.dooku_config

local job

Expand Down
2 changes: 1 addition & 1 deletion lua/dooku/backends/jsdoc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
local M = {}
local uv = vim.uv or vim.loop
local utils = require "dooku.utils"
local config = require "dooku.config"
local config = vim.g.dooku_config

local job

Expand Down
8 changes: 4 additions & 4 deletions lua/dooku/backends/rustdoc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
local M = {}
local uv = vim.uv or vim.loop
local utils = require "dooku.utils"
local config = require "dooku.config"
local config = vim.g.dooku_config

local job

Expand All @@ -11,21 +11,21 @@ local job
--- ignore the option on_generate_open.
function M.generate(is_autocmd)
local cwd = utils.os_path(utils.find_project_root(config.project_root))
local args = {"rustdoc", config.cargo_rustdoc_args, "--", config.rustdoc_args}
local cargo_file = utils.os_path(cwd .. "/Cargo.toml")
local cargo_file_exists = vim.loop.fs_stat(cargo_file) and vim.loop.fs_stat(cargo_file).type == 'file' or false

if cargo_file_exists then

-- Generate html docs
if config.on_generate_notification then
vim.notify("Generating rustdoc html docs...", vim.log.levels.INFO)
end

print(cwd)
print("cargo " .. "rustdoc " .. config.cargo_rustdoc_args .. " -- " .. config.rustdoc_args )
if job then uv.process_kill(job, 9) end -- Running already? kill it
job = uv.spawn(
"cargo",
{ args = { "rustdoc" }, cwd = cwd, detach = true }
{ args = args, cwd = cwd, detach = true }
)

-- Open html docs
Expand Down
2 changes: 1 addition & 1 deletion lua/dooku/backends/typedoc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
local M = {}
local uv = vim.uv or vim.loop
local utils = require "dooku.utils"
local config = require "dooku.config"
local config = vim.g.dooku_config

local job

Expand Down
5 changes: 5 additions & 0 deletions lua/dooku/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ function M.set(opts)

-- args for godoc
M.godoc_args = opts.godoc_args or ""

-- [GENERAL]
---------------------------------------------------------------
M = utils.sanitize_config(M) -- convert "" vales to nil
vim.g.dooku_config = M
end

return M
26 changes: 22 additions & 4 deletions lua/dooku/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
local M = {}


--- Given a string, convert 'slash' to 'inverted slash' if on windows, and vice versa on UNIX.
-- Then return the resulting string.
---Given a string, convert 'slash' to 'inverted slash' if on windows, and vice versa on UNIX.
---Then return the resulting string.
---@param path string
---@return string|nil,nil
function M.os_path(path)
Expand All @@ -24,8 +24,8 @@ function M.exists_in_table(obj, tbl)
return false
end

--- Function to find the project root based on a given list of files/directories.
-- Compatible with UNIX and Windows.
---Function to find the project root based on a given list of files/directories.
---Compatible with UNIX and Windows.
function M.find_project_root(roots)
local path = vim.fn.expand "%:p:h" -- Get the directory of the current buffer

Expand All @@ -52,4 +52,22 @@ function M.find_project_root(roots)
return nil -- If no root directory is found, return nil
end

---Given a table, return it back, converting every "" value to nil.
--
--This allow config values to be used on uv.spawn() args in the backend
--without having to check one by one.
---@param tbl table A table {}
---@return table result The original table without nil or "" values.
function M.sanitize_config(tbl)
local result = {}
for key, value in pairs(tbl) do
if type(value) == "string" and value == "" then
result[key] = nil
else
result[key] = value
end
end
return result
end

return M

0 comments on commit 4c2398b

Please sign in to comment.