Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Register shortcuts for widgets directly (API) #23161

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions spyder/api/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,14 @@ def register_shortcut_for_widget(
name = name.lower()
context = context.lower()

# Add observer to register shortcut when its associated option is
# broadcasted by CONF or updated in Preferences.
# Register shortcurt for widget
keystr = self.get_shortcut(name, context, plugin_name)
self._register_shortcut(
keystr, name, triggered, context, widget, plugin_name
)

# Add observer for shortcut so that it's updated when changed by users
# in Preferences
config_observer = functools.partial(
self._register_shortcut,
name=name,
Expand Down Expand Up @@ -200,6 +206,14 @@ def _register_shortcut(
# Disable current shortcut, if available
current_shortcut = self._shortcuts.get((context, name, plugin_name))
if current_shortcut:
# Don't do the rest if we're trying to register the same shortcut
# again. This happens at startup because shortcuts are registered
# on widget creation and then the observer attached to the shortcut
# tries to do it again after CONF.notify_all_observers() is called.
if current_shortcut.key().toString() == keystr:
return

# Disable current shortcut to create a new one below
current_shortcut.setEnabled(False)
current_shortcut.deleteLater()
self._shortcuts.pop((context, name, plugin_name))
Expand Down
4 changes: 0 additions & 4 deletions spyder/app/tests/test_mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5375,10 +5375,6 @@ def test_copy_paste(main_window, qtbot, tmpdir):
code_editor = main_window.editor.get_focus_widget()
code_editor.set_text(code)

# Register codeeditor shortcuts
CONF.notify_section_all_observers("shortcuts")
qtbot.wait(300)

# Test copy
cursor = code_editor.textCursor()
cursor.setPosition(69)
Expand Down
12 changes: 0 additions & 12 deletions spyder/plugins/editor/widgets/codeeditor/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,6 @@ def mock_completions_codeeditor(qtbot_module, request):
qtbot_module.addWidget(editor)
editor.show()

# Register shortcuts for CodeEditor
CONF.notify_section_all_observers("shortcuts")
qtbot_module.wait(300)

mock_response = Mock()

def perform_request(lang, method, params):
Expand Down Expand Up @@ -186,10 +182,6 @@ def completions_codeeditor(completion_plugin_all_started, qtbot_module,
completion_plugin, capabilities = completion_plugin_all_started
completion_plugin.wait_for_ms = 2000

# Register shortcuts for CodeEditor
CONF.notify_section_all_observers("shortcuts")
qtbot_module.wait(300)

CONF.set('completions', 'enable_code_snippets', False)
completion_plugin.after_configuration_update([])
CONF.notify_section_all_observers('completions')
Expand Down Expand Up @@ -257,10 +249,6 @@ def codeeditor(qtbot):
widget.resize(640, 480)
widget.show()

# Register shortcuts for CodeEditor
CONF.notify_section_all_observers("shortcuts")
qtbot.wait(300)

yield widget
widget.close()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,9 @@ def test_shortcut_for_widget_is_updated(config_dialog, codeeditor, qtbot):
text = ('aa\nbb\ncc\ndd\n')
editor.set_text(text)

# We need to wait for a bit so shortcuts are registered correctly
qtbot.wait(300)

# Check shortcuts were registered
assert editor._shortcuts != {}

Expand Down
24 changes: 22 additions & 2 deletions spyder/plugins/editor/widgets/editorstack/tests/test_shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ def editorstack(qtbot):
editorstack.show()
editorstack.go_to_line(1)

# Register shortcuts
CONF.notify_section_all_observers("shortcuts")
# We need to wait for a bit so shortcuts are registered correctly
qtbot.wait(300)

return editorstack
Expand Down Expand Up @@ -344,6 +343,27 @@ def test_builtin_undo_redo(editorstack, qtbot):
assert editor.toPlainText() == 'Something\nLine1\nLine2\nLine3\nLine4\n'


@pytest.mark.skipif(
sys.platform.startswith("linux") and running_in_ci(),
reason='Fails on Linux and CI'
)
def test_shortcuts_for_new_editors(editorstack, qtbot):
"""
Test that widget shortcuts are working for new editors.

This is a regression test for spyder-ide/spyder#23151.
"""
# Create new file and give it focus
editorstack.new('bar.py', 'utf-8', 'Line5\nLine6\nLine7\nLine8')
editorstack.tabs.setCurrentIndex(1)

# Go to its first line, click the shortcut to comment it and check it was
editorstack.go_to_line(1)
editor = editorstack.get_current_editor()
qtbot.keyClick(editor, Qt.Key_1, modifier=Qt.ControlModifier)
assert editor.toPlainText() == '# Line5\nLine6\nLine7\nLine8\n'


if __name__ == "__main__":
import os
pytest.main(['-x', os.path.basename(__file__), '-vv', '-rw'])
Loading