dnanhkhoa / nb_black

A simple extension for Jupyter Notebook and Jupyter Lab to beautify Python code automatically using black.
MIT License
367 stars 41 forks source link

Indentation around comments #33

Open esillekens opened 3 years ago

esillekens commented 3 years ago

Indentated comments have inconvenient behaviour with jupyter lab comment and uncomment function. Lab adds the # at the start of the line and black adds spaces to match indentation, uncommenting in lab just removes the # and the code is now floating with extra indentation.

E.g., I have a function

def fun(x):
    """function adds 1"""
    x += 1
    x *= 1
    return x

and I comment the third line with ctrl+/

def fun(x):
    """function adds 1"""
    x += 1
#     x *= 1
    return x

executing with black enabled will make it look like:

def fun(x):
    """function adds 1"""
    x += 1
    #     x *= 1
    return x

Then uncommenting with ctrl+/ again yields:

def fun(x):
    """function adds 1"""
    x += 1
        x *= 1
    return x

Related: https://github.com/jupyterlab/jupyterlab/issues/6957