Repository: spyder-ide/spyder

Issue:
Another KeyError when folding regions
## Description

### What steps will reproduce the problem?

<!--- You can use Markdown here --->

selected variable inspection window for a dataframe, then selected between another variable inspection window for a dict.  Recieved an error about mismatch (or something between the two, don't recall), and then returning to editor hit backspace a few times to delete and it crashed

### Traceback
```python-traceback
  File "C:\Users\nellypledge\anaconda3\lib\site-packages\spyder\plugins\editor\utils\editor.py", line 161, in _exec_requested_job
    self._job(*self._args, **self._kwargs)
  File "C:\Users\nellypledge\anaconda3\lib\site-packages\spyder\plugins\editor\panels\codefolding.py", line 451, in _highlight_block
    end_line = self.folding_regions[block_line]
KeyError: 67
```

## Versions

* Spyder version: 4.1.3 
* Python version: 3.7.7
* Qt version: 5.9.6
* PyQt5 version: 5.9.2
* Operating System: Windows 10

### Dependencies

```

# Mandatory:
atomicwrites >=1.2.0           :  1.4.0 (OK)
chardet >=2.0.0                :  3.0.4 (OK)
cloudpickle >=0.5.0            :  1.4.1 (OK)
diff_match_patch >=20181111    :  20181111 (OK)
intervaltree                   :  None (OK)
IPython >=4.0                  :  7.15.0 (OK)
jedi =0.15.2                   :  0.15.2 (OK)
nbconvert >=4.0                :  5.6.1 (OK)
numpydoc >=0.6.0               :  1.0.0 (OK)
paramiko >=2.4.0               :  2.7.1 (OK)
parso =0.5.2                   :  0.5.2 (OK)
pexpect >=4.4.0                :  4.8.0 (OK)
pickleshare >=0.4              :  0.7.5 (OK)
psutil >=5.3                   :  5.7.0 (OK)
pygments >=2.0                 :  2.6.1 (OK)
pylint >=0.25                  :  2.4.4 (OK)
pyls >=0.31.9;<0.32.0          :  0.31.10 (OK)
qdarkstyle >=2.8               :  2.8.1 (OK)
qtawesome >=0.5.7              :  0.7.2 (OK)
qtconsole >=4.6.0              :  4.7.5 (OK)
qtpy >=1.5.0                   :  1.9.0 (OK)
rtree >=0.8.3                  :  0.9.4 (OK)
sphinx >=0.6.6                 :  3.1.1 (OK)
spyder_kernels >=1.9.1;<1.10.0 :  1.9.1 (OK)
watchdog                       :  None (OK)
zmq >=17                       :  19.0.1 (OK)

# Optional:
cython >=0.21                  :  0.29.20 (OK)
matplotlib >=2.0.0             :  3.2.2 (OK)
numpy >=1.7                    :  1.18.5 (OK)
pandas >=0.13.1                :  1.0.5 (OK)
scipy >=0.17.0                 :  1.5.0 (OK)
sympy >=0.7.3                  :  1.6 (OK)
```



Discussion:
Hi @nranthony seems like this issue was fixed by #11310 and you should have the fix since the version you are using is 4.1.3. Could you give us an example code snippet to replicate the issue please?

This is kind of strange what do you think @ccordoba12 ?

 
I'm sorry, I can't recreate.  I don't know what caused it at this point.  The auto submit popped up and I added as much as I could recall.
Don't worry @nranthony thanks for the feedback !

There isn't much that we can do with out a way to reproduce the issue for the moment. However, if you manage to figure out a way to reproduce this let us know :)
> This is kind of strange what do you think @ccordoba12 ?

Although perhaps there's no easy way to reproduce this, it can still happen because #11310 added a try/except around `_highlight_runner`, but there are other calls to `_highlight_block` in `codefolding.py` that are not protected by that.

@steff456, please add a try/except KeyError around those other calls to `_highlight_block` in that file.
Cool, thanks for your time on these things.  I'll convey anything that happens that might be helpful.

Resolving patch:
diff --git a/spyder/plugins/editor/panels/codefolding.py b/spyder/plugins/editor/panels/codefolding.py
--- a/spyder/plugins/editor/panels/codefolding.py
+++ b/spyder/plugins/editor/panels/codefolding.py
@@ -504,7 +504,12 @@ def mouseMoveEvent(self, event):
                     # fold scope changed, a previous block was highlighter so
                     # we quickly update our highlighting
                     self._mouse_over_line = block.blockNumber()
-                    self._highlight_block(block)
+                    try:
+                        self._highlight_block(block)
+                    except KeyError:
+                        # Catching the KeyError above is necessary to avoid
+                        # issue spyder-ide/spyder#13230.
+                        pass
                 else:
                     # same fold scope, request highlight
                     self._mouse_over_line = block.blockNumber()
@@ -802,7 +807,12 @@ def _highlight_caret_scope(self):
             line_number = block.blockNumber()
             if line_number in self.folding_regions:
                 self._mouse_over_line = block.blockNumber()
-                self._highlight_block(block)
+                try:
+                    self._highlight_block(block)
+                except KeyError:
+                    # Catching the KeyError above is necessary to avoid
+                    # issue spyder-ide/spyder#13230.
+                    pass
             else:
                 self._clear_scope_decos()
         self._block_nbr = block_nbr


Test patch: