Repository: spyder-ide/spyder

Issue:
Highlighting word when there is only one occurence
<!--- **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
* [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)
* [x] Could not reproduce inside ``jupyter qtconsole`` (if console-related)
* [x] Tried basic troubleshooting (if a bug/error)
    * [x] Restarted Spyder
    * [x] Reset preferences with ``spyder --reset``
    * [x] Reinstalled the latest version of [Anaconda](https://www.anaconda.com/download/)
    * [x] Tried the other applicable steps from the Troubleshooting Guide
* [x] Completed the **Problem Description**, **Steps to Reproduce** and **Version** sections below


## Problem Description
This is perhaps a bit connected to https://github.com/spyder-ide/spyder/issues/2112

I have highlight occurrence set after 100ms. That is how it seems to work in other editors like VSCode. But there the word will only be highlighted if there several occurrences. (Two or more)

 In spyder everything will be highlighted after 100ms. 




### What steps reproduce the problem?

1. Set highlight occurrence after 100ms
2. Write something
3. It will be highlighted immediately even if there is only one occurrence

### What is the expected output? What do you see instead?
It should only be highlighted if there are several occurrences.


## Versions
Latest version spyder



Discussion:
Hi @cjh9,

I could reproduce this issue, we will work in this for our next release.

Thanks for reporting!
@steff456, to solve this issue, please take a look at the `__mark_occurrences` method of `CodeEditor`. There we build the list of occurrences, which we can avoid if the length of that list is equal to one.

Resolving patch:
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
@@ -2002,13 +2002,18 @@ def __mark_occurrences(self):
         cursor = self.__find_first(text)
         self.occurrences = []
         extra_selections = self.get_extra_selections('occurrences')
-
+        first_occurrence = None
         while cursor:
             self.occurrences.append(cursor.blockNumber())
-            selection = self.get_selection(
-                cursor, background_color=self.occurrence_color)
-            if selection and selection.cursor.selectedText():
+            selection = self.get_selection(cursor)
+            if len(selection.cursor.selectedText()) > 0:
                 extra_selections.append(selection)
+                if len(extra_selections) == 1:
+                    first_occurrence = selection
+                else:
+                    selection.format.setBackground(self.occurrence_color)
+                    first_occurrence.format.setBackground(
+                        self.occurrence_color)
             cursor = self.__find_next(text, cursor)
         self.set_extra_selections('occurrences', extra_selections)
         self.update_extra_selections()


Test patch: