Repository: spyder-ide/spyder

Issue:
Shift+return not working for run selection -  no solutions work
## Problem Description
Updated to most recent Spyder version (4.1.3). Wanted to change the "run selection" back to what I was used to in previous version, shift+return. So: preferences > "run selection" > pressed Shift+Return in new shortcut. Apply, Ok. 
Tried to run a selection with Shift+Return, nothing happens. The "run selection" button does work normally. Also tried to run it with the "enter" button on my numpad, doesn't work either. Restarted Spyder, used --reset, no changes. NOTE: if I change the "run selection" to something else (e.g. Shift+Backspace) it does work as expected.

### What steps reproduce the problem?
1. Update Spyder to 4.1.3
2. Set "run selection" to Shift+Return
3. Try to run selection with Shift+Return

### What is the expected output? What do you see instead?
Expected: to run the selection
Instead: nothing happens

## Versions
* Spyder version: 4.1.3
* Python version: 3.7
* Operating System name/version: windows

Thanks for this otherwise wonderful software!


Discussion:
Hi @EvaNewzoo I was able to reproduce this! It seems like changes in shortcuts that are detected as conflicts are not being set correctly if the old shortcut is not set to something else too or you restart Spyder:

![image](https://user-images.githubusercontent.com/16781833/84294120-53ad9c80-ab0e-11ea-94c2-8ab3f680c079.png)

As a workaround, could you try to redefine the shortcut for the `run selection` and also `run cell and advance` or restart Spyder after the shortcut change is applied? 

We will fix this in a future release :)

I think we should try to solve this for 4.1.4, if it's not that hard to do. What do you think?
Sure :+1:
Ok, please work on this one then.
Thanks, much appreciated!

Resolving patch:
diff --git a/spyder/app/mainwindow.py b/spyder/app/mainwindow.py
--- a/spyder/app/mainwindow.py
+++ b/spyder/app/mainwindow.py
@@ -3258,23 +3258,27 @@ def apply_shortcuts(self):
 
             if shortcut_sequence:
                 keyseq = QKeySequence(shortcut_sequence)
-                try:
-                    if isinstance(qobject, QAction):
-                        if (sys.platform == 'darwin'
-                                and qobject._shown_shortcut == 'missing'):
-                            qobject._shown_shortcut = keyseq
-                        else:
-                            qobject.setShortcut(keyseq)
+            else:
+                # Needed to remove old sequences that were cleared.
+                # See spyder-ide/spyder#12992
+                keyseq = QKeySequence()
+            try:
+                if isinstance(qobject, QAction):
+                    if (sys.platform == 'darwin'
+                            and qobject._shown_shortcut == 'missing'):
+                        qobject._shown_shortcut = keyseq
+                    else:
+                        qobject.setShortcut(keyseq)
 
-                        if add_shortcut_to_tip:
-                            add_shortcut_to_tooltip(qobject, context, name)
+                    if add_shortcut_to_tip:
+                        add_shortcut_to_tooltip(qobject, context, name)
 
-                    elif isinstance(qobject, QShortcut):
-                        qobject.setKey(keyseq)
+                elif isinstance(qobject, QShortcut):
+                    qobject.setKey(keyseq)
 
-                except RuntimeError:
-                    # Object has been deleted
-                    toberemoved.append(index)
+            except RuntimeError:
+                # Object has been deleted
+                toberemoved.append(index)
 
         for index in sorted(toberemoved, reverse=True):
             self.shortcut_data.pop(index)


Test patch:
diff --git a/spyder/app/tests/test_mainwindow.py b/spyder/app/tests/test_mainwindow.py
--- a/spyder/app/tests/test_mainwindow.py
+++ b/spyder/app/tests/test_mainwindow.py
@@ -2421,6 +2421,60 @@ def test_preferences_change_font_regression(main_window, qtbot):
                     timeout=5000)
 
 
+@pytest.mark.slow
+@pytest.mark.skipif(
+    sys.platform == 'darwin',
+    reason="Changes of Shitf+Return shortcut cause an ambiguos shortcut")
+def test_preferences_empty_shortcut_regression(main_window, qtbot):
+    """
+    Test for spyder-ide/spyder/#12992 regression.
+
+    Overwritting shortcuts results in a shortcuts conflict.
+    """
+    # Wait until the window is fully up
+    shell = main_window.ipyconsole.get_current_shellwidget()
+    qtbot.waitUntil(lambda: shell._prompt_html is not None,
+                    timeout=SHELL_TIMEOUT)
+
+    # Setup shortcuts (set run cell and advance shortcut to run selection)
+    base_run_cell_advance = CONF.get_shortcut(
+        'editor', 'run cell and advance')  # Should be Shift+Return
+    base_run_selection = CONF.get_shortcut(
+        'editor', 'run selection')  # Should be F9
+    assert base_run_cell_advance == 'Shift+Return'
+    assert base_run_selection == 'F9'
+    CONF.set_shortcut(
+        'editor', 'run cell and advance', '')
+    CONF.set_shortcut(
+        'editor', 'run selection', base_run_cell_advance)
+    main_window.apply_shortcuts()
+
+    # Check execution of shortcut
+    # Create new file
+    main_window.editor.new()
+    code_editor = main_window.editor.get_focus_widget()
+    code_editor.set_text(u'print(0)\nprint(ññ)')
+
+    with qtbot.waitSignal(shell.executed):
+        qtbot.keyClick(code_editor, Qt.Key_Return, modifier=Qt.ShiftModifier)
+    qtbot.waitUntil(lambda: u'print(0)' in shell._control.toPlainText())
+    assert u'ññ' not in shell._control.toPlainText()
+
+    # Reset shortcuts
+    CONF.set_shortcut(
+        'editor', 'run selection', 'F9')
+    CONF.set_shortcut(
+        'editor', 'run cell and advance', 'Shift+Return')
+    main_window.apply_shortcuts()
+    qtbot.wait(500)  # Wait for shortcut change to actually be applied
+
+    # Check shortcut run cell and advance reset
+    code_editor.setFocus()
+    with qtbot.waitSignal(shell.executed):
+        qtbot.keyClick(code_editor, Qt.Key_Return, modifier=Qt.ShiftModifier)
+    qtbot.waitUntil(lambda: 'runcell(0' in shell._control.toPlainText())
+
+
 @pytest.mark.slow
 def test_preferences_shortcut_reset_regression(main_window, qtbot):
     """