Skip to content

Commit 359872f

Browse files
committed
Why aren't events being responded to?
1 parent 81745d8 commit 359872f

File tree

10 files changed

+68
-66
lines changed

10 files changed

+68
-66
lines changed

CodeEdit.xcodeproj/project.pbxproj

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
archiveVersion = 1;
44
classes = {
55
};
6-
objectVersion = 55;
6+
objectVersion = 60;
77
objects = {
88

99
/* Begin PBXBuildFile section */
@@ -405,6 +405,7 @@
405405
6C52466B2D1E507500F57F11 /* SemanticTokenStorageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C52466A2D1E506C00F57F11 /* SemanticTokenStorageTests.swift */; };
406406
6C52466D2D1E515700F57F11 /* SemanticToken+Position.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C52466C2D1E515700F57F11 /* SemanticToken+Position.swift */; };
407407
6C5246702D1E5CC100F57F11 /* SemanticTokenRange.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C52466F2D1E5CC100F57F11 /* SemanticTokenRange.swift */; };
408+
6C5246742D1E612700F57F11 /* CodeEditSourceEditor in Frameworks */ = {isa = PBXBuildFile; productRef = 6C5246732D1E612700F57F11 /* CodeEditSourceEditor */; };
408409
6C53AAD829A6C4FD00EE9ED6 /* SplitView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C53AAD729A6C4FD00EE9ED6 /* SplitView.swift */; };
409410
6C578D8129CD294800DC73B2 /* ExtensionActivatorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C578D8029CD294800DC73B2 /* ExtensionActivatorView.swift */; };
410411
6C578D8429CD343800DC73B2 /* ExtensionDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C578D8329CD343800DC73B2 /* ExtensionDetailView.swift */; };
@@ -450,7 +451,6 @@
450451
6C9AE6992D1DD84600FAE8D2 /* CodeEditSourceEditor in Frameworks */ = {isa = PBXBuildFile; productRef = 6C9AE6982D1DD84600FAE8D2 /* CodeEditSourceEditor */; };
451452
6C9AE69B2D1DF80300FAE8D2 /* SemanticTokenStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C9AE69A2D1DF80300FAE8D2 /* SemanticTokenStorage.swift */; };
452453
6C9AE69D2D1DF84300FAE8D2 /* ConcreteSemanticTokenStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C9AE69C2D1DF84300FAE8D2 /* ConcreteSemanticTokenStorage.swift */; };
453-
6C9AE6A32D1DFFDD00FAE8D2 /* CodeEditSourceEditor in Frameworks */ = {isa = PBXBuildFile; productRef = 6C9AE6A22D1DFFDD00FAE8D2 /* CodeEditSourceEditor */; };
454454
6CA1AE952B46950000378EAB /* EditorInstance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CA1AE942B46950000378EAB /* EditorInstance.swift */; };
455455
6CAAF68A29BC9C2300A1F48A /* (null) in Sources */ = {isa = PBXBuildFile; };
456456
6CAAF69229BCC71C00A1F48A /* (null) in Sources */ = {isa = PBXBuildFile; };
@@ -1367,7 +1367,7 @@
13671367
6C0824A12C5C0C9700A0751E /* SwiftTerm in Frameworks */,
13681368
6C81916B29B41DD300B75C92 /* DequeModule in Frameworks */,
13691369
6CB94D032CA1205100E8651C /* AsyncAlgorithms in Frameworks */,
1370-
6C9AE6A32D1DFFDD00FAE8D2 /* CodeEditSourceEditor in Frameworks */,
1370+
6C5246742D1E612700F57F11 /* CodeEditSourceEditor in Frameworks */,
13711371
);
13721372
runOnlyForDeploymentPostprocessing = 0;
13731373
};
@@ -3804,7 +3804,7 @@
38043804
6CB94D022CA1205100E8651C /* AsyncAlgorithms */,
38053805
6CC00A8A2CBEF150004E8134 /* CodeEditSourceEditor */,
38063806
6C9AE6982D1DD84600FAE8D2 /* CodeEditSourceEditor */,
3807-
6C9AE6A22D1DFFDD00FAE8D2 /* CodeEditSourceEditor */,
3807+
6C5246732D1E612700F57F11 /* CodeEditSourceEditor */,
38083808
);
38093809
productName = CodeEdit;
38103810
productReference = B658FB2C27DA9E0F00EA4DBD /* CodeEdit.app */;
@@ -3902,7 +3902,7 @@
39023902
303E88462C276FD600EEA8D9 /* XCRemoteSwiftPackageReference "LanguageServerProtocol" */,
39033903
6C4E37FA2C73E00700AEE7B5 /* XCRemoteSwiftPackageReference "SwiftTerm" */,
39043904
6CB94D012CA1205100E8651C /* XCRemoteSwiftPackageReference "swift-async-algorithms" */,
3905-
6C9AE6A12D1DFFDD00FAE8D2 /* XCRemoteSwiftPackageReference "CodeEditSourceEditor" */,
3905+
6C5246722D1E612700F57F11 /* XCLocalSwiftPackageReference "../CodeEditSourceEditor" */,
39063906
);
39073907
productRefGroup = B658FB2D27DA9E0F00EA4DBD /* Products */;
39083908
projectDirPath = "";
@@ -5697,6 +5697,13 @@
56975697
};
56985698
/* End XCConfigurationList section */
56995699

