-
Notifications
You must be signed in to change notification settings - Fork 32
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
Add tabs to suggestions/autocomplete on iOS #998
Changes from 12 commits
b818ca1
637f6c0
d089274
4799ec3
ba1e222
638cee4
7b14725
05cbe7d
b01694e
808d9b4
1c8afaf
efc6177
fd6908e
492e70a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// BrowserTab.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
|
||
public protocol BrowserTab { | ||
|
||
var url: URL { get } | ||
var title: String { get } | ||
|
||
} | ||
|
||
extension Score { | ||
init(browserTab: BrowserTab, query: Query) { | ||
self.init(title: browserTab.title, url: browserTab.url, visitCount: 0, query: query) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ extension Score { | |
// swiftlint:disable:next cyclomatic_complexity | ||
init(title: String?, url: URL, visitCount: Int, query: Query, queryTokens: [Query]? = nil) { | ||
// To optimize, query tokens can be precomputed | ||
let query = query.lowercased() | ||
let queryTokens = queryTokens ?? Self.tokens(from: query) | ||
|
||
var score = 0 | ||
|
@@ -39,7 +40,9 @@ extension Score { | |
score += 300 | ||
// Prioritize root URLs most | ||
if url.isRoot { score += 2000 } | ||
} else if lowercasedTitle.starts(with: query) { | ||
} else if lowercasedTitle | ||
.trimmingCharacters(in: .alphanumerics.inverted) | ||
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. This fixes a bug where if a title was like 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. 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. Yeah that example won't work now because:
But honestly, I think that's gonna be rare? I assume people are more likely to search for I can make it an OR condition though if you feel strongly? 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. The new solution looks better to me 👍 Thank you, Brindy! |
||
.starts(with: query) { | ||
score += 200 | ||
if url.isRoot { score += 2000 } | ||
} else if queryCount > 2 && domain.contains(query) { | ||
|
@@ -52,7 +55,9 @@ extension Score { | |
var matchesAllTokens = true | ||
for token in queryTokens { | ||
// Match only from the begining of the word to avoid unintuitive matches. | ||
if !lowercasedTitle.starts(with: token) && !lowercasedTitle.contains(" \(token)") && !nakedUrl.starts(with: token) { | ||
if !lowercasedTitle | ||
.trimmingCharacters(in: .alphanumerics.inverted) | ||
.starts(with: token) && !lowercasedTitle.contains(" \(token)") && !nakedUrl.starts(with: token) { | ||
matchesAllTokens = false | ||
break | ||
} | ||
|
@@ -66,7 +71,9 @@ extension Score { | |
if let firstToken = queryTokens.first { // nakedUrlString - high score boost | ||
if nakedUrl.starts(with: firstToken) { | ||
score += 70 | ||
} else if lowercasedTitle.starts(with: firstToken) { // begining of the title - moderate score boost | ||
} else if lowercasedTitle | ||
.trimmingCharacters(in: .alphanumerics.inverted) | ||
.starts(with: firstToken) { // begining of the title - moderate score boost | ||
score += 50 | ||
} | ||
} | ||
|
@@ -104,12 +111,9 @@ extension Score { | |
} | ||
|
||
static func tokens(from query: Query) -> [Query] { | ||
return query | ||
.split(whereSeparator: { | ||
$0.unicodeScalars.contains(where: { CharacterSet.whitespacesAndNewlines.contains($0) }) | ||
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. This seemed superfluous and appeared to result in some strings not tokenising as expected, so this is aligned with the similar code for Bookmarks on iOS. |
||
}) | ||
return query.components(separatedBy: .whitespacesAndNewlines) | ||
.filter { !$0.isEmpty } | ||
.map { String($0).lowercased() } | ||
.map { $0.lowercased() } | ||
} | ||
|
||
} |
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.
Probably not strictly required because usually input will be lowercase, but while testing caused some confusion.