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

Commit 378aef5

Browse files
merge: sections only display after section start (#42)
* sections: only display after section start fixes #28 It was wrong to give all the contents to the preview/display command and then search on the output, because the command could change the format completely. So now we submit to the converter program only data after the section start, and we don't need to search the output anymore. * update: simplified & omit the next sections Simplified the implementation and implemented the next section drop * wip: filtering in telescope * wip: previewer with only buf_previewer * fix: picker cmd args --------- Co-authored-by: Luckas <[email protected]>
1 parent 3e12239 commit 378aef5

File tree

2 files changed

+67
-70
lines changed

2 files changed

+67
-70
lines changed

lua/nvim-devdocs/operations.lua

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,55 @@ M.get_all_entries = function()
187187
return entries
188188
end
189189

190-
M.open = function(entry, bufnr, pattern, float)
190+
M.filter_doc = function(lines, pattern)
191+
if not pattern then return lines end
192+
193+
-- https://stackoverflow.com/a/34953646/516188
194+
local function create_pattern(text) return text:gsub("([^%w])", "%%%1") end
195+
196+
local filtered_lines = {}
197+
local found = false
198+
local search_pattern = create_pattern(pattern)
199+
local split = vim.split(pattern, " ")
200+
local header = split[1]
201+
local top_header = header and header:sub(1, #header - 1)
202+
203+
for _, line in ipairs(lines) do
204+
if found and header then
205+
local line_split = vim.split(line, " ")
206+
local first = line_split[1]
207+
if first and first == header or first == top_header then break end
208+
end
209+
if line:match(search_pattern) then found = true end
210+
if found then table.insert(filtered_lines, line) end
211+
end
212+
213+
return filtered_lines
214+
end
215+
216+
M.render_cmd = function(bufnr, is_picker)
217+
vim.bo[bufnr].ft = "glow"
218+
219+
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
220+
local chan = vim.api.nvim_open_term(bufnr, {})
221+
local args = is_picker and plugin_config.picker_cmd_args or plugin_config.cmd_args
222+
local previewer = job:new({
223+
command = plugin_config.previewer_cmd,
224+
args = args,
225+
on_stdout = vim.schedule_wrap(function(_, data)
226+
if not data then return end
227+
local output_lines = vim.split(data, "\n", {})
228+
for _, line in ipairs(output_lines) do
229+
pcall(function() vim.api.nvim_chan_send(chan, line .. "\r\n") end)
230+
end
231+
end),
232+
writer = table.concat(lines, "\n"),
233+
})
234+
235+
previewer:start()
236+
end
237+
238+
M.open = function(entry, bufnr, float)
191239
vim.api.nvim_buf_set_option(bufnr, "modifiable", false)
192240

193241
if not float then
@@ -209,32 +257,10 @@ M.open = function(entry, bufnr, pattern, float)
209257
vim.wo[win].relativenumber = false
210258
end
211259

212-
vim.fn.search(pattern)
213-
214260
local ignore = vim.tbl_contains(plugin_config.cmd_ignore, entry.alias)
261+
215262
if plugin_config.previewer_cmd and not ignore then
216-
vim.bo[bufnr].ft = "glow"
217-
218-
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
219-
local chan = vim.api.nvim_open_term(bufnr, {})
220-
local previewer = job:new({
221-
command = plugin_config.previewer_cmd,
222-
args = plugin_config.cmd_args,
223-
on_stdout = vim.schedule_wrap(function(_, data)
224-
local output_lines = vim.split(data, "\n", {})
225-
for _, line in ipairs(output_lines) do
226-
vim.api.nvim_chan_send(chan, line .. "\r\n")
227-
end
228-
end),
229-
on_exit = vim.schedule_wrap(function()
230-
if pattern then
231-
local formatted_pattern = pattern:gsub("`", "")
232-
vim.defer_fn(function() vim.fn.search(formatted_pattern) end, 500)
233-
end
234-
end),
235-
writer = table.concat(lines, "\n"),
236-
})
237-
previewer:start()
263+
M.render_cmd(bufnr)
238264
else
239265
vim.bo[bufnr].ft = "markdown"
240266
end

lua/nvim-devdocs/pickers.lua

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -46,44 +46,28 @@ local metadata_previewer = previewers.new_buffer_previewer({
4646
end,
4747
})
4848

49-
local buf_doc_previewer = previewers.new_buffer_previewer({
49+
local doc_previewer = previewers.new_buffer_previewer({
5050
title = "Preview",
5151
keep_last_buf = true,
5252
define_preview = function(self, entry)
5353
local splited_path = vim.split(entry.value.path, ",")
5454
local file = splited_path[1]
5555
local file_path = path:new(plugin_config.dir_path, "docs", entry.value.alias, file .. ".md")
5656
local bufnr = self.state.bufnr
57-
58-
local display_lines = function(lines)
59-
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
60-
vim.bo[bufnr].ft = "markdown"
61-
-- TODO: highlight in picker
62-
-- vim.api.nvim_buf_call(bufnr, function()
63-
-- vim.fn.search(section)
64-
-- vim.fn.matchadd("Search", pattern)
65-
-- end)
66-
end
57+
local pattern = splited_path[2]
6758

6859
file_path:_read_async(vim.schedule_wrap(function(content)
6960
local lines = vim.split(content, "\n")
70-
display_lines(lines)
71-
end))
72-
end,
73-
})
74-
75-
local term_doc_previewer = previewers.new_termopen_previewer({
76-
title = "Preview",
77-
get_command = function(entry)
78-
local splited_path = vim.split(entry.value.path, ",")
79-
local file = splited_path[1]
80-
local file_path = path:new(plugin_config.dir_path, "docs", entry.value.alias, file .. ".md")
81-
local args = { plugin_config.previewer_cmd }
61+
local filtered_lines = operations.filter_doc(lines, pattern)
8262

83-
vim.list_extend(args, plugin_config.picker_cmd_args)
84-
table.insert(args, path.__tostring(file_path))
63+
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, filtered_lines)
8564

86-
return args
65+
if plugin_config.picker_cmd then
66+
operations.render_cmd(bufnr, true)
67+
else
68+
vim.bo[bufnr].ft = "markdown"
69+
end
70+
end))
8771
end,
8872
})
8973

@@ -96,15 +80,14 @@ local open_doc = function(float)
9680
local splited_path = vim.split(selection.value.path, ",")
9781
local file = splited_path[1]
9882
local file_path = path:new(plugin_config.dir_path, "docs", selection.value.alias, file .. ".md")
99-
local content = file_path:read()
100-
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, vim.split(content, "\n"))
83+
local lines = file_path:readlines()
84+
local filtered_lines = operations.filter_doc(lines, splited_path[2])
85+
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, filtered_lines)
10186
else
10287
bufnr = state.get_global_key("last_preview_bufnr")
10388
end
10489

