mformat: better handling of continuation lines

Fixes #13566. Fixes #13567.
pull/13609/head
Charles Brunet 3 months ago committed by Dylan Baker
parent d3ef02b2e4
commit 37c5bffbff
  1. 13
      mesonbuild/mformat.py
  2. 1
      mesonbuild/mparser.py
  3. 10
      test cases/format/1 default/meson.build
  4. 1
      test cases/format/1 default/meson.options

@ -276,6 +276,7 @@ class TrimWhitespaces(FullAstVisitor):
def move_whitespaces(self, from_node: mparser.BaseNode, to_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.value = from_node.whitespaces.value + to_node.whitespaces.value
to_node.whitespaces.is_continuation = from_node.whitespaces.is_continuation
from_node.whitespaces = None from_node.whitespaces = None
to_node.whitespaces.accept(self) to_node.whitespaces.accept(self)
@ -317,7 +318,10 @@ class TrimWhitespaces(FullAstVisitor):
for i, line in enumerate(lines): for i, line in enumerate(lines):
has_nl = line.endswith('\n') has_nl = line.endswith('\n')
line = line.strip() 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: if not in_block_comments:
node.value += self.config.indent_before_comments node.value += self.config.indent_before_comments
else: else:
@ -328,6 +332,8 @@ class TrimWhitespaces(FullAstVisitor):
in_block_comments = True in_block_comments = True
if node.value.endswith('\n'): if node.value.endswith('\n'):
node.value += self.indent_comments node.value += self.indent_comments
if node.is_continuation:
node.value += self.config.indent_by
def visit_SymbolNode(self, node: mparser.SymbolNode) -> None: def visit_SymbolNode(self, node: mparser.SymbolNode) -> None:
super().visit_SymbolNode(node) super().visit_SymbolNode(node)
@ -595,6 +601,9 @@ class ArgumentFormatter(FullAstVisitor):
def visit_MethodNode(self, node: mparser.MethodNode) -> None: def visit_MethodNode(self, node: mparser.MethodNode) -> None:
self.enter_node(node) self.enter_node(node)
node.source_object.accept(self) 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: if node.args.is_multiline:
self.level += 1 self.level += 1
self.add_nl_after(node.lpar, indent=self.level) self.add_nl_after(node.lpar, indent=self.level)
@ -602,6 +611,8 @@ class ArgumentFormatter(FullAstVisitor):
node.args.accept(self) node.args.accept(self)
if node.args.is_multiline: if node.args.is_multiline:
self.level -= 1 self.level -= 1
if is_cont:
self.level -= 1
self.exit_node(node) self.exit_node(node)
def visit_FunctionNode(self, node: mparser.FunctionNode) -> None: def visit_FunctionNode(self, node: mparser.FunctionNode) -> None:

@ -267,6 +267,7 @@ class WhitespaceNode(BaseNode):
self.value = '' self.value = ''
self.append(token) self.append(token)
self.block_indent = False self.block_indent = False
self.is_continuation = False
def append(self, token: Token[str]) -> None: def append(self, token: Token[str]) -> None:
self.value += token.value self.value += token.value

@ -13,6 +13,16 @@ meson_files = {
# Ensure empty function are formatted correctly on long lines # Ensure empty function are formatted correctly on long lines
a = '@0@@1@@2@@3@@4@'.format('one', 'two', 'three', 'four', 'five').strip().strip() 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 foreach name, f : meson_files
test(name, meson_cmd, args: ['format', '--check-only', f]) test(name, meson_cmd, args: ['format', '--check-only', f])
endforeach endforeach

@ -0,0 +1 @@
option('opt', type : 'feature', value : 'auto')
Loading…
Cancel
Save