Formatting AStyle Nested Classes

My project has the following code:

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 this 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 ); } }; ..... 

Astyle team:

 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 it Astil’s mistake, or am I forgetting some options? If this is a mistake, what can you suggest for formatting C ++ code using VIM?

+4
source share
1 answer

Of course, this is a mistake. AStyle is not supported these days. There are many mistakes that sat there forever and were never resolved. You should not expect an improvement in the near future. I highly recommend switching to Uncrustify . Of course, it also has some problems, but they are not so nasty and do not violate your code, as AStyle does. It has hundreds of configuration options - far more flexible than AStyle - so be patient, you will have to spend enough time to customize it to your taste and conventions.

When you are done, and if you want to integrate it correctly with Vim, here you can enter the code that you can add to 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 map this function ( Uncrustify ) to a key combination, or you can do a convenient trick that I use. Create a file ~/.vim/after/ftplugin/cpp.vim , where you can override any Vim settings, especially 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 using the previously created configuration file.

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

+1
source

Source: https://habr.com/ru/post/1433216/


All Articles