-
-
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 3 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 |
---|---|---|
|
@@ -16,6 +16,12 @@ | |
import os | ||
import os.path as osp | ||
import sys | ||
try: | ||
# Python 2 | ||
from itertools import izip | ||
except ImportError: | ||
# Python 3 | ||
izip = zip | ||
|
||
# Third party imports | ||
from qtpy import is_pyqt46 | ||
|
@@ -941,11 +947,73 @@ 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): | ||
def get_tab_text(self, index, filename, is_modified=None, is_readonly=None): | ||
"""Return tab title""" | ||
return self.__modified_readonly_title(osp.basename(filename), | ||
fname = self.get_file_title(index) | ||
return self.__modified_readonly_title(fname, | ||
is_modified, is_readonly) | ||
|
||
def get_file_title(self, index): | ||
"""Get tab title without ambiguation.""" | ||
finfo = self.data[index] | ||
fname = osp.basename(finfo.filename) | ||
for findex, file in enumerate(self.data): | ||
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.
|
||
fname_to_compare = osp.basename(file.filename) | ||
if findex is not index and fname == fname_to_compare: | ||
differ_path = self.differ_prefix( | ||
self.path_components(finfo.filename), | ||
self.path_components(file.filename)) | ||
differ_path_length = len(differ_path) | ||
if (differ_path_length > 20): | ||
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. This length is too long. Let's change it to 4. 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. |
||
path_components = self.path_components(differ_path) | ||
last_component_index = len(path_components) -1 | ||
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. There is a missing space here after the minus sign:
|
||
path_components = [path_components[0], '...', | ||
path_components[last_component_index]] | ||
differ_path = os.path.join(*path_components) | ||
fname = fname + " - " + differ_path | ||
break | ||
return fname | ||
|
||
def path_components(self, path): | ||
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. Let's move this function to |
||
"""Return the individual components of the given file path | ||
string (for the local operating system). Taked from | ||
http://stackoverflow.com/questions/21498939/how-to-circumvent | ||
-the-fallacy-of-pythons-os-path-commonprefix.""" | ||
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. This docstring should be formatted like this """
Return the individual components of a given file path
string (for the local operating system).
Taken from http://stackoverflow.com/q/21498939/438386
""" That link is the short link to the SO question you referenced. You can get those links by pressing the |
||
components = [] | ||
# The loop guarantees that the returned components can be | ||
# os.path.joined with the path separator and point to the same | ||
# location: | ||
while True: | ||
(new_path, tail) = os.path.split(path) # Works on any platform | ||
components.append(tail) | ||
if new_path == path: # Root (including drive, on Windows) reached | ||
break | ||
path = new_path | ||
components.append(new_path) | ||
components.reverse() # First component first | ||
return components | ||
|
||
def differ_prefix(self, path_components0, path_components1): | ||
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. Let's also move this function to 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.
|
||
"""Return the differ prefix of the given two iterables. Based | ||
on longest_prefix in http://stackoverflow.com/questions/21498939/how-to- | ||
circumvent-the-fallacy-of-pythons-os-path-commonprefix.""" | ||
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. Please reformat this docstring as I said above for |
||
longest_prefix = [] | ||
common_elmt = None | ||
for (elmt0, elmt1) in izip(path_components0, path_components1): | ||
if elmt0 != elmt1: | ||
break | ||
else: | ||
common_elmt = elmt0 | ||
longest_prefix.append(elmt0) | ||
file_name_length = len(path_components0[len(path_components0)-1]) | ||
path_0 = os.path.join(*path_components0)[:-file_name_length-1] | ||
if(len(longest_prefix)>3): | ||
longest_path_prefix = os.path.join(*longest_prefix) | ||
length_to_delete = len(longest_path_prefix)-len(common_elmt) | ||
return path_0[length_to_delete:] | ||
else: | ||
return path_0 | ||
|
||
def get_tab_tip(self, filename, is_modified=None, is_readonly=None): | ||
"""Return tab menu title""" | ||
if self.fullpath_sorting_enabled: | ||
|
@@ -979,7 +1047,7 @@ def add_to_data(self, finfo, set_current): | |
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)) | ||
self.tabs.insertTab(index, editor, self.get_tab_text(index, fname)) | ||
self.set_stack_title(index, False) | ||
if set_current: | ||
self.set_stack_index(index) | ||
|
@@ -995,7 +1063,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, finfo.filename, is_modified) | ||
tab_tip = self.get_tab_tip(finfo.filename) | ||
index = self.tabs.addTab(finfo.editor, tab_text) | ||
self.tabs.setTabToolTip(index, tab_tip) | ||
|
@@ -1029,7 +1098,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, fname, 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) | ||
|
@@ -1171,7 +1240,9 @@ 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): | ||
|
@@ -1558,6 +1629,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: | ||
|
@@ -1573,6 +1649,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 move this import to
py3compat