Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Auto indent on line continuation with list/tuple #22

Open
alvinchow86 opened this issue Apr 29, 2014 · 49 comments
Open

Auto indent on line continuation with list/tuple #22

alvinchow86 opened this issue Apr 29, 2014 · 49 comments

Comments

@alvinchow86
Copy link

Pressing enter after continuing a list or tuple on a second line. It should either auto indent one tab, or to the first char of the previous item, but it just goes to the beginning of the line. Manually pressing auto indent (Lines->Auto Indent, or cmd+i), doesn't do anything.

image

should be either

somefunc(bar,
    m)

or

somefunc(bar,
         m)
@rougeth
Copy link

rougeth commented Apr 29, 2014

I vote for:

f = function(foo,
   bar)

@rougeth
Copy link

rougeth commented May 2, 2014

Actually, that is not a good example since it's not recommended in PEP8:

# Shouldn't be used:

# Arguments on first line forbidden when not using vertical alignment.
foo = long_function_name(var_one, var_two,
    var_three, var_four)

# Further indentation required as indentation is not distinguishable.
def long_function_name(
    var_one, var_two, var_three,
    var_four):
    print(var_one)

But, for the recommended ones, this change would help:

# Aligned with opening delimiter.
foo = long_function_name(var_one, var_two,
                         var_three, var_four)

# More indentation included to distinguish this from the rest.
def long_function_name(
        var_one, var_two, var_three,
        var_four):
    print(var_one)

# Hanging indents should add a level.
foo = long_function_name(
    var_one, var_two,
    var_three, var_four)

@sebdah
Copy link

sebdah commented Aug 4, 2014

+1 for this and I also vote for following the PEP8 guidelines. The following indentation is usually what other editors use:

# More indentation included to distinguish this from the rest.
def long_function_name(
        var_one, var_two, var_three,
        var_four):
    print(var_one)

@thelastnode
Copy link

+1

My vote is for these two:

# Aligned with opening delimiter.
foo = long_function_name(var_one, var_two,
                         var_three, var_four)

# More indentation included to distinguish this from the rest.
def long_function_name(
        var_one, var_two, var_three,
        var_four):
    print(var_one)

@TakenPilot
Copy link

+1

1 similar comment
@denisgarci
Copy link

+1

@mattdeboard
Copy link

I was this close to replacing Emacs with Atom but I cannot use an editor that doesn't get autoindent right.

PEP8 compliance is obviously the only right choice, anything else would be unacceptable. Where PEP8 allows leeway, just do the base case:

[1, 2, 3
 4, 5, 6]

[
  1, 2, 3
  4, 5, 6
]

@fariza
Copy link

fariza commented May 21, 2015

👍

1 similar comment
@pikeas
Copy link

pikeas commented May 22, 2015

+1

@EntilZha
Copy link

+1, was trying out Atom today, wouldn't use it unless this was supported. Too used to Vim/PyCharm doing this for me.

@EntilZha
Copy link

Is there any work that would need to be done in order to make it happen? If its something someone unfamiliar with the project can do, I might be interested.

@tscholak
Copy link

+1

@radix
Copy link

radix commented Jun 3, 2015

Is there a third-party package that implements this behavior since it's not in core yet?

@mattdeboard
Copy link

So with Atom 1.0, this issue is still definitely legitimate. However, it looks like hitting tab at least allows you to manually indent the line, which is a passable bridge until it works properly. Auto-indent will completely remove any manual indentation, however, which seems like something that should be fixed.

@ogrisel
Copy link

ogrisel commented Jun 25, 2015

+1 for following pep8 guidelines by default. jupyter notebook does it by default and it feels like an instant reward whenever I use it. I would love atom behaving the same.

@radix
Copy link

radix commented Jun 25, 2015

@mattdeboard I'm not sure what you mean -- I've upgraded to 1.0 but I haven't noticed any difference with what my tab key does. It still just always either indents 4 spaces, or indents to the previous line's level.

