Wansmer / langmapper.nvim

A plugin that makes Neovim more friendly to non-English input methods 🤝
MIT License
146 stars 9 forks source link

Switching language and has delay in insert mode #28

Open kukushkawi opened 6 months ago

kukushkawi commented 6 months ago

First of all, thanks for your plugin. I've switched to windows from linux and it's tough to make something semi-working for notes in cyrillic language on windows. I have problem with using it with which-key in insert mode. When i type everything is ok, buy if i press space there is noticeable delay and sometimes when i press "y" after the space it writes "y" and other characters in english keymap. i guess it has to do with nvim-cmp, but i don't know to fix it. The other problem is with escaping from insert mode. When i press escape with non-cyrillic keymap on it escapes to normal mode but if i press just after it any movement key - it moves to the end or beginning of line (if i press h or l) and moves line up or down (if i press j or k). It also stays the same if i just pressing escape in normal mode. But only if i'm using non-cyrillic keymap, with english i can't reproduce it. Let me know if you need more info, thanks in advance.

kukushkawi commented 6 months ago

Just added require('langmapper').hack_get_keymap() to my config and issue with delay/'i' changes in insert mode are dissapeared, but issue with escape in normal mode is still there, so this is still a problem.

kukushkawi commented 6 months ago

And also now which-key only works with keybinds with one sign and doesn't with sequential, only with one key.

Wansmer commented 6 months ago

Hi. Try to reproduce the bug with <Esc> using minimal config and describe the sequence of actions so that I can reproduce it too. Also, there are very common problems with which-key and cmp - see previous issues, maybe there is a solution there.

alexveden commented 1 month ago

Hi all, I faced the same issue, and I have found a workaround. The problem was caused by cmp plugin, which uses 'InsertEnter' event for functioning. Long story short, cmp plugin heavily relies on key remapping on every event for its internal stuff, which launches weird cascade effects when used with langmapper.nvim. Also, the require('langmapper').hack_get_keymap() hack makes stuff works even slower.

Here's the solution, we need to unhack langmapper while cmp is doing its stuff, and hack back when it's done.

  1. We need to alter enabled function of the cmp plugin, see doc. This function is triggered every time when you enter insert mode.

    cmp.setup({
      enabled = function()
       -- other stuff here
    
       -- NOTE: this fuction is getting triggered every time you enter insert mode
        local langmapper = require 'langmapper'
        -- disable langmapper while cmp is working
        langmapper.put_back_keymap()
    
        return true
      end,
    -- .... other cmp settings
    })
  2. Also we need to add defer method when cmp is done

    -- TODO:  place this somewhere into config() part of he Lazy plugin setup
    cmp.event:on('menu_closed', function()
      local langmapper = require 'langmapper'
      langmapper._hack_keymap()
      langmapper.hack_get_keymap()
    end)
Wansmer commented 1 month ago

and I have found a workaround

Thanks for sharing. It's very helpful.