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

Commit 6cfab68

Browse files
committed
Add NamedStyles and some other base classes
1 parent 3a8818a commit 6cfab68

File tree

8 files changed

+107
-8
lines changed

8 files changed

+107
-8
lines changed

rewrite/rewrite/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@
4141

4242
# Style
4343
'Style',
44+
'NamedStyles',
4445
]

rewrite/rewrite/markers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ def find_first(self, type: type):
5454
return marker
5555
return None
5656

57+
def find_all(self, type: type):
58+
return [m for m in self.markers if isinstance(m, type)]
59+
5760
EMPTY: ClassVar[Markers]
5861

5962
def __eq__(self, other: object) -> bool:

rewrite/rewrite/python/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
# Style
5858
'PythonStyle',
5959
'SpacesStyle',
60+
'IntelliJ',
6061

6162
# Formatter
6263
'AutoFormat',

rewrite/rewrite/python/format/auto_format.py

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

33
from rewrite import Recipe, Tree, Cursor
4-
from rewrite.python import PythonVisitor
4+
from rewrite.java import JavaSourceFile
5+
from rewrite.python import PythonVisitor, SpacesStyle, IntelliJ
56
from rewrite.visitor import P, T
67

78

@@ -15,4 +16,14 @@ def __init__(self, stop_after: Tree = None):
1516
self._stop_after = stop_after
1617

1718
def visit(self, tree: Optional[Tree], p: P, parent: Optional[Cursor] = None) -> Optional[T]:
18-
pass
19+
self._cursor = parent if parent is not None else Cursor(None, Cursor.ROOT_VALUE)
20+
cu = tree if isinstance(tree, JavaSourceFile) else self._cursor.first_enclosing_or_throw(JavaSourceFile)
21+
22+
tree = SpacesVisitor(cu.get_style(SpacesStyle) or IntelliJ.spaces(), self._stop_after).visit(tree, p, self._cursor.fork())
23+
return tree
24+
25+
26+
class SpacesVisitor(PythonVisitor):
27+
def __init__(self, style: SpacesStyle, stop_after: Tree = None):
28+
self._style = style
29+
self._stop_after = stop_after

rewrite/rewrite/python/style.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from dataclasses import dataclass
22

3-
from ..style import Style
3+
from ..style import Style, NamedStyles
44

55

66
class PythonStyle(Style):
@@ -41,7 +41,6 @@ class Within:
4141
class Other:
4242
before_comma: bool
4343
after_comma: bool
44-
before_semicolon: bool
4544
before_for_semicolon: bool
4645
before_colon: bool
4746
after_colon: bool
@@ -53,3 +52,45 @@ class Other:
5352
aroundOperators: AroundOperators
5453
within: Within
5554
other: Other
55+
56+
57+
class IntelliJ(NamedStyles):
58+
@classmethod
59+
def spaces(cls) -> SpacesStyle:
60+
return SpacesStyle(
61+
SpacesStyle.BeforeParentheses(
62+
method_call_parentheses=False,
63+
method_parentheses=False,
64+
left_bracket=False,
65+
),
66+
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,
77+
),
78+
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,
85+
),
86+
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,
95+
),
96+
)

rewrite/rewrite/style.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,43 @@
11
from __future__ import annotations
22

3-
from typing import Protocol
3+
from dataclasses import dataclass
4+
from typing import Protocol, TypeVar, Type, Iterable, Optional, Set
5+
from uuid import UUID
6+
7+
from rewrite import Marker
48

59

610
class Style(Protocol):
711
def merge(self, lower_precedence: Style) -> Style:
8-
...
12+
return self
913

1014
def apply_defaults(self) -> Style:
1115
return self
16+
17+
18+
S = TypeVar('S', bound=Style)
19+
20+
21+
@dataclass(frozen=True)
22+
class NamedStyles(Marker):
23+
_id: UUID
24+
_name: str
25+
_display_name: str
26+
_description: Optional[str]
27+
_tags: Set[str]
28+
_styles: Iterable[Style]
29+
30+
@classmethod
31+
def merge(cls, style_type: Type[S], named_styles: Iterable[NamedStyles]) -> Optional[S]:
32+
merged = None
33+
for named_style in named_styles:
34+
styles = named_style._styles
35+
if styles is not None:
36+
for style in styles:
37+
if isinstance(style, style_type):
38+
style = style.apply_defaults()
39+
if merged is None:
40+
merged = style
41+
else:
42+
merged = merged.merge(style)
43+
return merged

rewrite/rewrite/tree.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
from dataclasses import dataclass
66
from datetime import datetime
77
from pathlib import Path
8-
from typing import Protocol, Optional, Any, TypeVar, runtime_checkable, cast, TYPE_CHECKING, Generic, ClassVar, Callable
8+
from typing import Protocol, Optional, Any, TypeVar, runtime_checkable, cast, TYPE_CHECKING, Generic, ClassVar, \
9+
Callable, Type
910
from uuid import UUID
1011

11-
from rewrite import Markers
12+
from .markers import Markers
13+
from .style import NamedStyles, Style
1214

1315
if TYPE_CHECKING:
1416
from rewrite import TreeVisitor, ExecutionContext
@@ -72,6 +74,8 @@ def create_printer(self, cursor: Cursor) -> TreeVisitor[Any, PrintOutputCapture[
7274
...
7375

7476

77+
S = TypeVar('S', bound=Style)
78+
7579
@runtime_checkable
7680
class SourceFile(Tree, Protocol):
7781
@property
@@ -100,6 +104,9 @@ def print_equals_input(self, input: 'ParserInput', ctx: ExecutionContext) -> boo
100104
printed = self.print_all()
101105
return printed == input.source().read()
102106

107+
def get_style(self, style: Type[S]) -> Optional[S]:
108+
return NamedStyles.merge(style, self.markers.find_all(NamedStyles))
109+
103110

104111
@dataclass(frozen=True)
105112
class FileAttributes:

rewrite/rewrite/visitor.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ def first_enclosing(self, type: Type[P]) -> P:
3939
c = c.parent
4040
return None
4141

42+
def fork(self) -> Cursor:
43+
return Cursor(self.parent.fork(), self.value)
44+
4245

4346
class TreeVisitor(Protocol[T, P]):
4447
_visit_count: int = 0

0 commit comments

Comments
 (0)