Skip to content

Commit f41ed9e

Browse files
committed
Adjust lock handling, upgrade example project to release builds
1 parent 6d919a1 commit f41ed9e

File tree

5 files changed

+86
-71
lines changed

5 files changed

+86
-71
lines changed

Example/.bsp/sourcekit-bazel-bsp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
../../.build/arm64-apple-macosx/debug/sourcekit-bazel-bsp
1+
../../.build/release/sourcekit-bazel-bsp

Example/HelloWorld/TodoObjCSupport/Sources/SKDateDistanceCalculator.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,24 @@
2424
/**
2525
* Calculates the time interval between the given date and now
2626
* @param date The reference date to calculate distance from
27-
* @return Time interval in seconds (positive if date is in the past, negative if in the future)
27+
* @return Time interval in seconds (positive if date is in the past, negative
28+
* if in the future)
2829
*/
2930
+ (NSTimeInterval)distanceFromNow:(NSDate *)date;
3031

3132
/**
3233
* Calculates the distance and returns a human-readable string
3334
* @param date The reference date to calculate distance from
34-
* @return A formatted string describing the time distance (e.g., "2 days ago", "3 hours from now")
35+
* @return A formatted string describing the time distance (e.g., "2 days ago",
36+
* "3 hours from now")
3537
*/
3638
+ (NSString *)humanReadableDistanceFromNow:(NSDate *)date;
3739

3840
/**
3941
* Calculates the distance in specific units
4042
* @param date The reference date to calculate distance from
41-
* @param unit The calendar unit to measure in (e.g., NSCalendarUnitDay, NSCalendarUnitHour)
43+
* @param unit The calendar unit to measure in (e.g., NSCalendarUnitDay,
44+
* NSCalendarUnitHour)
4245
* @return The distance in the specified unit
4346
*/
4447
+ (NSInteger)distanceFromNow:(NSDate *)date inUnit:(NSCalendarUnit)unit;

Example/HelloWorld/TodoObjCSupport/Sources/SKDateDistanceCalculator.m

Lines changed: 67 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -22,72 +22,86 @@
2222
@implementation SKDateDistanceCalculator
2323

2424
+ (NSTimeInterval)distanceFromNow:(NSDate *)date {
25-
if (!date) {
26-
return 0.0;
27-
}
25+
if (!date) {
26+
return 0.0;
27+
}
2828

29-
NSDate *now = [NSDate date];
30-
return [now timeIntervalSinceDate:date];
29+
NSDate *now = [NSDate date];
30+
return [now timeIntervalSinceDate:date];
3131
}
3232

3333
+ (NSString *)humanReadableDistanceFromNow:(NSDate *)date {
34-
if (!date) {
35-
return @"Invalid date";
36-
}
34+
if (!date) {
35+
return @"Invalid date";
36+
}
3737

38-
NSTimeInterval timeInterval = [self distanceFromNow:date];
39-
BOOL isPast = timeInterval > 0;
38+
NSTimeInterval timeInterval = [self distanceFromNow:date];
39+
BOOL isPast = timeInterval > 0;
4040

41-
NSCalendar *calendar = [NSCalendar currentCalendar];
42-
NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond
43-
fromDate:isPast ? date : [NSDate date]
44-
toDate:isPast ? [NSDate date] : date
45-
options:0];
41+
NSCalendar *calendar = [NSCalendar currentCalendar];
42+
NSDateComponents *components =
43+
[calendar components:NSCalendarUnitYear | NSCalendarUnitMonth |
44+
NSCalendarUnitDay | NSCalendarUnitHour |
45+
NSCalendarUnitMinute | NSCalendarUnitSecond
46+
fromDate:isPast ? date : [NSDate date]
47+
toDate:isPast ? [NSDate date] : date
48+
options:0];
4649

47-
NSString *suffix = isPast ? @"ago" : @"from now";
50+
NSString *suffix = isPast ? @"ago" : @"from now";
4851

49-
if (components.year > 0) {
50-
return [NSString stringWithFormat:@"%ld year%@ %@", (long)components.year, components.year == 1 ? @"" : @"s", suffix];
51-
} else if (components.month > 0) {
52-
return [NSString stringWithFormat:@"%ld month%@ %@", (long)components.month, components.month == 1 ? @"" : @"s", suffix];
53-
} else if (components.day > 0) {
54-
return [NSString stringWithFormat:@"%ld day%@ %@", (long)components.day, components.day == 1 ? @"" : @"s", suffix];
55-
} else if (components.hour > 0) {
56-
return [NSString stringWithFormat:@"%ld hour%@ %@", (long)components.hour, components.hour == 1 ? @"" : @"s", suffix];
57-
} else if (components.minute > 0) {
58-
return [NSString stringWithFormat:@"%ld minute%@ %@", (long)components.minute, components.minute == 1 ? @"" : @"s", suffix];
59-
} else {
60-
return [NSString stringWithFormat:@"%ld second%@ %@", (long)components.second, components.second == 1 ? @"" : @"s", suffix];
61-
}
52+
if (components.year > 0) {
53+
return
54+
[NSString stringWithFormat:@"%ld year%@ %@", (long)components.year,
55+
components.year == 1 ? @"" : @"s", suffix];
56+
} else if (components.month > 0) {
57+
return
58+
[NSString stringWithFormat:@"%ld month%@ %@", (long)components.month,
59+
components.month == 1 ? @"" : @"s", suffix];
60+
} else if (components.day > 0) {
61+
return [NSString stringWithFormat:@"%ld day%@ %@", (long)components.day,
62+
components.day == 1 ? @"" : @"s", suffix];
63+
} else if (components.hour > 0) {
64+
return
65+
[NSString stringWithFormat:@"%ld hour%@ %@", (long)components.hour,
66+
components.hour == 1 ? @"" : @"s", suffix];
67+
} else if (components.minute > 0) {
68+
return
69+
[NSString stringWithFormat:@"%ld minute%@ %@", (long)components.minute,
70+
components.minute == 1 ? @"" : @"s", suffix];
71+
} else {
72+
return
73+
[NSString stringWithFormat:@"%ld second%@ %@", (long)components.second,
74+
components.second == 1 ? @"" : @"s", suffix];
75+
}
6276
}
6377

