Repository: spyder-ide/spyder

Issue:
Replace selection in wrong tab on startup
<!--- **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] Reproduced the issue after updating with ``conda update spyder`` (or ``pip``, if not using Anaconda)
* [x] Tried basic troubleshooting (if a bug/error)
    * [x] Restarted Spyder
    * [ ] Reset preferences with ``spyder --reset``
    * [x] 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
After startup, if the first thing you do is: select a text, pressing ctrl+r, and press "Replace selection", the selection is performed in the last open tab, which is not necessarily the one on top. See movie (note that it is the rightmost tab that changes as seen by the *, I can confirm that the replacement happens there):

![replacewrongtab](https://user-images.githubusercontent.com/8114497/60194241-39b17600-9839-11e9-83c6-3ef4085e3600.gif)

It is also possible to change the search/replace patterns.

Probably the solution is to make sure that the tab on top is also the active editor. However, I have no idea where that should be done in the code. 

Now it is kept on top, but (probably) not activated after all tabs are opened. As soon as something other than selection happens in the top window, it will replace correctly. (I think it happens for the other replace buttons as well, but mainly tried the "Replace selection".)

### What steps reproduce the problem?

1. Close Spyder with multiple tabs open, with the focus not on the right-most one.
2. Open Spyder
3. Mark some text in the open tab
4. Press ctrl+r to open the find-replace widget
5. Press "Replace selection"

### What is the expected output? What do you see instead?
The replacement should be done in the active window. 



## 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.0.0.dev0
* Python version: 3.7.3 64-bit
* Qt version: 5.9.6
* PyQt version: 5.9.2
* Operating System name/version: Windows 10

### Dependencies
<!--- Please go to the menu entry Help > Dependencies,
press the Copy to clipboard button and paste below --->

```
pygments >=2.0    :  2.4.2 (OK)
qdarkstyle >=2.6.4:  2.7 (OK)
sphinx >=0.6.6    :  2.1.0 (OK)
pyls >=0.27.0     :  0.27.0 (OK)
nbconvert >=4.0   :  5.5.0 (OK)
pandas >=0.13.1   :  0.24.2 (OK)
numpy >=1.7       :  1.16.4 (OK)
sympy >=0.7.3     :  1.4 (OK)
cython >=0.21     :  0.29.10 (OK)
qtconsole >=4.5.0 :  4.5.1 (OK)
IPython >=4.0     :  7.5.0 (OK)
matplotlib >=2.0.0:  3.1.0 (OK)
pylint >=0.25     :  2.3.1 (OK)

```



Discussion:

Resolving patch:
diff --git a/spyder/plugins/editor/widgets/editor.py b/spyder/plugins/editor/widgets/editor.py
--- a/spyder/plugins/editor/widgets/editor.py
+++ b/spyder/plugins/editor/widgets/editor.py
@@ -780,7 +780,7 @@ def setup_editorstack(self, parent, layout):
            and not sys.platform == 'darwin':
             # Don't set document mode to true on OSX because it generates
             # a crash when the editor is detached from the main window
-            # Fixes Issue 561
+            # Fixes spyder-ide/spyder#561
             self.tabs.setDocumentMode(True)
         self.tabs.currentChanged.connect(self.current_changed)
 
@@ -1282,7 +1282,7 @@ def rename_in_data(self, original_filename, new_filename):
         if set_new_index:
             self.set_stack_index(new_index)
         else:
-            # Fixes Issue 1287
+            # Fixes spyder-ide/spyder#1287
             self.set_current_filename(current_fname)
         if self.outlineexplorer is not None:
             self.outlineexplorer.file_renamed(
@@ -1298,7 +1298,7 @@ def set_stack_title(self, index, is_modified):
         tab_tip = self.get_tab_tip(fname, is_modified, is_readonly)
 
         # Only update tab text if have changed, otherwise an unwanted scrolling
-        # will happen when changing tabs. See Issue #1170.
+        # will happen when changing tabs. See spyder-ide/spyder#1170.
         if tab_text != self.tabs.tabText(index):
             self.tabs.setTabText(index, tab_text)
         self.tabs.setTabToolTip(index, tab_tip)
@@ -1512,7 +1512,7 @@ def close_file(self, index=None, force=False):
             # We pass self object ID as a QString, because otherwise it would
             # depend on the platform: long for 64bit, int for 32bit. Replacing
             # by long all the time is not working on some 32bit platforms
-            # (see Issue 1094, Issue 1098)
+            # (see spyder-ide/spyder#1094, spyder-ide/spyder#1098)
             self.sig_close_file.emit(str(id(self)), filename)
 
             self.opened_files_list_changed.emit()
@@ -1765,9 +1765,9 @@ def save(self, index=None, force=False):
             # We pass self object ID as a QString, because otherwise it would
             # depend on the platform: long for 64bit, int for 32bit. Replacing
             # by long all the time is not working on some 32bit platforms
-            # (see Issue 1094, Issue 1098)
+            # (see spyder-ide/spyder#1094, spyder-ide/spyder#1098)
             # The filename is passed instead of an index in case the tabs
-            # have been rearranged (see issue 5703).
+            # have been rearranged (see spyder-ide/spyder#5703).
             self.file_saved.emit(str(id(self)),
                                  finfo.filename, finfo.filename)
 
@@ -1805,7 +1805,7 @@ def file_saved_in_other_editorstack(self, original_filename, filename):
 
         The original filename is passed instead of an index in case the tabs
         on the editor stacks were moved and are now in a different order - see
-        issue 5703.
+        spyder-ide/spyder#5703.
         Filename is passed in case file was just saved as another name.
         """
         index = self.has_filename(original_filename)
@@ -1834,7 +1834,7 @@ def select_savename(self, original_filename):
 
         # Don't use filters on KDE to not make the dialog incredible
         # slow
-        # Fixes issue 4156
+        # Fixes spyder-ide/spyder#4156
         if is_kde_desktop() and not is_anaconda():
             filters = ''
             selectedfilter = ''
@@ -1880,7 +1880,7 @@ def save_as(self, index=None):
         finfo = self.data[index]
         # The next line is necessary to avoid checking if the file exists
         # While running __check_file_status
-        # See issues 3678 and 3026
+        # See spyder-ide/spyder#3678 and spyder-ide/spyder#3026
         finfo.newly_created = True
         original_filename = finfo.filename
         filename = self.select_savename(original_filename)
@@ -1899,7 +1899,7 @@ def save_as(self, index=None):
             # We pass self object ID as a QString, because otherwise it would
             # depend on the platform: long for 64bit, int for 32bit. Replacing
             # by long all the time is not working on some 32bit platforms
-            # (see Issue 1094, Issue 1098)
+            # (see spyder-ide/spyder#1094, spyder-ide/spyder#1098)
             self.file_renamed_in_data.emit(str(id(self)),
                                            original_filename, filename)
 
@@ -2020,7 +2020,7 @@ def current_changed(self, index):
 
         # Needed to avoid an error generated after moving/renaming
         # files outside Spyder while in debug mode.
-        # See issue 8749.
+        # See spyder-ide/spyder#8749.
         try:
             logger.debug("Current changed: %d - %s" %
                          (index, self.data[index].editor.filename))
@@ -2028,9 +2028,13 @@ def current_changed(self, index):
             pass
 
         self.update_plugin_title.emit()
+        # Make sure that any replace happens in the editor on top
+        # See spyder-ide/spyder#9688
+        self.find_widget.set_editor(editor, refresh=False)
+
         if editor is not None:
             # Needed in order to handle the close of files open in a directory
-            # that has been renamed. See issue 5157
+            # that has been renamed. See spyder-ide/spyder#5157
             try:
                 self.current_file_changed.emit(self.data[index].filename,
                                                editor.get_position('cursor'))
@@ -2606,7 +2610,7 @@ def dragEnterEvent(self, event):
         # can return True but source.urls() is []
         # The third check is needed since a file could be dropped from
         # compressed files. In Windows mimedata2url(source) returns None
-        # Fixes issue 5218
+        # Fixes spyder-ide/spyder#5218
         if source.hasUrls() and source.urls() and mimedata2url(source):
             all_urls = mimedata2url(source)
             text = [encoding.is_text_file(url) for url in all_urls]
@@ -2620,7 +2624,7 @@ def dragEnterEvent(self, event):
             # This covers cases like dragging from compressed files,
             # which can be opened by the Editor if they are plain
             # text, but doesn't come with url info.
-            # Fixes Issue 2032
+            # Fixes spyder-ide/spyder#2032
             event.acceptProposedAction()
         else:
             event.ignore()
@@ -2631,7 +2635,7 @@ def dropEvent(self, event):
         source = event.mimeData()
         # The second check is necessary when mimedata2url(source)
         # returns None.
-        # Fixes issue 7742
+        # Fixes spyder-ide/spyder#7742
         if source.hasUrls() and mimedata2url(source):
             files = mimedata2url(source)
             files = [f for f in files if encoding.is_text_file(f)]
@@ -2853,7 +2857,7 @@ def set_layout_settings(self, settings, dont_goto=None):
                 editor = finfo.editor
                 # TODO: go_to_line is not working properly (the line it jumps
                 # to is not the corresponding to that file). This will be fixed
-                # in a future PR (which will fix issue #3857)
+                # in a future PR (which will fix spyder-ide/spyder#3857)
                 if dont_goto is not None:
                     # skip go to line for first file because is already there
                     pass


Test patch: