Skip to content
This repository was archived by the owner on Jan 13, 2026. It is now read-only.

Commit 56ace3f

Browse files
committed
Enable serialization of Python SpacesStyle
1 parent e9811d9 commit 56ace3f

File tree

7 files changed

+192
-61
lines changed

7 files changed

+192
-61
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.python.style;
17+
18+
import org.openrewrite.style.Style;
19+
20+
public interface PythonStyle extends Style {
21+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.python.style;
17+
18+
import lombok.Value;
19+
import lombok.With;
20+
21+
import java.util.UUID;
22+
23+
@Value
24+
@With
25+
public class SpacesStyle implements PythonStyle {
26+
27+
UUID id;
28+
BeforeParentheses beforeParentheses;
29+
AroundOperators aroundOperators;
30+
Within within;
31+
Other other;
32+
33+
@Value
34+
@With
35+
public static class BeforeParentheses {
36+
Boolean methodDeclaration;
37+
Boolean methodCall;
38+
Boolean leftBracket;
39+
}
40+
41+
@Value
42+
@With
43+
public static class AroundOperators {
44+
Boolean assignment;
45+
Boolean equality;
46+
Boolean relational;
47+
Boolean bitwise;
48+
Boolean additive;
49+
Boolean multiplicative;
50+
Boolean shift;
51+
Boolean power;
52+
Boolean eqInNamedParameter;
53+
Boolean eqInKeywordArgument;
54+
}
55+
56+
@Value
57+
@With
58+
public static class Within {
59+
Boolean brackets;
60+
Boolean methodDeclarationParentheses;
61+
Boolean emptyMethodDeclarationParentheses;
62+
Boolean methodCallParentheses;
63+
Boolean emptyMethodCallParentheses;
64+
Boolean braces;
65+
}
66+
67+
@Value
68+
@With
69+
public static class Other {
70+
Boolean beforeComma;
71+
Boolean afterComma;
72+
Boolean beforeForSemicolon;
73+
Boolean beforeColon;
74+
Boolean afterColon;
75+
Boolean beforeBackslash;
76+
Boolean beforeHash;
77+
Boolean afterHash;
78+
}
79+
80+
// @Override
81+
// public Style applyDefaults() {
82+
// return StyleHelper.merge(IntelliJ.spaces(), this);
83+
// }
84+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
@NullMarked
17+
@NonNullFields
18+
package org.openrewrite.python.style;
19+
20+
import org.jspecify.annotations.NullMarked;
21+
import org.openrewrite.internal.lang.NonNullFields;

rewrite/rewrite/python/format/auto_format.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ def __init__(self, style: SpacesStyle, stop_after: Tree = None):
3030

3131
def visit_method_declaration(self, method_declaration: MethodDeclaration, p: P) -> J:
3232
return method_declaration.padding.with_parameters(
33-
method_declaration.padding.parameters.with_before(Space.SINGLE_SPACE if self._style.beforeParentheses.method_parentheses else Space.EMPTY)
33+
method_declaration.padding.parameters.with_before(Space.SINGLE_SPACE if self._style.beforeParentheses._method_declaration else Space.EMPTY)
3434
)

rewrite/rewrite/python/style.py

Lines changed: 56 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,53 @@
11
from dataclasses import dataclass
2+
from typing import Protocol
23

34
from ..style import Style, NamedStyles
45

56

6-
class PythonStyle(Style):
7+
class PythonStyle(Style, Protocol):
78
pass
89

910

1011
@dataclass(frozen=True)
1112
class SpacesStyle(PythonStyle):
1213
@dataclass(frozen=True)
1314
class BeforeParentheses:
14-
method_call_parentheses: bool
15-
method_parentheses: bool
16-
left_bracket: bool
15+
_method_call: bool
16+
_method_declaration: bool
17+
_left_bracket: bool
1718

1819
@dataclass(frozen=True)
1920
class AroundOperators:
20-
assignment: bool
21-
equality: bool
22-
relational: bool
23-
bitwise: bool
24-
additive: bool
25-
multiplicative: bool
26-
shift: bool
27-
power: bool
28-
eq_in_named_parameter: bool
29-
eq_in_keyword_argument: bool
21+
_assignment: bool
22+
_equality: bool
23+
_relational: bool
24+
_bitwise: bool
25+
_additive: bool
26+
_multiplicative: bool
27+
_shift: bool
28+
_power: bool
29+
_eq_in_named_parameter: bool
30+
_eq_in_keyword_argument: bool
3031

3132
@dataclass(frozen=True)
3233
class Within:
33-
brackets: bool
34-
method_parentheses: bool
35-
empty_method_parentheses: bool
36-
method_call_parentheses: bool
37-
empty_method_call_parentheses: bool
38-
braces: bool
34+
_brackets: bool
35+
_method_declaration_parentheses: bool
36+
_empty_method_declaration_parentheses: bool
37+
_method_call_parentheses: bool
38+
_empty_method_call_parentheses: bool
39+
_braces: bool
3940

4041
@dataclass(frozen=True)
4142
class Other:
42-
before_comma: bool
43-
after_comma: bool
44-
before_for_semicolon: bool
45-
before_colon: bool
46-
after_colon: bool
47-
before_backslash: bool
48-
before_hash: bool
49-
after_hash: bool
43+
_before_comma: bool
44+
_after_comma: bool
45+
_before_for_semicolon: bool
46+
_before_colon: bool
47+
_after_colon: bool
48+
_before_backslash: bool
49+
_before_hash: bool
50+
_after_hash: bool
5051

5152
beforeParentheses: BeforeParentheses
5253
aroundOperators: AroundOperators
@@ -59,38 +60,38 @@ class IntelliJ(NamedStyles):
5960
def spaces(cls) -> SpacesStyle:
6061
return SpacesStyle(
6162
SpacesStyle.BeforeParentheses(
62-
method_call_parentheses=False,
63-
method_parentheses=False,
64-
left_bracket=False,
63+
_method_call=False,
64+
_method_declaration=False,
65+
_left_bracket=False,
6566
),
6667
SpacesStyle.AroundOperators(
67-
assignment=True,
68-
equality=True,
69-
relational=True,
70-
bitwise=True,
71-
additive=True,
72-
multiplicative=True,
73-
shift=True,
74-
power=True,
75-
eq_in_named_parameter=False,
76-
eq_in_keyword_argument=False,
68+
_assignment=True,
69+
_equality=True,
70+
_relational=True,
71+
_bitwise=True,
72+
_additive=True,
73+
_multiplicative=True,
74+
_shift=True,
75+
_power=True,
76+
_eq_in_named_parameter=False,
77+
_eq_in_keyword_argument=False,
7778
),
7879
SpacesStyle.Within(
79-
brackets=False,
80-
method_parentheses=False,
81-
empty_method_parentheses=False,
82-
method_call_parentheses=False,
83-
empty_method_call_parentheses=False,
84-
braces=False,
80+
_brackets=False,
81+
_method_declaration_parentheses=False,
82+
_empty_method_declaration_parentheses=False,
83+
_method_call_parentheses=False,
84+
_empty_method_call_parentheses=False,
85+
_braces=False,
8586
),
8687
SpacesStyle.Other(
87-
before_comma=False,
88-
after_comma=True,
89-
before_for_semicolon=False,
90-
before_colon=False,
91-
after_colon=True,
92-
before_backslash=True,
93-
before_hash=True,
94-
after_hash=True,
88+
_before_comma=False,
89+
_after_comma=True,
90+
_before_for_semicolon=False,
91+
_before_colon=False,
92+
_after_colon=True,
93+
_before_backslash=True,
94+
_before_hash=True,
95+
_after_hash=True,
9596
),
9697
)

rewrite/rewrite/style.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from __future__ import annotations
22

33
from dataclasses import dataclass, replace
4-
from typing import Protocol, TypeVar, Type, Iterable, Optional, Set
4+
from typing import Protocol, TypeVar, Type, Iterable, Optional, Set, runtime_checkable
55
from uuid import UUID
66

77
from rewrite import Marker, random_id
88

99

10+
@runtime_checkable
1011
class Style(Protocol):
1112
def merge(self, lower_precedence: Style) -> Style:
1213
return self

rewrite/tests/python/all/format/demo_test.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from typing import Optional
22

3+
from rewrite import NamedStyles
34
from rewrite.java import Space, P
4-
from rewrite.python import PythonVisitor, AutoFormat
5-
from rewrite.test import rewrite_run, python, from_visitor
5+
from rewrite.python import PythonVisitor, AutoFormat, PythonParserBuilder, IntelliJ
6+
from rewrite.test import rewrite_run, python, from_visitor, RecipeSpec
67

78

89
def test_remove_all_spaces_demo():
@@ -15,7 +16,9 @@ def getter(self, row):
1516
pass
1617
""", """classFoo:defgetter(self,row):pass"""
1718
),
18-
recipe=from_visitor(NoSpaces())
19+
spec=RecipeSpec()
20+
.with_recipe(from_visitor(NoSpaces()))
21+
.with_parsers([PythonParserBuilder().styles(NamedStyles.build(IntelliJ.spaces()))])
1922
)
2023

2124

@@ -34,7 +37,7 @@ def getter(self, row):
3437
pass
3538
"""
3639
),
37-
recipe=AutoFormat()
40+
spec=RecipeSpec(_recipe=AutoFormat())
3841
)
3942

4043
class NoSpaces(PythonVisitor):

0 commit comments

Comments
 (0)