Repository: spyder-ide/spyder

Issue:
Code cells that fill the whole screen lose their background highlight
<!--- **PLEASE READ:** When submitting here, please ensure you've completed the following checklist and checked the boxes to confirm. Issue reports without it may be closed. Thanks! --->

### Issue Report Checklist

* [x] Searched the [issues page](https://github.com/spyder-ide/spyder/issues?q=is%3Aissue) for similar reports
* [ ] Read the relevant sections of the [Spyder Troubleshooting Guide](https://github.com/spyder-ide/spyder/wiki/Troubleshooting-Guide-and-FAQ) and followed its advice
* [ ] Reproduced the issue after updating with ``conda update spyder`` (or ``pip``, if not using Anaconda)
* [ ] Could not reproduce inside ``jupyter qtconsole`` (if console-related)
* [ ] Tried basic troubleshooting (if a bug/error)
    * [ ] 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
* [ ] Completed the **Problem Description**, **Steps to Reproduce** and **Version** sections below


## Problem Description

If a code cell fills the whole screen it loses the code cell highlighting.

![Code cell unhighlight](https://user-images.githubusercontent.com/10513354/58441311-473ed900-809e-11e9-8944-c47d12c04e28.gif)

### What steps reproduce the problem?

1. Make a code cell bigger than the screen space of the editor
2. Scroll until the top of the cell is out of the screen

### What is the expected output? What do you see instead?

It should stay highlighted so the user knows that they are still in a cell.

## 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: master
 


Discussion:
Because of the some changes in PR:#9082 this bug has returned. 

It looks like `has_cell_separators` in `syntaxhighlighters` is set to false if a line is changed and that line isn't a code cell comment. `has_cell_separators` needs to indicate if the file has at least on code cell.

Now that the outliner tree is updated frequently we could change `has_cell_separators` based on whether there is a code cell in the outliner tree.

@impact27 Any thoughts on this?
I would probably:
 - Get rid of `found_cell_separators` as it is not reliable. (The syntax highlighter is not notified about deletions)
 - Check the document for any oedata that  are cells.

You can get all the outlineexplorer data with `CodeEditor`'s `outlineexplorer_data_list`. From one `OutlineExplorerData` you can use `_document_cells` to get all the cells.

(It would probably be a good idea to consolidate these two. You can find all the block iterations by searching for `while block.isValid():`.)
Also both of these are generators so you can stop the iteration as soon as you find a cell.

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
@@ -538,7 +538,12 @@ def highlight_current_cell(self):
         if whole_file_selected:
             self.clear_extra_selections('current_cell')
         elif whole_screen_selected:
-            if self.highlighter.found_cell_separators:
+            has_cell_separators = False
+            for oedata in self.outlineexplorer_data_list():
+                if oedata.def_type == oedata.CELL:
+                    has_cell_separators = True
+                    break
+            if has_cell_separators:
                 self.set_extra_selections('current_cell', [selection])
                 self.update_extra_selections()
             else:
diff --git a/spyder/utils/syntaxhighlighters.py b/spyder/utils/syntaxhighlighters.py
--- a/spyder/utils/syntaxhighlighters.py
+++ b/spyder/utils/syntaxhighlighters.py
@@ -156,9 +156,6 @@ def __init__(self, parent, font=None, color_scheme='Spyder'):
         self.fold_detector = None
         self.editor = None
 
-        # Cell separator could be there in any language
-        self.found_cell_separators = False
-        
     def get_background_color(self):
         return QColor(self.background_color)
 
@@ -467,7 +464,6 @@ def __init__(self, parent, font=None, color_scheme='Spyder'):
     def highlight_block(self, text):
         """Implement specific highlight for Python."""
         text = to_text_string(text)
-        self.found_cell_separators = False
         prev_state = tbh.get_state(self.currentBlock().previous())
         if prev_state == self.INSIDE_DQ3STRING:
             offset = -4
@@ -522,7 +518,6 @@ def highlight_block(self, text):
                         self.setFormat(start, end-start, self.formats[key])
                         if key == "comment":
                             if text.lstrip().startswith(self.cell_separators):
-                                self.found_cell_separators = True
                                 oedata = OutlineExplorerData(
                                     self.currentBlock())
                                 oedata.text = to_text_string(text).strip()
@@ -643,7 +638,6 @@ def get_import_statements(self):
         return statments
             
     def rehighlight(self):
-        self.found_cell_separators = False
         BaseSH.rehighlight(self)
 
 


Test patch: