From 76741cac77111d6243f12706d1f1f565e0337527 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sat, 29 Jul 2023 10:39:42 -0400 Subject: [PATCH] Add `global` and `nonlocal` formatting (#6170) ## Summary Adds `global` and `nonlocal` formatting, without the "deviation from black" outlined in the linked issue, which I'll do separately. See: https://github.com/astral-sh/ruff/issues/4798. ## Test Plan Added a fixture in the Ruff-specific directory since the Black fixtures don't seem to cover this. --- .../test/fixtures/ruff/statement/global.py | 12 ++++++ .../test/fixtures/ruff/statement/nonlocal.py | 12 ++++++ .../src/statement/stmt_global.rs | 12 ++++-- .../src/statement/stmt_nonlocal.rs | 12 ++++-- .../format@statement__global.py.snap | 38 +++++++++++++++++++ .../format@statement__nonlocal.py.snap | 38 +++++++++++++++++++ 6 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/global.py create mode 100644 crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/nonlocal.py create mode 100644 crates/ruff_python_formatter/tests/snapshots/format@statement__global.py.snap create mode 100644 crates/ruff_python_formatter/tests/snapshots/format@statement__nonlocal.py.snap diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/global.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/global.py new file mode 100644 index 0000000000..faec9b9e81 --- /dev/null +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/global.py @@ -0,0 +1,12 @@ +def f(): + global x, y, z + + +def f(): + # leading comment + global x, y, z # end-of-line comment + # trailing comment + + +def f(): + global analyze_featuremap_layer, analyze_featuremapcompression_layer, analyze_latencies_post, analyze_motions_layer, analyze_size_model diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/nonlocal.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/nonlocal.py new file mode 100644 index 0000000000..2699c513bf --- /dev/null +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/nonlocal.py @@ -0,0 +1,12 @@ +def f(): + nonlocal x, y, z + + +def f(): + # leading comment + nonlocal x, y, z # end-of-line comment + # trailing comment + + +def f(): + nonlocal analyze_featuremap_layer, analyze_featuremapcompression_layer, analyze_latencies_post, analyze_motions_layer, analyze_size_model diff --git a/crates/ruff_python_formatter/src/statement/stmt_global.rs b/crates/ruff_python_formatter/src/statement/stmt_global.rs index cf5ec1db92..abdaf7c710 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_global.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_global.rs @@ -1,12 +1,18 @@ -use crate::{not_yet_implemented, FormatNodeRule, PyFormatter}; -use ruff_formatter::{write, Buffer, FormatResult}; +use ruff_formatter::{format_args, write}; use ruff_python_ast::StmtGlobal; +use crate::prelude::*; +use crate::FormatNodeRule; + #[derive(Default)] pub struct FormatStmtGlobal; impl FormatNodeRule for FormatStmtGlobal { fn fmt_fields(&self, item: &StmtGlobal, f: &mut PyFormatter) -> FormatResult<()> { - write!(f, [not_yet_implemented(item)]) + write!(f, [text("global"), space()])?; + + f.join_with(format_args![text(","), space()]) + .entries(item.names.iter().formatted()) + .finish() } } diff --git a/crates/ruff_python_formatter/src/statement/stmt_nonlocal.rs b/crates/ruff_python_formatter/src/statement/stmt_nonlocal.rs index e1cb22cfbd..f3dd3b1966 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_nonlocal.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_nonlocal.rs @@ -1,12 +1,18 @@ -use crate::{not_yet_implemented, FormatNodeRule, PyFormatter}; -use ruff_formatter::{write, Buffer, FormatResult}; +use ruff_formatter::{format_args, write}; use ruff_python_ast::StmtNonlocal; +use crate::prelude::*; +use crate::FormatNodeRule; + #[derive(Default)] pub struct FormatStmtNonlocal; impl FormatNodeRule for FormatStmtNonlocal { fn fmt_fields(&self, item: &StmtNonlocal, f: &mut PyFormatter) -> FormatResult<()> { - write!(f, [not_yet_implemented(item)]) + write!(f, [text("nonlocal"), space()])?; + + f.join_with(format_args![text(","), space()]) + .entries(item.names.iter().formatted()) + .finish() } } diff --git a/crates/ruff_python_formatter/tests/snapshots/format@statement__global.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@statement__global.py.snap new file mode 100644 index 0000000000..1181cdba30 --- /dev/null +++ b/crates/ruff_python_formatter/tests/snapshots/format@statement__global.py.snap @@ -0,0 +1,38 @@ +--- +source: crates/ruff_python_formatter/tests/fixtures.rs +input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/global.py +--- +## Input +```py +def f(): + global x, y, z + + +def f(): + # leading comment + global x, y, z # end-of-line comment + # trailing comment + + +def f(): + global analyze_featuremap_layer, analyze_featuremapcompression_layer, analyze_latencies_post, analyze_motions_layer, analyze_size_model +``` + +## Output +```py +def f(): + global x, y, z + + +def f(): + # leading comment + global x, y, z # end-of-line comment + # trailing comment + + +def f(): + global analyze_featuremap_layer, analyze_featuremapcompression_layer, analyze_latencies_post, analyze_motions_layer, analyze_size_model +``` + + + diff --git a/crates/ruff_python_formatter/tests/snapshots/format@statement__nonlocal.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@statement__nonlocal.py.snap new file mode 100644 index 0000000000..fbffb2ede9 --- /dev/null +++ b/crates/ruff_python_formatter/tests/snapshots/format@statement__nonlocal.py.snap @@ -0,0 +1,38 @@ +--- +source: crates/ruff_python_formatter/tests/fixtures.rs +input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/nonlocal.py +--- +## Input +```py +def f(): + nonlocal x, y, z + + +def f(): + # leading comment + nonlocal x, y, z # end-of-line comment + # trailing comment + + +def f(): + nonlocal analyze_featuremap_layer, analyze_featuremapcompression_layer, analyze_latencies_post, analyze_motions_layer, analyze_size_model +``` + +## Output +```py +def f(): + nonlocal x, y, z + + +def f(): + # leading comment + nonlocal x, y, z # end-of-line comment + # trailing comment + + +def f(): + nonlocal analyze_featuremap_layer, analyze_featuremapcompression_layer, analyze_latencies_post, analyze_motions_layer, analyze_size_model +``` + + +