Skip to content

Commit 4804c7c

Browse files
author
Richard Howell
committed
add register and unregister requests and handle updates
1 parent 6313416 commit 4804c7c

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

Sources/SKCore/BuildServerBuildSystem.swift

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import SKSupport
1616
import Foundation
1717
import BuildServerProtocol
1818

19+
typealias Notification = LanguageServerProtocol.Notification
20+
1921
/// A `BuildSystem` based on communicating with a build server
2022
///
2123
/// Provides build settings from a build server launched based on a
@@ -25,19 +27,24 @@ public final class BuildServerBuildSystem {
2527
let projectRoot: AbsolutePath
2628
let buildFolder: AbsolutePath?
2729
let serverConfig: BuildServerConfig
30+
let requestQueue: DispatchQueue
2831

2932
var handler: BuildServerHandler?
3033
var buildServer: Connection?
3134
public private(set) var indexStorePath: AbsolutePath?
3235

3336
/// Delegate to handle any build system events.
34-
public weak var delegate: BuildSystemDelegate? = nil
37+
public weak var delegate: BuildSystemDelegate? {
38+
get { return self.handler?.delegate }
39+
set { self.handler?.delegate = newValue }
40+
}
3541

3642
public init(projectRoot: AbsolutePath, buildFolder: AbsolutePath?, fileSystem: FileSystem = localFileSystem) throws {
3743
let configPath = projectRoot.appending(component: "buildServer.json")
3844
let config = try loadBuildServerConfig(path: configPath, fileSystem: fileSystem)
3945
self.buildFolder = buildFolder
4046
self.projectRoot = projectRoot
47+
self.requestQueue = DispatchQueue(label: "build_server_request_queue")
4148
self.serverConfig = config
4249
try self.initializeBuildServer()
4350
}
@@ -114,24 +121,43 @@ private func readReponseDataKey(data: LSPAny?, key: String) -> String? {
114121
}
115122

116123
final class BuildServerHandler: LanguageServerEndpoint {
117-
override func _registerBuiltinHandlers() { }
124+
125+
public weak var delegate: BuildSystemDelegate? = nil
126+
127+
override func _registerBuiltinHandlers() {
128+
_register(BuildServerHandler.handleFileOptionsChanged)
129+
}
130+
131+
func handleFileOptionsChanged(_ notification: Notification<FileOptionsChangedNotification>) {
132+
// TODO: add delegate method to include the changed settings directly
133+
self.delegate?.fileBuildSettingsChanged([notification.params.uri])
134+
}
118135
}
119136

120137
extension BuildServerBuildSystem: BuildSystem {
121138

122139
/// Register the given file for build-system level change notifications, such as command
123140
/// line flag changes, dependency changes, etc.
124141
public func registerForChangeNotifications(for url: LanguageServerProtocol.URL) {
125-
// TODO: Implement via BSP extensions.
142+
let request = RegisterForChanges(uri: url, action: .register)
143+
_ = self.buildServer?.send(request, queue: requestQueue, reply: { result in
144+
if let error = result.failure {
145+
log("error registering \(url): \(error)", level: .error)
146+
}
147+
})
126148
}
127149

128150
/// Unregister the given file for build-system level change notifications, such as command
129151
/// line flag changes, dependency changes, etc.
130152
public func unregisterForChangeNotifications(for url: LanguageServerProtocol.URL) {
131-
// TODO: Implement via BSP extensions.
153+
let request = RegisterForChanges(uri: url, action: .unregister)
154+
_ = self.buildServer?.send(request, queue: requestQueue, reply: { result in
155+
if let error = result.failure {
156+
log("error unregistering \(url): \(error)", level: .error)
157+
}
158+
})
132159
}
133160

134-
135161
public var indexDatabasePath: AbsolutePath? {
136162
return buildFolder?.appending(components: "index", "db")
137163
}

0 commit comments

Comments
 (0)