Skip to content

Commit 32f429f

Browse files
committed
Support open document notification
1 parent 9317f66 commit 32f429f

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

Core/Sources/CopilotService/CopilotService.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import LanguageClient
44
import LanguageServerProtocol
55
import Preferences
66
import XPCShared
7+
import Logger
78

89
public protocol CopilotAuthServiceType {
910
func checkStatus() async throws -> CopilotStatus
@@ -25,10 +26,12 @@ public protocol CopilotSuggestionServiceType {
2526
) async throws -> [CopilotCompletion]
2627
func notifyAccepted(_ completion: CopilotCompletion) async
2728
func notifyRejected(_ completions: [CopilotCompletion]) async
29+
func openTextDocument(fileURL: URL, content: String) async throws
2830
}
2931

3032
protocol CopilotLSP {
3133
func sendRequest<E: CopilotRequestType>(_ endpoint: E) async throws -> E.Response
34+
func sendNotification(_ notif: ClientNotification) async throws
3235
}
3336

3437
public class CopilotBaseService {
@@ -57,7 +60,7 @@ public class CopilotBaseService {
5760
}
5861
let executionParams: Process.ExecutionParameters
5962
let runner = UserDefaults.shared.value(for: \.runNodeWith)
60-
63+
6164
switch runner {
6265
case .bash:
6366
let nodePath = UserDefaults.shared.value(for: \.nodePath)
@@ -248,6 +251,27 @@ public final class CopilotSuggestionService: CopilotBaseService, CopilotSuggesti
248251
CopilotRequest.NotifyRejected(completionUUIDs: completions.map(\.uuid))
249252
)
250253
}
254+
255+
public func openTextDocument(
256+
fileURL: URL,
257+
content: String
258+
) async throws {
259+
let languageId = languageIdentifierFromFileURL(fileURL)
260+
let uri = "file://\(fileURL.path)"
261+
Logger.service.debug(uri)
262+
try await server.sendNotification(
263+
.didOpenTextDocument(
264+
DidOpenTextDocumentParams(
265+
textDocument: .init(
266+
uri: uri,
267+
languageId: languageId.rawValue,
268+
version: 0,
269+
text: content
270+
)
271+
)
272+
)
273+
)
274+
}
251275
}
252276

253277
extension InitializingServer: CopilotLSP {

Core/Sources/Service/Workspace.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Foundation
66
import Preferences
77
import SuggestionInjector
88
import XPCShared
9+
import Logger
910

1011
@ServiceActor
1112
final class Filespace {
@@ -39,7 +40,7 @@ final class Filespace {
3940
Environment.now().timeIntervalSince(lastSuggestionUpdateTime) > 60 * 60 * 8
4041
}
4142

42-
init(fileURL: URL) {
43+
fileprivate init(fileURL: URL) {
4344
self.fileURL = fileURL
4445
}
4546

@@ -170,11 +171,15 @@ final class Workspace {
170171
let projectURL = try await Environment.fetchCurrentProjectRootURL(fileURL)
171172
let workspaceURL = projectURL ?? fileURL
172173
let workspace = workspaces[workspaceURL] ?? Workspace(projectRootURL: workspaceURL)
173-
let filespace = workspace.filespaces[fileURL] ?? .init(fileURL: fileURL)
174+
let existedFilespace = workspace.filespaces[fileURL]
175+
let filespace = existedFilespace ?? .init(fileURL: fileURL)
174176
if workspace.filespaces[fileURL] == nil {
175177
workspace.filespaces[fileURL] = filespace
176178
}
177179
workspaces[workspaceURL] = workspace
180+
if existedFilespace == nil {
181+
workspace.notifyOpenFile(filespace: filespace)
182+
}
178183
return (workspace, filespace)
179184
}
180185
}
@@ -295,6 +300,15 @@ extension Workspace {
295300

296301
return suggestion
297302
}
303+
304+
func notifyOpenFile(filespace: Filespace) {
305+
Task {
306+
try await copilotSuggestionService?.openTextDocument(
307+
fileURL: filespace.fileURL,
308+
content: try String(contentsOf: filespace.fileURL, encoding: .utf8)
309+
)
310+
}
311+
}
298312
}
299313

300314
extension Workspace {

0 commit comments

Comments
 (0)