forked from swiftlang/sourcekit-lsp
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reload a file when other files within the same module or a `.swiftmod…
…ule` file has been changed When the client sends us `workspace/didChangeWatchedFiles` notification of an updated `.swift` file, we should refresh the other open files in that module since they might be referencing functions from that updated file. If a `.swiftmodule` file has been updated, we refresh all the files within the package since they might import that module. Technically, we would only need to refresh files that are in module that are downstream of the updated module but we don’t currently have that information easily available from SwiftPM. Also, usually, if the client has a file from a low-level module open, he’ll be working on that module which means that such an optimization won’t help. The real solution here is to wait for us to finish preparation (which we would exactly know when it finishes since sourcekit-lsp would schedule it) but for that we need to implement background preparation. Fixes swiftlang#620 Fixes swiftlang#1116 rdar://99329579 rdar://123971779
- Loading branch information
Showing
13 changed files
with
575 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// Debounces calls to a function/closure. If multiple calls to the closure are made, it allows aggregating the | ||
/// parameters. | ||
public actor Debouncer<Parameter> { | ||
/// How long to wait for further `scheduleCall` calls before committing to actually calling `makeCall`. | ||
private let debounceDuration: Duration | ||
|
||
/// When `scheduleCall` is called while another `scheduleCall` was waiting to commit its call, combines the parameters | ||
/// of those two calls. | ||
/// | ||
/// ### Example | ||
/// | ||
/// Two `scheduleCall` calls that are made within a time period shorter than `debounceDuration` like the following | ||
/// ```swift | ||
/// debouncer.scheduleCall(5) | ||
/// debouncer.scheduleCall(10) | ||
/// ``` | ||
/// will call `combineParameters(5, 10)` | ||
private let combineParameters: (Parameter, Parameter) -> Parameter | ||
|
||
/// After the debounce duration has elapsed, commit the call. | ||
private let makeCall: (Parameter) async -> Void | ||
|
||
/// In the time between the call to `scheduleCall` and the call actually being committed (ie. in the time that the | ||
/// call can be debounced), the task that would commit the call (unless cancelled) and the parameter with which this | ||
/// call should be made. | ||
private var inProgressData: (Parameter, Task<Void, Never>)? | ||
|
||
public init( | ||
debounceDuration: Duration, | ||
combineResults: @escaping (Parameter, Parameter) -> Parameter, | ||
_ makeCall: @escaping (Parameter) async -> Void | ||
) { | ||
self.debounceDuration = debounceDuration | ||
self.combineParameters = combineResults | ||
self.makeCall = makeCall | ||
} | ||
|
||
/// Schedule a debounced call. If `scheduleCall` is called within `debounceDuration`, the parameters of the two | ||
/// `scheduleCall` calls will be combined using `combineParameters` and the new debounced call will be scheduled | ||
/// `debounceDuration` after the second `scheduleCall` call. | ||
public func scheduleCall(_ parameter: Parameter) { | ||
var parameter = parameter | ||
if let (inProgressParameter, inProgressTask) = inProgressData { | ||
inProgressTask.cancel() | ||
parameter = combineParameters(inProgressParameter, parameter) | ||
} | ||
let task = Task { | ||
do { | ||
try await Task.sleep(for: debounceDuration) | ||
try Task.checkCancellation() | ||
} catch { | ||
return | ||
} | ||
inProgressData = nil | ||
await makeCall(parameter) | ||
} | ||
inProgressData = (parameter, task) | ||
} | ||
} | ||
|
||
extension Debouncer<Void> { | ||
public init(debounceDuration: Duration, _ makeCall: @escaping () async -> Void) { | ||
self.init(debounceDuration: debounceDuration, combineResults: { _, _ in }, makeCall) | ||
} | ||
|
||
public func scheduleCall() { | ||
self.scheduleCall(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
|
||
extension FileManager { | ||
/// Returns the URLs of all files with the given file extension in the given directory (recursively). | ||
public func findFiles(withExtension extensionName: String, in directory: URL) -> [URL] { | ||
var result: [URL] = [] | ||
let enumerator = self.enumerator(at: directory, includingPropertiesForKeys: nil) | ||
while let url = enumerator?.nextObject() as? URL { | ||
if url.pathExtension == extensionName { | ||
result.append(url) | ||
} | ||
} | ||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import SKCore | ||
import XCTest | ||
|
||
final class DebouncerTests: XCTestCase { | ||
func testDebouncerDebounces() async throws { | ||
let expectation = self.expectation(description: "makeCallCalled") | ||
expectation.assertForOverFulfill = true | ||
let debouncer = Debouncer<Void>(debounceDuration: .seconds(0.1)) { | ||
expectation.fulfill() | ||
} | ||
await debouncer.scheduleCall() | ||
await debouncer.scheduleCall() | ||
try await self.fulfillmentOfOrThrow([expectation]) | ||
// Sleep for 0.2s to make sure the debouncer actually debounces and doesn't fulfill the expectation twice. | ||
try await Task.sleep(for: .seconds(0.2)) | ||
} | ||
|
||
func testDebouncerCombinesParameters() async throws { | ||
let expectation = self.expectation(description: "makeCallCalled") | ||
expectation.assertForOverFulfill = true | ||
let debouncer = Debouncer<Int>(debounceDuration: .seconds(0.1), combineResults: { $0 + $1 }) { param in | ||
XCTAssertEqual(param, 3) | ||
expectation.fulfill() | ||
} | ||
await debouncer.scheduleCall(1) | ||
await debouncer.scheduleCall(2) | ||
try await self.fulfillmentOfOrThrow([expectation]) | ||
} | ||
} |
Oops, something went wrong.