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 d87674bc7..119c89351 100644 --- a/mesonbuild/mformat.py +++ b/mesonbuild/mformat.py @@ -276,6 +276,7 @@ class TrimWhitespaces(FullAstVisitor): 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 @@ class TrimWhitespaces(FullAstVisitor): 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 @@ class TrimWhitespaces(FullAstVisitor): 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 @@ class ArgumentFormatter(FullAstVisitor): 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 @@ class ArgumentFormatter(FullAstVisitor): 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 79ece3789..4f4345546 100644 --- a/mesonbuild/mparser.py +++ b/mesonbuild/mparser.py @@ -267,6 +267,7 @@ class WhitespaceNode(BaseNode): 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 83ae19aa0..d3bb153ee 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 000000000..13f5e19c0 --- /dev/null +++ b/test cases/format/1 default/meson.options @@ -0,0 +1 @@ +option('opt', type : 'feature', value : 'auto')