Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim and c++11 lambda: auto indentation

Tags:

vim

c++11

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?

like image 727
Klaus Avatar asked Nov 09 '11 09:11

Klaus


People also ask

How do I turn on auto indent in Vim?

To automatically indent when editing a file in Vim, enable the auto indenting feature using the :set autoindent flag in command mode: Press Enter, and this will auto-indent the file you are currently editing. If you set the auto-indent feature in Vim in command mode, it does not persist upon closing the editor.

How do I turn off auto indent in Vim?

How to Turn Off Vim Auto Indent. You can also temporarily turn off automatic indenting by typing :set paste in command mode. (:unset paste to turn it back on again.) This is useful — as you might guess from the command name — if you're pasting in chunks of text or code to avoid getting annoying extraneous indents.

How do I set an indent in Vimrc?

To indent the current line, or a visual block: ctrl-t, ctrl-d - indent current line forward, backwards (insert mode) visual > or < - indent block by sw (repeat with . ) then try hitting the F5 key while in insert mode (or just :set paste ).


1 Answers

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).

like image 171
idbrii Avatar answered Sep 22 '22 23:09

idbrii