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;
}
}
}
I don’t think the
]mfamily of mappings can be customized. In such cases, the usual practice is to override it with custom logic. I came up with some vimscript that should do what you describe. Basically, it jumps through curly braces and looks at the relevant line to decide what to do. In this case, it just ignores “class” and “namespace” declarations.Bear in mind that I don’t really know a lot of C#, so it might not work properly in all cases, but if you give me examples that break, I might be able to figure something out.
To try it, you should put it somewhere under “ftplugin” in your vimfiles directory, as “cs.vim”. Any other filename that starts with “cs” and ends in “.vim” is good too, if you already have a “cs.vim” file there.