t3rn0 / ast-comments

Extension to the built-in ast module. Finds comments in source code and adds them to the parsed tree.
MIT License
31 stars 10 forks source link

Comments preserve the indentation structure. #15

Closed gvariable closed 1 year ago

gvariable commented 1 year ago

Say here is the source code I want to do some transformation like replace codes between #### Your solution #### and "#### End of solution ####" with pass. Compared with the source code, I got the wrong indention structure.

foo = """
def foo():
    #### Your solution ####
    c = a + b
    #### End of solution ####
    if a:
        #### Your solution ####
        a += 1
        #### End of solution ####
        #### Your solution ####
        b += 1
        #### End of solution ####
"""
print(ast.unparse(ast.parse(foo)))
bar = """
def bar():
    #### Your solution ####
    c = a + b
    #### End of solution ####
    if a:
        pass
        #### Your solution ####
        #### End of solution ####
    """
print(ast.unparse(ast.parse(bar)))

Expected Behavior

The code generated will have the identical indentation format as the original source code.

Current Behavior

def foo():
    #### Your solution ####
    c = a + b
    #### End of solution ####
    if a:
        #### Your solution ####
        a += 1
        #### End of solution ####
        #### Your solution ####
        b += 1
#### End of solution ####
def bar():
    #### Your solution ####
    c = a + b
    #### End of solution ####
    if a:
        pass
#### Your solution ####
#### End of solution ####
t3rn0 commented 1 year ago

Hi @gvariable. I'm afraid I won't be able to fix this.

See, ast_comments inherits all the basic parsing logic from the original ast. From the ast perspective, any logical block ends with the last "real" ast-statement:

if a:  # <- If starts
    # c1 
    do_something_1()  # <- If.body starts 
    # c2
    do_something_2()  # <- If.body ends. If ends
    # c3  # <- not If
# c4  # <- same as c3

There's no difference between c3 and c4. Except for the indentation which doesn't follow any particular language rules. Current behavior is the expected behavior. That's why I'll skip this issue and mark it as "won't fix". At least for now

gvariable commented 1 year ago

Current I have this demand, I want to base ast_comment for developing my own version, but I'm not very familiar with it. Do you have any suggestions?

t3rn0 commented 1 year ago

Hi. After #20 current behavior matches expected