2019-08-22 19:18:52 +02:00
|
|
|
"""Format C code according to QMK's style.
|
|
|
|
"""
|
|
|
|
import subprocess
|
2019-11-13 02:17:12 +01:00
|
|
|
from shutil import which
|
2019-08-22 19:18:52 +02:00
|
|
|
|
|
|
|
from milc import cli
|
2020-05-26 22:05:41 +02:00
|
|
|
|
|
|
|
from qmk.path import normpath
|
|
|
|
from qmk.c_parse import c_source_files
|
2019-08-22 19:18:52 +02:00
|
|
|
|
|
|
|
|
2020-02-23 05:57:19 +01:00
|
|
|
def cformat_run(files, all_files):
|
|
|
|
"""Spawn clang-format subprocess with proper arguments
|
2019-08-22 19:18:52 +02:00
|
|
|
"""
|
2019-11-13 02:17:12 +01:00
|
|
|
# Determine which version of clang-format to use
|
2019-08-22 19:18:52 +02:00
|
|
|
clang_format = ['clang-format', '-i']
|
2021-03-31 01:16:14 +02:00
|
|
|
for clang_version in range(20, 6, -1):
|
2019-11-13 02:17:12 +01:00
|
|
|
binary = 'clang-format-%d' % clang_version
|
|
|
|
if which(binary):
|
|
|
|
clang_format[0] = binary
|
|
|
|
break
|
2019-08-22 19:18:52 +02:00
|
|
|
try:
|
2020-02-23 05:57:19 +01:00
|
|
|
if not files:
|
|
|
|
cli.log.warn('No changes detected. Use "qmk cformat -a" to format all files')
|
|
|
|
return False
|
2020-04-18 22:00:56 +02:00
|
|
|
subprocess.run(clang_format + [file for file in files], check=True)
|
2019-08-22 19:18:52 +02:00
|
|
|
cli.log.info('Successfully formatted the C code.')
|
2019-08-22 22:33:34 +02:00
|
|
|
|
2019-08-22 19:18:52 +02:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
cli.log.error('Error formatting C code!')
|
2019-08-22 22:30:50 +02:00
|
|
|
return False
|
2020-02-23 05:57:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
@cli.argument('-a', '--all-files', arg_only=True, action='store_true', help='Format all core files.')
|
|
|
|
@cli.argument('-b', '--base-branch', default='origin/master', help='Branch to compare to diffs to.')
|
|
|
|
@cli.argument('files', nargs='*', arg_only=True, help='Filename(s) to format.')
|
2020-03-23 21:59:44 +01:00
|
|
|
@cli.subcommand("Format C code according to QMK's style.", hidden=False if cli.config.user.developer else True)
|
2020-02-23 05:57:19 +01:00
|
|
|
def cformat(cli):
|
|
|
|
"""Format C code according to QMK's style.
|
|
|
|
"""
|
|
|
|
# Empty array for files
|
|
|
|
files = []
|
|
|
|
# Core directories for formatting
|
2020-06-06 10:52:19 +02:00
|
|
|
core_dirs = ['drivers', 'quantum', 'tests', 'tmk_core', 'platforms']
|
|
|
|
ignores = ['tmk_core/protocol/usb_hid', 'quantum/template', 'platforms/chibios']
|
2020-02-23 05:57:19 +01:00
|
|
|
# Find the list of files to format
|
|
|
|
if cli.args.files:
|
2020-05-26 22:05:41 +02:00
|
|
|
files.extend(normpath(file) for file in cli.args.files)
|
2020-12-10 20:02:41 +01:00
|
|
|
if cli.args.all_files:
|
|
|
|
cli.log.warning('Filenames passed with -a, only formatting: %s', ','.join(map(str, files)))
|
2020-02-23 05:57:19 +01:00
|
|
|
# If -a is specified
|
|
|
|
elif cli.args.all_files:
|
2020-05-26 22:05:41 +02:00
|
|
|
all_files = c_source_files(core_dirs)
|
2020-02-23 05:57:19 +01:00
|
|
|
# The following statement checks each file to see if the file path is in the ignored directories.
|
|
|
|
files.extend(file for file in all_files if not any(i in str(file) for i in ignores))
|
|
|
|
# No files specified & no -a flag
|
|
|
|
else:
|
|
|
|
base_args = ['git', 'diff', '--name-only', cli.args.base_branch]
|
|
|
|
out = subprocess.run(base_args + core_dirs, check=True, stdout=subprocess.PIPE)
|
|
|
|
changed_files = filter(None, out.stdout.decode('UTF-8').split('\n'))
|
2020-05-26 22:05:41 +02:00
|
|
|
filtered_files = [normpath(file) for file in changed_files if not any(i in file for i in ignores)]
|
2020-02-23 05:57:19 +01:00
|
|
|
files.extend(file for file in filtered_files if file.exists() and file.suffix in ['.c', '.h', '.cpp'])
|
|
|
|
|
|
|
|
# Run clang-format on the files we've found
|
|
|
|
cformat_run(files, cli.args.all_files)
|