Skip to content
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

Fix message completion trigger to work anywhere in the message #3696

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ final class CompletionSuggestionService: CompletionSuggestionServiceProtocol {

let components = text.components(separatedBy: .whitespaces)

guard var lastComponent = components.last,
guard var lastComponent = components.first(where: { $0.first == SuggestionTriggerPattern.at.rawValue }),
let range = text.range(of: lastComponent, options: .backwards),
lastComponent.count > 0,
let suggestionKey = SuggestionTriggerPattern(rawValue: lastComponent.removeFirst()),
Expand Down
25 changes: 25 additions & 0 deletions UnitTests/Sources/CompletionSuggestionServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,31 @@ final class CompletionSuggestionServiceTests: XCTestCase {
service.setSuggestionTrigger(.init(type: .user, text: "", range: .init()))
try await deferred.fulfill()
}

func testUserSuggestionInDifferentMessagePositions() async throws {
let alice: RoomMemberProxyMock = .mockAlice
let members: [RoomMemberProxyMock] = [alice, .mockBob, .mockCharlie, .mockMe]
let roomProxyMock = JoinedRoomProxyMock(.init(name: "test", members: members))
let service = CompletionSuggestionService(roomProxy: roomProxyMock)

var deferred = deferFulfillment(service.suggestionsPublisher) { suggestion in
suggestion == [.user(item: .init(id: alice.userID, displayName: alice.displayName, avatarURL: alice.avatarURL, range: .init(location: 0, length: 3)))]
}
service.processTextMessage("@al hello")
try await deferred.fulfill()

deferred = deferFulfillment(service.suggestionsPublisher) { suggestion in
suggestion == [.user(item: .init(id: alice.userID, displayName: alice.displayName, avatarURL: alice.avatarURL, range: .init(location: 5, length: 3)))]
}
service.processTextMessage("test @al")
try await deferred.fulfill()

deferred = deferFulfillment(service.suggestionsPublisher) { suggestion in
suggestion == [.user(item: .init(id: alice.userID, displayName: alice.displayName, avatarURL: alice.avatarURL, range: .init(location: 5, length: 3)))]
}
service.processTextMessage("test @al test")
try await deferred.fulfill()
}
}

private extension MentionSuggestionItem {
Expand Down
Loading