fix: termopen previewer (#1901)

- buffer leaking
- still insert mode on confirm
This commit is contained in:
Simon Hauser
2022-05-01 09:46:50 +02:00
committed by GitHub
parent 280c4e3037
commit 544c5ee407
6 changed files with 65 additions and 68 deletions

View File

@@ -345,13 +345,11 @@ end
actions.close = function(prompt_bufnr)
action_state.get_current_history():reset()
local picker = action_state.get_current_picker(prompt_bufnr)
local prompt_win = state.get_status(prompt_bufnr).prompt_win
local original_win_id = picker.original_win_id
actions.close_pum(prompt_bufnr)
vim.api.nvim_win_close(prompt_win, true)
pcall(vim.cmd, string.format([[silent bdelete! %s]], prompt_bufnr))
require("telescope.pickers").on_close_prompt(prompt_bufnr)
pcall(a.nvim_set_current_win, original_win_id)
end

View File

@@ -731,23 +731,15 @@ end
---@param status table: table containing information on the picker
--- and associated windows. Generally obtained from `state.get_status`
function Picker.close_windows(status)
local prompt_win = status.prompt_win
local results_win = status.results_win
local preview_win = status.preview_win
utils.win_delete("results_win", status.results_win, true, true)
utils.win_delete("preview_win", status.preview_win, true, true)
local prompt_border_win = status.prompt_border_win
local results_border_win = status.results_border_win
local preview_border_win = status.preview_border_win
utils.win_delete("results_win", results_win, true, true)
utils.win_delete("preview_win", preview_win, true, true)
utils.win_delete("prompt_border_win", prompt_border_win, true, true)
utils.win_delete("results_border_win", results_border_win, true, true)
utils.win_delete("preview_border_win", preview_border_win, true, true)
utils.win_delete("prompt_border_win", status.prompt_border_win, true, true)
utils.win_delete("results_border_win", status.results_border_win, true, true)
utils.win_delete("preview_border_win", status.preview_border_win, true, true)
vim.defer_fn(function()
utils.win_delete("prompt_win", prompt_win, true)
utils.win_delete("prompt_win", status.prompt_win, true)
end, 10)
state.clear_status(status.prompt_bufnr)
@@ -1468,6 +1460,11 @@ function pickers.on_close_prompt(prompt_bufnr)
picker.close_windows(status)
mappings.clear(prompt_bufnr)
vim.api.nvim_clear_autocmds {
group = "PickerInsert",
event = "BufLeave",
buffer = prompt_bufnr,
}
end
function pickers.on_resize_window(prompt_bufnr)

View File

@@ -95,9 +95,6 @@ end
---
--- Furthermore, it will forward all `config.set_env` environment variables to
--- that terminal process.
---
--- While this interface is a good start, it was replaced with the way more
--- flexible `buffer_previewer` and is now deprecated.
previewers.new_termopen_previewer = term_previewer.new_termopen_previewer
--- Provides a `termopen_previewer` which has the ability to display files.

View File

@@ -1,14 +1,9 @@
local conf = require("telescope.config").values
local utils = require "telescope.utils"
local Path = require "plenary.path"
local putils = require "telescope.previewers.utils"
local from_entry = require "telescope.from_entry"
local Previewer = require "telescope.previewers.previewer"
local flatten = vim.tbl_flatten
local buf_delete = utils.buf_delete
local job_is_running = utils.job_is_running
local defaulter = utils.make_default_callable
local previewers = {}
@@ -56,7 +51,7 @@ local bat_maker = function(filename, lnum, start, finish)
end
end
return flatten {
return vim.tbl_flatten {
command,
bat_options,
"--",
@@ -120,27 +115,24 @@ previewers.new_termopen_previewer = function(opts)
local opt_teardown = opts.teardown
local old_bufs = {}
local bufentry_table = {}
local term_ids = {}
local function get_term_id(self)
if not self.state then
return nil
if self.state then
return self.state.termopen_id
end
return self.state.termopen_id
end
local function get_bufnr(self)
if not self.state then
return nil
if self.state then
return self.state.termopen_bufnr
end
return self.state.termopen_bufnr
end
local function set_term_id(self, value)
if job_is_running(get_term_id(self)) then
vim.fn.jobstop(get_term_id(self))
end
if self.state then
self.state.termopen_id = value
if self.state and term_ids[self.state.termopen_bufnr] == nil then
term_ids[self.state.termopen_bufnr] = value
end
end
@@ -153,6 +145,18 @@ previewers.new_termopen_previewer = function(opts)
end
end
local function get_bufnr_by_bufentry(self, value)
if self.state then
return bufentry_table[value]
end
end
local function set_bufentry(self, value)
if self.state and value then
bufentry_table[value] = get_bufnr(self)
end
end
function opts.setup(self)
local state = {}
if opt_setup then
@@ -166,17 +170,17 @@ previewers.new_termopen_previewer = function(opts)
opt_teardown(self)
end
local term_id = get_term_id(self)
if term_id and utils.job_is_running(term_id) then
vim.fn.jobclose(term_id)
end
set_term_id(self, nil)
set_bufnr(self, nil)
set_bufentry(self, nil)
for _, bufnr in ipairs(old_bufs) do
buf_delete(bufnr)
local term_id = term_ids[bufnr]
if term_id and utils.job_is_running(term_id) then
vim.fn.jobstop(term_id)
end
utils.buf_delete(bufnr)
end
bufentry_table = {}
end
function opts.preview_fn(self, entry, status)
@@ -184,24 +188,28 @@ previewers.new_termopen_previewer = function(opts)
set_bufnr(self, vim.api.nvim_win_get_buf(status.preview_win))
end
set_bufnr(self, vim.api.nvim_create_buf(false, true))
local prev_bufnr = get_bufnr_by_bufentry(self, entry)
if prev_bufnr then
self.state.bufnr = prev_bufnr
vim.api.nvim_win_set_buf(status.preview_win, self.state.bufnr)
else
local bufnr = vim.api.nvim_create_buf(false, true)
set_bufnr(self, bufnr)
vim.api.nvim_win_set_buf(status.preview_win, bufnr)
local bufnr = get_bufnr(self)
vim.api.nvim_win_set_buf(status.preview_win, bufnr)
local term_opts = {
cwd = opts.cwd or vim.loop.cwd(),
env = conf.set_env,
}
local term_opts = {
cwd = opts.cwd or vim.fn.getcwd(),
env = conf.set_env,
}
putils.with_preview_window(status, bufnr, function()
local cmd = opts.get_command(entry, status)
if cmd then
set_term_id(self, vim.fn.termopen(cmd, term_opts))
vim.api.nvim_buf_call(bufnr, function()
set_term_id(self, vim.fn.termopen(cmd, term_opts))
end)
end
end)
vim.api.nvim_buf_set_name(bufnr, tostring(bufnr))
set_bufentry(self, entry)
end
end
if not opts.send_input then