Skip to content

Commit 4bbd5f6

Browse files
committed
Merge tag '0.17.0' into develop
2 parents 076d40d + 2b1a27b commit 4bbd5f6

File tree

11 files changed

+120
-84
lines changed

11 files changed

+120
-84
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 {

Core/Sources/Environment/Environment.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,7 @@ public enum Environment {
114114
public static var fetchFocusedElementURI: () async throws -> URL = {
115115
guard let xcode = ActiveApplicationMonitor.activeXcode
116116
?? ActiveApplicationMonitor.latestXcode
117-
else {
118-
throw FailedToFetchFileURLError()
119-
}
117+
else { return URL(fileURLWithPath: "/global") }
120118

121119
let application = AXUIElementCreateApplication(xcode.processIdentifier)
122120
let focusedElement = application.focusedElement

Core/Sources/OpenAIService/ChatGPTService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public actor ChatGPTService: ChatGPTServiceType {
188188
continuation.yield(content)
189189
}
190190

191-
try await Task.sleep(nanoseconds: 2_000_000)
191+
try await Task.sleep(nanoseconds: 3_500_000)
192192
}
193193

194194
continuation.finish()

Core/Sources/Service/RealtimeSuggestionController.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ public class RealtimeSuggestionController {
131131
for await notification in notificationsFromEditor {
132132
guard let self else { return }
133133
try Task.checkCancellation()
134-
await cancelInFlightTasks()
135134

136135
switch notification.name {
137136
case kAXValueChangedNotification:
137+
await cancelInFlightTasks()
138138
self.triggerPrefetchDebounced()
139139
await self.notifyEditingFileChange(editor: focusElement)
140140
case kAXSelectedTextChangedNotification:
@@ -210,7 +210,6 @@ public class RealtimeSuggestionController {
210210
let isEnabled = workspace.isSuggestionFeatureEnabled
211211
if !isEnabled { return }
212212
}
213-
214213
if Task.isCancelled { return }
215214

216215
Logger.service.info("Prefetch suggestions.")

Core/Sources/ServiceUpdateMigration/ServiceUpdateMigrator.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public struct ServiceUpdateMigrator {
2929
try migrateFromLowerThanOrEqualToVersion135()
3030
}
3131

32-
if old <= 170 {
32+
if old < 170 {
3333
try migrateFromLowerThanOrEqualToVersion170()
3434
}
3535
}
@@ -89,7 +89,6 @@ func migrateFromLowerThanOrEqualToVersion170() throws {
8989
let key = try? oldKeychain.getString("codeiumKey")
9090
{
9191
try newKeychain.set(key, key: "codeiumAuthKey")
92-
try? oldKeychain.set("", key: "codeiumKey")
9392
}
9493
}
9594

Core/Sources/SuggestionWidget/SuggestionPanelContent/CodeBlock.swift

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ struct CodeBlock: View {
4242
@ViewBuilder
4343
func vstack(@ViewBuilder content: () -> some View) -> some View {
4444
if disableLazyVStack {
45-
VStack(spacing: 4) {
45+
VStack(spacing: 2) {
4646
content()
4747
}
4848
} else {
49-
LazyVStack(spacing: 4) {
49+
LazyVStack(spacing: 2) {
5050
content()
5151
}
5252
}
@@ -78,7 +78,7 @@ struct CodeBlock: View {
7878
}
7979
}
8080
.foregroundColor(.white)
81-
.font(.system(size: 12, design: .monospaced))
81+
.font(.system(size: fontSize, design: .monospaced))
8282
.padding(.leading, 4)
8383
.padding([.trailing, .top, .bottom])
8484
}
@@ -99,3 +99,22 @@ struct CodeBlock: View {
9999
)
100100
}
101101
}
102+
103+
// MARK: - Preview
104+
105+
struct CodeBlock_Previews: PreviewProvider {
106+
static var previews: some View {
107+
CodeBlock(
108+
code: """
109+
let foo = Foo()
110+
let bar = Bar()
111+
""",
112+
language: "swift",
113+
startLineIndex: 0,
114+
colorScheme: .dark,
115+
firstLinePrecedingSpaceCount: 0,
116+
fontSize: 12
117+
)
118+
}
119+
}
120+

Core/Tests/OpenAIServiceTests/ChatGPTServiceTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ final class ChatGPTServiceTests: XCTestCase {
4141
return "\(idCounter)"
4242
}
4343
var requestBody: CompletionRequestBody?
44-
await service.changeBuildCompletionStreamAPI { _apiKey, _, _requestBody in
44+
await service.changeBuildCompletionStreamAPI { _apiKey, _, _, _requestBody in
4545
requestBody = _requestBody
4646
return MockCompletionStreamAPI_Success()
4747
}

Core/Tests/ServiceTests/Environment.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ func completion(text: String, range: CursorRange, uuid: String = "") -> CodeSugg
3434
}
3535

3636
class MockSuggestionService: GitHubCopilotSuggestionServiceType {
37+
func terminate() async {
38+
fatalError()
39+
}
40+
3741
func cancelRequest() async {
3842
fatalError()
3943
}

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ You can detach the chat panel by simply dragging it away. Once detached, the cha
179179
#### Commands
180180

181181
- Open Chat: Open a chat window.
182-
- Explain Selection: Open a chat window and explain the selected code.
183182

184183
#### Keyboard Shortcuts
185184

@@ -234,7 +233,11 @@ This feature is recommended when you need to update a specific piece of code. So
234233

235234
### Custom Commands
236235

237-
You can create custom commands that run Chat and Prompt to Code with personalized prompts. These commands are easily accessible from both the Xcode menu bar and the context menu of the circular widget.
236+
You can create custom commands that run Chat and Prompt to Code with personalized prompts. These commands are easily accessible from both the Xcode menu bar and the context menu of the circular widget. There are 3 types of custom commands:
237+
238+
- Prompt to Code: Run Prompt to Code with the selected code, and update or write the code using the given prompt, if provided. You can provide additional information through the extra system prompt field.
239+
- Open Chat: Open the chat window and immediately send a message, if provided. You can provide more information through the extra system prompt field.
240+
- Custom Chat: Open the chat window and immediately send a message, if provided. You can overwrite the entire system prompt through the system prompt field.
238241

239242
## Key Bindings
240243

Version.xcconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
APP_VERSION = 0.16.1
2-
APP_BUILD = 161
1+
APP_VERSION = 0.17.0
2+
APP_BUILD = 170

0 commit comments

Comments
 (0)