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

Editor doesn't support hanging indentation #3480

Closed
yulkang opened this issue Sep 29, 2016 · 4 comments
Closed

Editor doesn't support hanging indentation #3480

yulkang opened this issue Sep 29, 2016 · 4 comments

Comments

@yulkang
Copy link
Contributor

yulkang commented Sep 29, 2016

Description of your problem

What steps will reproduce the problem?

  1. In Editor, type
    long_function_name(
    then press Enter.

What is the expected output? What do you see instead?
I wish there is an option for hanging indent (of a fixed number of spaces), rather than vertical alignment. That is, I want an option for:

# Hanging indent
def long_function_name(
        var_one, var_two, var_three,
        var_four):
    print(var_one)

rather than:

# Vertical alignment
def long_function_name(
                       var_one, var_two, var_three,
                       var_four):
    print(var_one)

Currently the vertical alignment seems to be the only option. But it saves little space when there is a big indentation. PEP also recommends both styles.

If I could wish for more, I would suggest having the hanging indent of twice the amount for one level (e.g., 4 x 2 = 8 spaces), so as to disambiguate it with subsequent indents that are not continuations, as I copied above from the second example of the PEP: https://www.python.org/dev/peps/pep-0008/#indentation.

Interestingly, hanging indent (of twice the amount for one level) is also the style of the MATLAB editor, so users converting from MATLAB would find it more familiar. It could be important since Spyder is the IDE of choice for many previous MATLAB users.

Please provide any additional information below

Versions and main components

  • Spyder Version: 3.0.0
  • Python Version: 2.7.12
  • Operating system: Mac OS X Sierra (10.12)

Dependencies

Please go to the menu entry Help > Optional Dependencies (or
Help > Dependencies), press the button Copy to clipboard
and paste the contents below:

jedi >=0.8.1 : 0.9.0 (OK)
matplotlib >=1.0 : 1.5.3 (OK)
nbconvert >=4.0 : 4.2.0 (OK)
numpy >=1.7 : 1.11.1 (OK)
pandas >=0.13.1 : 0.18.1 (OK)
pep8 >=0.6 : 1.7.0 (OK)
psutil >=0.3 : 4.3.0 (OK)
pyflakes >=0.5.0 : 1.2.3 (OK)
pygments >=2.0 : 2.1.3 (OK)
pylint >=0.25 : 1.6.4 (OK)
qtconsole >=4.2.0: 4.2.1 (OK)
rope >=0.9.4 : 0.9.4 (OK)
sphinx >=0.6.6 : 1.4.1 (OK)
sympy >=0.7.3 : 1.0 (OK)

@ccordoba12 ccordoba12 added this to the wishlist milestone Sep 29, 2016
@ccordoba12
Copy link
Member

Spyder kind of does what you want. If you manually indent your first variables like this

def long_function_name(
        var_one, var_two, var_three,

then the coming lines will respect your initial indentation.


If you want to improve this, you're welcome to send us a pull request to implement it because we don't have time to do it :-)

@Nodd
Copy link
Contributor

Nodd commented Sep 29, 2016

it used to work like @yulkang wants. From pep8 (from what I recall, at least), vertical alignment should be used only if there is something right after the open parenthesis.

# 1: OK
def long_function_name(
    var_one, var_two, var_three)

# 2: OK
def long_function_name(var_one,
                       var_two,
                       var_three)

# 3: NOT OK
def long_function_name(var_one,
    var_two,
    var_three)

# 4: NOT OK
def long_function_name(
                       var_one,
                       var_two,
                       var_three)

Spyder is currently doing 4 instead of 1.

@ccordoba12 ccordoba12 modified the milestones: v3.1, wishlist Sep 29, 2016
@yulkang
Copy link
Contributor Author

yulkang commented Sep 30, 2016

I made a preliminary version and made a pull request:
https://github.com/yulkang/spyder/tree/hangingindent

It has an option for hanging indent at menu - python - Preferences - Editor - Advanced settings - Hanging indentation. It is unchecked by default, but when checked, allows hanging indentation, as follows:

def long_function_name(
        var1, var2):
    print("This works. Yeah!")

if long_function_name(
        var1, var2):
    print("also works")

The case I found not working is the following, where the subsequent line is indented one step too much:

if long_array_name[
        var1]:
            print("this does not work yet")

But such cases are rarer for me, so it would be useful even as it stands now. I haven't found why this happens, and I would appreciate help.

I checked the following and they work as expected, too:

  • the new option is saved after quitting, and
  • the default behavior, vertical alignment, is recovered when the new option is unchecked.

Please let me know if there are other issues.

@ccordoba12
Copy link
Member

@yulkang, thanks a lot for looking at it. I'd like to say two things:

  1. Please create a pull request with your work so we can review it and merge it :-)
  2. Please don't add a new option for this (we have enough options already). Let's make this the default behavior ;-)

@ccordoba12 ccordoba12 changed the title Option for hanging indent Add hanging indentation to the Editor Nov 27, 2016
@ccordoba12 ccordoba12 changed the title Add hanging indentation to the Editor Editor doesn't support hanging indentation Nov 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants