From 5ec47da12741c9f4f83bbd37ced3c331bcc61466 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 13 Jun 2017 10:28:05 +0200 Subject: [PATCH] Get rid of configtypes.AutoSearch and IgnoreCase --- qutebrowser/browser/browsertab.py | 19 +++++- qutebrowser/browser/webengine/webenginetab.py | 20 +++--- qutebrowser/browser/webkit/webkittab.py | 23 +++---- qutebrowser/config/configdata.yml | 14 +++- qutebrowser/config/configtypes.py | 66 ------------------- qutebrowser/utils/urlutils.py | 6 +- tests/unit/utils/test_urlutils.py | 6 +- 7 files changed, 52 insertions(+), 102 deletions(-) diff --git a/qutebrowser/browser/browsertab.py b/qutebrowser/browser/browsertab.py index 7adb9608e..fd8f8872f 100644 --- a/qutebrowser/browser/browsertab.py +++ b/qutebrowser/browser/browsertab.py @@ -188,13 +188,28 @@ class AbstractSearch(QObject): self.text = None self.search_displayed = False - def search(self, text, *, ignore_case=False, reverse=False, + def _is_case_sensitive(self, ignore_case): + """Check if case-sensitivity should be used. + + This assumes self.text is already set properly. + + Arguments: + ignore_case: The ignore_case value from the config. + """ + mapping = { + 'smart': not self.text.islower(), + 'never': False, + 'always': True, + } + return mapping[ignore_case] + + def search(self, text, *, ignore_case='never', reverse=False, result_cb=None): """Find the given text on the page. Args: text: The text to search for. - ignore_case: Search case-insensitively. (True/False/'smart') + ignore_case: Search case-insensitively. ('always'/'never/'smart') reverse: Reverse search direction. result_cb: Called with a bool indicating whether a match was found. """ diff --git a/qutebrowser/browser/webengine/webenginetab.py b/qutebrowser/browser/webengine/webenginetab.py index 0a4c2dfc7..1345e8001 100644 --- a/qutebrowser/browser/webengine/webenginetab.py +++ b/qutebrowser/browser/webengine/webenginetab.py @@ -152,20 +152,16 @@ class WebEngineSearch(browsertab.AbstractSearch): callback(found) self._widget.findText(text, flags, wrapped_callback) - def search(self, text, *, ignore_case=False, reverse=False, + def search(self, text, *, ignore_case='never', reverse=False, result_cb=None): - flags = QWebEnginePage.FindFlags(0) - if ignore_case == 'smart': - if not text.islower(): - flags |= QWebEnginePage.FindCaseSensitively - elif not ignore_case: - flags |= QWebEnginePage.FindCaseSensitively - if reverse: - flags |= QWebEnginePage.FindBackward - self.text = text - self._flags = flags - self._find(text, flags, result_cb, 'search') + self._flags = QWebEnginePage.FindFlags(0) + if self._is_case_sensitive(ignore_case): + self._flags |= QWebEnginePage.FindCaseSensitively + if reverse: + self._flags |= QWebEnginePage.FindBackward + + self._find(text, self._flags, result_cb, 'search') def clear(self): self.search_displayed = False diff --git a/qutebrowser/browser/webkit/webkittab.py b/qutebrowser/browser/webkit/webkittab.py index fa626e863..e901f3a46 100644 --- a/qutebrowser/browser/webkit/webkittab.py +++ b/qutebrowser/browser/webkit/webkittab.py @@ -140,24 +140,21 @@ class WebKitSearch(browsertab.AbstractSearch): self._widget.findText('') self._widget.findText('', QWebPage.HighlightAllOccurrences) - def search(self, text, *, ignore_case=False, reverse=False, + def search(self, text, *, ignore_case='never', reverse=False, result_cb=None): self.search_displayed = True - flags = QWebPage.FindWrapsAroundDocument - if ignore_case == 'smart': - if not text.islower(): - flags |= QWebPage.FindCaseSensitively - elif not ignore_case: - flags |= QWebPage.FindCaseSensitively + self.text = text + self._flags = QWebPage.FindWrapsAroundDocument + if self._is_case_sensitive(ignore_case): + self._flags |= QWebPage.FindCaseSensitively if reverse: - flags |= QWebPage.FindBackward + self._flags |= QWebPage.FindBackward # We actually search *twice* - once to highlight everything, then again # to get a mark so we can navigate. - found = self._widget.findText(text, flags) - self._widget.findText(text, flags | QWebPage.HighlightAllOccurrences) - self.text = text - self._flags = flags - self._call_cb(result_cb, found, text, flags, 'search') + found = self._widget.findText(text, self._flags) + self._widget.findText(text, + self._flags | QWebPage.HighlightAllOccurrences) + self._call_cb(result_cb, found, text, self._flags, 'search') def next_result(self, *, result_cb=None): self.search_displayed = True diff --git a/qutebrowser/config/configdata.yml b/qutebrowser/config/configdata.yml index 93a802c19..51a3e8d7f 100644 --- a/qutebrowser/config/configdata.yml +++ b/qutebrowser/config/configdata.yml @@ -1,7 +1,12 @@ # general ignore_case: - type: IgnoreCase + type: + name: String + valid_values: + - always: Search case-insensitively + - never: Search case-sensitively + - smart: Search case-sensitively if there are capital chars default: smart desc: Whether to find text on a page case-insensitively. @@ -45,7 +50,12 @@ default_page: for a blank page. auto_search: - type: AutoSearch + type: + name: String + valid_values: + - naive: Use simple/naive check. + - dns: Use DNS requests (might be slow!). + - never: Never search automatically. default: naive desc: Whether to start a search when something else than a URL is entered. diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index e0b1c7084..f5255048c 100644 --- a/qutebrowser/config/configtypes.py +++ b/qutebrowser/config/configtypes.py @@ -1159,44 +1159,6 @@ class Encoding(BaseType): return value -class AutoSearch(BaseType): - - """Whether to start a search when something else than a URL is entered.""" - - def __init__(self, none_ok=False): - super().__init__(none_ok) - self.booltype = Bool(none_ok=none_ok) - self.valid_values = ValidValues( - ('naive', "Use simple/naive check."), - ('dns', "Use DNS requests (might be slow!)."), - ('false', "Never search automatically.")) - - def from_py(self, value): - self._basic_validation(value, pytype=(str, bool)) - if not value: - return None - - if isinstance(value, bool): - if self.booltype.from_py(value): - # boolean true is an alias for naive matching - return 'naive' - else: - return False - elif value.lower() in ['naive', 'dns']: - return value.lower() - else: - # Should be prevented by valid_values - assert False, value - - def transform(self, value): - if not value: - return None - elif value.lower() in ['naive', 'dns']: - return value.lower() - elif self.booltype.transform(value): - pass - - class Position(MappingType): """The position of the tab bar.""" @@ -1337,34 +1299,6 @@ class NewTabPosition(BaseType): ('last', "At the end.")) -class IgnoreCase(Bool): - - """Whether to ignore case when searching.""" - - def __init__(self, none_ok=False): - super().__init__(none_ok) - self.valid_values = ValidValues( - ('true', "Search case-insensitively"), - ('false', "Search case-sensitively"), - ('smart', "Search case-sensitively if there " - "are capital chars")) - - def transform(self, value): - if value.lower() == 'smart': - return 'smart' - else: - return super().transform(value) - - def validate(self, value): - self._basic_validation(value) - if not value: - return - if value.lower() == 'smart': - return - else: - super().validate(value) - - class UserAgent(BaseType): """The user agent to use.""" diff --git a/qutebrowser/utils/urlutils.py b/qutebrowser/utils/urlutils.py index 24bbfbc9c..5adc5d9c7 100644 --- a/qutebrowser/utils/urlutils.py +++ b/qutebrowser/utils/urlutils.py @@ -194,7 +194,7 @@ def fuzzy_url(urlstr, cwd=None, relative=False, do_search=True, url = qurl_from_user_input(urlstr) log.url.debug("Converting fuzzy term {!r} to URL -> {}".format( urlstr, url.toDisplayString())) - if do_search and config.val.auto_search and urlstr: + if do_search and config.val.auto_search != 'never' and urlstr: qtutils.ensure_valid(url) else: if not url.isValid(): @@ -248,7 +248,7 @@ def is_url(urlstr): qurl = QUrl(urlstr) qurl_userinput = qurl_from_user_input(urlstr) - if not autosearch: + if autosearch == 'never': # no autosearch, so everything is a URL unless it has an explicit # search engine. try: @@ -270,7 +270,7 @@ def is_url(urlstr): log.url.debug("Is localhost.") url = True elif is_special_url(qurl): - # Special URLs are always URLs, even with autosearch=False + # Special URLs are always URLs, even with autosearch=never log.url.debug("Is a special URL.") url = True elif autosearch == 'dns': diff --git a/tests/unit/utils/test_urlutils.py b/tests/unit/utils/test_urlutils.py index 10bd60bed..30de96a39 100644 --- a/tests/unit/utils/test_urlutils.py +++ b/tests/unit/utils/test_urlutils.py @@ -347,7 +347,7 @@ def test_get_search_url_invalid(urlutils_config_stub, url): # autosearch = False (False, True, False, 'This is a URL without autosearch'), ]) -@pytest.mark.parametrize('auto_search', ['dns', 'naive', False]) +@pytest.mark.parametrize('auto_search', ['dns', 'naive', 'never']) def test_is_url(urlutils_config_stub, fake_dns, is_url, is_url_no_autosearch, uses_dns, url, auto_search): """Test is_url(). @@ -384,11 +384,9 @@ def test_is_url(urlutils_config_stub, fake_dns, is_url, is_url_no_autosearch, assert not fake_dns.used assert result == is_url elif auto_search == 'naive': - urlutils_config_stub.data['general']['auto-search'] = 'naive' assert urlutils.is_url(url) == is_url assert not fake_dns.used - elif not auto_search: - urlutils_config_stub.data['general']['auto-search'] = False + elif auto_search == 'never': assert urlutils.is_url(url) == is_url_no_autosearch assert not fake_dns.used else: