spyder-ide / spyder

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

Hangs when trying to indent line with tab followed by non-blank line with spaces #16987

Open ethanbb opened 2 years ago

ethanbb commented 2 years ago

Issue Report Checklist

Problem Description

A hang/crash occurs in certain situations involving mixed tabs and spaces. A prototypical example is shown below; Spyder will crash if one highlights both lines and presses the Tab key.

image

More details:

What steps reproduce the problem?

Reproducing is a little tricky since it appears that the latest version of Spyder has another bug where changing the indentation preference doesn't take effect until restart.

  1. Enable Source > Show blank spaces (not necessary, just to see what you're doing)
  2. In any file, put a single tab character on its own line
  3. On the line below, type 4 spaces, then any non-space characters.
  4. Highlight both lines and press the Tab key.

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

Expected: both lines are indented according to the current indentation mode. Actual: Spyder hangs.

Versions

Dependencies

# Mandatory:
atomicwrites >=1.2.0          :  1.4.0 (OK)
chardet >=2.0.0               :  4.0.0 (OK)
cloudpickle >=0.5.0           :  2.0.0 (OK)
cookiecutter >=1.6.0          :  1.7.3 (OK)
diff_match_patch >=20181111   :  20200713 (OK)
intervaltree >=3.0.2          :  3.0.2 (OK)
IPython >=7.6.0               :  7.19.0 (OK)
jedi >=0.17.2;<0.19.0         :  0.18.0 (OK)
jellyfish >=0.7               :  0.8.9 (OK)
jsonschema >=3.2.0            :  3.2.0 (OK)
keyring >=17.0.0              :  21.2.1 (OK)
nbconvert >=4.0               :  6.0.7 (OK)
numpydoc >=0.6.0              :  1.1.0 (OK)
paramiko >=2.4.0              :  2.8.1 (OK)
parso >=0.7.0;<0.9.0          :  0.8.1 (OK)
pexpect >=4.4.0               :  4.8.0 (OK)
pickleshare >=0.4             :  0.7.5 (OK)
psutil >=5.3                  :  5.8.0 (OK)
pygments >=2.0                :  2.7.3 (OK)
pylint >=2.5.0                :  2.7.2 (OK)
pyls_spyder >=0.4.0           :  0.4.0 (OK)
pylsp >=1.3.1;<1.4.0          :  1.3.2 (OK)
pylsp_black >=1.0.0           :  None (OK)
qdarkstyle =3.0.2             :  3.0.2 (OK)
qstylizer >=0.1.10            :  0.2.1 (OK)
qtawesome >=1.0.2             :  1.1.1 (OK)
qtconsole >=5.2.1;<5.3.0      :  5.2.1 (OK)
qtpy >=1.5.0                  :  1.11.3 (OK)
rtree >=0.9.7                 :  0.9.7 (OK)
setuptools >=49.6.0           :  49.6.0.post20201009 (OK)
sphinx >=0.6.6                :  4.3.1 (OK)
spyder_kernels >=2.2.0;<2.3.0 :  2.2.0 (OK)
textdistance >=4.2.0          :  4.2.2 (OK)
three_merge >=0.1.1           :  0.1.1 (OK)
watchdog >=0.10.3             :  2.1.6 (OK)
zmq >=17                      :  20.0.0 (OK)
# Optional:
cython >=0.21                 :  None (OK)
matplotlib >=2.0.0            :  3.3.3 (OK)
numpy >=1.7                   :  1.19.2 (OK)
pandas >=1.1.1                :  1.2.0 (OK)
scipy >=0.17.0                :  1.7.2 (OK)
sympy >=0.7.3                 :  None (OK)
dalthviz commented 2 years ago

Hi @ethanbb thank you for the feedback. I was unable to reproduce the issue with the described steps. Could you post here a video or GIF to better understand how is this happening to you?

Any new info in order to reproduce this in our side is greatly appreciated. Let us know!

ethanbb commented 2 years ago

Hi @dalthviz, sure, here's a screen recording. This happened in exactly the same way on two different machines on Spyder 5.2.0 in a similar, but not identical, conda environment. Let me know if there's any more info I can provide.

https://user-images.githubusercontent.com/8973825/145664189-aab21158-8d9c-4cb7-a99e-89820acfb569.mp4

dalthviz commented 2 years ago

Hi again @ethanbb , just in case., could you update to Spyder 5.2.1 and check again? Let us know!

ethanbb commented 2 years ago

Hi @dalthviz, unfortunately I can confirm that it's still broken in 5.2.1.

Also to add, obviously the minimal example I gave was a bit contrived, but it can come up in real-world files where tabs and spaces are mixed. I came across the bug when I was trying to indent a whole block of code that included a pair of lines where the first had only tabs and the second had spaces followed by a statement.

Is there any way I can get more debugging info? This is such a mysterious bug, I'm curious what's going on. I tried looking at the internal console, but nothing came up.

dalthviz commented 2 years ago

To get more info you can try to get a debug log by running Spyder with some flags:

spyder --debug-info verbose --debug-output file

That will generate a spyder-debug.log file in the working directory of the cmd that you used to launch Spyder. After getting it you can check it and post it here (maybe it will enable us to see some traceback or some repetitive operation being done).

Also, maybe running in safe-mode (using a clean config in a temporary directory) could help discard issues with the preferences:

spyder --safe-mode

For more info related to the CLI options that Spyder has you can run spyder --help

Let us know @ethanbb !

ethanbb commented 2 years ago

OK, here it is - I don't see anything unusual, but maybe you can take a look.

spyder-debug.log

dalthviz commented 2 years ago

After some trying I was able to reproduce this @ethanbb ! In the preferences for the Editor under the Source tab, what is the preference for the Indentation characters you have set? My first guess is that this has something to do with setting that preference with the Tabulation value. Could you try setting it to 4 spaces instead an see if that helps?

Not totally sure why this could be happening but we will try to fix it in a future release :+1:

Let us know!

ethanbb commented 2 years ago

@dalthviz Glad to hear you could replicate it. For me it happens when the preference is set to either Tabulation or 4 spaces.

Another, probably unrelated quirk I noticed is that when using Shift+Tab to de-dent regions with indentation set to 4 spaces, only 3 spaces were removed from each line if and only if the highlighted region was only whitespace. (As I'm writing this I realize this bug is unlikely to ever inconvenience anyone...still, something's funky!)

dalthviz commented 2 years ago

Thank you for the new info @ethanbb ! Indeed quite strange behavior there. We need for sure to give it a check to the functionality to handle tabs vs spaces and indentation behavior (most probably the current logic is not properly handling specific cases)