From 37c5bffbffc6a930522762838012390b80ca833e Mon Sep 17 00:00:00 2001 From: Charles Brunet Date: Tue, 20 Aug 2024 14:52:16 -0400 Subject: [PATCH] mformat: better handling of continuation lines Fixes #13566. Fixes #13567. --- mesonbuild/mformat.py | 13 ++++++++++++- mesonbuild/mparser.py | 1 + test cases/format/1 default/meson.build | 10 ++++++++++ test cases/format/1 default/meson.options | 1 + 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test cases/format/1 default/meson.options diff --git a/mesonbuild/mformat.py b/mesonbuild/mformat.py index d87674bc7ea9..119c89351ec4 100644 --- a/mesonbuild/mformat.py +++ b/mesonbuild/mformat.py @@ -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) @@ -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: @@ -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) @@ -595,6 +601,9 @@ 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) @@ -602,6 +611,8 @@ def visit_MethodNode(self, node: mparser.MethodNode) -> None: 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: diff --git a/mesonbuild/mparser.py b/mesonbuild/mparser.py index 79ece3789cd6..4f43455468c6 100644 --- a/mesonbuild/mparser.py +++ b/mesonbuild/mparser.py @@ -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 diff --git a/test cases/format/1 default/meson.build b/test cases/format/1 default/meson.build index 83ae19aa0bde..d3bb153eeb92 100644 --- a/test cases/format/1 default/meson.build +++ b/test cases/format/1 default/meson.build @@ -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 diff --git a/test cases/format/1 default/meson.options b/test cases/format/1 default/meson.options new file mode 100644 index 000000000000..13f5e19c0e68 --- /dev/null +++ b/test cases/format/1 default/meson.options @@ -0,0 +1 @@ +option('opt', type : 'feature', value : 'auto')