5700+
/* Begin XCLocalSwiftPackageReference section */
5701+
6C5246722D1E612700F57F11 /* XCLocalSwiftPackageReference "../CodeEditSourceEditor" */ = {
5702+
isa = XCLocalSwiftPackageReference;
5703+
relativePath = ../CodeEditSourceEditor;
5704+
};
5705+
/* End XCLocalSwiftPackageReference section */
5706+
57005707
/* Begin XCRemoteSwiftPackageReference section */
57015708
2816F592280CF50500DD548B /* XCRemoteSwiftPackageReference "CodeEditSymbols" */ = {
57025709
isa = XCRemoteSwiftPackageReference;
@@ -5818,14 +5825,6 @@
58185825
minimumVersion = 1.2.0;
58195826
};
58205827
};
5821-
6C9AE6A12D1DFFDD00FAE8D2 /* XCRemoteSwiftPackageReference "CodeEditSourceEditor" */ = {
5822-
isa = XCRemoteSwiftPackageReference;
5823-
repositoryURL = "https://github.com/CodeEditApp/CodeEditSourceEditor";
5824-
requirement = {
5825-
kind = upToNextMajorVersion;
5826-
minimumVersion = 0.9.1;
5827-
};
5828-
};
58295828
6CB94D012CA1205100E8651C /* XCRemoteSwiftPackageReference "swift-async-algorithms" */ = {
58305829
isa = XCRemoteSwiftPackageReference;
58315830
repositoryURL = "https://github.com/apple/swift-async-algorithms.git";
@@ -5881,6 +5880,10 @@
58815880
package = 6C4E37FA2C73E00700AEE7B5 /* XCRemoteSwiftPackageReference "SwiftTerm" */;
58825881
productName = SwiftTerm;
58835882
};
5883+
6C5246732D1E612700F57F11 /* CodeEditSourceEditor */ = {
5884+
isa = XCSwiftPackageProductDependency;
5885+
productName = CodeEditSourceEditor;
5886+
};
58845887
6C66C31229D05CDC00DE9ED2 /* GRDB */ = {
58855888
isa = XCSwiftPackageProductDependency;
58865889
package = 6C66C31129D05CC800DE9ED2 /* XCRemoteSwiftPackageReference "GRDB.swift" */;
@@ -5919,11 +5922,6 @@
59195922
isa = XCSwiftPackageProductDependency;
59205923
productName = CodeEditSourceEditor;
59215924
};
5922-
6C9AE6A22D1DFFDD00FAE8D2 /* CodeEditSourceEditor */ = {
5923-
isa = XCSwiftPackageProductDependency;
5924-
package = 6C9AE6A12D1DFFDD00FAE8D2 /* XCRemoteSwiftPackageReference "CodeEditSourceEditor" */;
5925-
productName = CodeEditSourceEditor;
5926-
};
59275925
6CB4463F2B6DFF3A00539ED0 /* CodeEditSourceEditor */ = {
59285926
isa = XCSwiftPackageProductDependency;
59295927
productName = CodeEditSourceEditor;

CodeEdit.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 5 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CodeEdit/Features/Editor/Views/CodeFileView.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ struct CodeFileView: View {
1919
/// The current cursor positions in the view
2020
@State private var cursorPositions: [CursorPosition] = []
2121

22+
@State private var treeSitterClient: TreeSitterClient = TreeSitterClient()
23+
2224
/// Any coordinators passed to the view.
2325
private var textViewCoordinators: [TextViewCoordinator]
2426

25-
private var highlightProviders: [any HighlightProviding]
27+
@State private var highlightProviders: [any HighlightProviding] = []
2628

2729
@AppSettings(\.textEditing.defaultTabWidth)
2830
var defaultTabWidth
@@ -58,17 +60,19 @@ struct CodeFileView: View {
5860

5961
init(codeFile: CodeFileDocument, textViewCoordinators: [TextViewCoordinator] = [], isEditable: Bool = true) {
6062
self._codeFile = .init(wrappedValue: codeFile)
63+
6164
self.textViewCoordinators = textViewCoordinators
6265
+ [codeFile.contentCoordinator]
6366
+ [codeFile.lspCoordinator].compactMap({ $0 })
64-
self.highlightProviders = [TreeSitterClient()] + [codeFile.lspHighlightProvider].compactMap({ $0 })
6567
self.isEditable = isEditable
6668

6769
if let openOptions = codeFile.openOptions {
6870
codeFile.openOptions = nil
6971
self.cursorPositions = openOptions.cursorPositions
7072
}
7173

74+
updateHighlightProviders()
75+
7276
codeFile
7377
.contentCoordinator
7478
.textUpdatePublisher
@@ -158,6 +162,9 @@ struct CodeFileView: View {
158162
.onChange(of: bracketHighlight) { _ in
159163
bracketPairHighlight = getBracketPairHighlight()
160164
}
165+
.onReceive(codeFile.$lspHighlightProvider) { provider in
166+
updateHighlightProviders(provider)
167+
}
161168
}
162169

163170
private func getBracketPairHighlight() -> BracketPairHighlight? {
@@ -178,6 +185,10 @@ struct CodeFileView: View {
178185
return .underline(color: color)
179186
}
180187
}
188+
189+
private func updateHighlightProviders(_ lspHighlightProvider: HighlightProviding? = nil) {
190+
highlightProviders = [lspHighlightProvider].compactMap({ $0 }) + [treeSitterClient]
191+
}
181192
}
182193

183194
// This extension is kept here because it should not be used elsewhere in the app and may cause confusion

CodeEdit/Features/LSP/Editor/LSPContentCoordinator.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class LSPContentCoordinator: TextViewCoordinator, TextViewDelegate {
4242
self.stream = AsyncStream { continuation in
4343
self.sequenceContinuation = continuation
4444
}
45+
46+
setUpUpdatesTask()
4547
}
4648

4749
func setUpUpdatesTask() {
@@ -50,6 +52,7 @@ class LSPContentCoordinator: TextViewCoordinator, TextViewDelegate {
5052
task = Task.detached { [weak self] in
5153
// Send edit events every 250ms
5254
for await events in stream.chunked(by: .repeating(every: .milliseconds(250), clock: .continuous)) {
55+
5356
guard !Task.isCancelled, self != nil else { return }
5457
guard !events.isEmpty, let uri = events.first?.uri else { continue }
5558
// Errors thrown here are already logged, not much else to do right now.

CodeEdit/Features/LSP/Editor/SemanticTokenHighlightProvider.swift

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ final class SemanticTokenHighlightProvider<Storage: SemanticTokenStorage>: Highl
2929
typealias EditCallback = @MainActor (Result<IndexSet, any Error>) -> Void
3030

3131
private let tokenMap: SemanticTokenMap
32+
private let documentURI: String
3233
private weak var languageServer: LanguageServer?
3334
private weak var textView: TextView?
3435

@@ -39,14 +40,18 @@ final class SemanticTokenHighlightProvider<Storage: SemanticTokenStorage>: Highl
3940
textView?.documentRange ?? .zero
4041
}
4142

42-
init(tokenMap: SemanticTokenMap, languageServer: LanguageServer) {
43+
init(tokenMap: SemanticTokenMap, languageServer: LanguageServer, documentURI: String) {
4344
self.tokenMap = tokenMap
4445
self.languageServer = languageServer
46+
self.documentURI = documentURI
4547
self.storage = Storage()
4648
}
4749

48-
func documentDidChange(documentURI: String) async throws {
49-
guard let languageServer, let textView, let lastEditCallback else { return }
50+
func documentDidChange() async throws {
51+
guard let languageServer, let textView else {
52+
return
53+
}
54+
print("Doc did change")
5055

5156
// The document was updated. Update our cache and send the invalidated ranges for the editor to handle.
5257
if let lastRequestId = storage.lastRequestId {
@@ -73,6 +78,9 @@ final class SemanticTokenHighlightProvider<Storage: SemanticTokenStorage>: Highl
7378
func setUp(textView: TextView, codeLanguage: CodeLanguage) {
7479
// Send off a request to get the initial token data
7580
self.textView = textView
81+
Task {
82+
try await self.documentDidChange()
83+
}
7684
}
7785

7886
func applyEdit(textView: TextView, range: NSRange, delta: Int, completion: @escaping EditCallback) {
@@ -87,6 +95,7 @@ final class SemanticTokenHighlightProvider<Storage: SemanticTokenStorage>: Highl
8795
range: NSRange,
8896
completion: @escaping @MainActor (Result<[HighlightRange], any Error>) -> Void
8997
) {
98+
print("Querying highlights")
9099
guard let lspRange = textView.lspRangeFrom(nsRange: range) else {
91100
completion(.failure(HighlightError.lspRangeFailure))
92101
return
@@ -98,18 +107,20 @@ final class SemanticTokenHighlightProvider<Storage: SemanticTokenStorage>: Highl
98107

99108
// MARK: - Apply Response
100109

101-
private func applyDeltaResponse(_ data: SemanticTokensDelta, callback: EditCallback, textView: TextView?) async {
110+
private func applyDeltaResponse(_ data: SemanticTokensDelta, callback: EditCallback?, textView: TextView?) async {
111+
print("Applying delta: \(data)")
102112
let lspRanges = storage.applyDelta(data, requestId: data.resultId)
103113
await MainActor.run {
104114
let ranges = lspRanges.compactMap { textView?.nsRangeFrom($0) }
105-
callback(.success(IndexSet(ranges: ranges)))
115+
callback?(.success(IndexSet(ranges: ranges)))
106116
}
107117
lastEditCallback = nil // Don't use this callback again.
108118
}
109119

110-
private func applyEntireResponse(_ data: SemanticTokens, callback: EditCallback) async {
120+
private func applyEntireResponse(_ data: SemanticTokens, callback: EditCallback?) async {
121+
print("Applying entire: \(data)")
111122
storage.setData(data)
112-
await callback(.success(IndexSet(integersIn: documentRange)))
123+
await callback?(.success(IndexSet(integersIn: documentRange)))
113124
lastEditCallback = nil // Don't use this callback again.
114125
}
115126

CodeEdit/Features/LSP/LanguageServer/Capabilities/LanguageServer+DocumentSync.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ extension LanguageServer {
3030
try await lspInstance.textDocumentDidOpen(DidOpenTextDocumentParams(textDocument: textDocument))
3131

3232
await updateIsolatedDocument(document)
33+
try await document.lspHighlightProvider?.documentDidChange()
3334
} catch {
3435
logger.warning("addDocument: Error \(error)")
3536
throw error
@@ -78,10 +79,11 @@ extension LanguageServer {
7879
func documentChanged(uri: String, changes: [DocumentChange]) async throws {
7980
do {
8081
logger.debug("Document updated, \(uri, privacy: .private)")
82+
guard let document = openFiles.document(for: uri) else { return }
83+
8184
switch resolveDocumentSyncKind() {
8285
case .full:
83-
guard let document = openFiles.document(for: uri),
84-
let content = await getIsolatedDocumentContent(document) else {
86+
guard let content = await getIsolatedDocumentContent(document) else {
8587
return
8688
}
8789
let changeEvent = TextDocumentContentChangeEvent(range: nil, rangeLength: nil, text: content.string)
@@ -100,6 +102,10 @@ extension LanguageServer {
100102
case .none:
101103
return
102104
}
105+
106+
// Let the semantic token provider know about the update.
107+
// Note for future: If a related LSP object need notifying about document changes, do it here.
108+
try await document.lspHighlightProvider?.documentDidChange()
103109
} catch {
104110
logger.warning("closeDocument: Error \(error)")
105111
throw error

CodeEdit/Features/LSP/LanguageServer/Capabilities/LanguageServer+SemanticTokens.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ extension LanguageServer {
1313
func requestSemanticTokens(for documentURI: String) async throws -> SemanticTokensResponse {
1414
do {
1515
logger.log("Requesting all tokens")
16-
let params = SemanticTokensParams(
17-
textDocument: TextDocumentIdentifier(uri: documentURI)
18-
)
16+
let params = SemanticTokensParams(textDocument: TextDocumentIdentifier(uri: documentURI))
1917
return try await lspInstance.semanticTokensFull(params)
2018
} catch {
2119
logger.warning("requestSemanticTokens full: Error \(error)")

CodeEdit/Features/LSP/LanguageServer/LanguageServer.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ class LanguageServer {
5656
category: "LanguageServer.\(languageId.rawValue)"
5757
)
5858
if let semanticTokensProvider = serverCapabilities.semanticTokensProvider {
59-
logger.log("Setting up semantic tokens")
6059
self.highlightMap = SemanticTokenMap(semanticCapability: semanticTokensProvider)
6160
} else {
6261
self.highlightMap = nil // Server doesn't support semantic highlights

CodeEdit/Features/LSP/LanguageServer/LanguageServerFileMap.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class LanguageServerFileMap {
4141
)
4242

4343
if let tokenMap = server.highlightMap {
44-
docData.semanticHighlighter = .init(tokenMap: tokenMap, languageServer: server)
44+
docData.semanticHighlighter = .init(tokenMap: tokenMap, languageServer: server, documentURI: uri)
4545
}
4646

4747
trackedDocumentData[uri] = docData

0 commit comments

Comments
 (0)