Skip to content

Commit

Permalink
Merge from 3.x: PR #5777
Browse files Browse the repository at this point in the history
Fixes #5776
  • Loading branch information
ccordoba12 committed Nov 17, 2017
2 parents fb82098 + 3fa19b2 commit 8397cfb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
4 changes: 2 additions & 2 deletions spyder/plugins/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1917,7 +1917,7 @@ def new(self, fname=None, editorstack=None, text=None):
# QString when triggered by a Qt signal
fname = osp.abspath(to_text_string(fname))
index = current_es.has_filename(fname)
if index and not current_es.close_file(index):
if index is not None and not current_es.close_file(index):
return

# Creating the editor widget in the first editorstack (the one that
Expand All @@ -1939,7 +1939,7 @@ def update_recent_file_menu(self):
"""Update recent file menu"""
recent_files = []
for fname in self.recent_files:
if not self.is_file_opened(fname) and osp.isfile(fname):
if self.is_file_opened(fname) is None and osp.isfile(fname):
recent_files.append(fname)
self.recent_file_menu.clear()
if recent_files:
Expand Down
26 changes: 24 additions & 2 deletions spyder/widgets/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1377,10 +1377,20 @@ def get_current_filename(self):
return self.data[self.get_stack_index()].filename

def has_filename(self, filename):
"""Return the self.data index position for the filename.
Args:
filename: Name of the file to search for in self.data.
Returns:
The self.data index for the filename. Returns None
if the filename is not found in self.data.
"""
fixpath = lambda path: osp.normcase(osp.realpath(path))
for index, finfo in enumerate(self.data):
if fixpath(filename) == fixpath(finfo.filename):
return index
return None

def set_current_filename(self, filename, focus=True):
"""Set current filename and return the associated editor instance."""
Expand All @@ -1397,6 +1407,18 @@ def set_current_filename(self, filename, focus=True):
return editor

def is_file_opened(self, filename=None):
"""Return if filename is in the editor stack.
Args:
filename: Name of the file to search for. If filename is None,
then checks if any file is open.
Returns:
True: If filename is None and a file is open.
False: If filename is None and no files are open.
None: If filename is not None and the file isn't found.
integer: Index of file name in editor stack.
"""
if filename is None:
# Is there any file opened?
return len(self.data) > 0
Expand Down Expand Up @@ -1698,7 +1720,7 @@ def save_as(self, index=None):
if filename:
ao_index = self.has_filename(filename)
# Note: ao_index == index --> saving an untitled file
if ao_index and ao_index != index:
if ao_index is not None and ao_index != index:
if not self.close_file(ao_index):
return
if ao_index < index:
Expand Down Expand Up @@ -1729,7 +1751,7 @@ def save_copy_as(self, index=None):
if filename:
ao_index = self.has_filename(filename)
# Note: ao_index == index --> saving an untitled file
if ao_index and ao_index != index:
if ao_index is not None and ao_index != index:
if not self.close_file(ao_index):
return
if ao_index < index:
Expand Down

0 comments on commit 8397cfb

Please sign in to comment.