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

Commit

Permalink
feat: extend filetype to docs mappings (#30)
Browse files Browse the repository at this point in the history
Closes #30, #53.
  • Loading branch information
luckasRanarison committed Nov 29, 2023
1 parent d38fc71 commit af35741
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 55 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ Here is the default configuration:
{
dir_path = vim.fn.stdpath("data") .. "/devdocs", -- installation directory
telescope = {}, -- passed to the telescope picker
filetypes = {
-- extends the filetype to docs mappings used by the `DevdocsOpenCurrent` command, the version doesn't have to be specified
-- scss = "sass",
-- javascript = { "node", "javascript" }
},
float_win = { -- passed to nvim_open_win(), see :h api-floatwin
relative = "editor",
height = 25,
Expand Down
1 change: 1 addition & 0 deletions lua/nvim-devdocs/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local path = require("plenary.path")
local default = {
dir_path = vim.fn.stdpath("data") .. "/devdocs",
telescope = {},
filetypes = {},
float_win = {
relative = "editor",
height = 25,
Expand Down
26 changes: 9 additions & 17 deletions lua/nvim-devdocs/filetypes.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
local M = {
["sh"] = "bash",
["lua"] = "lua-5.4",
["vue"] = "vue-3",
["dart"] = "dart-2",
["ruby"] = "ruby-3.2",
["twig"] = "twig-3",
["less"] = "less-4",
["scss"] = "sass",
["make"] = "gnu_make",
["dockerfile"] = "docker",
["cjs"] = "node",
["mjs"] = "node",
["json"] = "jq",
["yaml"] = "ansible",
["python"] = "python-3.11",
["javascriptreact"] = "react",
["typescriptreact"] = "react",
sh = "bash",
scss = "sass",
make = "gnu_make",
dockerfile = "docker",
javascript = { "node", "javascript" },
json = "jq",
yaml = "ansible",
javascriptreact = { "javascript", "react" },
typescriptreact = { "typescript", "react" },
}

return M
21 changes: 17 additions & 4 deletions lua/nvim-devdocs/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ end

M.open_doc = function(args, float)
if vim.tbl_isempty(args.fargs) then
local entries = operations.get_all_entries()
pickers.open_picker(entries, float)
local installed = list.get_installed_alias()
local entries = operations.get_entries(installed)
pickers.open_picker(entries or {}, float)
else
local alias = args.fargs[1]
pickers.open_picker_alias(alias, float)
Expand All @@ -40,8 +41,20 @@ 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(alias, float)
local names = config.options.filetypes[filetype] or filetypes[filetype] or filetype

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)

if entries and not vim.tbl_isempty(entries) then
pickers.open_picker(entries, float)
else
notify.log_err("No documentation found for the current filetype")
end
end

M.update = function(args)
Expand Down
73 changes: 41 additions & 32 deletions lua/nvim-devdocs/operations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,44 +148,34 @@ M.uninstall = function(alias)
end
end

---@param alias string
---@param aliases string[]
---@return DocEntry[] | nil
M.get_entries = function(alias)
local installed = list.get_installed_alias()
local is_installed = vim.tbl_contains(installed, alias)
M.get_entries = function(aliases)
if not INDEX_PATH:exists() then return end

if not INDEX_PATH:exists() or not is_installed then return end
local entries = {}
local index = vim.fn.json_decode(INDEX_PATH:read())

local index_parsed = vim.fn.json_decode(INDEX_PATH:read())
local entries = index_parsed[alias].entries
for _, alias in pairs(aliases) do
if index[alias] then
local current_entries = index[alias].entries

for key, _ in ipairs(entries) do
entries[key].alias = alias
end
for idx, doc_entry in ipairs(current_entries) do
local next_path = nil
local entries_count = #current_entries

return entries
end
if idx < entries_count then next_path = current_entries[idx + 1].path end

---@return DocEntry[]
M.get_all_entries = function()
if not INDEX_PATH:exists() then return {} end
local entry = {
name = doc_entry.name,
path = doc_entry.path,
link = doc_entry.link,
alias = alias,
next_path = next_path,
}

local entries = {}
local index_parsed = vim.fn.json_decode(INDEX_PATH:read())

for alias, index in pairs(index_parsed) do
local entries_count = #index.entries
for idx, doc_entry in ipairs(index.entries) do
local next_path = nil
if idx < entries_count then next_path = index.entries[idx + 1].path end
local entry = {
name = doc_entry.name,
path = doc_entry.path,
link = doc_entry.link,
alias = alias,
next_path = next_path,
}
table.insert(entries, entry)
table.insert(entries, entry)
end
end
end

Expand Down Expand Up @@ -323,7 +313,7 @@ M.keywordprg = function(keyword)
local alias = state.get("current_doc")
local float = state.get("last_mode") == "float"
local bufnr = vim.api.nvim_create_buf(false, false)
local entries = M.get_entries(alias)
local entries = M.get_entries({ alias })
local entry

local function callback(filtered_lines)
Expand All @@ -344,4 +334,23 @@ M.keywordprg = function(keyword)
if not entry then notify.log("No documentation found for " .. keyword) end
end

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

local variants = {}
---@type RegisteryEntry[]
local entries = vim.fn.json_decode(REGISTERY_PATH:read())

for _, entry in pairs(entries) do
if vim.startswith(entry.slug, name) then
local alias = entry.slug:gsub("~", "-")
table.insert(variants, alias)
end
end

return variants
end

return M
6 changes: 4 additions & 2 deletions lua/nvim-devdocs/pickers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,11 @@ end
---@param alias string
---@param float? boolean
M.open_picker_alias = function(alias, float)
local entries = operations.get_entries(alias)
local entries = operations.get_entries({ alias })

if not entries then
if not entries then return end

if vim.tbl_isempty(entries) then
notify.log_err(alias .. " documentation is not installed")
else
plugin_state.set("current_doc", alias)
Expand Down

0 comments on commit af35741

Please sign in to comment.