Skip to content

Commit

Permalink
mformat: better handling of continuation lines
Browse files Browse the repository at this point in the history
Fixes #13566. Fixes #13567.
  • Loading branch information
bruchar1 authored and dcbaker committed Sep 6, 2024
1 parent d3ef02b commit 37c5bff
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
13 changes: 12 additions & 1 deletion mesonbuild/mformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ def exit_node(self, node: mparser.BaseNode) -> None:

def move_whitespaces(self, from_node: mparser.BaseNode, to_node: mparser.BaseNode) -> None:
to_node.whitespaces.value = from_node.whitespaces.value + to_node.whitespaces.value
to_node.whitespaces.is_continuation = from_node.whitespaces.is_continuation
from_node.whitespaces = None
to_node.whitespaces.accept(self)

Expand Down Expand Up @@ -317,7 +318,10 @@ def visit_WhitespaceNode(self, node: mparser.WhitespaceNode) -> None:
for i, line in enumerate(lines):
has_nl = line.endswith('\n')
line = line.strip()
if line.startswith('#'):
if line.startswith('\\'):
node.value += ' ' # add space before \
node.is_continuation = True
elif line.startswith('#'):
if not in_block_comments:
node.value += self.config.indent_before_comments
else:
Expand All @@ -328,6 +332,8 @@ def visit_WhitespaceNode(self, node: mparser.WhitespaceNode) -> None:
in_block_comments = True
if node.value.endswith('\n'):
node.value += self.indent_comments
if node.is_continuation:
node.value += self.config.indent_by

def visit_SymbolNode(self, node: mparser.SymbolNode) -> None:
super().visit_SymbolNode(node)
Expand Down Expand Up @@ -595,13 +601,18 @@ def visit_DictNode(self, node: mparser.DictNode) -> None:
def visit_MethodNode(self, node: mparser.MethodNode) -> None:
self.enter_node(node)
node.source_object.accept(self)
is_cont = node.source_object.whitespaces and node.source_object.whitespaces.is_continuation
if is_cont:
self.level += 1
if node.args.is_multiline:
self.level += 1
self.add_nl_after(node.lpar, indent=self.level)
self.is_function_arguments = True
node.args.accept(self)
if node.args.is_multiline:
self.level -= 1
if is_cont:
self.level -= 1
self.exit_node(node)

def visit_FunctionNode(self, node: mparser.FunctionNode) -> None:
Expand Down
1 change: 1 addition & 0 deletions mesonbuild/mparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ def __init__(self, token: Token[str]):
self.value = ''
self.append(token)
self.block_indent = False
self.is_continuation = False

def append(self, token: Token[str]) -> None:
self.value += token.value
Expand Down
10 changes: 10 additions & 0 deletions test cases/format/1 default/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ meson_files = {
# Ensure empty function are formatted correctly on long lines
a = '@0@@1@@2@@3@@4@'.format('one', 'two', 'three', 'four', 'five').strip().strip()

_opt = get_option('opt') \
.disable_if(
true,
error_message: 'This is an error message because opt can\'t be enabled',
) \
.enable_if(
false,
error_message: 'This is an error message because opt can\'t be disabled',
)

foreach name, f : meson_files
test(name, meson_cmd, args: ['format', '--check-only', f])
endforeach
1 change: 1 addition & 0 deletions test cases/format/1 default/meson.options
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
option('opt', type : 'feature', value : 'auto')

0 comments on commit 37c5bff

Please sign in to comment.