-
Notifications
You must be signed in to change notification settings - Fork 9
/
sort_includes.py
104 lines (90 loc) · 2.95 KB
/
sort_includes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import os
for root, _, files in os.walk(os.path.dirname(os.path.realpath(__file__))):
for file in files:
file_name, file_extension = os.path.splitext(file)
if "cmake-build-" in root:
continue
if file_extension not in [".h", ".hpp", ".c", ".cpp"]:
continue
# Read file
file_path = os.path.join(root, file)
with open(file_path) as file:
try:
all_lines = file.readlines()
except Exception as _:
continue
# Extract lines
include_lines = []
lines_before = []
lines_after = []
waiting_for_includes = True
for line in all_lines:
line = line.rstrip() + "\n" # remove trailing whitespace
if line.lstrip().startswith("#include"):
include_lines.append(str(line.lstrip()).replace("<", '"').replace(">", '"'))
waiting_for_includes = False
elif waiting_for_includes:
lines_before.append(line)
else:
lines_after.append(line)
# Alphabetise includes
include_lines.sort(key=lambda line: line.upper())
# Use angle brackets for standard libraries
standard_libraries = [
'"algorithm"',
'"cassert"',
'"chrono"',
'"cmath"',
'"cstring"',
'"ctype.h"',
'"filesystem"',
'"functional"',
'"inttypes.h"',
'"iostream"',
'"iostream.h"',
'"limits"',
'"list"',
'"map"',
'"memory"',
'"mutex"',
'"numeric"',
'"optional"',
'"Python.h"',
'"ranges"',
'"regex"',
'"span"',
'"stdarg.h"',
'"stdbool.h"',
'"stdint.h"',
'"stdio.h"',
'"stdlib.h"',
'"string"',
'"string.h"',
'"thread"',
'"time.h"',
'"type_traits"',
'"unordered_map"',
'"unordered_set"',
'"variant"',
'"vector"',
]
standard_libraries.extend(
[
"BinaryData.h",
"juce_gui_basics/juce_gui_basics.h",
"juce_gui_extra/juce_gui_extra.h",
"juce_opengl/juce_opengl",
]
)
for index, _ in enumerate(include_lines):
for standard_library in standard_libraries:
if standard_library in include_lines[index]:
include_lines[index] = include_lines[index].replace('"', ">").replace(" >", " <")
# Overwrite original file
with open(file_path, "w") as file:
for line in lines_before:
file.write(line)
for line in include_lines:
file.write(line)
for line in lines_after:
file.write(line)