Skip to content

Commit f392e2f

Browse files
committed
feat: clean up & finish all todo
1 parent 0e9c545 commit f392e2f

File tree

5 files changed

+108
-74
lines changed

5 files changed

+108
-74
lines changed

Sources/SKTestSupport/SkipUnless.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ public enum SkipUnless {
107107
await testClient.send(DocumentSemanticTokensRequest(textDocument: TextDocumentIdentifier(uri)))
108108
)
109109

110-
// TODO: This part is not directly related, should i change it to new struct?
111110
let tokens = SyntaxHighlightingTokens(lspEncodedTokens: response.data)
112111

113112
// If we don't have semantic token support in sourcekitd, the second token is an identifier based on the syntax

Sources/SourceKitLSP/Swift/SemanticTokens.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ extension SwiftLanguageService {
3535
guard let skTokens: SKDResponseArray = dict[keys.semanticTokens] else {
3636
return nil
3737
}
38-
39-
let parsedTokens = SyntaxHighlightingTokenParser(sourcekitd: sourcekitd).parseTokens(skTokens, in: snapshot)
40-
return SyntaxHighlightingTokens(members: parsedTokens)
38+
39+
return SyntaxHighlightingTokenParser(sourcekitd: sourcekitd).parseTokens(skTokens, in: snapshot)
4140
}
4241

4342
/// Computes an array of syntax highlighting tokens from the syntax tree that
@@ -67,7 +66,7 @@ extension SwiftLanguageService {
6766
.classifications(in: range)
6867
.flatMap({ $0.highlightingTokens(in: snapshot) })
6968

70-
return SyntaxHighlightingTokens(members: tokens)
69+
return SyntaxHighlightingTokens(tokens: tokens)
7170
.mergingTokens(with: semanticTokens ?? [])
7271
.sorted { $0.start < $1.start }
7372
}
@@ -130,7 +129,7 @@ extension SyntaxClassifiedRange {
130129
)
131130
}
132131

133-
return SyntaxHighlightingTokens(members: tokens)
132+
return SyntaxHighlightingTokens(tokens: tokens)
134133
}
135134
}
136135

Sources/SourceKitLSP/Swift/SyntaxHighlightingTokenParser.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,13 @@ struct SyntaxHighlightingTokenParser {
5252
let multiLineRange = start..<end
5353
let ranges = multiLineRange.splitToSingleLineRanges(in: snapshot)
5454

55-
tokens.members += ranges.map {
55+
tokens.tokens += ranges.map {
5656
SyntaxHighlightingToken(
5757
range: $0,
5858
kind: kind,
5959
modifiers: modifiers
6060
)
61+
}
6162
}
6263
}
6364

@@ -78,7 +79,7 @@ struct SyntaxHighlightingTokenParser {
7879
}
7980

8081
func parseTokens(_ response: SKDResponseArray, in snapshot: DocumentSnapshot) -> SyntaxHighlightingTokens {
81-
var tokens: SyntaxHighlightingTokens = SyntaxHighlightingTokens(members: [])
82+
var tokens: SyntaxHighlightingTokens = SyntaxHighlightingTokens(tokens: [])
8283
parseTokens(response, in: snapshot, into: &tokens)
8384
return tokens
8485
}

Sources/SourceKitLSP/Swift/SyntaxHighlightingTokens.swift

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import LSPLogging
1414
import LanguageServerProtocol
1515
import SourceKitD
1616

