Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Blink's auto_insert causes slashcommand not to get deleted #617

Open
3 tasks done
schilkp opened this issue Jan 8, 2025 · 4 comments
Open
3 tasks done

[Bug]: Blink's auto_insert causes slashcommand not to get deleted #617

schilkp opened this issue Jan 8, 2025 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@schilkp
Copy link
Contributor

schilkp commented Jan 8, 2025

Your minimal.lua config

---@diagnostic disable: missing-fields

--[[
NOTE: Set the config path to enable the copilot adapter to work.
It will search the following paths for a token:
  - "$CODECOMPANION_TOKEN_PATH/github-copilot/hosts.json"
  - "$CODECOMPANION_TOKEN_PATH/github-copilot/apps.json"
--]]
vim.env["CODECOMPANION_TOKEN_PATH"] = vim.fn.expand("~/.config")

vim.env.LAZY_STDPATH = ".repro"
load(vim.fn.system("curl -s https://raw.githubusercontent.com/folke/lazy.nvim/main/bootstrap.lua"))()

-- Your CodeCompanion setup
local plugins = {
  {
    "olimorris/codecompanion.nvim",
    dependencies = {
      { "nvim-treesitter/nvim-treesitter", build = ":TSUpdate" },
      { "nvim-lua/plenary.nvim" },
      -- Test with blink.cmp
      {
        "saghen/blink.cmp",
        lazy = false,
        version = "*",
        opts = {
          keymap = {
            preset = "enter",
            ["<S-Tab>"] = { "select_prev", "fallback" },
            ["<Tab>"] = { "select_next", "fallback" },
          },
          sources = {
            default = { "lsp", "path", "buffer", "codecompanion" },
            cmdline = {}, -- Disable sources for command-line mode
          },
          completion = {
            list = {
              selection = {
                preselect = false,
                auto_insert = true,
              }
            }
          },
        },
      },
      -- Test with nvim-cmp
      -- { "hrsh7th/nvim-cmp" },
    },
    opts = {
      --Refer to: https://github.com/olimorris/codecompanion.nvim/blob/main/lua/codecompanion/config.lua
      strategies = {
        --NOTE: Change the adapter as required
        chat = { adapter = "copilot" },
        inline = { adapter = "copilot" },
      },
      opts = {
        log_level = "DEBUG",
      },
    },
  },
}

require("lazy.minit").repro({ spec = plugins })

-- Setup Tree-sitter
local ts_status, treesitter = pcall(require, "nvim-treesitter.configs")
if ts_status then
  treesitter.setup({
    ensure_installed = { "lua", "markdown", "markdown_inline", "yaml" },
    highlight = { enable = true },
  })
end

-- Setup nvim-cmp
-- local cmp_status, cmp = pcall(require, "cmp")
-- if cmp_status then
--   cmp.setup({
--     mapping = cmp.mapping.preset.insert({
--       ["<C-b>"] = cmp.mapping.scroll_docs(-4),
--       ["<C-f>"] = cmp.mapping.scroll_docs(4),
--       ["<C-Space>"] = cmp.mapping.complete(),
--       ["<C-e>"] = cmp.mapping.abort(),
--       ["<CR>"] = cmp.mapping.confirm({ select = true }),
--       -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
--     }),
--   })
-- end

Error messages

N/A

Log output

N/A

Health check output

==============================================================================
codecompanion: require("codecompanion.health").check()

- Neovim version: 0.10.3
- Log file: /home/schilkp/projects/codecompanion.nvim/.repro//state/nvim/codecompanion.log

Dependencies: ~
- OK plenary.nvim installed
- OK nvim-treesitter installed

Tree-sitter parsers: ~
- OK markdown parser installed
- OK yaml parser installed

Libraries: ~
- OK curl installed

Describe the bug

What I expect: After executing /buffer, there is no spurious text to the buffer

What happens: After executing /buffer, the string of text I used to find the completion is left.

Reproduce the bug

