Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim [m motion with c#

Tags:

c#

vim

Vim provides very useful motion commands to jump to next start/end of a method: ]m, ]M, [m and ]m.

These works for Java or similar structured language. (as described in :help ]m and :help 29.3)

It seems to work considering the outermost pair of curly braces as class declaration and the next level of curly braces as method declarations.

These motion commands doesn't work when there is an outer pair of curly braces around class definition, which is somewhat common on languages as C#.

I was wondering if there is some trick to make those commands (alone and prefixed with operators, e.g., y[m, V]M) work on this code:

namespace ABC.DEF
{
    class A
    {
        protected string strID;
        public string PortID { get { return strID; } set { strID = value; } }

        protected MyType x;
        public MyType X
        {
            get { return x; }
            set { x = value; if ( x != null ) func1(); }
        }


        int func1()
        {
            return 1;
        }

        int func2(int flag)
        {
            if (flag == 0)
                return flag;


            if (flag > 3)
            {
                return flag;
            }
            return 2;
        }

        int func3()
        {
            return 3;
        }
    }
}
like image 792
mMontu Avatar asked Jul 27 '11 18:07

mMontu


People also ask

What are motions in Vim?

Motions (as in movements) are how you move around in Vim. They are commands that when typed move the cursor around with high speed and precision. There are many of them, and each one is best suited for different types and lengths of movement.

How do I move a line in Neovim?

Mappings to move lines In normal mode or in insert mode, press Alt-j to move the current line down, or press Alt-k to move the current line up.

What is G in Vim?

:g/pattern/cmd :range g/pattern/cmd. The g command first scans all lines in range and marks those that match pattern . It then iterates over the marked lines and executes cmd . (See multi-repeat in vim's documentation).


2 Answers

Andrew Radev gave an answer which idbrii turned into a plugin.

I have modified that plugin here: https://github.com/RobertCWebb/vim-jumpmethod/

I fixed various issues and added improved mappings for [[ and ]] as well

  • Fixed: failed to stop when function heading was split over multiple lines (this happens all the time for me)
  • Fixed: stopped at multi-line if-statements when it shouldn't
  • Fixed: failed to stop at template functions, like Func()
  • Fixed: failed to stop at functions with comments containing words like "if" or "for"
  • Fixed: failed to stop at functions with comment lines between the function name and the brace
  • Fixed previous position marker not being set, ie after using [m or ]m you should be able to use '' to go back to previous line
  • Also, when searching back, I made it scroll up a little so the function name is visible, even though the cursor still lands on the '{'
  • Added mappings for [[, ]], [] and ][ which stop at class, enum and property definitions in addition to functions. Their standard behaviour in vim is often useless for C#. [m and ]m still only stop at methods and functions

Regarding the latter, I found it's more useful to stop at class definitions and properties too.

Properties in C# can contain a get and a set method, but I decided it was probably sufficient to just stop once at the definition of the property itself.

For classes, I noticed that I would jump from a method in one function to a method in another, without even realising I was in a different class. That seems dangerous to me, so it's nice to stop at the class definition too. The [[ and ]] mappings will do this.

like image 74
Robert Webb Avatar answered Oct 12 '22 16:10

Robert Webb


A few weeks ago, a similar question has been asked on vim mailing list, but for C++. Here is the solution I came up with.

It relies on ctags and a few other plugins of mine: lh-dev, lh-tag, and lh-vim-lib. You can install lh-dev via vim-addon-manager, this will install lh-tag and lh-vim-lib in turn.

HTH,

like image 26
Luc Hermitte Avatar answered Oct 12 '22 16:10

Luc Hermitte