Skip to content

Commit f354102

Browse files
committed
Extract function for merging tokens
1 parent 4818ef3 commit f354102

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

Sources/SourceKitLSP/DocumentManager.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ public struct DocumentSnapshot {
2424

2525
public var text: String { lineTable.content }
2626

27-
public var allTokens: [SemanticToken] {
28-
// Only pick syntactic tokens for which no semantic token exists
29-
let semaRanges = Set(semanticTokens.map(\.range))
30-
return syntacticTokens.filter { !semaRanges.contains($0.range) } + semanticTokens
31-
}
27+
public var allTokens: [SemanticToken] { mergeSemanticTokens(syntacticTokens, semanticTokens) }
3228
public var sortedTokens: [SemanticToken] { allTokens.sorted { $0.start < $1.start } }
3329

3430
public init(

Sources/SourceKitLSP/Swift/SemanticTokens.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,14 @@ public func decodeFromIntArray(rawSemanticTokens rawTokens: [UInt32]) -> [Semant
204204
return tokens
205205
}
206206

207+
/// Merges the given token arrays into a new token array,
208+
/// preferring the second array's tokens if duplicate ranges are
209+
/// found.
210+
public func mergeSemanticTokens(_ lhs: [SemanticToken], _ rhs: [SemanticToken]) -> [SemanticToken] {
211+
let rhsRanges = Set(rhs.map(\.range))
212+
return lhs.filter { !rhsRanges.contains($0.range) } + rhs
213+
}
214+
207215
/// Parses semantic tokens from sourcekitd response dictionaries.
208216
struct SemanticTokenParser {
209217
private let sourcekitd: SourceKitD

0 commit comments

Comments
 (0)