Skip to content

Commit 1552e43

Browse files
committed
Document black code formatting
1 parent 7927554 commit 1552e43

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

docs/IDE.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Recommended _Format on Save_ configuration (**IntelliJ IDEA > Preferences > Tool
3333
2. Check "Optimize imports" (Files:Java)
3434
3. Check "Run code cleanup". This removes unused imports.
3535

36-
##### Making IntelliJ Feel Similar to Eclipse (Optional)
36+
#### Making IntelliJ Feel Similar to Eclipse (Optional)
3737

3838
Set IntelliJ to use the Eclipse compiler by going to *IntelliJ IDEA > Preferences > Build, Execution, Deployment > Java Compiler*
3939
To make IntelliJ work the same way as Eclipse with respect to Problems View and recompilation you need to:
@@ -42,6 +42,31 @@ To make IntelliJ work the same way as Eclipse with respect to Problems View and
4242
2. Open the problems view: View > Tool Windows > Problems
4343
3. Navigate the problems with Cmd ⌥ ↑ and Cmd ⌥ ↓
4444

45+
#### Mx Development
46+
47+
Developing mx itself can also be done using IntelliJ with the python plugin (or PyCharm) and `mx intellijinit` (in the
48+
mx repository or any other mx suite) as described above is sufficient to add projects for the mx sources.
49+
50+
The main source code is generated as an IntelliJ project named `mx`. In the *Project* view, it will appear
51+
as `src [mx]` (similarly there is the `mx_tests` project that appears as `tests [mx_tests]`).
52+
53+
##### Formatting
54+
55+
Since 2023.2, IntelliJ with the python plugin (and PyCharm) have built-in support for the *Black* formatter.
56+
It can be enabled under `Settings > Tools > Black` and it is recommended to turn on both `On code reformat`
57+
and `On save`.
58+
In the same setting window, a `black` executable with the correct version should be configured, see
59+
the [Style Guide](./Styleguide.md) for more information.
60+
61+
By default, this will produce a notification popup everytime *Black* fails to format a file, including when the file is
62+
ignored by the formatter.
63+
This can become annoying and can be turned off under:
64+
65+
```
66+
Settings > Appearance & Behavior > Notifications > Black > Popup type: No popup
67+
```
68+
69+
As of 2023.2, there is no way to separately configure the different popup severities (error vs. informational).
4570

4671
### Eclipse
4772
This section describes how to set up Eclipse for development. For convenience, `$GRAAL` denotes your local repository.

docs/Styleguide.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ but new code submitted to `mx` should nevertheless follow the guidelines.
99

1010
## Python
1111

12+
### Code Style
13+
14+
Code is formatted using [*Black*](https://github.com/psf/black).
15+
Formatting can be done manually using `mx pyformat`; this will format all eligible python files in this repository.
16+
It is recommended to set up auto-formatting in your [IDE](./IDE.md) to avoid formatting-only commits.
17+
18+
Only new python files are formatted; which files are excluded is specified in [`pyproject.toml`](../pyproject.toml)
19+
under `force-exclude`.
20+
21+
To ensure a consistent style, the major version of *Black* is fixed in [`pyproject.toml`](../pyproject.toml).
22+
Other major versions (newer or older) cannot be used as they could lead to slightly different formatting, failing the
23+
code format check in the CI.
24+
See also [*Black*'s Stability Policy](https://black.readthedocs.io/en/stable/the_black_code_style/index.html).
25+
1226
### Doc comments
1327

1428
Doc comments for modules, classes and methods should use the

mx.mx/mx_mx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def find_black() -> str | None:
4141
@mx.command(suite.name, "pyformat")
4242
@mx.no_suite_loading
4343
def pyformat(arg_list: [str]):
44-
parser = argparse.ArgumentParser(prog="mx pyformat", description="TODO document")
44+
parser = argparse.ArgumentParser(prog="mx pyformat", description="Format python code using the black formatter")
4545
parser.add_argument(
4646
"-n",
4747
"--dry-run",

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ target-version = ["py38", "py39", "py310", "py311", "py312"]
99
# The original python files
1010
# Formatting those would lead to large diffs and break git history
1111
# In addition, formatting for some of these files takes too long to be practical
12+
# force-exclude is necessary because it prevents formatting even when explicitly requested (e.g. through IDE auto-formatting).
1213
force-exclude = '(select_jdk)|(remove_jdks.py)|(src/mx/_impl/(mx|mx_[^/]*|select_jdk).py)|(mx.mx/suite.py)'

0 commit comments

Comments
 (0)