hrsh7th / nvim-cmp

A completion plugin for neovim coded in Lua.
MIT License
8.16k stars 407 forks source link

Ghost text appearing while editting a word... leads to confusion! #2035

Closed ifelsemaybe closed 2 months ago

ifelsemaybe commented 2 months ago

Hello,

I've noticed adding new characters within an already existing word still triggers ghost text. The downside of this is the confusion led by extra ghost characters appearing within the same word you are editing.

Has anybody managed to find an easy solution through the lua config, or is this something that needs fixing, within the codebase, through a pull request?

Would greatly appreciate any insights pertaining to this issue.

Picture for reference:

image

(I added the character f inside the word Function, and some ghost text appears)
mikecsmith commented 2 months ago

Here's how I've tried to solve it - hope it's useful:

      -- Consolidated list of characters that should toggle ghost_text
      local toggle_chars = {
        '"', "'", '`', '<', '>', '{', '}', '[', ']', '(', ')', ' ', '' 
      }

      local cmp_config = require('cmp.config')

      local function toggle_ghost_text()
        if vim.api.nvim_get_mode().mode ~= "i" then
          return
        end

        -- Get the current cursor column and line content
        local cursor_col = vim.fn.col('.')  -- Get cursor column
        local line = vim.fn.getline('.')    -- Get current line content

        -- Get the character after the cursor
        local char_after = line:sub(cursor_col, cursor_col)

        -- Check if the character after the cursor is in the toggle list (pair characters, spaces, or end of line)
        local should_enable_ghost_text = vim.tbl_contains(toggle_chars, char_after)

        -- Enable or disable ghost_text based on the conditions
        cmp_config.set_onetime({
          experimental = {
            ghost_text = should_enable_ghost_text,
          },
        })
      end

      vim.api.nvim_create_autocmd({ "InsertEnter", "CursorMovedI" }, {
        callback = toggle_ghost_text,
      })
ifelsemaybe commented 2 months ago

Yea, thanks a bunch. This solves my issue.

This works like a charm. Wonder if I can get it to work for all auto-completion in general, meaning also for the pop-up item menu.

Thanks again, Wishing you a happy weekend!

ifelsemaybe commented 2 months ago

I found the answer to my question above, for the completion menu.

I just put the value of "should_enable_ghost_text" inside of the "enable" property of the cmp.config, and it works!