2012-09-09 43 views
4

我已經在我的項目下面的代碼:的astyle嵌套的類格式

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

格式化該代碼的astyle後,我得到如下:

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命令:

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 

這是astyle的缺陷,還是我忘記了任何選擇? 如果它是一個錯誤,你會建議用VIM格式化C++代碼嗎?

回答

1

當然,這是一個錯誤。目前,AStyle得不到很好的支持。有很多的錯誤永遠坐在那裏,永遠不會解決。你不應該指望這種情況在不久的將來會有所改善。我強烈建議切換到Uncrustify。當然,它也有一些問題,但它們並不那麼討厭,不會像AStyle那樣破壞你的代碼。它有數百個配置選項 - 比AStyle更靈活 - 所以要耐心等待,你將不得不花費相當長的時間來調整它以適應你的口味和慣例。

當你完成,如果你想將它與Vim的正確整合,在這裏你走你的代碼可以添加到您的.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 

現在你可以映射此功能(Uncrustify)以組合鍵或你可以做我使用的方便伎倆。創建一個文件~/.vim/after/ftplugin/cpp.vim在這裏您可以覆蓋任何Vim的設置,特別是C++,並添加下面的行:

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

這基本上增加了預存掛鉤。現在,當您使用C++代碼保存文件時,它將使用您之前提供的配置文件由Uncrustify自動進行格式化。

注意:這裏呈現的一切都經過了充分測試,每天都在使用我。