Skip to content

The copilot.suggestion.is_visible() call returns incorrect state #638

@dokmic

Description

@dokmic

In some cases, when suggestions are accepted programmatically (like the super-tab behavior), the is_visible function incorrectly returns a truthy value.

Reproduction Path

  1. Use the following configuration:

    ~/.config/nvim/init.lua
    local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
    if not (vim.uv or vim.loop).fs_stat(lazypath) then
      local lazyrepo = "https://github.com/folke/lazy.nvim.git"
      local out = vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable", lazyrepo, lazypath })
      if vim.v.shell_error ~= 0 then
        vim.api.nvim_echo({
          { "Failed to clone lazy.nvim:\n", "ErrorMsg" },
          { out, "WarningMsg" },
          { "\nPress any key to exit..." },
        }, true, {})
        vim.fn.getchar()
        os.exit(1)
      end
    end
    vim.opt.rtp:prepend(lazypath)
    
    vim.g.mapleader = " "
    vim.g.maplocalleader = "\\"
    
    require("lazy").setup({
      spec = {
        {
          {
            "zbirenbaum/copilot.lua",
            cmd = "Copilot",
            build = ":Copilot auth",
            event = "BufReadPost",
            opts = {
              suggestion = {
                auto_trigger = true,
                keymap = {
                  accept = false,
                },
              },
              panel = { enabled = false },
            },
          },
        },
      },
      install = { colorscheme = { "habamax" } },
      checker = { enabled = true },
    })
    
    vim.keymap.set({ "i", "s" }, "<Tab>", function()
      local copilot = require("copilot.suggestion")
    
      vim.notify(string.format("is_visible() => %s", tostring(copilot.is_visible())), vim.log.levels.INFO)
    
      if copilot.is_visible() then
        copilot.accept()
      else
        vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Tab>", true, false, true), "n", false)
      end
    end)
    
  2. Use the following file:

    ~/test.ts
    const test = () => {
      return new Promise((resolve) => {
        setTimeout(() => {
          resolve('done');
        });
      });
    }
    
  3. Open the file at the 6th line in insert mode:

    nvim +'normal 6Gw' +startinsert ~/test.ts
  4. Press <Tab> two or more times.

  5. Open diagnostic messages via :messages

Expected Behavior

  • Tabs are being inserted as many times as the corresponding key is pressed.
  • The is_visible() function returns a falsy value:
    is_visible() => nil
    is_visible() => nil
    

Actual Behavior

  • There is only one tab inserted.
  • The is_visible() function returns a truthy value.
    is_visible() => nil
    is_visible() => 5
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions