fannheyward / coc-rust-analyzer

rust-analyzer extension for coc.nvim
MIT License
1.13k stars 40 forks source link

Completion overwrites text when `"suggest.noselect": true` #1270

Closed danielrainer closed 1 month ago

danielrainer commented 1 month ago

Using the following configuration:

init.lua:

local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({
        "git",
        "clone",
        "--filter=blob:none",
        "https://github.com/folke/lazy.nvim.git",
        "--branch=stable", -- latest stable release
        lazypath,
    })
end
vim.opt.rtp:prepend(lazypath)
require("lazy").setup({ { "neoclide/coc.nvim", build = "yarn install" } }, {})

vim.g.coc_global_extensions = { "coc-rust-analyzer" }
local opts = { silent = true, noremap = true, expr = true, replace_keycodes = false }
vim.keymap.set("i", "<cr>", [[coc#pum#visible() ? coc#_select_confirm() : "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"]], opts)
vim.keymap.set("i", "<TAB>",
               'coc#pum#visible() ? coc#pum#next(1) : v:lua.check_back_space() ? "<TAB>" : coc#refresh()',
               opts)

coc-settings.json:

{
  "suggest.noselect": true
}

I can reproduce the issue in the following way:

  1. Create a new rust project and change src/main.rs to the following code:
    fn main() {
    vec![].iter();
    }
  2. Save and exit.
  3. Restart the editor and wait for the language server to finish initialization.
  4. Go into insert mode in line 2 between ] and ..
  5. Enter .r<cr> (<cr> means enter, not the literal characters. Wait until suggestions appear before pressing enter.)
  6. Suggestion is inserted but overwrites part of the line. Result (assuming the first suggestion was the remove function):
    fn main() {
    vec![].remove(index)();
    }

    This is different from the expected result which would be:

    fn main() {
    vec![].remove(index).iter();
    }

This only seems to happen when suggest.noselect is true, no option is selected (e.g. via <tab>), and at least one character is entered after the .. The more characters are entered before accepting the suggestion with <cr>, the fewer characters get deleted. I'm not sure if this issue is specific to coc-rust-analyzer or if it affects other extensions as well, but I've not observed it with other extensions.

What's the output of :CocInfo

## versions

vim version: NVIM v0.10.2
node version: v22.9.0
coc.nvim version: 0.0.82-9fd85781 2024-09-21 00:59:00 +0900
coc.nvim directory: /home/drainer/.local/share/nvim/lazy/coc.nvim
term: xterm-kitty
platform: linux

## Log of coc.nvim

2024-10-15T02:28:28.838 INFO (pid:94176) [plugin] - coc.nvim initialized with node: v22.9.0 after 165
2024-10-15T02:28:28.839 INFO (pid:94176) [services] - LanguageClient Rust Analyzer Language Server state change: stopped => starting
2024-10-15T02:28:28.848 INFO (pid:94176) [language-client-index] - Language server "rust-analyzer" started with 94188
2024-10-15T02:28:28.862 INFO (pid:94176) [services] - LanguageClient Rust Analyzer Language Server state change: starting => running
2024-10-15T02:28:28.890 INFO (pid:94176) [services] - service rust-analyzer started
2024-10-15T02:28:28.958 ERROR (pid:94176) [extension:coc-rust-analyzer] - {"message":"API rate limit exceeded for 83.175.72.117. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)","documentation_url":"https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting"}

What's the output of :CocCommand rust-analyzer.serverVersion

rust-analyzer 0.3.2146-standalone

What's your coc-rust-analyzer version? You can get it from :CocList extensions

0.77.5

fannheyward commented 1 month ago

Set "suggest.insertMode": "insert", check :h coc-config-suggest-insertMode.