AStyle nested class formatting

I have the following code in my project:

class RangeConverter {
private:
    struct Converter {
                    double MinimumInput;
                    double MaximumInput;

                    double MinimumOutput;
                    double MaximumOutput;

                    template <typename RangeType>
                    RangeType Convert ( RangeType invalue ) const {
                        double v = static_cast<double> ( invalue );
                        if ( v < MinimumInput ) {
                            v = MinimumInput;
                        } else if ( v > MaximumInput ) {
                            v = MaximumInput;
                        }
                        double interpolationfactor = ( v - MinimumInput ) / ( MaximumInput - MinimumInput );
                        return static_cast<RangeType> ( ( interpolationfactor * ( MaximumOutput - MinimumOutput ) ) + MinimumOutput );
                    }
                };
.....

After formatting that code with AStyle I get the following:

class RangeConverter {
private:
    struct Converter {
        ngeConverter {
        private:
            struct Converter {
                double MinimumInput;
                double MaximumInput;

                double MinimumOutput;
                double MaximumOutput;

                template <typename RangeType>
                RangeType Convert ( RangeType invalue ) const {
                    double v = static_cast<double> ( invalue );
                    if ( v < MinimumInput ) {
                        v = MinimumInput;
                    } else if ( v > MaximumInput ) {
                        v = MaximumInput;
                    }
                    double interpolationfactor = ( v - MinimumInput ) / ( MaximumInput - MinimumInput );
                    return static_cast<RangeType> ( ( interpolationfactor * ( MaximumOutput - MinimumOutput ) ) + MinimumOutput );
                }
            };
.....

The astyle command:

astyle
    \   --style=java
    \   --indent=force-tab=2
    \   --indent-classes
    \   --indent-switches
    \   --indent-labels
    \   --indent-preprocessor
    \   --indent-col1-comments
    \   --pad-oper
    \   --pad-paren
    \   --delete-empty-lines
    \   --add-brackets
    \   --align-pointer=type
    \   --align-reference=type

Is that a bug of astyle, or I forget any options? If it is a bug, what could you suggest to format C++ code with VIM?

Answers


Sure, it's a bug. AStyle is not supported well these days. There are lots of bugs that were sitting there forever and never got resolved. You shouldn't expect the situation to improve in the nearest future. I highly recommend switching to Uncrustify. Certainly, it has some issues too, but they are not so nasty and do not break your code like AStyle does. It has hundreds of configuration options - much more flexible than AStyle - so be patient you'll have to spend quite some time to tweak it to your tastes and conventions.

When you are done and if you'd like to integrate it with Vim properly, here you go the code you could add into your .vimrc:

" Restore cursor position, window position, and last search after running a
" command.
function! Preserve(command)
  " Save the last search.
  let search = @/

  " Save the current cursor position.
  let cursor_position = getpos('.')

  " Save the current window position.
  normal! H
  let window_position = getpos('.')
  call setpos('.', cursor_position)

  " Execute the command.
  execute a:command

  " Restore the last search.
  let @/ = search

  " Restore the previous window position.
  call setpos('.', window_position)
  normal! zt

  " Restore the previous cursor position.
  call setpos('.', cursor_position)
endfunction

" Specify path to your Uncrustify configuration file.
let g:uncrustify_cfg_file_path =
    \ shellescape(fnamemodify('~/.uncrustify.cfg', ':p'))

" Don't forget to add Uncrustify executable to $PATH (on Unix) or 
" %PATH% (on Windows) for this command to work.
function! Uncrustify(language)
  call Preserve(':silent %!uncrustify'
      \ . ' -q '
      \ . ' -l ' . a:language
      \ . ' -c ' . g:uncrustify_cfg_file_path)
endfunction

Now you can either map this function (Uncrustify) to a combination of keys or you could do the convenient trick that I use. Create a file ~/.vim/after/ftplugin/cpp.vim where you can override any Vim settings particularly for C++ and add the following line there:

autocmd BufWritePre <buffer> :call Uncrustify('cpp')

This basically adds a pre-save hook. Now when you save the file with C++ code it will be automatically formatted by Uncrustify utilizing the configuration file you supplied earlier.

NOTE: Everything presented here is well-tested and used every day by me.


Need Your Help

Accessing html parameter in PHP

php session

I'm trying to do a simple test php script for sessions. Basically it increments a counter (stored in $_SESSION) every time you refresh that page. That works, but I'm trying to have a link to destro...

Change themes in Vaadin 7 via code

themes vaadin

I am doing a project in Vaadin 7. In that I need to change the theme of a page.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.