17-
/// A ranged token in the document used for syntax highlighting.
17+
/// A wrapper around an array of syntax highlighting tokens.
1818
public struct SyntaxHighlightingTokens {
19-
public var members: [SyntaxHighlightingToken]
19+
public var tokens: [SyntaxHighlightingToken]
2020

21-
public init(members: [SyntaxHighlightingToken]) {
22-
self.members = members
21+
public init(tokens: [SyntaxHighlightingToken]) {
22+
self.tokens = tokens
2323
}
2424

2525
/// The LSP representation of syntax highlighting tokens. Note that this
@@ -29,26 +29,26 @@ public struct SyntaxHighlightingTokens {
2929
var rawTokens: [UInt32] = []
3030
rawTokens.reserveCapacity(count * 5)
3131

32-
for token in self.members {
33-
let lineDelta = token.start.line - previous.line
34-
let charDelta =
35-
token.start.utf16index - (
36-
// The character delta is relative to the previous token's start
37-
// only if the token is on the previous token's line.
38-
previous.line == token.start.line ? previous.utf16index : 0)
39-
40-
// We assert that the tokens are actually sorted
41-
assert(lineDelta >= 0)
42-
assert(charDelta >= 0)
43-
44-
previous = token.start
45-
rawTokens += [
46-
UInt32(lineDelta),
47-
UInt32(charDelta),
48-
UInt32(token.utf16length),
49-
token.kind.tokenType,
50-
token.modifiers.rawValue,
51-
]
32+
for token in self.tokens {
33+
let lineDelta = token.start.line - previous.line
34+
let charDelta =
35+
token.start.utf16index - (
36+
// The character delta is relative to the previous token's start
37+
// only if the token is on the previous token's line.
38+
previous.line == token.start.line ? previous.utf16index : 0)
39+
40+
// We assert that the tokens are actually sorted
41+
assert(lineDelta >= 0)
42+
assert(charDelta >= 0)
43+
44+
previous = token.start
45+
rawTokens += [
46+
UInt32(lineDelta),
47+
UInt32(charDelta),
48+
UInt32(token.utf16length),
49+
token.kind.tokenType,
50+
token.modifiers.rawValue,
51+
]
5252
}
5353

5454
return rawTokens
@@ -58,12 +58,49 @@ public struct SyntaxHighlightingTokens {
5858
/// preferring the given array's tokens if duplicate ranges are
5959
/// found.
6060
public func mergingTokens(with other: SyntaxHighlightingTokens) -> SyntaxHighlightingTokens {
61-
let otherRanges = Set(other.members.map(\.range))
62-
return SyntaxHighlightingTokens(members: members.filter { !otherRanges.contains($0.range) } + other.members)
61+
let otherRanges = Set(other.tokens.map(\.range))
62+
return SyntaxHighlightingTokens(tokens: tokens.filter { !otherRanges.contains($0.range) } + other.tokens)
6363
}
6464

6565
/// Sorts the tokens in this array by their start position.
6666
public func sorted(_ areInIncreasingOrder: (SyntaxHighlightingToken, SyntaxHighlightingToken) -> Bool) -> SyntaxHighlightingTokens {
67-
SyntaxHighlightingTokens(members: members.sorted(by: areInIncreasingOrder))
67+
SyntaxHighlightingTokens(tokens: tokens.sorted(by: areInIncreasingOrder))
6868
}
6969
}
70+
71+
extension SyntaxHighlightingTokens {
72+
/// Decodes the LSP representation of syntax highlighting tokens
73+
public init(lspEncodedTokens rawTokens: [UInt32]) {
74+
self.init(tokens: [])
75+
assert(rawTokens.count.isMultiple(of: 5))
76+
reserveCapacity(rawTokens.count / 5)
77+
78+
var current = Position(line: 0, utf16index: 0)
79+
80+
for i in stride(from: 0, to: rawTokens.count, by: 5) {
81+
let lineDelta = Int(rawTokens[i])
82+
let charDelta = Int(rawTokens[i + 1])
83+
let length = Int(rawTokens[i + 2])
84+
let rawKind = rawTokens[i + 3]
85+
let rawModifiers = rawTokens[i + 4]
86+
87+
current.line += lineDelta
88+
89+
if lineDelta == 0 {
90+
current.utf16index += charDelta
91+
} else {
92+
current.utf16index = charDelta
93+
}
94+
95+
let kind = SemanticTokenTypes.all[Int(rawKind)]
96+
let modifiers = SemanticTokenModifiers(rawValue: rawModifiers)
97+
98+
self.tokens += SyntaxHighlightingToken(
99+
start: current,
100+
utf16length: length,
101+
kind: kind,
102+
modifiers: modifiers
103+
)
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)