nvim-telescope / telescope.nvim

Find, Filter, Preview, Pick. All lua, all the time.
MIT License
15.35k stars 821 forks source link

Custom picker leaves terminal mode #2257

Open srinathava opened 1 year ago

srinathava commented 1 year ago

Description

I am trying to write a custom picker and some of the actions involve opening a terminal which is running some build process. I have noticed that executing those commands via the telescope picker always leaves me in "normal" mode, while executing the command from Neovim directly correctly goes into terminal mode as expected.

Neovim version

$ nvim --version
NVIM v0.8.0-1210-gd367ed9b2
Build type: Release
LuaJIT 2.1.0-beta3
Compiled by runner@fv-az505-656

Features: +acl +iconv +tui
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/share/nvim"

Operating system and version

debian 11

Telescope version / branch / rev

telescope 0.1.0

checkhealth telescope

telescope: require("telescope.health").check()
========================================================================
## Checking for required plugins
  - OK: plenary installed.
  - WARNING: nvim-treesitter not found. 

## Checking external dependencies
  - OK: rg: found ripgrep 12.1.1
  - OK: fd: found fd 8.2.1

## ===== Installed extensions =====

## Telescope Extension: `fzf`
  - OK: lib working as expected
  - OK: file_sorter correctly configured
  - OK: generic_sorter correctly configured

Steps to reproduce

  1. nvim -nu minimal.lua with the attached minimal.lua

  2. Once you see the message "Ready" echoed, issue the command :lua colors()

Note that the command for picking any color is to just issue the Neovim terminal command.

Expected behavior

We open a new terminal in TERMINAL mode

Actual behavior

The terminal opens, but in normal mode. You have to press i or a to enter TERMINAL mode.

Minimal config

vim.cmd [[set runtimepath=$VIMRUNTIME]]
vim.cmd [[set packpath=/tmp/nvim/site]]
local package_root = "/tmp/nvim/site/pack"
local install_path = package_root .. "/packer/start/packer.nvim"
local function load_plugins()
  require("packer").startup {
    {
      "wbthomason/packer.nvim",
      {
        "nvim-telescope/telescope.nvim",
        requires = {
          "nvim-lua/plenary.nvim",
          { "nvim-telescope/telescope-fzf-native.nvim", run = "make" },
        },
      },
      -- ADD PLUGINS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
    },
    config = {
      package_root = package_root,
      compile_path = install_path .. "/plugin/packer_compiled.lua",
      display = { non_interactive = true },
    },
  }
end
_G.load_config = function()
  require("telescope").setup()
  require("telescope").load_extension "fzf"
end

_G.colors = function(opts)
  local pickers = require "telescope.pickers"
  local finders = require "telescope.finders"
  local conf = require("telescope.config").values
  local actions = require "telescope.actions"
  -- local action_state = require "telescope.actions.state"

  opts = opts or {}
  pickers.new(opts, {
    prompt_title = "colors",
    finder = finders.new_table {
      results = { "red", "green", "blue" }
    },
    sorter = conf.generic_sorter(opts),
    attach_mappings = function(prompt_bufnr, map)
      actions.select_default:replace(function()
        actions.close(prompt_bufnr)
        vim.cmd('terminal')
      end)
      return true
    end,
  }):find()
end

if vim.fn.isdirectory(install_path) == 0 then
  print "Installing Telescope and dependencies."
  vim.fn.system { "git", "clone", "--depth=1", "https://github.com/wbthomason/packer.nvim", install_path }
end
load_plugins()
require("packer").sync()
vim.cmd [[autocmd User PackerComplete ++once echo "Ready!" | lua load_config()]]
srinathava commented 1 year ago

I found a workaround for this. Instead of

    attach_mappings = function(prompt_bufnr, map)
      actions.select_default:replace(function()
        actions.close(prompt_bufnr)
        vim.cmd('terminal')
      end)
      return true
    end,

if I instead defer executing the action using vim.schedule, it seems to work as expected:

    attach_mappings = function(prompt_bufnr, map)
      actions.select_default:replace(function()
        actions.close(prompt_bufnr)
        vim.schedule(function()
          vim.cmd('terminal')
        end)
      end)
      return true
    end,