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

Commit fb4c462

Browse files
feat: search words using K
Implemented keywordprg. Close #55 and #48.
1 parent 8e0cb37 commit fb4c462

File tree

4 files changed

+70
-12
lines changed

4 files changed

+70
-12
lines changed

lua/nvim-devdocs/init.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ M.update_all = function()
6262
end
6363
end
6464

65+
M.keywordprg = function(args)
66+
local keyword = args.fargs[1]
67+
68+
if keyword then
69+
operations.keywordprg(keyword)
70+
else
71+
notify.log("No keyword provided")
72+
end
73+
end
74+
6575
M.setup = function(opts)
6676
config.setup(opts)
6777

@@ -78,6 +88,7 @@ M.setup = function(opts)
7888
cmd("DevdocsOpenFloat", M.open_doc_float, { nargs = "?", complete = completion.get_installed })
7989
cmd("DevdocsOpenCurrent", function() M.open_doc_current_file() end, {})
8090
cmd("DevdocsOpenCurrentFloat", function() M.open_doc_current_file(true) end, {})
91+
cmd("DevdocsKeywordprg", M.keywordprg, { nargs = "?" })
8192
cmd("DevdocsUpdate", M.update, { nargs = "*", complete = completion.get_updatable })
8293
cmd("DevdocsUpdateAll", M.update_all, {})
8394
end

lua/nvim-devdocs/operations.lua

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,20 @@ M.get_all_entries = function()
180180
return entries
181181
end
182182

183+
M.read_entry = function(entry, callback)
184+
local splited_path = vim.split(entry.path, ",")
185+
local file = splited_path[1]
186+
local file_path = DOCS_DIR:joinpath(entry.alias, file .. ".md")
187+
188+
file_path:_read_async(vim.schedule_wrap(function(content)
189+
local pattern = splited_path[2]
190+
local lines = vim.split(content, "\n")
191+
local filtered_lines = M.filter_doc(lines, pattern)
192+
193+
callback(filtered_lines)
194+
end))
195+
end
196+
183197
-- if we have a pattern to search for, only consider lines after the pattern
184198
M.filter_doc = function(lines, pattern)
185199
if not pattern then return lines end
@@ -245,6 +259,7 @@ M.open = function(entry, bufnr, float)
245259

246260
float_opts.row = plugin_config.row or row
247261
float_opts.col = plugin_config.col or col
262+
float_opts.zindex = 10
248263

249264
local win = nil
250265
local last_win = state.get("last_win")
@@ -271,8 +286,35 @@ M.open = function(entry, bufnr, float)
271286
vim.bo[bufnr].ft = "markdown"
272287
end
273288

289+
vim.bo[bufnr].keywordprg = ":DevdocsKeywordprg"
290+
274291
config.set_keymaps(bufnr, entry)
275292
plugin_config.after_open(bufnr)
276293
end
277294

295+
M.keywordprg = function(keyword)
296+
local alias = state.get("current_doc")
297+
local float = state.get("last_mode") == "float"
298+
local bufnr = state.get("last_bufnr")
299+
local entries = M.get_entries(alias)
300+
local entry
301+
302+
local function callback(filtered_lines)
303+
vim.bo[bufnr].modifiable = true
304+
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, filtered_lines)
305+
vim.bo[bufnr].modifiable = false
306+
307+
M.open(entry, bufnr, float)
308+
end
309+
310+
for _, value in pairs(entries or {}) do
311+
if value.name == keyword or value.link == keyword then
312+
entry = value
313+
M.read_entry(entry, callback)
314+
end
315+
end
316+
317+
if not entry then notify.log("No documentation found for " .. keyword) end
318+
end
319+
278320
return M

lua/nvim-devdocs/pickers.lua

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,9 @@ local doc_previewer = previewers.new_buffer_previewer({
5050
title = "Preview",
5151
keep_last_buf = true,
5252
define_preview = function(self, entry)
53-
local splited_path = vim.split(entry.value.path, ",")
54-
local file = splited_path[1]
55-
local file_path = DOCS_DIR:joinpath(entry.value.alias, file .. ".md")
5653
local bufnr = self.state.bufnr
57-
local pattern = splited_path[2]
58-
59-
file_path:_read_async(vim.schedule_wrap(function(content)
60-
local lines = vim.split(content, "\n")
61-
local filtered_lines = operations.filter_doc(lines, pattern)
6254

55+
operations.read_entry(entry.value, function(filtered_lines)
6356
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, filtered_lines)
6457

6558
if plugin_config.previewer_cmd and plugin_config.picker_cmd then
@@ -68,13 +61,12 @@ local doc_previewer = previewers.new_buffer_previewer({
6861
else
6962
vim.bo[bufnr].ft = "markdown"
7063
end
71-
end))
64+
end)
7265
end,
7366
})
7467

75-
local open_doc = function(float)
68+
local open_doc = function(selection, float)
7669
local bufnr = nil
77-
local selection = action_state.get_selected_entry()
7870

7971
if plugin_config.picker_cmd then
8072
bufnr = vim.api.nvim_create_buf(false, true)
@@ -84,6 +76,8 @@ local open_doc = function(float)
8476
bufnr = state.get_global_key("last_preview_bufnr")
8577
end
8678

79+
plugin_state.set("last_bufnr", bufnr)
80+
plugin_state.set("last_mode", float and "float" or "normal")
8781
operations.open(selection.value, bufnr, float)
8882
end
8983

@@ -163,7 +157,14 @@ M.open_picker = function(entries, float)
163157
attach_mappings = function()
164158
actions.select_default:replace(function(prompt_bufnr)
165159
actions.close(prompt_bufnr)
166-
open_doc(float)
160+
161+
local selection = action_state.get_selected_entry()
162+
local name = selection.value.name
163+
local match = name:match("%[([^%]]+)%]")
164+
165+
if match then plugin_state.set("current_doc", match) end
166+
167+
open_doc(selection, float)
167168
end)
168169
return true
169170
end,
@@ -178,6 +179,7 @@ M.open_picker_alias = function(alias, float)
178179
if not entries then
179180
notify.log_err(alias .. " documentation is not installed")
180181
else
182+
plugin_state.set("current_doc", alias)
181183
M.open_picker(entries, float)
182184
end
183185
end

lua/nvim-devdocs/state.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
local M = {}
22

33
local state = {
4+
current_doc = nil, -- ex: "javascript", used for `keywordprg`
45
preview_lines = nil,
56
last_win = nil,
7+
last_bufnr = nil,
8+
last_mode = nil, -- "normal" | "float"
69
}
710

811
---@return any

0 commit comments

Comments
 (0)