105-
local splited_path = vim.split(selection.value.path, ",")
106-
107-
operations.open(selection.value, bufnr, splited_path[2], float)
90+
operations.open(selection.value, bufnr, float)
10891
end
10992

11093
M.installation_picker = function()
@@ -175,12 +158,6 @@ M.open_picker = function(alias, float)
175158
return
176159
end
177160

178-
local previewer = buf_doc_previewer
179-
180-
if plugin_config.previewer_cmd and plugin_config.picker_cmd then
181-
previewer = term_doc_previewer
182-
end
183-
184161
local picker = pickers.new(plugin_config.telescope, {
185162
prompt_title = "Select an entry",
186163
finder = finders.new_table({
@@ -194,7 +171,7 @@ M.open_picker = function(alias, float)
194171
end,
195172
}),
196173
sorter = config.generic_sorter(plugin_config.telescope),
197-
previewer = previewer,
174+
previewer = doc_previewer,
198175
attach_mappings = function()
199176
actions.select_default:replace(function(prompt_bufnr)
200177
actions.close(prompt_bufnr)
@@ -209,12 +186,6 @@ end
209186

210187
M.global_search_picker = function(float)
211188
local entries = operations.get_all_entries()
212-
local previewer = buf_doc_previewer
213-
214-
if plugin_config.previewer_cmd and plugin_config.picker_cmd then
215-
previewer = term_doc_previewer
216-
end
217-
218189
local picker = pickers.new(plugin_config.telescope, {
219190
prompt_title = "Select an entry",
220191
finder = finders.new_table({
@@ -228,7 +199,7 @@ M.global_search_picker = function(float)
228199
end,
229200
}),
230201
sorter = config.generic_sorter(plugin_config.telescope),
231-
previewer = previewer,
202+
previewer = doc_previewer,
232203
attach_mappings = function()
233204
actions.select_default:replace(function(prompt_bufnr)
234205
actions.close(prompt_bufnr)

0 commit comments

Comments
 (0)