Skip to content

Commit a3fbc2d

Browse files
committed
Apply PR suggestions regarding semantic tokens
- Use edit-provided snapshot again in changeDocument - Add omittingEmptyRanges to splitToSingleLineRanges Also optimize for the single-line case - Fix typo in doc comment for DocumentManager.edit
1 parent 34e6fb1 commit a3fbc2d

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

Sources/SourceKitLSP/DocumentManager.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ extension DocumentManager {
270270
}
271271
}
272272

273-
/// Convenience wrapper for `edit(_:newVersion:edits:beforeCallback:)` that logs on failure.
273+
/// Convenience wrapper for `edit(_:newVersion:edits:beforeCallback:afterCallback:)`
274+
/// that logs on failure.
274275
@discardableResult
275276
func edit(
276277
_ note: DidChangeTextDocumentNotification,

Sources/SourceKitLSP/Swift/SwiftLanguageServer.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,10 +513,9 @@ extension SwiftLanguageServer {
513513
let keys = self.keys
514514

515515
self.queue.async {
516-
let uri = note.textDocument.uri
517516
var lastResponse: SKDResponseDictionary? = nil
518517

519-
self.documentManager.edit(note) { (before: DocumentSnapshot, edit: TextDocumentContentChangeEvent) in
518+
let snapshot = self.documentManager.edit(note) { (before: DocumentSnapshot, edit: TextDocumentContentChangeEvent) in
520519
let req = SKDRequestDictionary(sourcekitd: self.sourcekitd)
521520
req[keys.request] = self.requests.editor_replacetext
522521
req[keys.name] = note.textDocument.uri.pseudoPath
@@ -545,7 +544,7 @@ extension SwiftLanguageServer {
545544
}
546545
}
547546

548-
if let dict = lastResponse, let snapshot = self.documentManager.latestSnapshot(uri) {
547+
if let dict = lastResponse, let snapshot = snapshot {
549548
let compileCommand = self.commandsByFile[note.textDocument.uri]
550549
self.publishDiagnostics(response: dict, for: snapshot, compileCommand: compileCommand)
551550
}

Sources/SourceKitLSP/Swift/SyntaxHighlightingToken.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,21 @@ extension Array where Element == SyntaxHighlightingToken {
235235

236236
extension Range where Bound == Position {
237237
/// Splits a potentially multi-line range to multiple single-line ranges.
238-
fileprivate func splitToSingleLineRanges(in snapshot: DocumentSnapshot) -> [Self] {
238+
fileprivate func splitToSingleLineRanges(
239+
in snapshot: DocumentSnapshot,
240+
omittingEmptyRanges: Bool = true
241+
) -> [Self] {
242+
if lowerBound.line == upperBound.line {
243+
return omittingEmptyRanges && isEmpty ? [] : [self]
244+
}
245+
239246
guard let startIndex = snapshot.index(of: lowerBound),
240247
let endIndex = snapshot.index(of: upperBound) else {
241248
fatalError("Range \(self) reaches outside of the document")
242249
}
243250

244251
let text = snapshot.text[startIndex..<endIndex]
245-
let lines = text.split(separator: "\n")
252+
let lines = text.split(separator: "\n", omittingEmptySubsequences: omittingEmptyRanges)
246253

247254
return lines
248255
.enumerated()

0 commit comments

Comments
 (0)