6478
+ (NSInteger)distanceFromNow:(NSDate *)date inUnit:(NSCalendarUnit)unit {
65-
if (!date) {
66-
return 0;
67-
}
79+
if (!date) {
80+
return 0;
81+
}
6882

69-
NSCalendar *calendar = [NSCalendar currentCalendar];
70-
NSDateComponents *components = [calendar components:unit
71-
fromDate:date
72-
toDate:[NSDate date]
73-
options:0];
83+
NSCalendar *calendar = [NSCalendar currentCalendar];
84+
NSDateComponents *components = [calendar components:unit
85+
fromDate:date
86+
toDate:[NSDate date]
87+
options:0];
7488

75-
switch (unit) {
76-
case NSCalendarUnitYear:
77-
return components.year;
78-
case NSCalendarUnitMonth:
79-
return components.month;
80-
case NSCalendarUnitDay:
81-
return components.day;
82-
case NSCalendarUnitHour:
83-
return components.hour;
84-
case NSCalendarUnitMinute:
85-
return components.minute;
86-
case NSCalendarUnitSecond:
87-
return components.second;
88-
default:
89-
return 0;
90-
}
89+
switch (unit) {
90+
case NSCalendarUnitYear:
91+
return components.year;
92+
case NSCalendarUnitMonth:
93+
return components.month;
94+
case NSCalendarUnitDay:
95+
return components.day;
96+
case NSCalendarUnitHour:
97+
return components.hour;
98+
case NSCalendarUnitMinute:
99+
return components.minute;
100+
case NSCalendarUnitSecond:
101+
return components.second;
102+
default:
103+
return 0;
104+
}
91105
}
92106

93107
@end

Example/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This is a simple **iOS** app that lets you see sourcekit-bazel-bsp in action. Th
77
- Make sure you fulfill the toolchain requirements for sourcekit-bazel-bsp, available at the main README.
88
- Install [bazelisk](https://github.com/bazelbuild/bazelisk) if you haven't already.
99
- On macOS: `brew install bazelisk`
10-
- On the parent folder, build sourcekit-bazel-bsp for debug: `swift build`
10+
- On the parent folder, build sourcekit-bazel-bsp for release: `swift build -c release`
1111
- On Cursor, open a workspace **targeting this specific folder.**
1212
- On the settings page for the Swift extension, enable `SourceKit-LSP: Background Indexing` at the **workspace level**. It **has** to be workspace settings; this specific setting is not supported at the folder level.
1313
- Reload your workspace (`Cmd+Shift+P -> Reload Window`)

Sources/SourceKitBazelBSP/Server/MessageHandler/BSPMessageHandler.swift

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,30 @@ final class BSPMessageHandler: MessageHandler {
3535

3636
// We currently use a single-threaded setup for simplicity,
3737
// but we can eventually reply asynchronously if we find a need for it.
38-
private let lock: OSAllocatedUnfairLock<State> = .init(uncheckedState: State())
38+
private let lock: OSAllocatedUnfairLock<Void> = .init()
39+
// FIXME: Can't put state into the lock for now because of recursiveness in the registration.
40+
nonisolated(unsafe) private var state: State = State()
3941

4042
init() {}
4143

4244
func register<Request: RequestType>(
4345
requestHandler: @escaping BSPRequestHandler<Request>
4446
) {
45-
lock.withLockUnchecked { state in
46-
state.requestHandlers[Request.method] = AnyRequestHandler(
47-
handler: requestHandler
48-
)
49-
}
47+
state.requestHandlers[Request.method] = AnyRequestHandler(
48+
handler: requestHandler
49+
)
5050
}
5151

5252
func register<Notification: NotificationType>(
5353
notificationHandler: @escaping BSPNotificationHandler<Notification>
5454
) {
55-
lock.withLockUnchecked { state in
56-
state.notificationHandlers[Notification.method] = AnyNotificationHandler(
57-
handler: notificationHandler
58-
)
59-
}
55+
state.notificationHandlers[Notification.method] = AnyNotificationHandler(
56+
handler: notificationHandler
57+
)
6058
}
6159

6260
func handle<Notification: NotificationType>(_ notification: Notification) {
63-
lock.withLockUnchecked { state in
61+
lock.withLockUnchecked {
6462
logger.info(
6563
"Received notification: \(Notification.method, privacy: .public)"
6664
)
@@ -78,7 +76,7 @@ final class BSPMessageHandler: MessageHandler {
7876
id: RequestID,
7977
reply: @escaping (LSPResult<Request.Response>) -> Void
8078
) {
81-
lock.withLockUnchecked { state in
79+
lock.withLockUnchecked {
8280
logger.info(
8381
"Received request: \(Request.method, privacy: .public)"
8482
)

0 commit comments

Comments
 (0)