Skip to content

Commit 6229ab1

Browse files
committed
Address review comments from Jendrik
1 parent a7a829f commit 6229ab1

File tree

4 files changed

+28
-29
lines changed

4 files changed

+28
-29
lines changed

rednotebook/data.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import datetime
2020
import re
21+
import sys
2122

2223

2324
TEXT_RESULT_LENGTH = 42
@@ -201,38 +202,36 @@ def search(self, words, tags):
201202
if add_text_to_results:
202203
results.append(get_text_with_dots(self.text, 0, TEXT_RESULT_LENGTH))
203204
else:
204-
for word in words:
205-
# Any of the word matches with the date.
206-
if word in str(self):
207-
# We don't want to show search results from the text matching
208-
# this date.
209-
words.remove(word)
210-
results.append(get_text_with_dots(self.text, 0, TEXT_RESULT_LENGTH))
211-
# if all the words matched agains the date, return
212-
if not words:
213-
return str(self), words
214-
text_result = self.search_in_text(words)
205+
non_date_words = [word for word in words if word not in str(self)]
206+
words_contain_date = len(non_date_words) != len(words)
207+
if words_contain_date:
208+
results.append(get_text_with_dots(self.text, 0, TEXT_RESULT_LENGTH))
209+
# If all the words matched agains the date, return.
210+
if not non_date_words:
211+
return str(self), results
212+
213+
text_result = self.search_in_text(non_date_words)
215214
if text_result:
216215
results.append(text_result)
217-
results.extend(self.search_in_categories(words))
216+
results.extend(self.search_in_categories(non_date_words))
218217
return str(self), results
219218

220219
def search_in_text(self, words):
221-
"""All words should be present in the text"""
222-
matches = {}
220+
"""
221+
If all words are in the text, return a suitable text substring.
222+
Otherwise, return None.
223+
"""
224+
match_word, smallest_index = None, sys.maxsize
223225
for word in words:
224-
index = self.text.upper().find(word.upper())
226+
index = self.text.lower().find(word.lower())
225227
if index < 0:
226228
return
227-
matches[word] = index
228-
229-
# Of all the matching words, find the one which appears first
230-
match_word = min(matches, key=matches.get)
231-
first_occurrence = matches[match_word]
229+
if index < smallest_index:
230+
match_word, smallest_index = word, index
232231

233-
found_text = self.text[first_occurrence : first_occurrence + len(match_word)]
232+
found_text = self.text[smallest_index : smallest_index + len(match_word)]
234233
return get_text_with_dots(
235-
self.text, first_occurrence, first_occurrence + len(match_word), found_text
234+
self.text, smallest_index, smallest_index + len(match_word), found_text
236235
)
237236

238237
def search_in_categories(self, words):

rednotebook/gui/browser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ def set_font_size(self, size):
6868
def highlight(self, search_words):
6969
# Tell the webview which text to highlight after the html is loaded
7070
self.search_words = search_words
71-
for query in search_words:
71+
for word in search_words:
7272
self.get_find_controller().search(
73-
query, WebKit2.FindOptions.CASE_INSENSITIVE, MAX_HITS
73+
word, WebKit2.FindOptions.CASE_INSENSITIVE, MAX_HITS
7474
)
7575

7676
def on_load_changed(self, webview, event):

rednotebook/gui/main_window.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -814,9 +814,9 @@ def _get_buffer(self, key, text):
814814
def _get_buffer_for_day(self, day):
815815
return self._get_buffer(day.date, day.text)
816816

817-
def scroll_to_text(self, words):
817+
def scroll_to_non_date_text(self, words):
818818
"""
819-
Finds the first non-date word in words, and passes it on to
819+
Find the first non-date word in words, and pass it on to
820820
`Editor.scroll_to_text`.
821821
"""
822822
for word in words:
@@ -835,7 +835,7 @@ def show_day(self, new_day):
835835

836836
if self.search_words:
837837
# If a search is currently made, scroll to the text and return.
838-
GObject.idle_add(self.scroll_to_text, self.search_words)
838+
GObject.idle_add(self.scroll_to_non_date_text, self.search_words)
839839
GObject.idle_add(self.highlight, self.search_words)
840840
return
841841

rednotebook/gui/search.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ def search(self, search_text):
5959
else:
6060
words.append(part)
6161

62-
# Highlight all occurrences in the current day's text
62+
# Highlight all occurrences in the current day's text.
6363
self.main_window.highlight_text(words)
6464

6565
# Scroll to query.
6666
if words:
67-
self.main_window.day_text_field.scroll_to_text(tags + words)
67+
self.main_window.day_text_field.scroll_to_non_date_text(tags + words)
6868

6969
self.main_window.search_tree_view.update_data(words, tags)
7070

0 commit comments

Comments
 (0)