It's really painful to match up indentation to PEP8 style, especially since atom makes it very difficult to indent to levels indivisible by the tabstop. I often find myself deleting an "indent" (4 spaces) and then spamming my spacebar to get my code to line up properly. 😭

@mattdeboard
Copy link

@radix Pre-1.0, when you hit enter after an open-paren (or any sequence literal), the cursor would start at the beginning of the next line, instead of indented by one level. Hitting <Tab> would not move the cursor.

Now, the cursor still starts at the beginning of the next line, but tab moves the cursor. This is good-enough behavior, I think. Especially since it appears this is the same behavior as in SublimeText (and presumably TextMate, but I didn't check).

@mattdeboard
Copy link

FWIW that pain you have adhering to PEP 8 is because, IMO, only regex is being used to define indentation behavior.

@huang475
Copy link

huang475 commented Aug 4, 2015

+1 voting for this, this is the reason I prefer pycharm over atom for python file editing.

@michaelaye
Copy link

This is NOT the same behavior as in SublimeText, there I get PEP8 correct indenting of function parameter lists that become too long to fit on one line. Without this, I cannot use Atom, alas. :(

@fdegrave
Copy link

fdegrave commented Sep 9, 2015

+1

@neotrinity
Copy link

yep this is very annoying. Atom has so much potential..alas if basic code indentation doesn't work then I would be able to continue using atom.

@Twizzledrizzle
Copy link

For one that is new to atom and its packages, is this a python package specific problem or an atom problem?

@patrys
Copy link

patrys commented Oct 8, 2015

Part of it is this package (missing a lot of regex rules) and part is atom (no way to define dynamic indentation rules required by PEP8).

@lzkelley
Copy link

+1 ... come on ...

@xguse
Copy link

xguse commented Oct 30, 2015

+1 dear god this is ****ing ridiculous. How is this not a thing. Why even have support for pep8 linters if you can't ACTUALLY support pep8?

@EntilZha
Copy link

Can we get an update from a maintainer? This bug/defect has been around for a very long time, and clearly there are many people who are interested in having this fixed. What would it take to fix this? I haven't contributed to Atom before, but am willing give it a try if thats what it takes for this to move to in progress.

@50Wliu
Copy link
Contributor

50Wliu commented Oct 30, 2015

What would it take to fix this?

The current auto-indentation rules are regex based, which makes matching these types of scenarios very hard.

@mattdeboard
Copy link

It's also worth mentioning that TextMate, upon which Atom's language mode is based, doesn't get this right either (last I checked anyway).

python-mode for emacs does.

@EntilZha
Copy link

How do editors that do this correctly accomplish it? Some method that is not regex based?

@mattdeboard
Copy link

emacs uses regex, but I can confirm that it does get very complicated with look-ahead/-behind rules and so forth. Emacs has SMIE, a generic framework for indentation in language modes.

@lzkelley
Copy link

As mentioned above, there are linters that look for correct indentation (e.g. linter-flake8, linter-pep8, linter-python-pep8). Could the same method be used somehow?

@patrys
Copy link

patrys commented Oct 31, 2015

Not until atom/atom#9059 is fixed.

@lzkelley
Copy link

I'm unclear* on why this is a difficult fix --- it looks like the suggested indentation is based on first finding the indentation of the previous row, and then seeing if this should be incremented or decremented based on matches to the increaseIndentPattern and decreaseIndentPattern regexes, defined in the language-__.cson.

Wouldn't adding regex for lines ending with open brackets/parenthesis to the increaseIndentPattern solve the problem?

*I have no idea how Atom works, nor have I ever even looked at coffeescript before... so this shouldn't be surprising. Sorry if my comments are insultingly stupid, I'm trying to help instead of just complaining.

@patrys
Copy link

patrys commented Oct 31, 2015

Python uses dynamic indenting, it's not a matter of increasing indent after an open parenthesis.

@lzkelley
Copy link

What do you mean by 'dynamic indenting'? Do you mean that indents are used as delimiters?

@patrys
Copy link

patrys commented Oct 31, 2015

No I mean that PEP8 demands at least three different kinds of indents. One is eight spaces and one depends on the position of the last open parenthesis in the line before. See the Atom issue linked above.

@lzkelley
Copy link

Okay, I see what you mean. You're right that it definitely wouldn't allow for all of the acceptable indentation patterns. At the same time, PEP8 allows for many different types of indents. My suggestion won't work for the 'Aligned with opening delimiter' case --- but I think it will work for the 'Hanging indents should add a level' case. Wouldn't one working case be better than none?

@EntilZha
Copy link

Given the above, is there low hanging fruit that can be accomplished via regex even if it isn't fully PEP8 compliant?

@patrys
Copy link

patrys commented Oct 31, 2015

Yes, the regex is far from perfect. I think Sublime Text's grammar files may carry a better version for Python even though Sublime no longer depends on the regex itself.

@DSpeckhals
Copy link

I scanned through the core editor code, and definitely see the complication with adding some forms of Python indentation to language-python. I think a long term solution would require changes to both atom/atom and atom/language-python. The missing pieces I see are:

  1. Necessity to indent at a level not divisible by the current tab-width. It looks like there's an optional column param in the code that would give an offset back from the tab setting, but I don't see it utilized anywhere yet.
  2. Necessity to look back multiple lines to the corresponding opening delimiter after the "fluid/dynamic" indentation is complete. atom/atom language-mode.coffee and atom/language-python do not currently leave room for this from what I can see.

In the meantime, I wrote a relatively simple package that hooks into Python new line events, and indents/unindents based on PEP8 style: python-indent. I hope this helps to fill the gap for those hesitant to write Python in Atom. Feel free to offer suggestions too; this is my first Atom package! 😄

EDIT: I just added a couple of features to the python-indent package to cover the "hanging" indent suggested by some. Now, it has the option to line up succeeding lines with the opening delimiter OR hang the indent 1 or 2 tabs from the current block.

@EntilZha
Copy link

Very nice work @DSpeckhals! I tried the package out and it worked great and definitely bridges a much needed gap.

@nzufelt
Copy link

nzufelt commented Feb 12, 2016

Beautiful, @DSpeckhals, works like a charm!

@cbcoutinho
Copy link

cbcoutinho commented Sep 24, 2016

Exactly what solves (sidesteps?) the indentation issue in atom w.r.t python. Thanks again @DSpeckhals.

@kepbod
Copy link

kepbod commented Apr 26, 2017

Seem like python-indent has solved this problem.

@sah
Copy link

sah commented Mar 2, 2018

Would still be nicer if this were implemented as part of this package, so that e.g. autoIndentBufferRow (and therefore things like emacs-tab) would work correctly.

@apometta
Copy link

apometta commented Oct 23, 2018

+1 this is also a problem in C++. Anyone know of a package that solves it for this language? It also happens when formatting long multi-line string literals.

@kmayerb
Copy link

kmayerb commented Jun 21, 2019

FOR PYTHON:
https://atom.io/packages/python-indent
"Python Indent is the indentation behavior you've been waiting for in Atom! You should no longer have to worry about mashing your tab/space/backspace key every time you press enter in the middle of coding. Also, compared to other editors, there is no need to change an app configuration if you want to have a mixture of different types of indents (namely hanging and opening-delimiter-aligned).

The main obstacle with Atom's native indentation behavior is that it doesn't yet have the necessary API's to do what Python's PEP8 style guide suggests. Enhancement requests and issues have been opened in Atom Core on a few occasions, but none have been resolved yet.

language-python - Auto indent on line continuation with list/tuple
atom - Autoindent not working properly
This package was made to give you expected indentation behavior; python-indent listens for editor:newline events in Python source files, and when triggered, adjusts the indentation to be lined up relative to the opening delimiter of the statement or "hanging" (for parameters, tuples, or lists)."

@jazzlw
Copy link

jazzlw commented Oct 12, 2019

Thanks a bunch @DSpeckhals, this was driving me crazy and I'm so happy to have just found this. Don't know why this isn't integrated into Atom, but I'm just happy that the pain is over. 😸

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests