Klaus
Klaus

Reputation: 25663

vim and c++11 lambda: auto indentation

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?

Upvotes: 22

Views: 5840

Answers (6)

cprn
cprn

Reputation: 1710

What you seem to be looking for is clang-format.py from LLVM guys or vim-clang-format from Linda_pp a.k.a. rhysd (thanks idbrii for the latter) - both use clang therefore both support all of the C++11 new structures and you can run it as follows:

:pyf ~/src/llvm.git/tools/clang/tools/clang-format/clang-format.py

or :ClangFormat

The latter seems better as it (among other features) allows to follow the "one config file for the same behaviour on every environment" rule, especially when you use a VIM add-on manager (e.g. VAM).

Upvotes: 0

idbrii
idbrii

Reputation: 11966

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

Upvotes: 25

kevr
kevr

Reputation: 455

I just struggled with this for a few days, looks like toggling on smartindent with cindent enabled helped me out with it lambda indentation in C++11.

In your ~/.vimrc

set cindent
set smartindent

Hope this helps peeps out.

Upvotes: 0

NomadThanatos
NomadThanatos

Reputation: 1

set smartindent autoindent

Then you type above code in vim,you will get the right indentation.

BUT,if you use gg=G or commands that contain "=" to re-indent above code,the indentation is still wrong.

Upvotes: 0

Codie CodeMonkey
Codie CodeMonkey

Reputation: 7946

There is this project by Michael Small. I haven't tried it yet, but perhaps it's what you're looking for.

Upvotes: 1

Benoit
Benoit

Reputation: 79243

Use

:let c_no_curly_error = 1

You can put that into your vimrc for example.

As for your assumption, it is wrong. There is a c.vim syntax file (in vim runtime). You can see a reference in :help c.vim and the implementation by looking it up in your vim runtime paths (:echo &rtp).

Upvotes: 1

Related Questions