Note: blink changed the config structure one or two days ago. The minimal.lua only works with the latest blink.

  • nvim --clean -u minimal.lua
  • :CodeCompanionChat
  • Type "/buff", press "Arrow Down", press enter
  • Select a buffer
  • "buff" appears in the chat buffer
bug.mp4

I dug around the code a bit, and there is a very good chance this is not a codecompanion issue, or not directly fixable in codecompanion. As far as I can tell, the "buff" text gets inserted into the buffer after blink/init.lua's "execute" func runs.

It also does not get reported as part of "length" in item.ctx.bounds.length - that is consistently zero.

As of today or yesterday, blink supports a callback for the "auto_insert" option, so the following is a possible workaround:

require('blink.cmp').setup({
  
   -- ... other config ----

    completion = {
        list = {
            selection = {
                preselect = false,
                auto_insert = function(ctx)
                    return vim.bo[ctx.bufnr].filetype ~= "codecompanion"
                end,
            }
        },
    }
})

Adapter and model

n/a

Final checks

  • I have made sure this issue exists in the latest version of the plugin
  • I have tested with the minimal.lua file from above and have shared this
  • I have shared the contents of the log file
@schilkp schilkp added the bug Something isn't working label Jan 8, 2025
@schilkp
Copy link
Contributor Author

schilkp commented Jan 8, 2025

FWIW, this is a bit of what I mean with "using autocompletion to re-implement commands will always see you fighting many edge-case bugs" ;)

@olimorris
Copy link
Owner

This will be a change in something Blink related. Right now it feels most of my bug fixing time is related to Blink issues.

I'll dig into this later in the week.

@schilkp
Copy link
Contributor Author

schilkp commented Jan 8, 2025

I imagine. Just keeping my blink config up-to-date has been annoying.

@olimorris
Copy link
Owner

Unfortunately, the problem gets slightly more murky. In the execute function in my implementation, we have two parameters, ctx and item. The latter also includes a table called ctx which is the context after completion has taken place:

For reference:

-- ctx table
{
  bounds = {
    length = 2,
    line_number = 3,
    start_col = 30
  },
  bufnr = 7,
  cursor = { 3, 31 },
  id = 0,
  line = "I wish blink.cmp would work /fi",
  mode = "default",
  providers = { "lsp", "path", "snippets", "buffer", "codecompanion" },
  trigger = {
    initial_character = "/",
    initial_kind = "trigger_character",
    kind = "keyword"
  },
  <metatable> = {
    __index = {
      get_bounds = <function 1>,
      get_cursor = <function 2>,
      get_keyword = <function 3>,
      get_line = <function 4>,
      get_mode = <function 5>,
      new = <function 6>,
      set_cursor = <function 7>,
      within_query_bounds = <function 8>
    }
  }
}

-- item.ctx table
{
  bounds = {
    length = 0,
    line_number = 3,
    start_col = 30
  },
  bufnr = 7,
  cursor = { 3, 29 },
  id = 0,
  line = "I wish blink.cmp would work /",
  mode = "default",
  providers = { "lsp", "path", "snippets", "buffer", "codecompanion" },
  trigger = {
    character = "/",
    initial_character = "/",
    initial_kind = "trigger_character",
    kind = "trigger_character"
  },
  <metatable> = {
    __index = {
      get_bounds = <function 1>,
      get_cursor = <function 2>,
      get_keyword = <function 3>,
      get_line = <function 4>,
      get_mode = <function 5>,
      new = <function 6>,
      set_cursor = <function 7>,
      within_query_bounds = <function 8>
    }
  }
}

In the ctx table you can see line = "I wish blink.cmp would work /fi" where /fi was what I typed before triggering the slash command. in the item.ctx table we have line = "I wish blink.cmp would work /" which is a bit mystifying as what I'm actually left with in my buffer is I wish blink.cmp would work /()fi. So it's added () bracket between the trigger character and the fi.

I think this is an issue upstream so I will duly raise it there.

@schilkp schilkp changed the title [Bug]: Blink's auto_insert causes slashcommand not get deleted [Bug]: Blink's auto_insert causes slashcommand not to get deleted Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants