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

Properly indent new statements #12

Open
isidentical opened this issue Aug 14, 2021 · 2 comments
Open

Properly indent new statements #12

isidentical opened this issue Aug 14, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@isidentical
Copy link
Owner

Problem

The indentation for the statements are not preserved, since ast.unparse() uses 4 spaces by default. We should infer the common indentation level (even in some blocks it might be different), and smartly use it for the ast.unparse;

Rule

import ast
import refactor
from refactor import Rule, Action

class MakeAsyncWith(Action):
    def build(self):
        new_node = self.branch()
        new_node.__class__ = ast.AsyncWith
        return new_node

class ReplaceToAsync(Rule):
    def match(self, node):
        assert isinstance(node, ast.With)
        return MakeAsyncWith(node)

if __name__ == "__main__":
    refactor.run(rules=[ReplaceToAsync])

Input

if something: # comment
  with something: # comment2
    a = 1 # comment3
    b = 2 # comment4
  # comment5
  c()

Diff

--- t1.py

+++ t1.py

@@ -1,6 +1,6 @@

 if something: # comment
-  with something: # comment2
-    a = 1 # comment3
-    b = 2 # comment4
+  async with something:
+      a = 1
+      b = 2 # comment4
   # comment5
   c()

Expected Difff

--- t1.py

+++ t1.py

@@ -1,6 +1,6 @@

 if something: # comment
-  with something: # comment2
-    a = 1 # comment3
-    b = 2 # comment4
+  async with something:
+    a = 1
+    b = 2 # comment4
   # comment5
   c()
@MementoRC
Copy link
Contributor

MementoRC commented Dec 29, 2022

PR: #66 should address this. See the added passing case to test_ast.py:

    source = """def func():
    if something:
        # Comments are retrieved
        with something: # comment2
             a = 1 # Non-standard indent
             b = 2 # Non-standard indent becomes standard
"""

    expected_src = """def func():
    if something:
        # Comments are retrieved
        async with something:
            a = 1
            b = 2 # Non-standard indent becomes standard
"""

Note the non-standard indent becomes standard

MementoRC added a commit to MementoRC/refactor that referenced this issue Dec 29, 2022
@MementoRC
Copy link
Contributor

MementoRC commented Dec 29, 2022

Now would we want to have something like this (keeping the comments when possible):

    expected_src = """def func():
    if something:
        # Comments are retrieved
        async with something:  # comment2
            a = 1  # Non-standard indent
            b = 2  # Non-standard indent becomes standard

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants