Skip to content

Commit

Permalink
fixed title search ('!t' would not match 'Other:Test' since 't' was i…
Browse files Browse the repository at this point in the history
…ncorrectly consumed in 'Other')
  • Loading branch information
e3rd committed Aug 2, 2021
1 parent 5ee2aea commit 50d08dc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 1.2 Python3.6 (unreleased)
# 1.2 Python3.6 (2021-08-02)
* CHANGED: Check plugin preferences, some of them (using cache) resets to their defaults.
* preview: When navigating search menu, the page is previewed first after 150 ms by default and open after 1 500 ms. When page is not very small, navigation stuck when displaying a new page.
* results are being updated every 200 ms, not instantly, so that the search process might take about 30 % less time
Expand All @@ -11,6 +11,7 @@
* page base name is in bold in results
* good caret behaviour
* good keyboard navigation
* fixed title search ('!t' would not match 'Other:Test' since 't' was incorrectly consumed in 'Other')

# 1.1 last Python3.5- (2020-06-09)

Expand Down
20 changes: 15 additions & 5 deletions instantsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,15 @@ def start_search(self):
return self.process_menu() # show for now results of title search

# 'te' matches these page titles: 'test' or 'Journal:test' or 'foo test' or 'foo (test)'
sub_queries = [re.compile(r"(^|:|\s|\()?" + q, re.IGNORECASE) for q in query.split(" ")]
sub_queries_benevolent = [re.compile(r"(^|:|\s|\()?" + q, re.IGNORECASE) for q in query.split(" ")]
# 'st' does not match those
sub_queries_strict = [re.compile(r"(^|:|\s|\()" + q, re.IGNORECASE) for q in query.split(" ")]

def in_query(txt):
""" False if any part of the query does not match.
If the query is longer >3 characters:
* +10 for every query part that matches a title part beginning
Ex: query 'te' -> +3 for these page titles:
Ex: query 'te' -> +10 for these page titles:
'test' or 'Journal:test' or 'foo test' or 'foo (test)'
* +1 for every query part
Ex: query 'st' -> +1 for those page titles
Expand All @@ -254,21 +256,23 @@ def in_query(txt):
False otherwise ('st' for 'test') so that you do not end up messed
with page titles, after writing a single letter.
"""
arr = (q.search(txt) for q in sub_queries)
try:
if len(query) <= 3:
# raises if subquery m does not match or is not at a page chunk beginning
return sum(10 if m.group(1) is not None else None for m in arr)
return sum(10 if m.group(1) is not None else None
for m in (q.search(txt) for q in sub_queries_strict))
else:
# raises if subquery m does not match
return sum(10 if m.group(1) is not None else 1 for m in arr)
return sum(10 if m.group(1) is not None else 1
for m in (q.search(txt) for q in sub_queries_benevolent))
except (AttributeError, TypeError): # one of the sub_queries is not part of the page title
return False

# we loop either all cached page titles or menu that should be built from previous superset-query menu
it = ((x, x.lower()) for x in list(menu)) if menu else self.cached_titles
for path, path_low in it: # quick search in titles
score = in_query(path_low)

if score: # 'te' matches 'test' or 'Journal:test' etc
# "foo" in "foo:bar", but not in "bar"
# when looping "foo:bar", page "foo" receives +1 for having a subpage
Expand Down Expand Up @@ -805,6 +809,12 @@ def __init__(self, raw_query):

# we are subset of this state from the longest shorter query
self.previous = next((State._states[r[:i]] for i in range(len(r), 0, -1) if r[:i] in State._states), None)

# since having <= 3 letters uses less benevolent searching method, we cannot reduce the next step
# ex: "!est" should not match "testing" but "!esti" should
if self.previous and self.previous.page_title_only:
self.previous = None

if self.previous:
self.menu = deepcopy(self.previous.menu)
for item in self.menu.values():
Expand Down

0 comments on commit 50d08dc

Please sign in to comment.