From dbfd3e8c41b96d40511f9a683d9718e2aca32275 Mon Sep 17 00:00:00 2001 From: Charles Brunet Date: Wed, 26 Jun 2024 10:44:46 -0400 Subject: [PATCH] format: fix edge case with empty functions format was adding a new empty line each time when trying to split a long line containing a function with no arguments --- mesonbuild/mformat.py | 2 +- mesonbuild/mparser.py | 2 +- test cases/format/1 default/gh13242.meson | 4 +--- test cases/format/1 default/meson.build | 3 +++ 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/mesonbuild/mformat.py b/mesonbuild/mformat.py index e20e45851..42f562e7f 100644 --- a/mesonbuild/mformat.py +++ b/mesonbuild/mformat.py @@ -744,7 +744,7 @@ class ComputeLineLengths(FullAstVisitor): self.exit_node(node) def split_if_needed(self, node: mparser.ArgumentNode) -> None: - if not node.is_multiline and self.length > self.config.max_line_length: + if len(node) and not node.is_multiline and self.length > self.config.max_line_length: arg = self.argument_stack[self.level] if len(self.argument_stack) > self.level else node if not arg.is_multiline: arg.is_multiline = True diff --git a/mesonbuild/mparser.py b/mesonbuild/mparser.py index ec08ccfb2..50d720313 100644 --- a/mesonbuild/mparser.py +++ b/mesonbuild/mparser.py @@ -377,7 +377,7 @@ class ArgumentNode(BaseNode): return self.order_error def __len__(self) -> int: - return self.num_args() # Fixme + return self.num_args() + self.num_kwargs() @dataclass(unsafe_hash=True) class ArrayNode(BaseNode): diff --git a/test cases/format/1 default/gh13242.meson b/test cases/format/1 default/gh13242.meson index b9122ec81..67f7dd1bb 100644 --- a/test cases/format/1 default/gh13242.meson +++ b/test cases/format/1 default/gh13242.meson @@ -10,9 +10,7 @@ test( ) test( - should_fail: (settings.get('x', false) and not settings['y'] and dep.version( - - ).version_compare( + should_fail: (settings.get('x', false) and not settings['y'] and dep.version().version_compare( '>=1.2.3', )), ) diff --git a/test cases/format/1 default/meson.build b/test cases/format/1 default/meson.build index 35e5b9694..83ae19aa0 100644 --- a/test cases/format/1 default/meson.build +++ b/test cases/format/1 default/meson.build @@ -10,6 +10,9 @@ meson_files = { 'gh13242': files('gh13242.meson'), } +# Ensure empty function are formatted correctly on long lines +a = '@0@@1@@2@@3@@4@'.format('one', 'two', 'three', 'four', 'five').strip().strip() + foreach name, f : meson_files test(name, meson_cmd, args: ['format', '--check-only', f]) endforeach