问题描述
使用vim与c ++ 11 lambda函数实际上很差。缩进不工作,很多方括号被标记为错误。
由于我知道c和c ++的缩进和语法高亮是内部编程,而不是通过ac [。 ..] vim文件。有没有机会为c ++ 11设置vim,特别是对于带lambda函数的源代码?可能有人可以给出一个提示在内部vim语法检查中如何以及在哪里添加lambda解析?
编辑:(根据注释请求的示例代码)
代码应类似于以下示例,但格式化为单列。
MyLoop([](int a,int b){
{
:: i ++;
for(;;)
{
SomeFunc();
}
cout<<Result:<< a * b<< endl;
}
}
更新vim 7.4:
现在vim不再将lambda表达式作为错误处理,但它仍然没有在lambda表达式中做任何缩进,因此它仍然不能用于c + +: - (
BTW:有任何人一个好的自动格式化工具可以添加到vim环境中,这样按下一个键外部做格式化
Vim 7.4现在有一个 jN
正确的Indent Java匿名类的cinoption。(:help java-cinoptions
)这提高了C ++ 11's lambdas。
使用这些选项(放在〜/ .vim / after / ftplugin / cpp.vim中):
setlocal cindent cino = j1,(0,ws,Ws
$ b b
如果我把你的for循环的大括号移动到同一行(否则是疯狂的),然后vim缩进你的代码如下:
MyLoop([](int a,int b){
{
:: i ++;
for(;;){
SomeFunc();
}
cout<< Result:<< a * b }
});
它不会给你想要的悬挂缩进。
有关所有选项,请参阅:help cinoptions -values
。
如果您想要更精确的缩进程序, 设置equalprg = clang-format
以使用,因此 = ip
将缩进当前段落。这不会使vim在你输入时正确缩进(你需要设置indentexpr,这很复杂)。
还有,似乎做同样设置equalprg,但更多的代码。不知道如果它是更好的。它应该是(来自Cyprian Guerra的回答)的替代方法。 / p>
Using vim with c++11 lambda functions is actually poor. Indentation is not working and a lot of brackets are marked as erroneous.
As I know the indent and syntax highlighting for c and c++ is programmed internally and not via a c[...].vim file. Is there any chance to set up vim for c++11, especially for source code with lambda functions? Maybe someone can give a hint how and where to add lambda parsing in the internal vim syntax checking?
EDIT: (example code as requested from comments)
The code should look like the following example but is formatted to a single column.
MyLoop( [](int a, int b){
{
::i++;
for (;;)
{
SomeFunc();
}
cout << "Result: " << a*b<<endl;
}
});
Update for vim 7.4:Now vim did not longer handle a lambda expression as an error, but it still did NOT do any indentation in the lambda expression and so it is still not usable for c++ anymore :-(
BTW: Has anyone a good auto formatting tool which can be added to vim environment, so that pressing a key externally do the formatting?
Vim 7.4 now has an jN
cinoption for "Indent Java anonymous classes correctly." (:help java-cinoptions
) This improves indenting behavior for C++11's lambdas.
With these options (put in your ~/.vim/after/ftplugin/cpp.vim):
setlocal cindent cino=j1,(0,ws,Ws
And if I move your for loop's opening brace to the same line (otherwise it's crazy) then vim indents your code like this:
MyLoop( [](int a, int b){
{
::i++;
for (;;) {
SomeFunc();
}
cout << "Result: " << a*b<<endl;
}
});
It doesn't give the hanging indent that you want either. If you move the initial opening brace it its own line, then you get your desired hanging indent.
For all the options, see :help cinoptions-values
.
If you want at smarter indenting program, this user recommends set equalprg=clang-format
to use ClangFormat so =ip
will indent the current paragraph. This won't make vim correctly indent as you type (you need to setup indentexpr for that and that's quite complicated).
There's also a vim plugin that seems to do the same as setting equalprg, but with more code. Not sure if it's any better. It's supposed to be an alternative to clang-format.py (from Cyprian Guerra's answer).
这篇关于vim和c ++ 11 lambda:自动缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!