2015-12-30 43 views
-1

我希望我的預處理器以這種方式進行縮進:如何使用美化器以這種方式縮進宏?

int foo_func() 
{ 
    normal_code(); 
    normal_code(); 
#ifdef FOO 
# define aaa 
# define bbb 
    code_that_only_foo(); 
    code_that_only_foo(); 
#endif 
    normal_code_again(); 
    normal_code_again(); 
} 

我已經試過鐺格式,但之後#指令刪除所有的空間,我沒有找到任何選項來控制這些行爲。所以,

  • 可能clang-format以這種方式執行預處理器縮進嗎?
  • 或者是否有任何其他C++美化器可以做到這一點?
+3

你說 「好地」;我說「ew」 –

+0

@LightnessRacesinOrbit已修改 – jiandingzhe

+0

lol ok –

回答

0

如果你不能找到一個工具,沒有工作,寫一個工具,做:

#!/usr/bin/env python3 

from sys import stdin 
from string import whitespace 


def main(): 
    not_directive = whitespace + '#' 
    indentation = 0 
    for line in stdin: 
     stripped = line.lstrip() 
     if stripped.startswith('#'): 
      directive = stripped.lstrip(not_directive) 
      if directive.startswith('endif'): 
       indentation -= 1 
      print('#{}{}'.format(' ' * indentation, directive), end='') 
      if directive.startswith('if'): 
       indentation += 1 
     else: 
      print(line, end='') 


if __name__ == '__main__': 
    main() 

See it working online

它讀取從標準輸入源,並寫入更改的源到標準輸出。
您可以使用shell輕鬆重定向輸入和輸出。

如果你不知道Python3,並希望瞭解:

  • string.lstrip(chars)回報stringchars刪除從它的beggining。
    chars默認爲whitespace
  • 'test' * 0 =>'''test' * 1 =>'test''test' * 3 =>'testtesttest'
  • '#{}{}'.format('textA', 'textB') =>'#textAtextB'