Skip to content

Commit f5ed510

Browse files
committed
Improve Codeium request cancellation
1 parent bb8cf1f commit f5ed510

File tree

1 file changed

+70
-66
lines changed

1 file changed

+70
-66
lines changed

Core/Sources/CodeiumService/CodeiumService.swift

Lines changed: 70 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public class CodeiumSuggestionService {
5656

5757
var xcodeVersion = "14.0.0"
5858
var languageServerVersion = CodeiumInstallationManager.latestSupportedVersion
59+
60+
private var ongoingTasks = Set<Task<[CodeSuggestion], Error>>()
5961

6062
init(designatedServer: CodeiumLSP) {
6163
projectRootURL = URL(fileURLWithPath: "/")
@@ -226,83 +228,85 @@ extension CodeiumSuggestionService: CodeiumSuggestionServiceType {
226228
usesTabsForIndentation: Bool,
227229
ignoreSpaceOnlySuggestions: Bool
228230
) async throws -> [CodeSuggestion] {
231+
ongoingTasks.forEach { $0.cancel() }
232+
ongoingTasks.removeAll()
233+
await cancelRequest()
234+
229235
requestCounter += 1
230236
let languageId = languageIdentifierFromFileURL(fileURL)
231-
232237
let relativePath = getRelativePath(of: fileURL)
233-
234-
let request = await CodeiumRequest.GetCompletion(requestBody: .init(
235-
metadata: try getMetadata(),
236-
document: .init(
237-
absolute_path: fileURL.path,
238-
relative_path: relativePath,
239-
text: content,
240-
editor_language: languageId.rawValue,
241-
language: .init(codeLanguage: languageId),
242-
cursor_position: .init(
243-
row: cursorPosition.line,
244-
col: cursorPosition.character
245-
)
246-
),
247-
editor_options: .init(tab_size: indentSize, insert_spaces: !usesTabsForIndentation),
248-
other_documents: openedDocumentPool.getOtherDocuments(exceptURL: fileURL)
249-
.map { openedDocument in
250-
let languageId = languageIdentifierFromFileURL(openedDocument.url)
251-
return .init(
252-
absolute_path: openedDocument.url.path,
253-
relative_path: openedDocument.relativePath,
254-
text: openedDocument.content,
255-
editor_language: languageId.rawValue,
256-
language: .init(codeLanguage: languageId)
238+
239+
let task = Task {
240+
let request = await CodeiumRequest.GetCompletion(requestBody: .init(
241+
metadata: try getMetadata(),
242+
document: .init(
243+
absolute_path: fileURL.path,
244+
relative_path: relativePath,
245+
text: content,
246+
editor_language: languageId.rawValue,
247+
language: .init(codeLanguage: languageId),
248+
cursor_position: .init(
249+
row: cursorPosition.line,
250+
col: cursorPosition.character
257251
)
258-
}
259-
))
260-
261-
if request.requestBody.metadata.request_id <= cancellationCounter {
262-
throw CancellationError()
263-
}
252+
),
253+
editor_options: .init(tab_size: indentSize, insert_spaces: !usesTabsForIndentation),
254+
other_documents: openedDocumentPool.getOtherDocuments(exceptURL: fileURL)
255+
.map { openedDocument in
256+
let languageId = languageIdentifierFromFileURL(openedDocument.url)
257+
return .init(
258+
absolute_path: openedDocument.url.path,
259+
relative_path: openedDocument.relativePath,
260+
text: openedDocument.content,
261+
editor_language: languageId.rawValue,
262+
language: .init(codeLanguage: languageId)
263+
)
264+
}
265+
))
266+
267+
try Task.checkCancellation()
264268

265-
let result = try await (try await setupServerIfNeeded()).sendRequest(request)
269+
let result = try await (try await setupServerIfNeeded()).sendRequest(request)
270+
271+
try Task.checkCancellation()
266272

267-
if request.requestBody.metadata.request_id <= cancellationCounter {
268-
throw CancellationError()
273+
return result.completionItems?.filter { item in
274+
if ignoreSpaceOnlySuggestions {
275+
return !item.completion.text.allSatisfy { $0.isWhitespace || $0.isNewline }
276+
}
277+
return true
278+
}.map { item in
279+
CodeSuggestion(
280+
text: item.completion.text,
281+
position: cursorPosition,
282+
uuid: item.completion.completionId,
283+
range: CursorRange(
284+
start: .init(
285+
line: item.range.startPosition?.row.flatMap(Int.init) ?? 0,
286+
character: item.range.startPosition?.col.flatMap(Int.init) ?? 0
287+
),
288+
end: .init(
289+
line: item.range.endPosition?.row.flatMap(Int.init) ?? 0,
290+
character: item.range.endPosition?.col.flatMap(Int.init) ?? 0
291+
)
292+
),
293+
displayText: item.completion.text
294+
)
295+
} ?? []
269296
}
297+
298+
ongoingTasks.insert(task)
270299

271-
return result.completionItems?.filter { item in
272-
if ignoreSpaceOnlySuggestions {
273-
return !item.completion.text.allSatisfy { $0.isWhitespace || $0.isNewline }
274-
}
275-
return true
276-
}.map { item in
277-
CodeSuggestion(
278-
text: item.completion.text,
279-
position: cursorPosition,
280-
uuid: item.completion.completionId,
281-
range: CursorRange(
282-
start: .init(
283-
line: item.range.startPosition?.row.flatMap(Int.init) ?? 0,
284-
character: item.range.startPosition?.col.flatMap(Int.init) ?? 0
285-
),
286-
end: .init(
287-
line: item.range.endPosition?.row.flatMap(Int.init) ?? 0,
288-
character: item.range.endPosition?.col.flatMap(Int.init) ?? 0
289-
)
290-
),
291-
displayText: item.completion.text
292-
)
293-
} ?? []
300+
return try await task.value
294301
}
295302

296303
public func cancelRequest() async {
297-
Task {
298-
try await server?.sendRequest(
299-
CodeiumRequest.CancelRequest(requestBody: .init(
300-
request_id: requestCounter,
301-
session_id: CodeiumSuggestionService.sessionId
302-
))
303-
)
304-
}
305-
cancellationCounter = requestCounter
304+
_ = try? await server?.sendRequest(
305+
CodeiumRequest.CancelRequest(requestBody: .init(
306+
request_id: requestCounter,
307+
session_id: CodeiumSuggestionService.sessionId
308+
))
309+
)
306310
}
307311

308312
public func notifyAccepted(_ suggestion: CodeSuggestion) async {

0 commit comments

Comments
 (0)