LibWebView+QT: Cleanup code for bangs

This commit is contained in:
Saksham Goyal 2024-11-17 18:02:31 -05:00
parent 992c80c355
commit 19551bfce5

View file

@ -39,22 +39,24 @@ LocationEdit::LocationEdit(QWidget* parent)
auto query = ak_string_from_qstring(text()); auto query = ak_string_from_qstring(text());
auto const bang_char = '!'; // should this be a setting?
Optional<StringView> search_engine_url; Optional<StringView> search_engine_url;
if (Settings::the()->enable_search()) { if (Settings::the()->enable_search()) {
search_engine_url = Settings::the()->search_engine().query_url; search_engine_url = Settings::the()->search_engine().query_url;
if (query.starts_with('!')) {
auto splits = MUST(query.split(' ')); // FIXME: some missing error handling here. auto const splits = query.split(' ');
auto exist = WebView::find_search_engine_by_bang(splits[0]); if (!splits.is_error()) {
if (exist.has_value()) { auto const first = splits.value().first();
search_engine_url = exist->query_url; if (first.starts_with(bang_char)) {
query = MUST(query.substring_from_byte_offset(splits[0].bytes().size())); auto exist = WebView::find_search_engine_by_bang(first);
if (exist.has_value()) {
search_engine_url = exist->query_url;
query = MUST(query.substring_from_byte_offset(first.bytes().size() + 1));
}
} }
} auto const last = splits.value().last();
// FIXME: low quality code. this can be merged into the if branch if (last.starts_with(bang_char)) {
auto const temp = query.split(' ');
if (!temp.is_error()) {
auto const last = temp.value().last();
if (temp.value().last().starts_with('!')) {
auto exist = WebView::find_search_engine_by_bang(last); auto exist = WebView::find_search_engine_by_bang(last);
if (exist.has_value()) { if (exist.has_value()) {
search_engine_url = exist->query_url; search_engine_url = exist->query_url;