diff --git a/lua/telescope/_extensions/model.lua b/lua/telescope/_extensions/model.lua index bd9deed..aabbaa8 100644 --- a/lua/telescope/_extensions/model.lua +++ b/lua/telescope/_extensions/model.lua @@ -28,9 +28,10 @@ local function entry_maker(bufnr) display = display .. ' [' .. tostring(#chat_buf.contents.messages) .. ']' return { - value = bufnr, + value = display, display = display, ordinal = bufnr, + bufnr = bufnr, } end @@ -43,16 +44,20 @@ local function create_bare_buffer_previewer(title) if self.state and self.state.winid then -- https://github.com/nvim-telescope/telescope.nvim/blob/0c12735d5aff6a48ffd8111bf144dc2ff44e5975/lua/telescope/previewers/buffer_previewer.lua#L396 -- push a new empty buffer because telescope deletes the last buffer - local bufnr = vim.api.nvim_create_buf(false, true) - utils.win_set_buf_noautocmd(self.state.winid, bufnr) + if vim.api.nvim_win_is_valid(self.state.winid) then -- can become invalid somehow + local bufnr = vim.api.nvim_create_buf(false, true) + utils.win_set_buf_noautocmd(self.state.winid, bufnr) + end end end, preview_fn = function(self, entry, status) local preview_winid = status.layout.preview and status.layout.preview.winid - vim.api.nvim_win_set_buf(preview_winid, entry.value) - self.state.winid = preview_winid + if vim.api.nvim_win_is_valid(preview_winid) then + vim.api.nvim_win_set_buf(preview_winid, entry.bufnr) + self.state.winid = preview_winid + end end, title = title, }) @@ -68,15 +73,11 @@ local function mchat(opts) results = get_mchat_buffers(), entry_maker = entry_maker, }), + -- TODO the prompt doesn't actually filter the entries but I have no idea how to make telescope do that + -- there doesn't seem to be a place to plug in a function to just filter the entries based on prompt + -- is it part of the finder? sorter? + -- currently the prompt seems to filter results by the initial results (so the buffer number) sorter = conf.generic_sorter(opts), - attach_mappings = function(prompt_bufnr) - actions.select_default:replace(function() - actions.close(prompt_bufnr) - local selection = action_state.get_selected_entry() - vim.api.nvim_set_current_buf(selection.value) - end) - return true - end, previewer = create_bare_buffer_previewer('model.nvim mchat buffers'), }) :find()