Skip to content

Commit 5fbdd28

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
1 parent 34e6fb1 commit 5fbdd28

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

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)