Skip to content

Commit

Permalink
meson format command
Browse files Browse the repository at this point in the history
  • Loading branch information
bruchar1 committed Oct 3, 2023
1 parent 2a0b569 commit faf3d33
Show file tree
Hide file tree
Showing 38 changed files with 2,002 additions and 5 deletions.
71 changes: 71 additions & 0 deletions docs/markdown/Commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,74 @@ format should be used. There are currently 3 formats supported:
seems to be properly supported by vscode.

{{ devenv_arguments.inc }}


### format

*(since 1.3.0)*

{{ format_usage.inc }}

Format specified `meson.build` documents. For compatibility with `muon`, `fmt`
is an alias to `format`.

{{ format_arguments.inc }}

The configuration file is a `.ini` file.
The following options are recognized:

- max_line_length (int): When an array, a dict, a function or a method
would be longer that this, it is formatted one argument per line
(Default is 80).
- indent_by (str): Intentation to use (Default is four spaces `' '`).
- space_array (bool): Whether to add spaces betwen `[]` and array
arguments (default is false).
- kwargs_force_multiline (bool): If true, arguments are formatted one per
line as soon as there is a keyword argument (default is false).
- wide_colon (bool): If true, a space is placed before colon in dict
and in keyword arguments (default is false).
- no_single_comma_function (bool): If true, a comma is never appended
to function arguments if there is only one argument, even if
using multiline arguments (default is false).
- end_of_line ('cr', 'lf', 'crlf', 'native'): Line ending to use
(applied when using `--output` or `--inline` argument) (default
is 'native).
- indent_before_comments (str): Indentation to use before inline comments
(default is two spaces `' '`).
- simplify_string_literals (bool): When true, multiline strings are
converted to single line strings if they don't contain newlines.
Formatted strings are converted to normal strings if they don't
contain substitutions (default is true).
- inserty_final_newline (bool): If true, force the `meson.build` file
to end with a newline character (default is true).
- tab_width (int): Width of tab stops, used to compute line length
when `indent_by` uses tab characters (default is 4).
- sort_files (bool): When true, arguments of `files()` function are
sorted alphabetically (default is false).
- group_arg_value (bool): When true, string argument with `--` prefix
followed by string argument without `--` prefix are grouped on the
same line, in multiline arguments (default is false).

The first six options are the same than for the `muon fmt` command.

It is also possible to use a `.editorconfig` file, when providing
the `--editor-config` option on the command line.

When `--recursive` option is specified, `meson.build` files from
`subdir` are also analysed (must be used in conjunction with `--inplace`
or `--check-only` option).


#### Differences with `muon fmt`

The `meson format` command should be compatible with the `muon fmt` command.
However, it has more features, and some diffences:

- By default, `meson format` put two spaces before inline comments,
while `muon fmt` only puts one.
- `muon fmt` can potentially mix crlf and lf end-of-lines, as it is not aware
of them. `meson format` will always be consistent in the output it produces.
- `muon fmt` only recognize the
`meson format` recognizes max_line_length, end_of_line, insert_final_newline,
and tab_width options in .editorconfig files.
- `meson format` has many additional format rules (see option list above).
4 changes: 4 additions & 0 deletions docs/markdown/snippets/meson_format_cmd.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## New meson format command

This command is similar to `muon fmt` and allow to format a `meson.build`
document.
4 changes: 1 addition & 3 deletions mesonbuild/ast/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
# or an interpreter-based tool
from __future__ import annotations

from mesonbuild import mparser

from .. import mparser
from .visitor import AstVisitor, FullAstVisitor

Expand Down Expand Up @@ -244,7 +242,7 @@ def visit_default_func(self, node: mparser.BaseNode) -> None:
assert hasattr(node, 'value')
self.result += node.value
self.exit_node(node)

def visit_EmptyNode(self, node: mparser.EmptyNode) -> None:
self.enter_node(node)
self.exit_node(node)
Expand Down
4 changes: 3 additions & 1 deletion mesonbuild/mesonmain.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def errorhandler(e, command):
class CommandLineParser:
def __init__(self):
# only import these once we do full argparse processing
from . import mconf, mdist, minit, minstall, mintro, msetup, mtest, rewriter, msubprojects, munstable_coredata, mcompile, mdevenv
from . import mconf, mdist, minit, minstall, mintro, msetup, mtest, rewriter, msubprojects, munstable_coredata, mcompile, mdevenv, mformat
from .scripts import env2mfile
from .wrap import wraptool
import shutil
Expand Down Expand Up @@ -109,6 +109,8 @@ def __init__(self):
help_msg='Run commands in developer environment')
self.add_command('env2mfile', env2mfile.add_arguments, env2mfile.run,
help_msg='Convert current environment to a cross or native file')
self.add_command('format', mformat.add_arguments, mformat.run, aliases=['fmt'],
help_msg='Format meson source file')
# Add new commands above this line to list them in help command
self.add_command('help', self.add_help_arguments, self.run_help_command,
help_msg='Print help of a subcommand')
Expand Down
Loading

0 comments on commit faf3d33

Please sign in to comment.