-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstyles.py
170 lines (155 loc) · 6.45 KB
/
styles.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import copy
import yaml
class StyleOption(object):
def __init__(self, name, options):
self.name = name
self.options = options
class Style(object):
def __init__(self, base=None, style=None, hidden_base_style=None):
if style is None:
if base is not None:
style = {'BasedOnStyle': base}
elif style is None:
style = {}
elif base is None:
base = style['BasedOnStyle']
self.base = base
self.style_dict = style
self.hidden_base_style = hidden_base_style
def __repr__(self):
return 'Style(base=%r, style=%r)' % (self.base, self.style_dict)
def dump(self, output_stream):
yaml.safe_dump(self.style_dict, stream=output_stream, default_flow_style=False)
if self.hidden_base_style:
hidden_keys = [key for key in self.hidden_base_style.style_dict if key not in self.style_dict]
if hidden_keys:
hidden_yaml = yaml.safe_dump({k:self.hidden_base_style.style_dict[k] for k in hidden_keys}, default_flow_style=False)
hidden_yaml = hidden_yaml.splitlines()
output_stream.write('## Other available options and their default values:\n')
for line in hidden_yaml:
output_stream.write('# %s\n' % line)
def style_with_overrides(self, overrides):
# Find the base config to override.
new_base = overrides.get('BasedOnStyle', self.base)
new_style_dict = copy.deepcopy(self.style_dict)
new_style_dict.update(overrides)
return Style(base=new_base, style=new_style_dict)
def style_with_defaults_hidden(self, base_style):
return Style(base=self.base, style=copy.deepcopy(self.style_dict), hidden_base_style=base_style)
# The top-level styles that clang-format supports.
BASE_STYLE_TYPES = [
'LLVM',
'Google',
'Chromium',
'Mozilla',
'WebKit',
]
# Keys that we explicitly ignore, either because they don't really apply to what
# we're doing or they are very obscure.
SKIP_STYLE_OPTIONS = [
'Language',
'CommentPragmas',
'DisableFormat',
'ForEachMacros',
'JavaScriptQuotes',
'JavaScriptWrapImports',
'MacroBlockEnd',
'MacroBlockBegin',
'BreakAfterJavaFieldAnnotations',
]
# Set of all styling options.
# The keys of this dictionary represent each setting that can be tweaked.
# The values are arrays of the options for each setting. Each "option" is a dictionary of key-value pairs
# where the key usually repeats the style's top-level name. This is so that some styles (like the UseTab ones)
# can be coupled and handled together.
STYLE_OPTIONS = {}
# Set up the stylings that are just on or off.
STYLE_OPTIONS.update({
key: StyleOption(key, [
{key:True},
{key:False},
])
for key in [
"AlignConsecutiveAssignments",
"AlignConsecutiveDeclarations",
"AlignEscapedNewlinesLeft",
"AlignOperands",
"AlignTrailingComments",
"AllowAllParametersOfDeclarationOnNextLine",
"AllowShortBlocksOnASingleLine",
"AllowShortCaseLabelsOnASingleLine",
"AllowShortIfStatementsOnASingleLine",
"AllowShortLoopsOnASingleLine",
"AlwaysBreakBeforeMultilineStrings",
"AlwaysBreakTemplateDeclarations",
"BinPackArguments",
"BinPackParameters",
"BreakBeforeTernaryOperators",
"BreakConstructorInitializersBeforeComma",
"BreakStringLiterals",
"ConstructorInitializerAllOnOneLineOrOnePerLine",
"Cpp11BracedListStyle",
"DerivePointerAlignment",
"ExperimentalAutoDetectBinPacking",
"IndentCaseLabels",
"IndentWrappedFunctionNames",
"KeepEmptyLinesAtTheStartOfBlocks",
"ObjCSpaceAfterProperty",
"ObjCSpaceBeforeProtocolList",
"ReflowComments",
"SortIncludes",
"SpaceAfterCStyleCast",
"SpaceAfterTemplateKeyword",
"SpaceBeforeAssignmentOperators",
"SpaceInEmptyParentheses",
"SpacesInAngles",
"SpacesInContainerLiterals",
"SpacesInCStyleCastParentheses",
"SpacesInParentheses",
"SpacesInSquareBrackets",
]
})
# Set up the stylings that have specific options.
STYLE_OPTIONS.update({
key: StyleOption(key, [
{key:option} for option in options
])
for key,options in {
'AccessModifierOffset': [-4, -2, -1, 0, 1, 2, 4],
'AlignAfterOpenBracket': ['Align', 'DontAlign', 'AlwaysBreak'],
'AllowShortFunctionsOnASingleLine': ['All', 'Inline', 'None', 'Empty'],
'AlwaysBreakAfterDefinitionReturnType': ['TopLevel', 'None'],
'AlwaysBreakAfterReturnType': ['None', 'TopLevelDefinitions'],
'BasedOnStyle': ['WebKit', 'Mozilla', 'Chromium', 'LLVM', 'Google'],
'BreakBeforeBinaryOperators': ['None', 'NonAssignment', 'All'],
'BreakBeforeBraces': ['GNU', 'Allman', 'Mozilla', 'Attach', 'Stroustrup', 'Linux', 'WebKit'],
'ColumnLimit': [0, 80, 90, 100, 110, 120],
'ConstructorInitializerIndentWidth': [0, 2, 4],
'ContinuationIndentWidth': [0, 2, 4],
'IncludeCategories': [[{'Regex': '^"(llvm|llvm-c|clang|clang-c)/', 'Priority': 2}, {'Regex': '^(<|"(gtest|isl|json)/)', 'Priority': 3}, {'Regex': '.*', 'Priority': 1}], [{'Regex': '^<.*\\.h>', 'Priority': 1}, {'Regex': '^<.*', 'Priority': 2}, {'Regex': '.*', 'Priority': 3}]],
'IncludeIsMainRegex': ['$', '([-_](test|unittest))?$'],
'IndentWidth': [2, 3, 4, 8],
"MaxEmptyLinesToKeep": [0, 1, 2, 3, 4],
'NamespaceIndentation': ['All', 'None', 'Inner'],
'ObjCBlockIndentWidth': [0, 2, 4],
'PenaltyBreakBeforeFirstCallParameter': [1, 19],
'PenaltyBreakComment': [300, 150],
'PenaltyBreakFirstLessLess': [120, 60],
'PenaltyBreakString': [1000, 500],
'PenaltyExcessCharacter': [1000000, 500000],
'PenaltyReturnTypeOnItsOwnLine': [200, 60],
'PointerAlignment': ['Middle', 'Right', 'Left'],
'SpaceBeforeParens': ['Always', 'Never', 'ControlStatements'],
'SpacesBeforeTrailingComments': [1, 2],
'Standard': ['Auto','Cpp03','Cpp11'],
'Standard': ['Cpp11', 'Cpp03', 'Auto'],
}.iteritems()
})
# The UseTab & TabWidth are coupled.
STYLE_OPTIONS['UseTab'] = StyleOption('UseTab', [
{'UseTab': 'Never', 'TabWidth': 8},
{'UseTab': 'ForIndentation', 'TabWidth': 4},
{'UseTab': 'ForIndentation', 'TabWidth': 8},
{'UseTab': 'Always', 'TabWidth': 4},
{'UseTab': 'Always', 'TabWidth': 8},
])