spyder-ide / spyder

Official repository for Spyder - The Scientific Python Development Environment
https://www.spyder-ide.org
MIT License
8.35k stars 1.62k forks source link

Inconsistent indentation after parentheses, brackets, braces #14811

Open kbauer opened 3 years ago

kbauer commented 3 years ago

Problem Description

Automatic indentation on printing is inconsistent, when a line-break is inserted before the first item / function argument / key in multi-line argument lists, list expressions and dictionary/set expressions, and the opening parenthesis / bracket / brace is less than 8 characters away from the line indentation.

This is likely strongly related to https://github.com/spyder-ide/spyder/issues/887.

What steps reproduce the problem?

  1. Type a=[<ENTER>item1,<ENTER>item2]
  2. Type abcdef=[<ENTER>item1,<ENTER>item2]
  3. Type abcdefg=[<ENTER>item1,<ENTER>item2]
  4. Type abcdefghi=[ item1,<ENTER>item2]

What is the expected output? What do you see instead?

I would expect the output to be consistent across both cases, i.e.

a=[
    item1,
    item2]
abcdef=[
    item1,
    item2]
abcdefg=[
    item1,
    item2]
abcdefghi=[  item1,
             item2]

but instead for a line length of up to 8 on the first line of the statement, indentation is aligned with the opening bracket,

a=[
   item1,
   item2]
abcdef=[
        item1,
        item2]
abcdefg=[
    item1,
    item2]
abcdefghi=[  item1,
           item2]

This feels inconsistent:

Suggestion: Entirely remove special treatment of short first lines. They are already covered consistently by the different treatment of continuation lines, where the opening parenthesis/bracket/brace was followed by an item on the first line. However:

The same issues appear with argument lists including keyword arguments, and braces of dictionary expressions.

More examples


a=[
   item1,
   item2]
abcdef=[
        item1,
        item2]
abcdefg=[
    item1,
    item2]

abcd = [
        item1,
        item2]
abcde = [
    item1,
    item2]

abcdefg(
        arg1,
        arg2)
abcdefgh(
    arg1,
    arg2)

abcdef={
        arg1: value1,
        arg2: value2}
abcdefg={
    key1: value1,
    key2: value2}

abc=DEF(
        arg1,
        arg2)
abc=DEFG(
    arg1,
    arg2)

abcdefgh = [ item1,
            item2]

Versions

Dependencies

# Mandatory:
atomicwrites >=1.2.0            :  1.4.0 (OK)
chardet >=2.0.0                 :  3.0.4 (OK)
cloudpickle >=0.5.0             :  1.6.0 (OK)
diff_match_patch >=20181111     :  20200713 (OK)
intervaltree >=3.0.2            :  3.1.0 (OK)
IPython >=7.6.0                 :  7.19.0 (OK)
jedi =0.17.2                    :  0.17.2 (OK)
jsonschema >=3.2.0              :  3.2.0 (OK)
keyring >=17.0.0                :  21.4.0 (OK)
nbconvert >=4.0                 :  6.0.7 (OK)
numpydoc >=0.6.0                :  1.1.0 (OK)
parso =0.7.0                    :  0.7.0 (OK)
pexpect >=4.4.0                 :  4.8.0 (OK)
pickleshare >=0.4               :  0.7.5 (OK)
psutil >=5.3                    :  5.7.2 (OK)
pygments >=2.0                  :  2.7.2 (OK)
pylint >=1.0                    :  2.6.0 (OK)
pyls >=0.36.2;<1.0.0            :  0.36.2 (OK)
pyls_black >=0.4.6              :  0.4.6 (OK)
pyls_spyder >=0.3.0             :  0.3.0 (OK)
qdarkstyle >=2.8                :  2.8.1 (OK)
qtawesome >=0.5.7               :  1.0.1 (OK)
qtconsole >=5.0.1               :  5.0.2 (OK)
qtpy >=1.5.0                    :  1.9.0 (OK)
rtree >=0.8.3                   :  0.9.4 (OK)
setuptools >=39.0.0             :  50.3.1.post20201107 (OK)
sphinx >=0.6.6                  :  3.2.1 (OK)
spyder_kernels >=1.10.1;<1.11.0 :  1.10.1 (OK)
textdistance >=4.2.0            :  4.2.1 (OK)
three_merge >=0.1.1             :  0.1.1 (OK)
watchdog >=0.10.3               :  0.10.3 (OK)
xdg >=0.26                      :  0.27 (OK)
zmq >=17                        :  19.0.2 (OK)

# Optional:
cython >=0.21                   :  0.29.21 (OK)
matplotlib >=2.0.0              :  3.3.2 (OK)
numpy >=1.7                     :  1.19.5 (OK)
pandas >=1.1.1                  :  1.2.1 (OK)
scipy >=0.17.0                  :  1.5.2 (OK)
sympy >=0.7.3                   :  1.6.2 (OK)
dalthviz commented 3 years ago

Hi @kbauer thanks for the feedback! What do you think @spyder-ide/core-developers ? Maybe there was some reason to have the current different treatments taking into account the length of the line?

Dan-Patterson commented 3 years ago

In 4.2 in the Editor, not the Console, to throw into the "discussion". I use a space before and after assignment which doesn't seem to matter. It behaves as expected for me

item1 = "abcdefgh"
item2 = "12345678"
a = [item1,
     item2
     ]
abcdefgh = [item1,
            item2
            ]
a = [
     item1,
     item2
     ]
abcdefgh = [
    item1,
    item2
    ]
# with not space around assignment
a=[
   item1,
   item2
   ]
abcdefgh=[
    item1,
    item2
    ]
kbauer commented 3 years ago
a = [
     item1,
     item2
     ]

This is actually an example of the inconsistent behavior. It's just a less obvious example with an indentation of 5 characters.

Dan-Patterson commented 3 years ago

Good catch. It appears in that example, to be a tradeoff between aligning the text under the [ or adding an extra space to make it appear "correct"