-
-
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: More find in files improvements #4599
Merged
Merged
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ae540fc
Search items are now expanded automatically, lines are cropped if 80 …
andfoy 208c466
Line cropping limited to 20 characters from left and right
andfoy 0e104e8
Convert lines to unicode before truncating them
ccordoba12 faff20c
Update with 3.x
ccordoba12 2962742
Merge remote-tracking branch 'upstream/3.x' into more_findinfiles
andfoy 8fe921e
Truncate lines up to 20 characters
andfoy bc0c7cc
Merge branch 'more_findinfiles' of github.com:andfoy/spyder into more…
andfoy 4b5006f
Escaping HTML strings before formatting results
andfoy 9b0e732
Manual HTML escaping definition
andfoy 94a8b9c
File results are sorted once the search process has completed
andfoy 9d0bc77
Duplicate line removed
andfoy ed43cdd
Minor sorting status dict refactor
andfoy 15cd1b5
Sorting results only if requested by user
andfoy c547ac0
General code cleanup
andfoy c69d277
Removed more left over comments
andfoy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ | |
from spyder.config.base import _ | ||
from spyder.py3compat import getcwd, to_text_string | ||
from spyder.utils import icon_manager as ima | ||
from spyder.utils.qthelpers import create_toolbutton, get_filetype_icon | ||
from spyder.utils.qthelpers import create_toolbutton | ||
from spyder.utils.encoding import is_text_file | ||
from spyder.widgets.comboboxes import PatternComboBox | ||
from spyder.widgets.onecolumntree import OneColumnTree | ||
|
@@ -128,42 +128,68 @@ def find_files_in_path(self, path): | |
|
||
def truncate_result(self, line, start, end): | ||
ellipsis = '...' | ||
|
||
max_line_length = 80 | ||
max_num_char_fragment = 40 | ||
|
||
html_escape_table = { | ||
"&": "&", | ||
'"': """, | ||
"'": "'", | ||
">": ">", | ||
"<": "<", | ||
} | ||
|
||
def html_escape(text): | ||
"""Produce entities within text.""" | ||
return "".join(html_escape_table.get(c, c) for c in text) | ||
|
||
line = to_text_string(line) | ||
left, match, right = line[:start], line[start:end], line[end:] | ||
max_line_length = 40 | ||
offset = (len(line) - len(match)) // 2 | ||
|
||
left = left.split(' ') | ||
num_left_words = len(left) | ||
if len(line) > max_line_length: | ||
offset = (len(line) - len(match)) // 2 | ||
|
||
left = left.split(' ') | ||
num_left_words = len(left) | ||
|
||
if num_left_words == 1: | ||
left = left[0] | ||
if len(left) > max_num_char_fragment: | ||
left = ellipsis + left[-offset:] | ||
left = [left] | ||
|
||
if num_left_words == 1: | ||
left = left[0] | ||
if len(left) > max_line_length: | ||
left = ellipsis + left[-offset:] | ||
left = [left] | ||
right = right.split(' ') | ||
num_right_words = len(right) | ||
|
||
right = right.split(' ') | ||
num_right_words = len(right) | ||
if num_right_words == 1: | ||
right = right[0] | ||
if len(right) > max_num_char_fragment: | ||
right = right[:offset] + ellipsis | ||
right = [right] | ||
|
||
if num_right_words == 1: | ||
right = right[0] | ||
if len(right) > max_line_length: | ||
right = right[:offset] + ellipsis | ||
right = [right] | ||
left = left[-4:] | ||
right = right[:4] | ||
|
||
left = left[-3:] | ||
right = right[:3] | ||
if len(left) < num_left_words: | ||
left = [ellipsis] + left | ||
|
||
if len(left) < num_left_words: | ||
left = [ellipsis] + left | ||
if len(right) < num_right_words: | ||
right = right + [ellipsis] | ||
|
||
if len(right) < num_right_words: | ||
right = right + [ellipsis] | ||
left = ' '.join(left) | ||
right = ' '.join(right) | ||
|
||
left = ' '.join(left) | ||
right = ' '.join(right) | ||
if len(left) > max_num_char_fragment: | ||
left = ellipsis + left[-30:] | ||
|
||
trunc_line = '{0}<b>{1}</b>{2}'.format(left, match, right) | ||
if len(right) > max_num_char_fragment: | ||
right = right[:30] + ellipsis | ||
|
||
line_match_format = to_text_string('{0}<b>{1}</b>{2}') | ||
left = html_escape(left) | ||
right = html_escape(right) | ||
match = html_escape(match) | ||
trunc_line = line_match_format.format(left, match, right) | ||
return trunc_line | ||
|
||
def find_string_in_file(self, fname): | ||
|
@@ -493,23 +519,31 @@ def __ge__(self, x): | |
|
||
|
||
class FileMatchItem(QTreeWidgetItem): | ||
def __init__(self, parent, filename): | ||
def __init__(self, parent, filename, sorting_status): | ||
|
||
self.sorting_status = sorting_status | ||
self.filename = osp.basename(filename) | ||
|
||
title = ('<b>{0}</b><br>' | ||
'<small><em>{1}</em>' | ||
'</small>'.format(osp.basename(filename), | ||
osp.dirname(filename))) | ||
title_format = to_text_string('<b>{0}</b><br>' | ||
'<small><em>{1}</em>' | ||
'</small>') | ||
title = (title_format.format(osp.basename(filename), | ||
osp.dirname(filename))) | ||
QTreeWidgetItem.__init__(self, parent, [title], QTreeWidgetItem.Type) | ||
|
||
self.setToolTip(0, filename) | ||
|
||
def __lt__(self, x): | ||
return self.filename < x.filename | ||
if self.sorting_status['status']: | ||
return self.filename < x.filename | ||
else: | ||
return False | ||
|
||
def __ge__(self, x): | ||
return self.filename >= x.filename | ||
if self.sorting_status['status']: | ||
return self.filename >= x.filename | ||
else: | ||
return False | ||
|
||
|
||
class ItemDelegate(QStyledItemDelegate): | ||
|
@@ -559,13 +593,15 @@ def __init__(self, parent): | |
self.total_matches = None | ||
self.error_flag = None | ||
self.completed = None | ||
self.sorting_status = {} | ||
self.data = None | ||
self.files = None | ||
self.set_title('') | ||
self.root_items = None | ||
self.sortByColumn(0, Qt.AscendingOrder) | ||
self.enable_sorting(False) | ||
self.setSortingEnabled(True) | ||
self.header().setSectionsClickable(True) | ||
self.root_items = None | ||
self.sortByColumn(0, Qt.AscendingOrder) | ||
self.setItemDelegate(ItemDelegate(self)) | ||
self.setUniformRowHeights(False) | ||
|
||
|
@@ -576,6 +612,10 @@ def activated(self, item): | |
filename, lineno, colno = itemdata | ||
self.parent().edit_goto.emit(filename, lineno, self.search_text) | ||
|
||
def enable_sorting(self, flag): | ||
"""Enable result sorting after search is complete.""" | ||
self.sorting_status['status'] = flag | ||
|
||
def clicked(self, item): | ||
"""Click event""" | ||
self.activated(item) | ||
|
@@ -585,6 +625,7 @@ def clear_title(self, search_text): | |
self.num_files = 0 | ||
self.data = {} | ||
self.files = {} | ||
self.enable_sorting(False) | ||
self.search_text = search_text | ||
title = "'%s' - " % search_text | ||
text = _('String not found') | ||
|
@@ -596,7 +637,8 @@ def append_result(self, results, num_matches): | |
filename, lineno, colno, line = results | ||
|
||
if filename not in self.files: | ||
item = FileMatchItem(self, filename) | ||
item = FileMatchItem(self, filename, self.sorting_status) | ||
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 disable sorting only on file entries and not line entries, to accomplish that I use the dict |
||
item.setExpanded(True) | ||
self.files[filename] = item | ||
self.num_files += 1 | ||
|
||
|
@@ -755,6 +797,7 @@ def closing_widget(self): | |
|
||
def search_complete(self, completed): | ||
"""Current search thread has finished""" | ||
self.result_browser.enable_sorting(True) | ||
self.find_options.ok_button.setEnabled(True) | ||
self.find_options.stop_button.setEnabled(False) | ||
self.status_bar.hide() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why this has to be a dictionary instead of a property? I mean, you could simply have
instead of having a method called
enable_sorting
that writes to a dict.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.
But if I modify a property, will it propagate along all elements?
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.
Ok, then let's refactor this a bit so it's easier to understand. What about
?