From e5c0804e6c6f06e0aab5868aa89654a32dbdfb33 Mon Sep 17 00:00:00 2001 From: Angela Remolina Date: Fri, 14 Apr 2023 19:26:56 -0500 Subject: [PATCH 01/16] declare methods that manager calls --- spyder/plugins/editor/plugin.py | 2 +- spyder/plugins/editor/widgets/editor.py | 4 +- spyder/plugins/switcher/container.py | 23 +------- spyder/plugins/switcher/manager.py | 4 +- spyder/plugins/switcher/plugin.py | 76 ++++++++++++++++++++++++- 5 files changed, 81 insertions(+), 28 deletions(-) diff --git a/spyder/plugins/editor/plugin.py b/spyder/plugins/editor/plugin.py index 4c72c208c88..7ec53a2dcd5 100644 --- a/spyder/plugins/editor/plugin.py +++ b/spyder/plugins/editor/plugin.py @@ -1369,7 +1369,7 @@ def register_plugin(self): self.switcher_manager = EditorSwitcherManager( self, - self.main.switcher.get_container().switcher, + self.main.switcher, self.get_current_editor, self.get_current_editorstack, section=self.get_plugin_title()) diff --git a/spyder/plugins/editor/widgets/editor.py b/spyder/plugins/editor/widgets/editor.py index 25278c56e11..b94dfda4fca 100644 --- a/spyder/plugins/editor/widgets/editor.py +++ b/spyder/plugins/editor/widgets/editor.py @@ -793,8 +793,8 @@ def open_switcher_dlg(self, initial_text=''): self.switcher_dlg.clear() return if self.switcher_dlg is None: - from spyder.plugins.switcher.widgets.switcher import Switcher - self.switcher_dlg = Switcher(self) + from spyder.api.plugins import Plugins + self.switcher_dlg = Plugins.Switcher self.switcher_manager = EditorSwitcherManager( self.get_plugin(), self.switcher_dlg, diff --git a/spyder/plugins/switcher/container.py b/spyder/plugins/switcher/container.py index 428c5440ba6..0ff1f6a89df 100644 --- a/spyder/plugins/switcher/container.py +++ b/spyder/plugins/switcher/container.py @@ -9,10 +9,8 @@ # Third-party imports from qtpy.QtCore import Qt -from qtpy.QtCore import Signal # Spyder imports -from spyder.api.config.decorators import on_conf_change from spyder.api.translations import _ from spyder.api.widgets.main_container import PluginMainContainer from spyder.plugins.switcher.widgets.switcher import Switcher @@ -20,18 +18,6 @@ class SwitcherContainer(PluginMainContainer): - # Signals - # signals needed when the EditorSwitcherManager is migrated - # Dismissed switcher - sig_rejected = Signal() - # Search/Filter text changes - sig_text_changed = Signal() - # Current item changed - sig_item_changed = Signal() - # List item selected, mode and cleaned search text - sig_item_selected = Signal() - sig_mode_selected = Signal() - # --- PluginMainContainer API # ------------------------------------------------------------------------ def setup(self): @@ -47,7 +33,7 @@ def setup(self): register_shortcut=True, context=Qt.ApplicationShortcut ) - + self.create_action( 'symbol finder', _('Symbol finder...'), @@ -58,13 +44,6 @@ def setup(self): context=Qt.ApplicationShortcut ) - # Signals - self.switcher.sig_rejected.connect(self.sig_rejected) - self.switcher.sig_text_changed.connect(self.sig_text_changed) - self.switcher.sig_item_changed.connect(self.sig_item_changed) - self.switcher.sig_item_selected.connect(self.sig_item_selected) - self.switcher.sig_mode_selected.connect(self.sig_mode_selected) - def update_actions(self): pass diff --git a/spyder/plugins/switcher/manager.py b/spyder/plugins/switcher/manager.py index 8b98fb60800..0de5e174a63 100644 --- a/spyder/plugins/switcher/manager.py +++ b/spyder/plugins/switcher/manager.py @@ -32,7 +32,7 @@ class EditorSwitcherManager(object): LINE_MODE = ':' FILES_MODE = '' - def __init__(self, plugin, switcher_instance, get_codeeditor, + def __init__(self, plugin, switcher_plugin, get_codeeditor, get_editorstack, section=_("Editor")): """ 'get_codeeditor' and 'get_editorstack' params should be callables @@ -42,7 +42,7 @@ def __init__(self, plugin, switcher_instance, get_codeeditor, current_editorstack = get_editorstack() """ self._plugin = plugin - self._switcher = switcher_instance + self._switcher = switcher_plugin self._editor = get_codeeditor self._editorstack = get_editorstack self._section = section diff --git a/spyder/plugins/switcher/plugin.py b/spyder/plugins/switcher/plugin.py index 30ac15c415d..19749502de0 100644 --- a/spyder/plugins/switcher/plugin.py +++ b/spyder/plugins/switcher/plugin.py @@ -9,15 +9,20 @@ Switcher Plugin. """ +# Third-party imports +from qtpy.QtCore import Signal + # Standard library imports import sys # Local imports from spyder.api.translations import _ from spyder.api.plugins import Plugins, SpyderPluginV2 -from spyder.api.plugin_registration.decorators import on_plugin_available, on_plugin_teardown +from spyder.api.plugin_registration.decorators import (on_plugin_available, + on_plugin_teardown) from spyder.plugins.switcher.container import SwitcherContainer from spyder.plugins.mainmenu.api import ApplicationMenus, FileMenuSections +from spyder.py3compat import TEXT_TYPES # --- Constants @@ -41,6 +46,15 @@ class Switcher(SpyderPluginV2): CONF_FILE = False # --- Signals + # Dismissed switcher + sig_rejected = Signal() + # Search/Filter text changes + sig_text_changed = Signal(TEXT_TYPES[-1]) + # Current item changed + sig_item_changed = Signal(object) + # List item selected, mode and cleaned search text + sig_item_selected = Signal(object, TEXT_TYPES[-1], TEXT_TYPES[-1]) + sig_mode_selected = Signal(TEXT_TYPES[-1]) # --- SpyderPluginV2 API # ------------------------------------------------------------------------ @@ -56,6 +70,14 @@ def get_icon(self): def on_initialize(self): container = self.get_container() + self._switcher = container.switcher + self._modes = {} + + self._switcher.sig_rejected.connect(self.sig_rejected) + self._switcher.sig_text_changed.connect(self.sig_text_changed) + self._switcher.sig_item_changed.connect(self.sig_item_changed) + self._switcher.sig_item_selected.connect(self.sig_item_selected) + self._switcher.sig_mode_selected.connect(self.sig_mode_selected) @on_plugin_available(plugin=Plugins.MainMenu) def on_main_menu_available(self): @@ -89,3 +111,55 @@ def on_main_menu_teardown(self): # --- Public API # ------------------------------------------------------------------------ + + # Switcher methods + def set_placeholder_text(self, text): + """Set the text appearing on the empty line edit.""" + self._switcher.set_placeholder_text(text) + + def setup(self): + """Set-up list widget content based on the filtering.""" + self._switcher.setup() + + # Item methods + def current_item(self): + """Return the current selected item in the list widget.""" + return self._switcher.current_item() + + def add_item(self, icon=None, title=None, description=None, shortcut=None, + section=None, data=None, tool_tip=None, action_item=False, + last_item=True): + """Add switcher list item.""" + self._switcher.add_item(icon, title, description, shortcut, + section, data, tool_tip, action_item, + last_item) + + def add_separator(self): + """Add separator item.""" + self._switcher.add_separator() + + def clear(self): + """Remove all items from the list and clear the search text.""" + self._switcher.clear() + + # Mode methods + def add_mode(self, token, description): + """Add mode by token key and description.""" + self._switcher.add_mode(token, description) + + def get_mode(self): + """Get the current mode the switcher is in.""" + self._switcher.get_mode() + + def remove_mode(self, token): + """Remove mode by token key.""" + self._switcher.remove_mode(token) + + def clear_modes(self): + """Delete all modes spreviously defined.""" + self._switcher.clear_modes() + + # Lineedit methods + def set_search_text(self, string): + """Set the content of the search text.""" + self._switcher.set_search_text(string) From a38c1d2a0543efb5149c9a795c44bbd963e6b882 Mon Sep 17 00:00:00 2001 From: Angela Remolina Date: Fri, 14 Apr 2023 22:28:45 -0500 Subject: [PATCH 02/16] Call methods from QDialog used in manager --- spyder/plugins/switcher/plugin.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spyder/plugins/switcher/plugin.py b/spyder/plugins/switcher/plugin.py index 19749502de0..e51ab29a006 100644 --- a/spyder/plugins/switcher/plugin.py +++ b/spyder/plugins/switcher/plugin.py @@ -121,6 +121,14 @@ def setup(self): """Set-up list widget content based on the filtering.""" self._switcher.setup() + def hide(self): + """Hide switcher widget with QT hide method for QDialog.""" + self._switcher.hide() + + def setVisible(self, visible): + """Show or hide switcher widget with QT hide method for QDialog.""" + self._switcher.setVisible(visible) + # Item methods def current_item(self): """Return the current selected item in the list widget.""" From 5659c41c238b90709377c6d50fe794e52845ddab Mon Sep 17 00:00:00 2001 From: Angela Remolina Date: Fri, 14 Apr 2023 22:29:37 -0500 Subject: [PATCH 03/16] Move EditorSwitcherManager back to editor utils --- spyder/plugins/editor/plugin.py | 2 +- .../{switcher/manager.py => editor/utils/switcher_manager.py} | 0 spyder/plugins/editor/widgets/editor.py | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename spyder/plugins/{switcher/manager.py => editor/utils/switcher_manager.py} (100%) diff --git a/spyder/plugins/editor/plugin.py b/spyder/plugins/editor/plugin.py index 7ec53a2dcd5..64bd459696e 100644 --- a/spyder/plugins/editor/plugin.py +++ b/spyder/plugins/editor/plugin.py @@ -52,7 +52,7 @@ SelectionContextModificator, ExtraAction) from spyder.plugins.editor.confpage import EditorConfigPage from spyder.plugins.editor.utils.autosave import AutosaveForPlugin -from spyder.plugins.switcher.manager import EditorSwitcherManager +from spyder.plugins.editor.utils.switcher_manager import EditorSwitcherManager from spyder.plugins.editor.widgets.codeeditor import CodeEditor from spyder.plugins.editor.widgets.editor import (EditorMainWindow, EditorSplitter, diff --git a/spyder/plugins/switcher/manager.py b/spyder/plugins/editor/utils/switcher_manager.py similarity index 100% rename from spyder/plugins/switcher/manager.py rename to spyder/plugins/editor/utils/switcher_manager.py diff --git a/spyder/plugins/editor/widgets/editor.py b/spyder/plugins/editor/widgets/editor.py index b94dfda4fca..23a86ba747b 100644 --- a/spyder/plugins/editor/widgets/editor.py +++ b/spyder/plugins/editor/widgets/editor.py @@ -38,7 +38,7 @@ get_filter, is_kde_desktop, is_anaconda) from spyder.plugins.editor.utils.autosave import AutosaveForStack from spyder.plugins.editor.utils.editor import get_file_language -from spyder.plugins.switcher.manager import EditorSwitcherManager +from spyder.plugins.editor.utils.switcher_manager import EditorSwitcherManager from spyder.plugins.editor.widgets import codeeditor from spyder.plugins.editor.widgets.editorstack_helpers import ( ThreadManager, FileInfo, StackHistory) From 157784bae685a1030664d3dc0b13be11a66b0048 Mon Sep 17 00:00:00 2001 From: Angela Remolina Date: Fri, 14 Apr 2023 23:42:59 -0500 Subject: [PATCH 04/16] Define more methods called in editor manager --- spyder/plugins/editor/plugin.py | 5 ----- spyder/plugins/switcher/plugin.py | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/spyder/plugins/editor/plugin.py b/spyder/plugins/editor/plugin.py index 64bd459696e..c039b932000 100644 --- a/spyder/plugins/editor/plugin.py +++ b/spyder/plugins/editor/plugin.py @@ -1362,11 +1362,6 @@ def register_plugin(self): self.add_dockwidget() - # Add modes to switcher - # TODO: 'Switcher' object has no attribute 'add_mode' - # it is needed to create a public API that contains the methods - # that handles the EditorSwitcherManager - self.switcher_manager = EditorSwitcherManager( self, self.main.switcher, diff --git a/spyder/plugins/switcher/plugin.py b/spyder/plugins/switcher/plugin.py index e51ab29a006..9a8a9d0abf0 100644 --- a/spyder/plugins/switcher/plugin.py +++ b/spyder/plugins/switcher/plugin.py @@ -121,6 +121,15 @@ def setup(self): """Set-up list widget content based on the filtering.""" self._switcher.setup() + def set_position(self, top): + """Set the position of the dialog.""" + self._switcher.set_position(top) + + # QDialog methods + def show(self): + """Show switcher widget with QT hide method for QDialog.""" + self._switcher.show() + def hide(self): """Hide switcher widget with QT hide method for QDialog.""" self._switcher.hide() @@ -129,6 +138,11 @@ def setVisible(self, visible): """Show or hide switcher widget with QT hide method for QDialog.""" self._switcher.setVisible(visible) + def isVisible(self): + """Return if switcher widget is visible with QT hide method + for QDialog.""" + return self._switcher.isVisible() + # Item methods def current_item(self): """Return the current selected item in the list widget.""" From a468438a60c246c0e211eb35362cb885e76ff6f8 Mon Sep 17 00:00:00 2001 From: Angela Remolina Date: Fri, 14 Apr 2023 23:44:51 -0500 Subject: [PATCH 05/16] Call plugin instance in editor widget --- spyder/plugins/editor/widgets/editor.py | 38 ++++++++++--------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/spyder/plugins/editor/widgets/editor.py b/spyder/plugins/editor/widgets/editor.py index 23a86ba747b..e935fb3b6e3 100644 --- a/spyder/plugins/editor/widgets/editor.py +++ b/spyder/plugins/editor/widgets/editor.py @@ -287,7 +287,7 @@ def __init__(self, parent, actions): self.setLayout(layout) self.menu = None - self.switcher_dlg = None + self.switcher_plugin = self.get_plugin().main.switcher self.switcher_manager = None self.tabs = None self.tabs_switcher = None @@ -303,16 +303,9 @@ def __init__(self, parent, actions): self.data = [] - switcher_action = create_action( - self, - _("File switcher..."), - icon=ima.icon('filelist'), - triggered=self.open_switcher_dlg) - symbolfinder_action = create_action( - self, - _("Find symbols in file..."), - icon=ima.icon('symbol_find'), - triggered=self.open_symbolfinder_dlg) + switcher_action = self.switcher_plugin.get_action("file switcher") + symbolfinder_action = self.switcher_plugin.get_action("symbol finder") + copy_to_cb_action = create_action(self, _("Copy path to clipboard"), icon=ima.icon('editcopy'), triggered=lambda: @@ -788,30 +781,29 @@ def open_switcher_dlg(self, initial_text=''): """Open file list management dialog box""" if not self.tabs.count(): return - if self.switcher_dlg is not None and self.switcher_dlg.isVisible(): - self.switcher_dlg.hide() - self.switcher_dlg.clear() - return - if self.switcher_dlg is None: - from spyder.api.plugins import Plugins - self.switcher_dlg = Plugins.Switcher + if self.switcher_plugin is not None: self.switcher_manager = EditorSwitcherManager( self.get_plugin(), - self.switcher_dlg, + self.switcher_plugin, self.get_current_editor, lambda: self, section=self.get_plugin_title()) + if self.switcher_plugin.isVisible(): + self.switcher_plugin.hide() + self.switcher_plugin.clear() + return + if isinstance(initial_text, bool): initial_text = '' - self.switcher_dlg.set_search_text(initial_text) - self.switcher_dlg.setup() - self.switcher_dlg.show() + self.switcher_plugin.set_search_text(initial_text) + self.switcher_plugin.setup() + self.switcher_plugin.show() # Note: the +1 pixel on the top makes it look better delta_top = (self.tabs.tabBar().geometry().height() + self.fname_label.geometry().height() + 1) - self.switcher_dlg.set_position(delta_top) + self.switcher_plugin.set_position(delta_top) @Slot() def open_symbolfinder_dlg(self): From 019bbcf72a644531374dbd29b8891f12306b417d Mon Sep 17 00:00:00 2001 From: Angela Remolina Date: Sat, 15 Apr 2023 18:16:45 -0500 Subject: [PATCH 06/16] Fix position of switcher in new window --- spyder/plugins/editor/widgets/editor.py | 35 --------------------- spyder/plugins/switcher/container.py | 28 +++++++++++------ spyder/plugins/switcher/plugin.py | 4 +-- spyder/plugins/switcher/widgets/switcher.py | 6 ++-- 4 files changed, 24 insertions(+), 49 deletions(-) diff --git a/spyder/plugins/editor/widgets/editor.py b/spyder/plugins/editor/widgets/editor.py index e935fb3b6e3..b45d086ef4a 100644 --- a/spyder/plugins/editor/widgets/editor.py +++ b/spyder/plugins/editor/widgets/editor.py @@ -38,7 +38,6 @@ get_filter, is_kde_desktop, is_anaconda) from spyder.plugins.editor.utils.autosave import AutosaveForStack from spyder.plugins.editor.utils.editor import get_file_language -from spyder.plugins.editor.utils.switcher_manager import EditorSwitcherManager from spyder.plugins.editor.widgets import codeeditor from spyder.plugins.editor.widgets.editorstack_helpers import ( ThreadManager, FileInfo, StackHistory) @@ -775,40 +774,6 @@ def clone_from(self, other): self.clone_editor_from(other_finfo, set_current=True) self.set_stack_index(other.get_stack_index()) - @Slot() - @Slot(str) - def open_switcher_dlg(self, initial_text=''): - """Open file list management dialog box""" - if not self.tabs.count(): - return - if self.switcher_plugin is not None: - self.switcher_manager = EditorSwitcherManager( - self.get_plugin(), - self.switcher_plugin, - self.get_current_editor, - lambda: self, - section=self.get_plugin_title()) - - if self.switcher_plugin.isVisible(): - self.switcher_plugin.hide() - self.switcher_plugin.clear() - return - - if isinstance(initial_text, bool): - initial_text = '' - - self.switcher_plugin.set_search_text(initial_text) - self.switcher_plugin.setup() - self.switcher_plugin.show() - # Note: the +1 pixel on the top makes it look better - delta_top = (self.tabs.tabBar().geometry().height() + - self.fname_label.geometry().height() + 1) - self.switcher_plugin.set_position(delta_top) - - @Slot() - def open_symbolfinder_dlg(self): - self.open_switcher_dlg(initial_text='@') - def get_plugin(self): """Get the plugin of the parent widget.""" # Needed for the editor stack to use its own switcher instance. diff --git a/spyder/plugins/switcher/container.py b/spyder/plugins/switcher/container.py index 0ff1f6a89df..bf1bccac0c2 100644 --- a/spyder/plugins/switcher/container.py +++ b/spyder/plugins/switcher/container.py @@ -9,6 +9,7 @@ # Third-party imports from qtpy.QtCore import Qt +from qtpy.QtWidgets import QApplication # Spyder imports from spyder.api.translations import _ @@ -63,18 +64,25 @@ def open_switcher(self, symbol=False): switcher.set_search_text('') switcher.setup() - switcher.show() - - # Note: The +8 pixel on the top makes it look better - # FIXME: Why is this using the toolbars menu? A: To not be on top of - # the toolbars. - # Probably toolbars should be taken into account for this 'delta' only - # when are visible + # Set position mainwindow = self._plugin.get_main() - delta_top = (mainwindow.toolbar.toolbars_menu.geometry().height() + - mainwindow.menuBar().geometry().height() + 8) + # Note: The +8 pixel on the top makes it look better + default_top = (mainwindow.toolbar.toolbars_menu.geometry().height() + + mainwindow.menuBar().geometry().height() + 8) - switcher.set_position(delta_top) + current_window = QApplication.activeWindow() + if current_window == mainwindow: + option = 'toolbars_visible' + section = 'toolbar' + if self.get_conf(option, section): + delta_top = default_top + else: + delta_top = mainwindow.menuBar().geometry().height() + 8 + else: + delta_top = default_top + + switcher.set_position(delta_top, current_window) + switcher.show() def open_symbolfinder(self): """Open symbol list management dialog box.""" diff --git a/spyder/plugins/switcher/plugin.py b/spyder/plugins/switcher/plugin.py index 9a8a9d0abf0..d42dda5a2f3 100644 --- a/spyder/plugins/switcher/plugin.py +++ b/spyder/plugins/switcher/plugin.py @@ -121,9 +121,9 @@ def setup(self): """Set-up list widget content based on the filtering.""" self._switcher.setup() - def set_position(self, top): + def set_position(self, top, parent=None): """Set the position of the dialog.""" - self._switcher.set_position(top) + self._switcher.set_position(top, parent) # QDialog methods def show(self): diff --git a/spyder/plugins/switcher/widgets/switcher.py b/spyder/plugins/switcher/widgets/switcher.py index 64907b8217c..801cb7cf27e 100644 --- a/spyder/plugins/switcher/widgets/switcher.py +++ b/spyder/plugins/switcher/widgets/switcher.py @@ -333,9 +333,11 @@ def set_height(self): switcher_height = self._MIN_HEIGHT self.setFixedHeight(int(switcher_height)) - def set_position(self, top): + def set_position(self, top, parent=None): """Set the position of the dialog.""" - parent = self.parent() + if parent is None: + parent = self.parent() + if parent is not None: geo = parent.geometry() width = self.list.width() # This has been set in setup From 3f5dfc4b18291d8b3c49fd43d46a014d7892cbd9 Mon Sep 17 00:00:00 2001 From: Angela Remolina Date: Mon, 17 Apr 2023 13:04:20 -0500 Subject: [PATCH 07/16] Add signals docstrings --- spyder/plugins/switcher/plugin.py | 43 +++++++++++++++++-- spyder/plugins/switcher/widgets/switcher.py | 46 ++++++++++++++++++--- 2 files changed, 80 insertions(+), 9 deletions(-) diff --git a/spyder/plugins/switcher/plugin.py b/spyder/plugins/switcher/plugin.py index d42dda5a2f3..f7de822298f 100644 --- a/spyder/plugins/switcher/plugin.py +++ b/spyder/plugins/switcher/plugin.py @@ -46,15 +46,50 @@ class Switcher(SpyderPluginV2): CONF_FILE = False # --- Signals - # Dismissed switcher sig_rejected = Signal() - # Search/Filter text changes + """ + This signal is emitted when the plugin is dismissed. + """ + sig_text_changed = Signal(TEXT_TYPES[-1]) - # Current item changed + """ + This signal is emitted when the plugin search/filter text changes. + + Parameters + ---------- + search_text: str + The current search/filter text. + """ + sig_item_changed = Signal(object) - # List item selected, mode and cleaned search text + """ + This signal is emitted when the plugin current item changes. + """ + sig_item_selected = Signal(object, TEXT_TYPES[-1], TEXT_TYPES[-1]) + """ + This signal is emitted when an item is selected from the switcher plugin + list. + + Parameters + ---------- + item: object + The current selected item from the switcher list (QStandardItem). + mode: str + The current selected mode (open files "", symbol "@" or line ":"). + search_text: str + Cleaned search/filter text. + """ + sig_mode_selected = Signal(TEXT_TYPES[-1]) + """ + This signal is emitted when a mode is selected. + + Parameters + ---------- + mode: str + The selected mode (open files "", symbol "@" or line ":"). + """ # --- SpyderPluginV2 API # ------------------------------------------------------------------------ diff --git a/spyder/plugins/switcher/widgets/switcher.py b/spyder/plugins/switcher/widgets/switcher.py index 801cb7cf27e..f133bb4f75c 100644 --- a/spyder/plugins/switcher/widgets/switcher.py +++ b/spyder/plugins/switcher/widgets/switcher.py @@ -93,15 +93,51 @@ class Switcher(QDialog): SwitcherItem: [title description section] """ - # Dismissed switcher + # --- Signals sig_rejected = Signal() - # Search/Filter text changes + """ + This signal is emitted when the plugin is dismissed. + """ + sig_text_changed = Signal(TEXT_TYPES[-1]) - # Current item changed + """ + This signal is emitted when the plugin search/filter text changes. + + Parameters + ---------- + search_text: str + The current search/filter text. + """ + sig_item_changed = Signal(object) - # List item selected, mode and cleaned search text - sig_item_selected = Signal(object, TEXT_TYPES[-1], TEXT_TYPES[-1], ) + """ + This signal is emitted when the plugin current item changes. + """ + + sig_item_selected = Signal(object, TEXT_TYPES[-1], TEXT_TYPES[-1]) + """ + This signal is emitted when an item is selected from the switcher plugin + list. + + Parameters + ---------- + item: object + The current selected item from the switcher list (QStandardItem). + mode: str + The current selected mode (open files "", symbol "@" or line ":"). + search_text: str + Cleaned search/filter text. + """ + sig_mode_selected = Signal(TEXT_TYPES[-1]) + """ + This signal is emitted when a mode is selected. + + Parameters + ---------- + mode: str + The selected mode (open files "", symbol "@" or line ":"). + """ _MAX_NUM_ITEMS = 15 _MIN_WIDTH = 580 From 015f60f5ef1363e616a60d2fa650d34cb2efb561 Mon Sep 17 00:00:00 2001 From: Angela Remolina Date: Tue, 18 Apr 2023 18:38:40 -0500 Subject: [PATCH 08/16] add use_switcher flag for testing --- spyder/plugins/editor/widgets/editor.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/spyder/plugins/editor/widgets/editor.py b/spyder/plugins/editor/widgets/editor.py index b45d086ef4a..d2ba19ca9e1 100644 --- a/spyder/plugins/editor/widgets/editor.py +++ b/spyder/plugins/editor/widgets/editor.py @@ -269,7 +269,7 @@ class EditorStack(QWidget): :py:meth:spyder.plugins.editor.widgets.editor.EditorStack.send_to_help """ - def __init__(self, parent, actions): + def __init__(self, parent, actions, use_switcher=True): QWidget.__init__(self, parent) self.setAttribute(Qt.WA_DeleteOnClose) @@ -286,10 +286,17 @@ def __init__(self, parent, actions): self.setLayout(layout) self.menu = None - self.switcher_plugin = self.get_plugin().main.switcher self.switcher_manager = None self.tabs = None self.tabs_switcher = None + self.switcher_plugin = None + + switcher_action = None + symbolfinder_action = None + if use_switcher: + self.switcher_plugin = self.get_plugin().main.switcher + switcher_action = self.switcher_plugin.get_action("file switcher") + symbolfinder_action = self.switcher_plugin.get_action("symbol finder") self.stack_history = StackHistory(self) @@ -302,9 +309,6 @@ def __init__(self, parent, actions): self.data = [] - switcher_action = self.switcher_plugin.get_action("file switcher") - symbolfinder_action = self.switcher_plugin.get_action("symbol finder") - copy_to_cb_action = create_action(self, _("Copy path to clipboard"), icon=ima.icon('editcopy'), triggered=lambda: @@ -2970,7 +2974,8 @@ class EditorSplitter(QSplitter): """QSplitter for editor windows.""" def __init__(self, parent, plugin, menu_actions, first=False, - register_editorstack_cb=None, unregister_editorstack_cb=None): + register_editorstack_cb=None, unregister_editorstack_cb=None, + use_switcher=True): """Create a splitter for dividing an editor window into panels. Adds a new EditorStack instance to this splitter. If it's not @@ -3006,7 +3011,7 @@ def __init__(self, parent, plugin, menu_actions, first=False, self.unregister_editorstack_cb = unregister_editorstack_cb self.menu_actions = menu_actions - self.editorstack = EditorStack(self, menu_actions) + self.editorstack = EditorStack(self, menu_actions, use_switcher) self.register_editorstack_cb(self.editorstack) if not first: self.plugin.clone_editorstack(editorstack=self.editorstack) @@ -3524,13 +3529,14 @@ def __init__(self): self.editorstacks = [] self.editorwindows = [] - self.last_focused_editorstack = {} # fake + self.last_focused_editorstack = {} # fake self.find_widget = FindReplace(self, enable_replace=True) self.outlineexplorer = OutlineExplorerWidget(None, self, self) self.outlineexplorer.edit_goto.connect(self.go_to_file) self.editor_splitter = EditorSplitter(self, self, menu_actions, - first=True) + first=True, + use_switcher=False) editor_widgets = QWidget(self) editor_layout = QVBoxLayout() From 3e25c3f7eede816f8c48311a72c6dd5d62efbdce Mon Sep 17 00:00:00 2001 From: Angela Remolina Date: Tue, 18 Apr 2023 18:40:47 -0500 Subject: [PATCH 09/16] change switcher_dlg attribute name to switcher_plugin --- spyder/app/tests/conftest.py | 4 ++-- spyder/app/tests/test_mainwindow.py | 22 +++++++++---------- .../widgets/tests/test_editorsplitter.py | 2 +- spyder/plugins/switcher/plugin.py | 8 +++++++ 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/spyder/app/tests/conftest.py b/spyder/app/tests/conftest.py index 580a975cff2..09aa9b37d4d 100755 --- a/spyder/app/tests/conftest.py +++ b/spyder/app/tests/conftest.py @@ -477,8 +477,8 @@ def main_window(request, tmpdir, qtbot): for editorwindow in window.editor.editorwindows: editorwindow.close() editorstack = window.editor.get_current_editorstack() - if editorstack.switcher_dlg: - editorstack.switcher_dlg.close() + if editorstack.switcher_plugin: + editorstack.switcher_plugin.close() window.projects.close_project() diff --git a/spyder/app/tests/test_mainwindow.py b/spyder/app/tests/test_mainwindow.py index 5cb3faba5f9..7c44bb86374 100644 --- a/spyder/app/tests/test_mainwindow.py +++ b/spyder/app/tests/test_mainwindow.py @@ -2457,7 +2457,7 @@ def example_def_2(): @flaky(max_runs=3) -def test_edidorstack_open_switcher_dlg(main_window, tmpdir, qtbot): +def test_editorstack_open_switcher_dlg(main_window, tmpdir, qtbot): """ Test that the file switcher is working as expected when called from the editorstack. @@ -2472,16 +2472,15 @@ def test_edidorstack_open_switcher_dlg(main_window, tmpdir, qtbot): # Add a file to the editor. file = tmpdir.join('test_file_open_switcher_dlg.py') - file.write("a test file for test_edidorstack_open_switcher_dlg") + file.write("a test file for test_editorstack_open_switcher_dlg") main_window.editor.load(str(file)) # Test that the file switcher opens as expected from the editorstack. editorstack = main_window.editor.get_current_editorstack() - assert editorstack.switcher_dlg is None - editorstack.open_switcher_dlg() - assert editorstack.switcher_dlg - assert editorstack.switcher_dlg.isVisible() - assert (editorstack.switcher_dlg.count() == + editorstack.switcher_plugin.get_container().open_switcher() + assert editorstack.switcher_plugin + assert editorstack.switcher_plugin.isVisible() + assert (editorstack.switcher_plugin.count() == len(main_window.editor.get_filenames())) @@ -2527,11 +2526,10 @@ def example_def_2(): # Test that the symbol finder opens as expected from the editorstack. editorstack = main_window.editor.get_current_editorstack() - assert editorstack.switcher_dlg is None - editorstack.open_symbolfinder_dlg() - assert editorstack.switcher_dlg - assert editorstack.switcher_dlg.isVisible() - assert editorstack.switcher_dlg.count() == 2 + editorstack.switcher_plugin.get_container().open_switcher() + assert editorstack.switcher_plugin + assert editorstack.switcher_plugin.isVisible() + assert editorstack.switcher_plugin.count() == 2 @flaky(max_runs=3) diff --git a/spyder/plugins/editor/widgets/tests/test_editorsplitter.py b/spyder/plugins/editor/widgets/tests/test_editorsplitter.py index 0c876f5d8ec..bc06634ac8b 100644 --- a/spyder/plugins/editor/widgets/tests/test_editorsplitter.py +++ b/spyder/plugins/editor/widgets/tests/test_editorsplitter.py @@ -27,7 +27,7 @@ # ---- Qt Test Fixtures def editor_stack(): - editor_stack = EditorStack(None, []) + editor_stack = EditorStack(None, [], False) editor_stack.set_find_widget(Mock()) editor_stack.set_io_actions(Mock(), Mock(), Mock(), Mock()) return editor_stack diff --git a/spyder/plugins/switcher/plugin.py b/spyder/plugins/switcher/plugin.py index f7de822298f..18d1d9f5bce 100644 --- a/spyder/plugins/switcher/plugin.py +++ b/spyder/plugins/switcher/plugin.py @@ -169,6 +169,10 @@ def hide(self): """Hide switcher widget with QT hide method for QDialog.""" self._switcher.hide() + def close(self): + """Close switcher widget with QT close method for QDialog.""" + self._switcher.close() + def setVisible(self, visible): """Show or hide switcher widget with QT hide method for QDialog.""" self._switcher.setVisible(visible) @@ -199,6 +203,10 @@ def clear(self): """Remove all items from the list and clear the search text.""" self._switcher.clear() + def count(self): + """Get the item count in the list widget.""" + return self._switcher.count() + # Mode methods def add_mode(self, token, description): """Add mode by token key and description.""" From 1814716038f188a924cfd5ef7ac076dd71d3672e Mon Sep 17 00:00:00 2001 From: Angela Remolina Date: Tue, 18 Apr 2023 21:20:46 -0500 Subject: [PATCH 10/16] set switcher flag to false in all tests --- spyder/plugins/editor/widgets/tests/conftest.py | 2 +- spyder/plugins/editor/widgets/tests/test_editor.py | 2 +- .../plugins/editor/widgets/tests/test_editor_and_outline.py | 4 ++-- spyder/plugins/editor/widgets/tests/test_save.py | 2 +- spyder/plugins/editor/widgets/tests/test_shortcuts.py | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/spyder/plugins/editor/widgets/tests/conftest.py b/spyder/plugins/editor/widgets/tests/conftest.py index d030067df56..ba9f94b2a9f 100644 --- a/spyder/plugins/editor/widgets/tests/conftest.py +++ b/spyder/plugins/editor/widgets/tests/conftest.py @@ -50,7 +50,7 @@ def codeeditor_factory(): def editor_factory(new_file=True, text=None): - editorstack = EditorStack(None, []) + editorstack = EditorStack(None, [], False) editorstack.set_find_widget(FindReplace(editorstack)) editorstack.set_io_actions(Mock(), Mock(), Mock(), Mock()) if new_file: diff --git a/spyder/plugins/editor/widgets/tests/test_editor.py b/spyder/plugins/editor/widgets/tests/test_editor.py index f7c7c3e4a7d..f47ffef19e1 100644 --- a/spyder/plugins/editor/widgets/tests/test_editor.py +++ b/spyder/plugins/editor/widgets/tests/test_editor.py @@ -36,7 +36,7 @@ # ============================================================================= @pytest.fixture def base_editor_bot(qtbot): - editor_stack = EditorStack(None, []) + editor_stack = EditorStack(None, [], False) editor_stack.set_find_widget(Mock()) editor_stack.set_io_actions(Mock(), Mock(), Mock(), Mock()) return editor_stack diff --git a/spyder/plugins/editor/widgets/tests/test_editor_and_outline.py b/spyder/plugins/editor/widgets/tests/test_editor_and_outline.py index 1fb4d9f1e34..04afbf2c184 100644 --- a/spyder/plugins/editor/widgets/tests/test_editor_and_outline.py +++ b/spyder/plugins/editor/widgets/tests/test_editor_and_outline.py @@ -55,7 +55,7 @@ def get_tree_elements(treewidget): this_tree = {node.name: []} parent_tree.append(this_tree) this_stack = [(this_tree[node.name], child) - for child in node.children] + for child in node.children] stack = this_stack + stack return root_tree @@ -119,7 +119,7 @@ def completions_codeeditor_outline(completions_codeeditor, outlineexplorer): @pytest.fixture def editorstack(qtbot, outlineexplorer): def _create_editorstack(files): - editorstack = editor.EditorStack(None, []) + editorstack = editor.EditorStack(None, [], False) editorstack.set_find_widget(Mock()) editorstack.set_io_actions(Mock(), Mock(), Mock(), Mock()) editorstack.analysis_timer = Mock() diff --git a/spyder/plugins/editor/widgets/tests/test_save.py b/spyder/plugins/editor/widgets/tests/test_save.py index bdcf205c2c4..15d521efb2e 100644 --- a/spyder/plugins/editor/widgets/tests/test_save.py +++ b/spyder/plugins/editor/widgets/tests/test_save.py @@ -47,7 +47,7 @@ def add_files(editorstack): # ---- Qt Test Fixtures @pytest.fixture def base_editor_bot(qtbot): - editor_stack = editor.EditorStack(None, []) + editor_stack = editor.EditorStack(None, [], False) editor_stack.set_find_widget(Mock()) editor_stack.set_io_actions(Mock(), Mock(), Mock(), Mock()) return editor_stack, qtbot diff --git a/spyder/plugins/editor/widgets/tests/test_shortcuts.py b/spyder/plugins/editor/widgets/tests/test_shortcuts.py index cb63eee2d61..1d1c2062d0f 100644 --- a/spyder/plugins/editor/widgets/tests/test_shortcuts.py +++ b/spyder/plugins/editor/widgets/tests/test_shortcuts.py @@ -32,7 +32,7 @@ def editor_bot(qtbot): Set up EditorStack with CodeEditors containing some Python code. The cursor is at the empty line below the code. """ - editorstack = EditorStack(None, []) + editorstack = EditorStack(None, [], False) editorstack.set_find_widget(Mock()) editorstack.set_io_actions(Mock(), Mock(), Mock(), Mock()) editorstack.close_action.setEnabled(False) From ae9bf6dcda5145243960e04563516e934ddb1e22 Mon Sep 17 00:00:00 2001 From: Angela Remolina Date: Tue, 18 Apr 2023 23:50:36 -0500 Subject: [PATCH 11/16] mock switcher in MainWindowMock --- spyder/plugins/editor/tests/test_editor_config_dialog.py | 1 + 1 file changed, 1 insertion(+) diff --git a/spyder/plugins/editor/tests/test_editor_config_dialog.py b/spyder/plugins/editor/tests/test_editor_config_dialog.py index 7de7d0d08e5..a9af216c538 100644 --- a/spyder/plugins/editor/tests/test_editor_config_dialog.py +++ b/spyder/plugins/editor/tests/test_editor_config_dialog.py @@ -28,6 +28,7 @@ class MainWindowMock(QMainWindow): ipyconsole = Mock() mainmenu = Mock() sig_setup_finished = Mock() + switcher = Mock() @pytest.mark.parametrize( From 37a95c1d06c314624f929aa249df393ede09f9aa Mon Sep 17 00:00:00 2001 From: Angela Remolina Date: Thu, 20 Apr 2023 22:29:55 -0500 Subject: [PATCH 12/16] Apply suggestions from code review Co-authored-by: Carlos Cordoba --- spyder/plugins/switcher/container.py | 5 ++-- spyder/plugins/switcher/plugin.py | 26 ++++++++++----------- spyder/plugins/switcher/widgets/switcher.py | 6 ++--- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/spyder/plugins/switcher/container.py b/spyder/plugins/switcher/container.py index bf1bccac0c2..24f055a7219 100644 --- a/spyder/plugins/switcher/container.py +++ b/spyder/plugins/switcher/container.py @@ -66,15 +66,14 @@ def open_switcher(self, symbol=False): # Set position mainwindow = self._plugin.get_main() + # Note: The +8 pixel on the top makes it look better default_top = (mainwindow.toolbar.toolbars_menu.geometry().height() + mainwindow.menuBar().geometry().height() + 8) current_window = QApplication.activeWindow() if current_window == mainwindow: - option = 'toolbars_visible' - section = 'toolbar' - if self.get_conf(option, section): + if self.get_conf('toolbars_visible', section='toolbar'): delta_top = default_top else: delta_top = mainwindow.menuBar().geometry().height() + 8 diff --git a/spyder/plugins/switcher/plugin.py b/spyder/plugins/switcher/plugin.py index 18d1d9f5bce..58441546711 100644 --- a/spyder/plugins/switcher/plugin.py +++ b/spyder/plugins/switcher/plugin.py @@ -9,12 +9,12 @@ Switcher Plugin. """ -# Third-party imports -from qtpy.QtCore import Signal - # Standard library imports import sys +# Third-party imports +from qtpy.QtCore import Signal + # Local imports from spyder.api.translations import _ from spyder.api.plugins import Plugins, SpyderPluginV2 @@ -22,7 +22,6 @@ on_plugin_teardown) from spyder.plugins.switcher.container import SwitcherContainer from spyder.plugins.mainmenu.api import ApplicationMenus, FileMenuSections -from spyder.py3compat import TEXT_TYPES # --- Constants @@ -51,7 +50,7 @@ class Switcher(SpyderPluginV2): This signal is emitted when the plugin is dismissed. """ - sig_text_changed = Signal(TEXT_TYPES[-1]) + sig_text_changed = Signal(str) """ This signal is emitted when the plugin search/filter text changes. @@ -66,7 +65,7 @@ class Switcher(SpyderPluginV2): This signal is emitted when the plugin current item changes. """ - sig_item_selected = Signal(object, TEXT_TYPES[-1], TEXT_TYPES[-1]) + sig_item_selected = Signal(object, str, str) """ This signal is emitted when an item is selected from the switcher plugin list. @@ -81,7 +80,7 @@ class Switcher(SpyderPluginV2): Cleaned search/filter text. """ - sig_mode_selected = Signal(TEXT_TYPES[-1]) + sig_mode_selected = Signal(str) """ This signal is emitted when a mode is selected. @@ -162,11 +161,11 @@ def set_position(self, top, parent=None): # QDialog methods def show(self): - """Show switcher widget with QT hide method for QDialog.""" + """Show switcher.""" self._switcher.show() def hide(self): - """Hide switcher widget with QT hide method for QDialog.""" + """Hide switcher.""" self._switcher.hide() def close(self): @@ -177,9 +176,8 @@ def setVisible(self, visible): """Show or hide switcher widget with QT hide method for QDialog.""" self._switcher.setVisible(visible) - def isVisible(self): - """Return if switcher widget is visible with QT hide method - for QDialog.""" + def is_visible(self): + """Return if the switcher is visible.""" return self._switcher.isVisible() # Item methods @@ -190,13 +188,13 @@ def current_item(self): def add_item(self, icon=None, title=None, description=None, shortcut=None, section=None, data=None, tool_tip=None, action_item=False, last_item=True): - """Add switcher list item.""" + """Add a switcher list item.""" self._switcher.add_item(icon, title, description, shortcut, section, data, tool_tip, action_item, last_item) def add_separator(self): - """Add separator item.""" + """Add a separator item.""" self._switcher.add_separator() def clear(self): diff --git a/spyder/plugins/switcher/widgets/switcher.py b/spyder/plugins/switcher/widgets/switcher.py index f133bb4f75c..17711acb037 100644 --- a/spyder/plugins/switcher/widgets/switcher.py +++ b/spyder/plugins/switcher/widgets/switcher.py @@ -99,7 +99,7 @@ class Switcher(QDialog): This signal is emitted when the plugin is dismissed. """ - sig_text_changed = Signal(TEXT_TYPES[-1]) + sig_text_changed = Signal(str) """ This signal is emitted when the plugin search/filter text changes. @@ -114,7 +114,7 @@ class Switcher(QDialog): This signal is emitted when the plugin current item changes. """ - sig_item_selected = Signal(object, TEXT_TYPES[-1], TEXT_TYPES[-1]) + sig_item_selected = Signal(object, str, str) """ This signal is emitted when an item is selected from the switcher plugin list. @@ -129,7 +129,7 @@ class Switcher(QDialog): Cleaned search/filter text. """ - sig_mode_selected = Signal(TEXT_TYPES[-1]) + sig_mode_selected = Signal(str) """ This signal is emitted when a mode is selected. From f668e3b6d3c699e7dae96ae0dcb4135d88bd7fa2 Mon Sep 17 00:00:00 2001 From: Angela Remolina Date: Thu, 20 Apr 2023 22:53:51 -0500 Subject: [PATCH 13/16] Apply suggestions from code review Co-authored-by: Carlos Cordoba --- spyder/plugins/switcher/plugin.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/spyder/plugins/switcher/plugin.py b/spyder/plugins/switcher/plugin.py index 58441546711..34c7bf43a56 100644 --- a/spyder/plugins/switcher/plugin.py +++ b/spyder/plugins/switcher/plugin.py @@ -155,10 +155,6 @@ def setup(self): """Set-up list widget content based on the filtering.""" self._switcher.setup() - def set_position(self, top, parent=None): - """Set the position of the dialog.""" - self._switcher.set_position(top, parent) - # QDialog methods def show(self): """Show switcher.""" @@ -172,8 +168,8 @@ def close(self): """Close switcher widget with QT close method for QDialog.""" self._switcher.close() - def setVisible(self, visible): - """Show or hide switcher widget with QT hide method for QDialog.""" + def set_visible(self, visible): + """Show or hide switcher.""" self._switcher.setVisible(visible) def is_visible(self): From 8044055f03d71ce5c7ebe3f6c3dc34af57902ac3 Mon Sep 17 00:00:00 2001 From: Angela Remolina Date: Thu, 20 Apr 2023 22:56:03 -0500 Subject: [PATCH 14/16] Add code suggestions --- spyder/plugins/editor/utils/switcher_manager.py | 2 +- spyder/plugins/switcher/plugin.py | 2 +- spyder/plugins/switcher/widgets/switcher.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/spyder/plugins/editor/utils/switcher_manager.py b/spyder/plugins/editor/utils/switcher_manager.py index 0de5e174a63..a2b93b685ac 100644 --- a/spyder/plugins/editor/utils/switcher_manager.py +++ b/spyder/plugins/editor/utils/switcher_manager.py @@ -231,7 +231,7 @@ def line_switcher_handler(self, data, search_text, visible=False): try: line_number = int(line_number) editorstack.go_to_line(line_number) - self._switcher.setVisible(visible) + self._switcher.set_visible(visible) # Closing the switcher if not visible: self._current_line = None diff --git a/spyder/plugins/switcher/plugin.py b/spyder/plugins/switcher/plugin.py index 34c7bf43a56..eb465cee9d9 100644 --- a/spyder/plugins/switcher/plugin.py +++ b/spyder/plugins/switcher/plugin.py @@ -165,7 +165,7 @@ def hide(self): self._switcher.hide() def close(self): - """Close switcher widget with QT close method for QDialog.""" + """Close switcher widget.""" self._switcher.close() def set_visible(self, visible): diff --git a/spyder/plugins/switcher/widgets/switcher.py b/spyder/plugins/switcher/widgets/switcher.py index 17711acb037..a42b8f0a2b5 100644 --- a/spyder/plugins/switcher/widgets/switcher.py +++ b/spyder/plugins/switcher/widgets/switcher.py @@ -18,7 +18,7 @@ from spyder.plugins.switcher.widgets.proxymodel import SwitcherProxyModel from spyder.plugins.switcher.widgets.item import ( SwitcherItem, SwitcherSeparatorItem) -from spyder.py3compat import TEXT_TYPES, to_text_string +from spyder.py3compat import to_text_string from spyder.utils.icon_manager import ima from spyder.widgets.helperwidgets import HTMLDelegate from spyder.utils.stringmatching import get_search_scores From 095ae77a654ad485026710af647c923f9c8bfe71 Mon Sep 17 00:00:00 2001 From: Angela Remolina Date: Fri, 21 Apr 2023 10:25:28 -0500 Subject: [PATCH 15/16] Update switcher tests involving isVisible method --- spyder/app/tests/test_mainwindow.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spyder/app/tests/test_mainwindow.py b/spyder/app/tests/test_mainwindow.py index 7c44bb86374..f5164928ade 100644 --- a/spyder/app/tests/test_mainwindow.py +++ b/spyder/app/tests/test_mainwindow.py @@ -2479,7 +2479,7 @@ def test_editorstack_open_switcher_dlg(main_window, tmpdir, qtbot): editorstack = main_window.editor.get_current_editorstack() editorstack.switcher_plugin.get_container().open_switcher() assert editorstack.switcher_plugin - assert editorstack.switcher_plugin.isVisible() + assert editorstack.switcher_plugin.is_visible() assert (editorstack.switcher_plugin.count() == len(main_window.editor.get_filenames())) @@ -2528,7 +2528,7 @@ def example_def_2(): editorstack = main_window.editor.get_current_editorstack() editorstack.switcher_plugin.get_container().open_switcher() assert editorstack.switcher_plugin - assert editorstack.switcher_plugin.isVisible() + assert editorstack.switcher_plugin.is_visible() assert editorstack.switcher_plugin.count() == 2 From 0df8d0024c776bba45b534836c5bfda9a445346e Mon Sep 17 00:00:00 2001 From: Angela Remolina Date: Mon, 24 Apr 2023 01:31:27 -0500 Subject: [PATCH 16/16] Suggestions from review Rename close method to on_close and add open_switcher and open_symbolfinder to the public API --- spyder/app/tests/conftest.py | 2 +- spyder/app/tests/test_mainwindow.py | 39 +++++++++++++++-------------- spyder/plugins/switcher/plugin.py | 17 +++++++++---- 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/spyder/app/tests/conftest.py b/spyder/app/tests/conftest.py index 09aa9b37d4d..5386c178138 100755 --- a/spyder/app/tests/conftest.py +++ b/spyder/app/tests/conftest.py @@ -478,7 +478,7 @@ def main_window(request, tmpdir, qtbot): editorwindow.close() editorstack = window.editor.get_current_editorstack() if editorstack.switcher_plugin: - editorstack.switcher_plugin.close() + editorstack.switcher_plugin.on_close() window.projects.close_project() diff --git a/spyder/app/tests/test_mainwindow.py b/spyder/app/tests/test_mainwindow.py index f5164928ade..8365e31f66e 100644 --- a/spyder/app/tests/test_mainwindow.py +++ b/spyder/app/tests/test_mainwindow.py @@ -2392,7 +2392,8 @@ def test_tight_layout_option_for_inline_plot(main_window, qtbot, tmpdir): @pytest.mark.order(after="test_debug_unsaved_function") def test_switcher(main_window, qtbot, tmpdir): """Test the use of shorten paths when necessary in the switcher.""" - switcher = main_window.switcher.get_container().switcher + switcher = main_window.switcher + switcher_widget = switcher._switcher # Assert that the full path of a file is shown in the switcher file_a = tmpdir.join('test_file_a.py') @@ -2405,11 +2406,11 @@ def example_def_2(): ''') main_window.editor.load(str(file_a)) - main_window.switcher.get_container().open_switcher() - switcher_paths = [switcher.model.item(item_idx).get_description() - for item_idx in range(switcher.model.rowCount())] + switcher.open_switcher() + switcher_paths = [switcher_widget.model.item(item_idx).get_description() + for item_idx in range(switcher_widget.model.rowCount())] assert osp.dirname(str(file_a)) in switcher_paths or len(str(file_a)) > 75 - switcher.close() + switcher.on_close() # Assert that long paths are shortened in the switcher dir_b = tmpdir @@ -2419,21 +2420,21 @@ def example_def_2(): file_b.write('bar\n') main_window.editor.load(str(file_b)) - main_window.switcher.get_container().open_switcher() - file_b_text = switcher.model.item( - switcher.model.rowCount() - 1).get_description() + switcher.open_switcher() + file_b_text = switcher_widget.model.item( + switcher_widget.model.rowCount() - 1).get_description() assert '...' in file_b_text - switcher.close() + switcher.on_close() # Assert search works correctly search_texts = ['test_file_a', 'file_b', 'foo_spam'] expected_paths = [file_a, file_b, None] for search_text, expected_path in zip(search_texts, expected_paths): - main_window.switcher.get_container().open_switcher() - qtbot.keyClicks(switcher.edit, search_text) + switcher.open_switcher() + qtbot.keyClicks(switcher_widget.edit, search_text) qtbot.wait(200) - assert switcher.count() == bool(expected_path) - switcher.close() + assert switcher_widget.count() == bool(expected_path) + switcher.on_close() # Assert symbol switcher works main_window.editor.set_current_filename(str(file_a)) @@ -2449,11 +2450,11 @@ def example_def_2(): qtbot.wait(9000) - main_window.switcher.get_container().open_switcher() - qtbot.keyClicks(switcher.edit, '@') + switcher.open_switcher() + qtbot.keyClicks(switcher_widget.edit, '@') qtbot.wait(200) - assert switcher.count() == 2 - switcher.close() + assert switcher_widget.count() == 2 + switcher.on_close() @flaky(max_runs=3) @@ -2477,7 +2478,7 @@ def test_editorstack_open_switcher_dlg(main_window, tmpdir, qtbot): # Test that the file switcher opens as expected from the editorstack. editorstack = main_window.editor.get_current_editorstack() - editorstack.switcher_plugin.get_container().open_switcher() + editorstack.switcher_plugin.open_switcher() assert editorstack.switcher_plugin assert editorstack.switcher_plugin.is_visible() assert (editorstack.switcher_plugin.count() == @@ -2526,7 +2527,7 @@ def example_def_2(): # Test that the symbol finder opens as expected from the editorstack. editorstack = main_window.editor.get_current_editorstack() - editorstack.switcher_plugin.get_container().open_switcher() + editorstack.switcher_plugin.open_symbolfinder() assert editorstack.switcher_plugin assert editorstack.switcher_plugin.is_visible() assert editorstack.switcher_plugin.count() == 2 diff --git a/spyder/plugins/switcher/plugin.py b/spyder/plugins/switcher/plugin.py index eb465cee9d9..29a8e9599e1 100644 --- a/spyder/plugins/switcher/plugin.py +++ b/spyder/plugins/switcher/plugin.py @@ -105,7 +105,6 @@ def get_icon(self): def on_initialize(self): container = self.get_container() self._switcher = container.switcher - self._modes = {} self._switcher.sig_rejected.connect(self.sig_rejected) self._switcher.sig_text_changed.connect(self.sig_text_changed) @@ -113,6 +112,10 @@ def on_initialize(self): self._switcher.sig_item_selected.connect(self.sig_item_selected) self._switcher.sig_mode_selected.connect(self.sig_mode_selected) + def on_close(self, cancellable=True): + """Close switcher widget.""" + self._switcher.close() + @on_plugin_available(plugin=Plugins.MainMenu) def on_main_menu_available(self): mainmenu = self.get_plugin(Plugins.MainMenu) @@ -155,6 +158,14 @@ def setup(self): """Set-up list widget content based on the filtering.""" self._switcher.setup() + def open_switcher(self, symbol=False): + """Open switcher dialog.""" + self.get_container().open_switcher(symbol) + + def open_symbolfinder(self): + """Open symbol list management dialog.""" + self.get_container().open_symbolfinder() + # QDialog methods def show(self): """Show switcher.""" @@ -164,10 +175,6 @@ def hide(self): """Hide switcher.""" self._switcher.hide() - def close(self): - """Close switcher widget.""" - self._switcher.close() - def set_visible(self, visible): """Show or hide switcher.""" self._switcher.setVisible(visible)