Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoformat/spacevisitor #96

Merged
merged 42 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
7534a55
Space visitor
nielsdebruin Nov 29, 2024
7b6c432
Add support for left brackets
nielsdebruin Nov 29, 2024
dbf27f5
Add support after comma for method call
nielsdebruin Nov 29, 2024
5af2396
Fixes
nielsdebruin Nov 29, 2024
dda74d0
Polish
nielsdebruin Dec 2, 2024
a95f2b9
Merge branch 'main' into autoformat/spacevisitor
nielsdebruin Dec 2, 2024
da70321
Fix bug
nielsdebruin Dec 2, 2024
d68b8b2
Add TODO for type parameters
nielsdebruin Dec 2, 2024
b910c1c
Add support for assignments
nielsdebruin Dec 2, 2024
9656e5f
Add support for assignment operator
nielsdebruin Dec 2, 2024
d03249d
Merge branch 'main' into autoformat/spacevisitor
nielsdebruin Dec 2, 2024
94532f7
Add support for binary operators
nielsdebruin Dec 3, 2024
211a69e
Add support for changed assignments
nielsdebruin Dec 3, 2024
7e970e6
Merge branch 'main' into autoformat/spacevisitor
nielsdebruin Dec 3, 2024
b3509a0
Add support for method declaration
nielsdebruin Dec 4, 2024
b2ea621
Add support for if else statements
nielsdebruin Dec 4, 2024
c7b5331
Small fix
nielsdebruin Dec 4, 2024
166abf4
Merge remote-tracking branch 'origin/main' into autoformat/spacevisitor
nielsdebruin Dec 4, 2024
0aff351
Add block statements
nielsdebruin Dec 4, 2024
cc5bc07
Add support for class declarations
nielsdebruin Dec 4, 2024
320e543
Make static methods top-level functions
knutwannheden Dec 4, 2024
6d66764
Split tests
nielsdebruin Dec 5, 2024
0ee4e83
Refactor some functions to use listutils
nielsdebruin Dec 5, 2024
913c12e
Merge remote-tracking branch 'origin/main' into autoformat/spacevisitor
nielsdebruin Dec 5, 2024
9ed550c
Add progress, added support for-loop, array-dec
nielsdebruin Dec 5, 2024
5eb2342
Add support for dicts
nielsdebruin Dec 6, 2024
6397f47
Merge remote-tracking branch 'origin/main' into autoformat/spacevisitor
nielsdebruin Dec 6, 2024
a800af7
Add support for type hints
nielsdebruin Dec 6, 2024
56838b1
Fix bug
nielsdebruin Dec 6, 2024
0435bf0
Fix bug
nielsdebruin Dec 6, 2024
466ef03
Add test/partial support for import formatting
nielsdebruin Dec 6, 2024
482605f
Correct formatting of import
nielsdebruin Dec 6, 2024
e80c55a
Replace some comprehensions with listmap
nielsdebruin Dec 7, 2024
e925adf
Remove more list comprehensions
nielsdebruin Dec 8, 2024
d122d7a
Add support for training spaces
nielsdebruin Dec 9, 2024
2015577
Fix bug
nielsdebruin Dec 10, 2024
3d61404
Merge remote-tracking branch 'origin/main' into autoformat/spacevisitor
nielsdebruin Dec 10, 2024
49f109e
Refactor compute_by_type
nielsdebruin Dec 10, 2024
28a903c
Format
nielsdebruin Dec 10, 2024
41e2bac
Fix bug and add tests
nielsdebruin Dec 10, 2024
fa9a5de
Add support and test for Union type hints.
nielsdebruin Dec 10, 2024
186ebfa
Fix bug and fix support for union types and sets
nielsdebruin Dec 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions rewrite/rewrite/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import traceback
from abc import ABC, abstractmethod
from dataclasses import dataclass, replace
from typing import List, ClassVar, cast, TYPE_CHECKING, Optional
from typing import List, ClassVar, cast, TYPE_CHECKING, Callable, TypeVar, Type, Optional
from uuid import UUID

if TYPE_CHECKING:
from .parser import Parser
from .utils import random_id
from .utils import random_id, list_map


class Marker(ABC):
Expand All @@ -30,6 +30,8 @@ def __hash__(self) -> int:
return hash(self.id)


M = TypeVar('M', bound=Marker)
knutwannheden marked this conversation as resolved.
Show resolved Hide resolved

@dataclass(frozen=True, eq=False)
class Markers:
_id: UUID
Expand All @@ -50,14 +52,24 @@ def markers(self) -> List[Marker]:
def with_markers(self, markers: List[Marker]) -> Markers:
return self if markers is self._markers else Markers(self._id, markers)

def find_first(self, type: type) -> Optional[Marker]:
def find_first(self, cls: Type[M]) -> Optional[M]:
for marker in self.markers:
if isinstance(marker, type):
if isinstance(marker, cls):
return marker
return None

def find_all(self, type: type) -> List[Marker]:
return [m for m in self.markers if isinstance(m, type)]
def find_all(self, cls: Type[M]) -> List[M]:
return [m for m in self.markers if isinstance(m, cls)]

def compute_by_type(self, cls: Type[M], remap_fn: Callable[[M], Marker]) -> Markers:
"""
Replace all markers of the given type with the result of the function.

:param cls: type of the markers to remap
:param remap_fn: function to remap the marker
:return: new Markers instance with the updated markers, or the same instance if no markers were updated
"""
return self.with_markers(list_map(lambda m: remap_fn(m) if isinstance(m, cls) else m, self.markers))

EMPTY: ClassVar[Markers]

Expand Down
31 changes: 3 additions & 28 deletions rewrite/rewrite/python/format/auto_format.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from typing import Optional, cast, TypeVar
from typing import Optional

from .blank_lines import BlankLinesVisitor
from .normalize_format import NormalizeFormatVisitor
from .spaces_visitor import SpacesVisitor
from .normalize_tabs_or_spaces import NormalizeTabsOrSpacesVisitor
from .. import TabsAndIndentsStyle
from ..style import BlankLinesStyle, SpacesStyle, IntelliJ
from ..visitor import PythonVisitor
from ... import Recipe, Tree, Cursor
from ...java import JavaSourceFile, MethodDeclaration, J, Space
from ...java import JavaSourceFile
from ...visitor import P, T


Expand All @@ -32,29 +33,3 @@ def visit(self, tree: Optional[Tree], p: P, parent: Optional[Cursor] = None) ->
self._stop_after
).visit(tree, p, self._cursor.fork())
return tree


J2 = TypeVar('J2', bound=J)


class SpacesVisitor(PythonVisitor):
def __init__(self, style: SpacesStyle, stop_after: Tree = None):
self._style = style
self._before_parentheses = style.before_parentheses
self._stop_after = stop_after

def visit_method_declaration(self, md: MethodDeclaration, p: P) -> J:
md: MethodDeclaration = cast(MethodDeclaration, super().visit_method_declaration(md, p))
return md.padding.with_parameters(
md.padding.parameters.with_before(
Space.SINGLE_SPACE if self._before_parentheses.method_declaration else Space.EMPTY
)
)

def space_before(self, j: J2, space_before: bool) -> J2:
space: Space = cast(Space, j.prefix)
if space.comments or '\\' in space.whitespace:
# don't touch whitespaces with comments or continuation characters
return j

return j.with_prefix(Space.SINGLE_SPACE if space_before else Space.EMPTY)
Loading
Loading