Skip to content

Commit 9a9d137

Browse files
committed
style: Handle continuation line indentation in formatting script
1 parent 91e7384 commit 9a9d137

1 file changed

Lines changed: 34 additions & 18 deletions

File tree

scripts/cpp/convert_leading_spaces_to_tabs.py

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,18 @@ def get_indent_depth(node):
107107
return depth
108108

109109

110-
def is_continuation_line(node, line_num):
111-
"""Check if this line is a continuation of a multi-line statement.
110+
def get_continuation_parent(node, line_num):
111+
"""Find the parent statement that this line is a continuation of.
112+
113+
Returns the parent statement node if this is a continuation line,
114+
or None if it's not a continuation.
112115
113116
A continuation line is one where the innermost meaningful AST node
114-
started on a previous line. We skip these because their alignment
115-
is a style choice we don't want to change in this PR.
117+
started on a previous line (e.g., multi-line function arguments,
118+
constructor initializer lists, multi-line conditions).
116119
"""
117120
if node is None:
118-
return False
121+
return None
119122

120123
# Walk up to find the nearest statement-level node
121124
current = node
@@ -137,10 +140,21 @@ def is_continuation_line(node, line_num):
137140
'field_initializer_list', 'field_initializer',
138141
'base_class_clause', 'initializer_pair',
139142
):
140-
return True
143+
return current
141144
current = current.parent
142145

143-
return False
146+
return None
147+
148+
149+
def is_closing_delimiter_line(stripped):
150+
"""Check if a line is just a closing delimiter, possibly with trailing punctuation.
151+
152+
Matches lines like ')', ');', ') {', ']);', etc.
153+
These should be at the parent statement's depth, not +1.
154+
"""
155+
# Strip trailing semicolons, braces, commas
156+
s = stripped.rstrip(';,{ ')
157+
return s in (')', ']', '}')
144158

145159

146160

@@ -260,16 +274,18 @@ def process_file(filepath, dry_run=False, verbose=False):
260274
# However, single-line /* */ comments on their own line are also
261275
# fine to reindent.
262276

263-
# Skip continuation lines
264-
if is_continuation_line(node, line_idx):
265-
new_lines.append(line)
266-
skipped += 1
267-
if verbose:
268-
print(f" SKIP continuation L{line_idx+1}: {line.rstrip()[:80]}")
269-
continue
270-
271-
# Calculate expected depth
272-
depth = get_indent_depth(node)
277+
# Continuation lines: indent at parent statement depth + 1.
278+
# Closing delimiters on their own line go at parent depth (no +1).
279+
continuation_parent = get_continuation_parent(node, line_idx)
280+
if continuation_parent is not None:
281+
parent_depth = get_indent_depth(continuation_parent)
282+
if is_closing_delimiter_line(stripped):
283+
depth = parent_depth
284+
else:
285+
depth = parent_depth + 1
286+
else:
287+
# Calculate expected depth
288+
depth = get_indent_depth(node)
273289

274290
# Special handling: the opening/closing braces of a compound_statement
275291
# should be at the parent's depth, not the compound_statement's depth.
@@ -389,7 +405,7 @@ def main():
389405

390406
action = "Would change" if args.dry_run else "Changed"
391407
print(f"\n{action} {total_changed} lines across {total_files_modified} files")
392-
print(f"Skipped {total_skipped} lines (continuations, ambiguous)")
408+
print(f"Skipped {total_skipped} lines (ambiguous)")
393409

394410

395411
if __name__ == '__main__':

0 commit comments

Comments
 (0)