2014-11-04 34 views
4

我在我的主目錄.clang-format並設置indentwidth 4如下。設置IndentWidth不工作在鐺格式

BasedOnStyle: LLVM 
Standard:  Cpp11 
IndentWidth:  4 
TabWidth:  4 
UseTab:   Never 

但是當我使用clang-format -style='~/.clang-format' a.cpp格式化我的代碼,縮進寬度變爲2。等:

// See this indent width 2. The original file has width 4, 
// but clang-format changes it to width 2. 
int main(int argc, char const *argv[]) { 
    A a; 
    a.bar(); 

鐺格式--version的輸出是

LLVM (http://llvm.org/): 
    LLVM version 3.4.2 
    Optimized build. 
    Default target: x86_64-unknown-linux-gnu 
    Host CPU: core-avx-i 

我怎樣才能讓我的代碼(.h,.c,..)以縮進寬度4格式格式化?

回答

8

http://clang.llvm.org/docs/ClangFormat.html

-style選項不使用文件路徑。它使用字符串file來指示.clang格式文件的使用,並且在轉換stdin時,它會在要處理的文件的父目錄或工作目錄及其父目錄中查找該文件。

你也可以把它直接設置你想要的選項字符串:

clang-format -style="{IndentWidth: 4,TabWidth: 4}" 

您還可以使用-dump-config選項來檢查配置。


-style='~/.clang-format'

使用~指你的主目錄通常依賴於外殼通配。 shell不會爲你這樣做。所以即使-style確實走上了一條路,這也不會產生正確的道路。

+1

感謝您對-style = file的解釋。我從來沒有想過'file'意味着一個文字'文件'。 – 2014-11-04 17:09:44