diff options
author | skullY <skullydazed@gmail.com> | 2019-08-22 10:18:52 -0700 |
---|---|---|
committer | skullydazed <skullydazed@users.noreply.github.com> | 2019-08-31 08:50:25 -0700 |
commit | 95477749629407e2a9e33c6ccf26ecc8b24ab07a (patch) | |
tree | 77749d8b0a0aaa6e7045aa2f12138f005886b463 | |
parent | d076234fd115ca7a3e5c0f9418e2b28b80c7ec20 (diff) | |
download | qmk_firmware-95477749629407e2a9e33c6ccf26ecc8b24ab07a.tar.gz qmk_firmware-95477749629407e2a9e33c6ccf26ecc8b24ab07a.zip |
CLI command to format C code
-rw-r--r-- | docs/cli.md | 10 | ||||
-rw-r--r-- | lib/python/qmk/cli/cformat.py | 27 |
2 files changed, 37 insertions, 0 deletions
diff --git a/docs/cli.md b/docs/cli.md index 1843f42cd..4b8472b19 100644 --- a/docs/cli.md +++ b/docs/cli.md | |||
@@ -36,3 +36,13 @@ qmk compile <configuratorExport.json> | |||
36 | ``` | 36 | ``` |
37 | qmk compile -kb <keyboard_name> -km <keymap_name> | 37 | qmk compile -kb <keyboard_name> -km <keymap_name> |
38 | ``` | 38 | ``` |
39 | |||
40 | ## `qmk cformat` | ||
41 | |||
42 | This command formats C code using clang-format. Run it with no arguments to format all core code, or pass filenames on the command line to run it on specific files. | ||
43 | |||
44 | **Usage**: | ||
45 | |||
46 | ``` | ||
47 | qmk cformat [file1] [file2] [...] [fileN] | ||
48 | ``` | ||
diff --git a/lib/python/qmk/cli/cformat.py b/lib/python/qmk/cli/cformat.py new file mode 100644 index 000000000..f7020f4c5 --- /dev/null +++ b/lib/python/qmk/cli/cformat.py | |||
@@ -0,0 +1,27 @@ | |||
1 | """Format C code according to QMK's style. | ||
2 | """ | ||
3 | import os | ||
4 | import subprocess | ||
5 | |||
6 | from milc import cli | ||
7 | |||
8 | |||
9 | @cli.entrypoint("Format C code according to QMK's style.") | ||
10 | def main(cli): | ||
11 | """Format C code according to QMK's style. | ||
12 | """ | ||
13 | clang_format = ['clang-format', '-i'] | ||
14 | code_files = [] | ||
15 | for dir in ['drivers', 'quantum', 'tests', 'tmk_core']: | ||
16 | for dirpath, dirnames, filenames in os.walk(dir): | ||
17 | if 'tmk_core/protocol/usb_hid' in dirpath: | ||
18 | continue | ||
19 | for name in filenames: | ||
20 | if name.endswith('.c') or name.endswith('.h') or name.endswith('.cpp'): | ||
21 | code_files.append(os.path.join(dirpath, name)) | ||
22 | |||
23 | try: | ||
24 | subprocess.run(clang_format + code_files, check=True) | ||
25 | cli.log.info('Successfully formatted the C code.') | ||
26 | except subprocess.CalledProcessError: | ||
27 | cli.log.error('Error formatting C code!') | ||