-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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: Add disambiguation functionality for Editor tabs #3715
Changes from all commits
e99a311
69ced28
5f62542
c65c066
f302a7b
42a4052
c9d4d38
4295fa8
ff2e144
da30594
81f98ba
f4d596f
606e4d0
2cc4574
3aea355
75ddace
7cbbdd1
2d9acbe
1473091
858414e
b8b8b7a
a4d1c73
4e0bdb0
92a7b7a
0163546
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright © Spyder Project Contributors | ||
# Licensed under the terms of the MIT License | ||
|
||
"""Tests for sourcecode.py""" | ||
|
||
import os | ||
import sys | ||
|
||
import pytest | ||
|
||
from spyder.utils import sourcecode | ||
|
||
|
||
def test_get_primary_at(): | ||
code = 'import functools\nfunctools.partial' | ||
assert sourcecode.get_primary_at(code, len(code)) == 'functools.partial' | ||
|
||
|
||
def test_get_identifiers(): | ||
code = 'import functools\nfunctools.partial' | ||
assert set(sourcecode.get_identifiers(code)) == set(['import', 'functools', | ||
'functools.partial']) | ||
|
||
|
||
def test_split_source(): | ||
code = 'import functools\nfunctools.partial' | ||
assert sourcecode.split_source(code) == ['import functools', 'functools.partial'] | ||
code = code.replace('\n', '\r\n') | ||
assert sourcecode.split_source(code) == ['import functools', 'functools.partial'] | ||
|
||
|
||
def test_path_components(): | ||
if sys.platform.startswith('linux'): | ||
path_components0 = ['','','documents','test','test.py'] | ||
else: | ||
path_components0 = ['c:','','documents','test','test.py'] | ||
path0 = os.path.join(*path_components0) | ||
assert sourcecode.path_components(path0) == path_components0 | ||
|
||
|
||
def test_differentiate_prefix(): | ||
if sys.platform.startswith('linux'): | ||
path_components0 = ['','','documents','test','test.py'] | ||
path_components1 = ['','','documents','projects','test','test.py'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to test two There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, one is enough. I will change it. 👍 |
||
else: | ||
path_components0 = ['c:','','documents','test','test.py'] | ||
path_components1 = ['c:','','documents','projects','test','test.py'] | ||
diff_path0 = os.path.join(*['test']) | ||
diff_path1 = os.path.join(*['projects','test']) | ||
assert sourcecode.differentiate_prefix( | ||
path_components0, path_components1) == diff_path0 | ||
assert sourcecode.differentiate_prefix( | ||
path_components1, path_components0) == diff_path1 | ||
|
||
def test_get_same_name_files(): | ||
files_path_list = [] | ||
if sys.platform.startswith('linux'): | ||
fname0 = os.path.join(*['','','documents','test','test.py']) | ||
files_path_list.append(fname0) | ||
fname1 = os.path.join(*['','','documents','projects','test','test.py']) | ||
files_path_list.append(fname1) | ||
same_name_files = [['','','documents','test','test.py'], | ||
['','','documents','projects','test','test.py']] | ||
else: | ||
fname0 = os.path.join(*['c:','','documents','test','test.py']) | ||
files_path_list.append(fname0) | ||
fname1 = os.path.join(*['c:','','documents','projects','test','test.py']) | ||
files_path_list.append(fname1) | ||
same_name_files = [['c:','','documents','test','test.py'], | ||
['c:','','documents','projects','test','test.py']] | ||
assert sourcecode.get_same_name_files(files_path_list | ||
,'test.py') == same_name_files | ||
|
||
def test_shortest_path(): | ||
if sys.platform.startswith('linux'): | ||
files_path_list =[['','','documents','test','test.py'], | ||
['','','documents','projects','test','test.py']] | ||
shortest_path = os.path.join(*['','','documents','test','test.py']) | ||
else: | ||
files_path_list =[['c:','','documents','test','test.py'], | ||
['c:','','documents','projects','test','test.py']] | ||
shortest_path = os.path.join(*['c:','','documents','test','test.py']) | ||
assert sourcecode.shortest_path(files_path_list) == shortest_path | ||
|
||
def test_get_file_title(): | ||
files_path_list = [] | ||
if sys.platform.startswith('linux'): | ||
fname0 = os.path.join(*['','','documents','test','test.py']) | ||
files_path_list.append(fname0) | ||
fname1 = os.path.join(*['','','documents','projects','test','test.py']) | ||
files_path_list.append(fname1) | ||
else: | ||
fname0 = os.path.join(*['c:','','documents','test','test.py']) | ||
files_path_list.append(fname0) | ||
fname1 = os.path.join(*['c:','','documents','projects','test','test.py']) | ||
files_path_list.append(fname1) | ||
title0 = 'test.py - ' + os.path.join(*['test']) | ||
title1 = 'test.py - ' + os.path.join(*['projects','test']) | ||
assert sourcecode.get_file_title(files_path_list, fname0) == title0 | ||
assert sourcecode.get_file_title(files_path_list, fname1) == title1 | ||
|
||
if __name__ == '__main__': | ||
pytest.main() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -966,9 +966,12 @@ def __modified_readonly_title(self, title, is_modified, is_readonly): | |
title = "(%s)" % title | ||
return title | ||
|
||
def get_tab_text(self, filename, is_modified=None, is_readonly=None): | ||
"""Return tab title""" | ||
return self.__modified_readonly_title(osp.basename(filename), | ||
def get_tab_text(self, index, is_modified=None, is_readonly=None): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @ccordoba12, CircleCi (pydocstyle) is giving me an error of docstring (D400), however the line that is pointing out ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I fixed this in the latest 3.x branch. Please merge it here again :-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I finally understood what's this error is all about: you just need to add a dot to the docstring below this line, so that it reads like this """Return tab title.""" |
||
"""Return tab title.""" | ||
files_path_list = [finfo.filename for finfo in self.data] | ||
fname = self.data[index].filename | ||
fname = sourcecode.get_file_title(files_path_list, fname) | ||
return self.__modified_readonly_title(fname, | ||
is_modified, is_readonly) | ||
|
||
def get_tab_tip(self, filename, is_modified=None, is_readonly=None): | ||
|
@@ -1003,8 +1006,8 @@ def add_to_data(self, finfo, set_current): | |
self.data.append(finfo) | ||
self.data.sort(key=self.__get_sorting_func()) | ||
index = self.data.index(finfo) | ||
fname, editor = finfo.filename, finfo.editor | ||
self.tabs.insertTab(index, editor, self.get_tab_text(fname)) | ||
editor = finfo.editor | ||
self.tabs.insertTab(index, editor, self.get_tab_text(index)) | ||
self.set_stack_title(index, False) | ||
if set_current: | ||
self.set_stack_index(index) | ||
|
@@ -1020,7 +1023,8 @@ def __repopulate_stack(self): | |
is_modified = True | ||
else: | ||
is_modified = None | ||
tab_text = self.get_tab_text(finfo.filename, is_modified) | ||
index = self.data.index(finfo) | ||
tab_text = self.get_tab_text(index, is_modified) | ||
tab_tip = self.get_tab_tip(finfo.filename) | ||
index = self.tabs.addTab(finfo.editor, tab_text) | ||
self.tabs.setTabToolTip(index, tab_tip) | ||
|
@@ -1054,7 +1058,7 @@ def set_stack_title(self, index, is_modified): | |
fname = finfo.filename | ||
is_modified = (is_modified or finfo.newly_created) and not finfo.default | ||
is_readonly = finfo.editor.isReadOnly() | ||
tab_text = self.get_tab_text(fname, is_modified, is_readonly) | ||
tab_text = self.get_tab_text(index, is_modified, is_readonly) | ||
tab_tip = self.get_tab_tip(fname, is_modified, is_readonly) | ||
self.tabs.setTabText(index, tab_text) | ||
self.tabs.setTabToolTip(index, tab_tip) | ||
|
@@ -1198,7 +1202,7 @@ def close_file(self, index=None, force=False): | |
if self.get_stack_count() == 0 and self.create_new_file_if_empty: | ||
self.sig_new_file[()].emit() | ||
return False | ||
|
||
self.__modify_stack_title() | ||
return is_ok | ||
|
||
def close_all_files(self): | ||
|
@@ -1599,6 +1603,11 @@ def __check_file_status(self, index): | |
# Finally, resetting temporary flag: | ||
self.__file_status_flag = False | ||
|
||
def __modify_stack_title(self): | ||
for index, finfo in enumerate(self.data): | ||
state = finfo.editor.document().isModified() | ||
self.set_stack_title(index, state) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you have to add this function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this function in order to refresh the names in the tabs after a file was closed or opened, this includes the name of the file that is currently on focus in the editor. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then this piece of code could also solve issue #2907. Could you verify if that's the case or not? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, this function solves that issue 😄 |
||
|
||
def refresh(self, index=None): | ||
"""Refresh tabwidget""" | ||
if index is None: | ||
|
@@ -1614,6 +1623,7 @@ def refresh(self, index=None): | |
self.__refresh_statusbar(index) | ||
self.__refresh_readonly(index) | ||
self.__check_file_status(index) | ||
self.__modify_stack_title() | ||
self.update_plugin_title.emit() | ||
else: | ||
editor = None | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a blank line before this one, to separate standard library imports from Spyder ones.