cformat.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. """Format C code according to QMK's style.
  2. """
  3. import os
  4. import subprocess
  5. from milc import cli
  6. @cli.argument('files', nargs='*', arg_only=True, help='Filename(s) to format.')
  7. @cli.subcommand("Format C code according to QMK's style.")
  8. def cformat(cli):
  9. """Format C code according to QMK's style.
  10. """
  11. clang_format = ['clang-format', '-i']
  12. # Find the list of files to format
  13. if not cli.args.files:
  14. for dir in ['drivers', 'quantum', 'tests', 'tmk_core']:
  15. for dirpath, dirnames, filenames in os.walk(dir):
  16. if 'tmk_core/protocol/usb_hid' in dirpath:
  17. continue
  18. for name in filenames:
  19. if name.endswith('.c') or name.endswith('.h') or name.endswith('.cpp'):
  20. cli.args.files.append(os.path.join(dirpath, name))
  21. # Run clang-format on the files we've found
  22. try:
  23. subprocess.run(clang_format + cli.args.files, check=True)
  24. cli.log.info('Successfully formatted the C code.')
  25. except subprocess.CalledProcessError:
  26. cli.log.error('Error formatting C code!')
  27. return False