Skip to content

PR: Add Ctrl+Shift+T shortcut to reopen the last closed Editor tab #3620

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

Merged
merged 16 commits into from
Dec 7, 2016
Merged
Show file tree
Hide file tree
Changes from 15 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
1 change: 1 addition & 0 deletions spyder/config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@
'editor/go to previous file': 'Ctrl+Tab',
'editor/go to next file': 'Ctrl+Shift+Tab',
'editor/new file': "Ctrl+N",
'editor/open last closed':"Ctrl+Shift+T",
'editor/open file': "Ctrl+O",
'editor/save file': "Ctrl+S",
'editor/save all': "Ctrl+Alt+S",
Expand Down
28 changes: 24 additions & 4 deletions spyder/plugins/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
from spyder.utils import codeanalysis, encoding, programs, sourcecode
from spyder.utils import icon_manager as ima
from spyder.utils.introspection.manager import IntrospectionManager
from spyder.utils.qthelpers import add_actions, create_action
from spyder.utils.qthelpers import (create_action, add_actions,
add_shortcut_to_tooltip)
from spyder.widgets.findreplace import FindReplace
from spyder.widgets.editor import (EditorMainWindow, EditorSplitter,
EditorStack, Printer)
Expand Down Expand Up @@ -631,6 +632,15 @@ def get_plugin_actions(self):
self.register_shortcut(self.new_action, context="Editor",
name="New file", add_sc_to_tip=True)

add_shortcut_to_tooltip(self.new_action, context="Editor",
name="New file")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this couple of lines, and the import of add_shortcut_to_tooltip. They are not needed anymore.


self.open_last_closed = create_action(self, _("O&pen last closed"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please call this self.open_last_closed_action

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And that's because you named the method associated with this action exactly as the action, so we need to disambiguate them :-)

tip=_("Open last closed"),
triggered=self.open_last_closed)
self.register_shortcut(self.open_last_closed, context="Editor",
name="Open last closed")

self.open_action = create_action(self, _("&Open..."),
icon=ima.icon('fileopen'), tip=_("Open file"),
triggered=self.load,
Expand Down Expand Up @@ -976,7 +986,7 @@ def get_plugin_actions(self):
self.recent_file_menu.aboutToShow.connect(self.update_recent_file_menu)

file_menu_actions = [self.new_action,
None,
self.open_last_closed,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't remove None here and move self.open_last_closed after self.open_action.

We use None to create separators in our menus :-)

Copy link
Contributor

@goanpeca goanpeca Nov 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# spyder.utils.qt_helpers.py
class MenuSeparator:
    pass

# some_file.py
from spyder.utils.qt_helpers import MenuSeparator

file_menu_actions = [self.new_action, 
                     MenuSeparator,
                     self.open_last_closed]

I think we could add something like MenuSeparator so that it is clear what None really does... (for another PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@goanpeca I just did that in this issue: #3794

self.open_action,
self.recent_file_menu,
None,
Expand Down Expand Up @@ -1608,7 +1618,7 @@ def __add_recent_file(self, fname):
self.recent_files.insert(0, fname)
if len(self.recent_files) > self.get_option('max_recent_files'):
self.recent_files.pop(-1)

def _clone_file_everywhere(self, finfo):
"""Clone file (*src_editor* widget) in all editorstacks
Cloning from the first editorstack in which every single new editor
Expand Down Expand Up @@ -1942,7 +1952,17 @@ def replace(self):
"""Replace slot"""
editorstack = self.get_current_editorstack()
editorstack.find_widget.show_replace()


def open_last_closed(self):
""" Reopens the last closed tab."""
editorstack = self.get_current_editorstack()
last_closed_files = editorstack.get_last_closed_files()
if (len(last_closed_files) > 0):
file_to_open = last_closed_files[0]
last_closed_files.remove(file_to_open)
editorstack.set_last_closed_files(last_closed_files)
self.load(file_to_open)

#------ Explorer widget
def close_file_from_name(self, filename):
"""Close file from its name"""
Expand Down
19 changes: 19 additions & 0 deletions spyder/widgets/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,9 @@ def __init__(self, parent, actions):
# Local shortcuts
self.shortcuts = self.create_shortcuts()

#For opening last closed tabs
self.last_closed_files = []

def create_shortcuts(self):
"""Create local shortcuts"""
# --- Configurable shortcuts
Expand Down Expand Up @@ -1168,6 +1171,8 @@ def close_file(self, index=None, force=False):
new_index -= 1
self.set_stack_index(new_index)

self.add_last_closed_file(finfo.filename)

if self.get_stack_count() == 0 and self.create_new_file_if_empty:
self.sig_new_file[()].emit()
return False
Expand All @@ -1191,6 +1196,20 @@ def close_all_but_this(self):
self.close_all_right()
for i in range(0, self.get_stack_count()-1 ):
self.close_file(0)

def add_last_closed_file(self, fname):
"""Add to last closed file list."""
if fname in self.last_closed_files:
self.last_closed_files.remove(fname)
self.last_closed_files.insert(0, fname)
if len(self.last_closed_files) > 10:
self.last_closed_files.pop(-1)

def get_last_closed_files(self):
return self.last_closed_files

def set_last_closed_files(self, fnames):
self.last_closed_files = fnames

#------ Save
def save_if_changed(self, cancelable=False, index=None):
Expand Down