The implementation of this feature consists of three parts:
When the user switches from normal mode to insert mode, if the user enters a string or comment, the Input Method (IM) will switch to the previous one; otherwise, the default IM will be used.
When the user switches from insert mode to normal mode, if the user exits a string or comment back to normal mode, the IM will switch back to the default IM, and remember this IM. If the user exits from other positions to normal mode, this IM will not be remembered.
When the user is in insert mode and there is a cursor movement (typing characters or simply moving the cursor), the character before the cursor will be examined to determine if it is the beginning or end of a string or comment. If it is the beginning, the IM will switch to the previous one; if it is the end, the IM will switch from the previous one to the default IM, and the previous IM will be recorded.
Unimplemented parts:
Distinguishing between IM used for comments and strings. Sometimes, it is desired for strings to use the default IM while comments use the previous IM. However, implementing this feature would essentially overturn the logic of saving IM.
Handling block comments (feasible but not yet implemented).
Listening to CursorMovedI events combined with Treesitter for precise detection might introduce some performance issues (even if using asynchronous logic). Listening for specific character inputs and then processing the logic might significantly improve performance (for example, in Lua language, entering comments requires typing --, so listening for the - character might be more efficient than listening to CursorMovedI events).
The implementation of this feature consists of three parts:
Unimplemented parts: