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

Commit

Permalink
fix: buffer issues (#68)
Browse files Browse the repository at this point in the history
Fixed the case where there is no telescope preview and catch possible undefined buffer behaviour.
  • Loading branch information
luckasRanarison committed Nov 30, 2023
1 parent c77c5b8 commit 9d14163
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lua/nvim-devdocs/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ M.toggle = function()
local buf = state.get("last_buf")
local win = state.get("last_win")

if not buf then return end
if not buf or not vim.api.nvim_buf_is_valid(buf) then return end

if win and vim.api.nvim_win_is_valid(win) then
vim.api.nvim_win_close(win, true)
Expand Down
24 changes: 22 additions & 2 deletions lua/nvim-devdocs/operations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,37 @@ M.uninstall = function(alias)
end
end

---@param entry DocEntry
---@return string[]
M.read_entry = function(entry)
local splited_path = vim.split(entry.path, ",")
local file = splited_path[1]
local file_path = DOCS_DIR:joinpath(entry.alias, file .. ".md")
local content = file_path:read()
local pattern = splited_path[2]
local next_pattern = nil

if entry.next_path ~= nil then next_pattern = vim.split(entry.next_path, ",")[2] end

local lines = vim.split(content, "\n")
local filtered_lines = M.filter_doc(lines, pattern, next_pattern)

return filtered_lines
end

---@param entry DocEntry
---@param callback function
M.read_entry = function(entry, callback)
M.read_entry_async = function(entry, callback)
local splited_path = vim.split(entry.path, ",")
local file = splited_path[1]
local file_path = DOCS_DIR:joinpath(entry.alias, file .. ".md")

file_path:_read_async(vim.schedule_wrap(function(content)
local pattern = splited_path[2]
local next_pattern = nil

if entry.next_path ~= nil then next_pattern = vim.split(entry.next_path, ",")[2] end

local lines = vim.split(content, "\n")
local filtered_lines = M.filter_doc(lines, pattern, next_pattern)

Expand Down Expand Up @@ -289,7 +309,7 @@ M.keywordprg = function(keyword)
for _, value in pairs(entries or {}) do
if value.name == keyword or value.link == keyword then
entry = value
M.read_entry(entry, callback)
M.read_entry_async(entry, callback)
end
end

Expand Down
10 changes: 4 additions & 6 deletions lua/nvim-devdocs/pickers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ local doc_previewer = previewers.new_buffer_previewer({
define_preview = function(self, entry)
local bufnr = self.state.bufnr

operations.read_entry(entry.value, function(filtered_lines)
operations.read_entry_async(entry.value, function(filtered_lines)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, filtered_lines)

if plugin_config.options.previewer_cmd and plugin_config.options.picker_cmd then
Expand All @@ -71,14 +71,12 @@ local doc_previewer = previewers.new_buffer_previewer({
})

local function open_doc(selection, float)
local bufnr = nil
local bufnr = state.get_global_key("last_preview_bufnr")

if plugin_config.options.picker_cmd then
if plugin_config.options.picker_cmd or not bufnr or not vim.api.nvim_buf_is_valid(bufnr) then
bufnr = vim.api.nvim_create_buf(false, true)
local lines = plugin_state.get("preview_lines")
local lines = plugin_state.get("preview_lines") or operations.read_entry(selection.value)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
else
bufnr = state.get_global_key("last_preview_bufnr")
end

plugin_state.set("last_mode", float and "float" or "normal")
Expand Down

0 comments on commit 9d14163

Please sign in to comment.