Repository: spyder-ide/spyder

Issue:
Code cells not properly executed from splitted editors or new window editors
### Issue Report Checklist

* [x] Searched the [issues page](https://github.com/spyder-ide/spyder/issues?q=is%3Aissue) for similar reports
* [x] Read the relevant sections of the [Spyder Troubleshooting Guide](https://github.com/spyder-ide/spyder/wiki/Troubleshooting-Guide-and-FAQ) and followed its advice
* [x] Reproduced the issue after updating with ``conda update spyder`` (or ``pip``, if not using Anaconda)
* [ ] Could not reproduce inside ``jupyter qtconsole`` (if console-related)
* [x] Tried basic troubleshooting (if a bug/error)
    * [x] Restarted Spyder
    * [ ] Reset preferences with ``spyder --reset``
    * [ ] Reinstalled the latest version of [Anaconda](https://www.anaconda.com/download/)
    * [ ] Tried the other applicable steps from the Troubleshooting Guide
* [x] Completed the **Problem Description**, **Steps to Reproduce** and **Version** sections below


## Problem Description

The behaviour is:

    - the editor shows a red line above each occurence of #%%
    - press Ctrl-Enter in any cell N
    - console shows runcell(0,...
    - the whole file runs as if no cells were defined

### What steps reproduce the problem?

It appears to happen randomly in different files, but can also be reproduced more reliably when a second editor window is opened.  The malfunction (sofar) always happened in the new window.

### What is the expected output? What do you see instead?
See above


### Paste Traceback/Error Below (if applicable)
n/a


## Versions
<!--- You can get this information from Help > About Spyder...
or (if Spyder won't launch) the "conda list" command
from the Anaconda Prompt/Terminal/command line. --->

* Spyder version: 4.1.2 (problem first noticed a few months ago already)
* Python version: 3.7.2 64b
* Qt version: 5.9.6
* PyQt version: 5.9.2
* Operating System name/version: Win7 64b

### Dependencies
<!--- Please go to the menu entry Help > Dependencies,
press the Copy to clipboard button and paste below --->

```

# Mandatory:
atomicwrites >=1.2.0           :  1.2.1 (OK)
chardet >=2.0.0                :  3.0.4 (OK)
cloudpickle >=0.5.0            :  0.6.1 (OK)
diff_match_patch >=20181111    :  20181111 (OK)
intervaltree                   :  None (OK)
IPython >=4.0                  :  7.2.0 (OK)
jedi =0.15.2                   :  0.15.2 (OK)
nbconvert >=4.0                :  5.3.1 (OK)
numpydoc >=0.6.0               :  0.8.0 (OK)
paramiko >=2.4.0               :  2.4.2 (OK)
parso =0.5.2                   :  0.5.2 (OK)
pexpect >=4.4.0                :  4.7.0 (OK)
pickleshare >=0.4              :  0.7.5 (OK)
psutil >=5.3                   :  5.4.8 (OK)
pygments >=2.0                 :  2.3.1 (OK)
pylint >=0.25                  :  2.2.2 (OK)
pyls >=0.31.9;<0.32.0          :  0.31.9 (OK)
qdarkstyle >=2.8               :  2.8 (OK)
qtawesome >=0.5.7              :  0.5.7 (OK)
qtconsole >=4.6.0              :  4.6.0 (OK)
qtpy >=1.5.0                   :  1.5.2 (OK)
rtree >=0.8.3                  :  0.8.3 (OK)
sphinx >=0.6.6                 :  1.8.2 (OK)
spyder_kernels >=1.9.0;<1.10.0 :  1.9.0 (OK)
watchdog                       :  None (OK)
zmq >=17                       :  17.1.2 (OK)

# Optional:
cython >=0.21                  :  0.29.2 (OK)
matplotlib >=2.0.0             :  3.0.2 (OK)
numpy >=1.7                    :  1.15.4 (OK)
pandas >=0.13.1                :  0.24.2 (OK)
scipy >=0.17.0                 :  1.2.1 (OK)
sympy >=0.7.3                  :  1.3 (OK)

```



Discussion:
Thanks for the bug report.

I haven’t seen errors in the main window yet but code cells in a new editor window and a split view are broken in 4.x. Runcell function will always return 0 and the whole file will be executed

This is a recent regression (within the last 2-3 months)
@impact27, could you take a look at this one?
Here's another observation:  When I split the editor into two panels, the new one has the problem.  The work-around appears to be to just run code cells from the original editor pane.
@steff456, please take a look at this one.
I had a quick look and it looks like the first code editor stack receives the command even if it was sent from another window. I think these should be handled at the plugin level
@impact27, I was checking this issue out all the day and I found out that there's something weird with the cells in splitted editors. For example, the creation of the cells is alright in the code, but in some point it changes and even the cell highlight isn't working as expected. And I think that probably fixing the highlighting out will fix as well the `run cell` action.

![image](https://user-images.githubusercontent.com/20992645/81766677-2f718800-949c-11ea-8ce7-933b8edd37fd.png)

Can you please help us out to see how we may fix this issue?

Resolving patch:
diff --git a/spyder/plugins/editor/widgets/base.py b/spyder/plugins/editor/widgets/base.py
--- a/spyder/plugins/editor/widgets/base.py
+++ b/spyder/plugins/editor/widgets/base.py
@@ -99,8 +99,6 @@ def __init__(self, parent=None):
         # Useful to avoid recomputing while scrolling.
         self.current_cell = None
 
-        self._cell_list = []
-
         def reset_current_cell():
             self.current_cell = None
 
@@ -380,23 +378,10 @@ def keyPressEvent(self, event):
             super(TextEditBaseWidget, self).keyPressEvent(event)
 
     #------Text: get, set, ...
-    def add_to_cell_list(self, oedata):
-        """Add new cell to cell list."""
-        self._cell_list.append(oedata)
-
     def get_cell_list(self):
         """Get all cells."""
-        # Filter out old cells
-
-        def good(oedata):
-            return oedata.is_valid() and oedata.def_type == oedata.CELL
-
-        self._cell_list = [
-            oedata for oedata in self._cell_list if good(oedata)]
-
-        return sorted(
-            {oedata.get_block_number(): oedata
-             for oedata in self._cell_list}.items())
+        # Reimplemented in childrens
+        return []
 
     def get_selection_as_executable_code(self, cursor=None):
         """Return selected text as a processed text,
diff --git a/spyder/plugins/editor/widgets/codeeditor.py b/spyder/plugins/editor/widgets/codeeditor.py
--- a/spyder/plugins/editor/widgets/codeeditor.py
+++ b/spyder/plugins/editor/widgets/codeeditor.py
@@ -1739,11 +1739,33 @@ def _set_highlighter(self, sh_class):
         self.highlighter = self.highlighter_class(self.document(),
                                                   self.font(),
                                                   self.color_scheme)
+        self.highlighter._cell_list = []
         self.highlighter.sig_new_cell.connect(self.add_to_cell_list)
         self._apply_highlighter_color_scheme()
 
         self.highlighter.editor = self
 
+    def add_to_cell_list(self, oedata):
+        """Add new cell to cell list."""
+        if self.highlighter is None:
+            return
+        self.highlighter._cell_list.append(oedata)
+
+    def get_cell_list(self):
+        """Get all cells."""
+        if self.highlighter is None:
+            return []
+        # Filter out old cells
+        def good(oedata):
+            return oedata.is_valid() and oedata.def_type == oedata.CELL
+
+        self.highlighter._cell_list = [
+            oedata for oedata in self.highlighter._cell_list if good(oedata)]
+
+        return sorted(
+            {oedata.get_block_number(): oedata
+             for oedata in self.highlighter._cell_list}.items())
+
     def is_json(self):
         return (isinstance(self.highlighter, sh.PygmentsSH) and
                 self.highlighter._lexer.name == 'JSON')


Test patch: