Search code examples
vimneovim

neovim: command not behaving as expected for text objects such as parentheses


I have a caret on line 8:

enter image description here

I do c+i+( and this is what I get:

enter image description here

But I would expect it to go into test2() parenthesis as this is where the caret was when i did the combo. It doesn't. It goes all the way up to the callback().

Why is that happening and how to workaround it?

Code:

/* eslint-disable */

function callback(fn: Function) {
    return fn();
}

const whatever = callback(function test1() {
    function test2() {}

    return test2;
});

Solution

  • Nominal behavior:

    If the cursor is between an opening parenthesis and a closing parenthesis (inclusive), then the expected behavior of <operator>i( is to "operate" on the text located between those two parentheses.

    Same logic for a(.

    Fallback behavior:

    If there are no parentheses around the cursor, then the expected behavior is to "operate" on the content of the closest pair of parentheses forward.

    Same logic for a(.

    In short, you appear to want the fallback behavior despite being in a situation that favors the nominal behavior:

                             +-- opening parenthesis before the cursor
                             |
    const whatever = callback(function test1() {
        function test2() {}
    
        return test2;
    });
     |
     +-- closing parenthesis after the cursor
    

    In that context, what is happening when you do ci( is a) totally normal and expected, and b) not what you want. Therefore, you will have to use a different command, more in line with what you actually want to happen.

    All the following commands do what you want:

    f(a
    f)i
    t)a
    f(ci(
    f)ci(
    

    …and there are certainly other ways. f(a is the more intuitive, IMO.