Vim提供了非常有用的运动命令来跳转到方法的下一个开始/结束:] m,] M,[m和] m.
这些适用于Java或类似的结构化语言.(如:help]中所述m和:help 29.3)
它似乎工作考虑最外层的花括号作为类声明和下一级花括号作为方法声明.
当类定义周围有一对外部花括号时,这些运动命令不起作用,这在C#语言中有点常见.
我想知道是否有一些技巧可以使这些命令(单独和带有运算符的前缀,例如,y [m,V] M)对此代码起作用:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不认为]m映射族可以定制.在这种情况下,通常的做法是使用自定义逻辑覆盖它.我想出了一些应该按照你的描述做的vimscript .基本上,它跳过花括号,并查看相关的行来决定做什么.在这种情况下,它只是忽略"类"和"命名空间"声明.
nnoremap <buffer> ]m :<c-u>call <SID>JumpMethod('{', 'W', 'n')<cr>
nnoremap <buffer> [m :<c-u>call <SID>JumpMethod('{', 'Wb', 'n')<cr>
nnoremap <buffer> ]M :<c-u>call <SID>JumpMethod('}', 'W', 'n')<cr>
nnoremap <buffer> [M :<c-u>call <SID>JumpMethod('}', 'Wb', 'n')<cr>
xnoremap <buffer> ]m :<c-u>call <SID>JumpMethod('{', 'W', 'v')<cr>
xnoremap <buffer> [m :<c-u>call <SID>JumpMethod('{', 'Wb', 'v')<cr>
xnoremap <buffer> ]M :<c-u>call <SID>JumpMethod('}', 'W', 'v')<cr>
xnoremap <buffer> [M :<c-u>call <SID>JumpMethod('}', 'Wb', 'v')<cr>
onoremap <buffer> ]m :<c-u>call <SID>JumpMethod('{', 'W', 'o')<cr>
onoremap <buffer> [m :<c-u>call <SID>JumpMethod('{', 'Wb', 'o')<cr>
onoremap <buffer> ]M :<c-u>call <SID>JumpMethod('}', 'W', 'o')<cr>
onoremap <buffer> [M :<c-u>call <SID>JumpMethod('}', 'Wb', 'o')<cr>
function! s:JumpMethod(char, flags, mode)
let original_cursor = getpos('.')
if a:mode == 'v'
normal! gv
elseif a:mode == 'o'
normal! v
endif
while search(a:char, a:flags) > 0
if a:char == '}'
" jump to the opening one to analyze the definition
normal! %
endif
let current_line = line('.')
if getline(current_line) =~ '^\s*{'
" it's alone on the line, check the above one
let method_line = current_line - 1
else
let method_line = current_line
endif
let method_line_body = getline(method_line)
if method_line_body =~ '\k\+\s*(.*)' && method_line_body !~ '\<\(for\|foreach\|if\|while\|switch\|using\|catch\|get\|set\)\>'
" it's probably a function call
if a:char == '}'
" we need to go back to the closing bracket
normal! %
endif
echo
return
else
if a:char == '}'
" we still need to go back to the closing bracket
normal! %
endif
endif
endwhile
" if we're here, the search has failed, restore cursor position
echo
call setpos('.', original_cursor)
endfunction
Run Code Online (Sandbox Code Playgroud)
请记住,我并不是真的了解很多C#,所以在所有情况下都可能无法正常工作,但如果你给我一些破解的例子,我或许可以找出一些东西.
要尝试它,你应该把它放在你的vimfiles目录下的"ftplugin"下,作为"cs.vim".任何其他以"cs"开头并以".vim"结尾的文件名也是好的,如果你已经有一个"cs.vim"文件.
| 归档时间: |
|
| 查看次数: |
1812 次 |
| 最近记录: |