mardi 17 septembre 2013

Coding Style: Detect tabulation and use a given number of space instead.

 

If you need to respect a Coding Style, and that your coding style require tab to be replaced by a given number of space (2 in my case), you may have to check before each commit that the files you have in your working copy doesn’t contain any tab.

To realize that preliminary step, you can use grep.

Run: grep -n -P "\t" *.h && grep -n -P "\t" *.cpp

It will get you the file name and the line number where grep found tabulation.

Now if we want to automatically apply the Coding Styles rules and replace tabs by 2 spaces we can also use a command line.

find ./ -type f -name "*.h" -exec sed -i 's/\t/  /g' {} \;

and

find ./ -type f -name "*.cpp" -exec sed -i 's/\t/  /g' {} \;

The number of space between \t/ and /g has to be the number of space for tab !

Note that grep, find and sed are Linux command, but Windows developer can also use those command if they setup Cygwin.

Aucun commentaire :

Enregistrer un commentaire