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 11 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
38 changes: 8 additions & 30 deletions rewrite/rewrite/python/format/auto_format.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
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 ..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 @@ -23,32 +24,9 @@ def visit(self, tree: Optional[Tree], p: P, parent: Optional[Cursor] = None) ->
cu = tree if isinstance(tree, JavaSourceFile) else self._cursor.first_enclosing_or_throw(JavaSourceFile)

tree = NormalizeFormatVisitor(self._stop_after).visit(tree, p, self._cursor.fork())
tree = BlankLinesVisitor(cu.get_style(BlankLinesStyle) or IntelliJ.blank_lines(), self._stop_after).visit(tree, p, self._cursor.fork())
tree = SpacesVisitor(cu.get_style(SpacesStyle) or IntelliJ.spaces(), self._stop_after).visit(tree, p, self._cursor.fork())
tree = BlankLinesVisitor(cu.get_style(BlankLinesStyle) or IntelliJ.blank_lines(), self._stop_after).visit(tree,
p,
self._cursor.fork())
tree = SpacesVisitor(cu.get_style(SpacesStyle) or IntelliJ.spaces(), 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)
166 changes: 166 additions & 0 deletions rewrite/rewrite/python/format/spaces_visitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
from typing import Optional, cast, TypeVar

from rewrite import Tree
from rewrite.java import J, Assignment, JLeftPadded, AssignmentOperation
from rewrite.java import MethodInvocation, MethodDeclaration, Empty, ArrayAccess, Space
from rewrite.python import PythonVisitor, SpacesStyle
from rewrite.visitor import P

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

def visit_method_invocation(self, method_invocation: MethodInvocation, p: P) -> J:
m: MethodInvocation = cast(MethodInvocation, super().visit_method_invocation(method_invocation, p))

# Handle space before parenthesis for method e.g. foo (..) <-> foo(..)
m = m.padding.with_arguments(m.padding.arguments.with_before(
Space.SINGLE_SPACE if self._style.before_parentheses.method_call else Space.EMPTY))

if not m.arguments or isinstance(m.arguments[0], Empty):
# Handle within method call parenthesis with no arguments e.g. foo() <-> foo( )
use_space = self._style.within.empty_method_call_parentheses
m = m.padding.with_arguments(
m.padding.arguments.padding.with_elements(
[arg.with_element(self.space_before(arg.element, use_space)) for arg in
m.padding.arguments.padding.elements]
)
)
else:
# Handle:
# - Within method call parenthesis with arguments e.g. foo( 1, 2 ) <-> foo(1, 2).
# Note: this only applies to the space left of the first argument and right of the last argument.
# - Space after comma e.g. foo(1,2,3) <-> foo(1, 2, 3)
args_size = len(m.arguments)
use_space = self._style.within.method_call_parentheses

def _process_argument(index, arg, args_size, use_space):
if index == 0:
arg = arg.with_element(self.space_before(arg.element, use_space))
else:
arg = arg.with_element(self.space_before(arg.element, self._style.other.after_comma))

if index == args_size - 1:
arg = self.space_after(arg, use_space)
else:
arg = self.space_after(arg, self._style.other.before_comma)

return arg

m = m.padding.with_arguments(
m.padding.arguments.padding.with_elements(
[_process_argument(index, arg, args_size, use_space)
for index, arg in enumerate(m.padding.arguments.padding.elements)]
)
)
# TODO: Handle type parameters, relevant for constructors in Python.
return m

def visit_array_access(self, array_access: ArrayAccess, p: P) -> J:
a: ArrayAccess = cast(ArrayAccess, super().visit_array_access(array_access, p))
use_space_within_brackets = self._style.within.brackets
index_padding = a.dimension.padding.index
element_prefix = update_space(index_padding.element.prefix, use_space_within_brackets)
index_after = update_space(index_padding.after, use_space_within_brackets)

a = a.with_dimension(
a.dimension.padding.with_index(
index_padding.with_element(
index_padding.element.with_prefix(element_prefix)
).with_after(index_after)
).with_prefix(update_space(a.dimension.prefix, self._style.before_parentheses.left_bracket))
)
return a

def visit_assignment(self, assignment: Assignment, p: P) -> J:
knutwannheden marked this conversation as resolved.
Show resolved Hide resolved
"""
Handle assignment operator e.g. a = 1 <-> a=1
"""
a: Assignment = cast(Assignment, super().visit_assignment(assignment, p))
a = a.padding.with_assignment(
self.space_before_jleftpadded(a.padding.assignment, self._style.around_operators.assignment))
a = a.padding.with_assignment(
a.padding.assignment.with_element(
self.space_before(a.padding.assignment.element, self._style.around_operators.assignment)))
return a

def visit_assignment_operation(self, assignment_operation: AssignmentOperation, p: P) -> J:
"""
Handle assignment operation e.g. a += 1 <-> a+=1
"""
a: AssignmentOperation = cast(AssignmentOperation, super().visit_assignment_operation(assignment_operation, p))
operator: JLeftPadded = a.padding.operator
a = a.padding.with_operator(
operator.with_before(update_space(operator.before, self._style.around_operators.assignment)))
return a.with_assignment(self.space_before(a.assignment, self._style.around_operators.assignment))

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)

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

if space_before and not_single_space(space.whitespace):
return j.with_before(space.with_whitespace(" "))
elif not space_before and only_spaces_and_not_empty(space.whitespace):
return j.with_before(space.with_whitespace(""))
return j

def contains_comments(self, space: Space) -> bool:
return space.comments is not None or '\\' in space.whitespace

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

if space_after and not_single_space(space.whitespace):
return j.with_after(space.with_whitespace(" "))
elif not space_after and only_spaces_and_not_empty(space.whitespace):
return j.with_after(space.with_whitespace(""))
return j

def update_space(s: Space, have_space: bool) -> Space:
if s.comments:
return s

if have_space and not_single_space(s.whitespace):
return s.with_whitespace(" ")
elif not have_space and only_spaces_and_not_empty(s.whitespace):
return s.with_whitespace("")
else:
return s


def only_spaces(s: Optional[str]) -> bool:
return s is not None and all(c in {' ', '\t'} for c in s)


def only_spaces_and_not_empty(s: Optional[str]) -> bool:
return bool(s) and only_spaces(s)


def not_single_space(s: Optional[str]) -> bool:
return s is not None and only_spaces(s) and s != " "
2 changes: 1 addition & 1 deletion rewrite/tests/manual_parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
RemotePrinterFactory(remoting_context.client).set_current()
remoting_context.client.reset()

parser = PythonParser()
parser = PythonParser(None)
res = parser.parse_inputs([ParserInput(pathlib.Path(path), None, False, lambda: open(path, 'r', newline='', encoding='utf-8'))], relative_to=None, ctx=InMemoryExecutionContext())
for r in res:
if isinstance(r, ParseError):
Expand Down
Loading
Loading