Skip to content

Commit 80f905b

Browse files
Add parameterLabel modifier for argument labels in semantic tokens
1 parent 4517e82 commit 80f905b

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

Sources/SourceKitLSP/SemanticTokensLegend+SourceKitLSPLegend.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,28 @@ extension SemanticTokenTypes {
2222
}
2323
}
2424

25+
extension SemanticTokenModifiers {
26+
// Argument labels are considered part of the function name, but this modifier
27+
// allows themes to style them differently if desired.
28+
package static var parameterLabel: Self { Self(rawValue: UInt32(1) << UInt32(SemanticTokenModifiers.all.count)) }
29+
30+
package static let allSourceKitLSPModifiers: [Self] = [
31+
.parameterLabel
32+
]
33+
34+
package var sourceKitLSPName: String? {
35+
switch self {
36+
case .parameterLabel: return "parameterLabel"
37+
default: return nil
38+
}
39+
}
40+
}
41+
2542
extension SemanticTokensLegend {
2643
/// The semantic tokens legend that is used between SourceKit-LSP and the editor.
2744
package static let sourceKitLSPLegend = SemanticTokensLegend(
2845
tokenTypes: SemanticTokenTypes.all.map(\.name),
2946
tokenModifiers: SemanticTokenModifiers.all.compactMap(\.name)
47+
+ SemanticTokenModifiers.allSourceKitLSPModifiers.compactMap(\.sourceKitLSPName)
3048
)
3149
}

Sources/SwiftLanguageService/SemanticTokens.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ extension SyntaxClassification {
161161
case .docLineComment, .docBlockComment:
162162
return (.comment, .documentation)
163163
case .argumentLabel:
164-
return (.function, [])
164+
return (.function, .parameterLabel)
165165
#if RESILIENT_LIBRARIES
166166
@unknown default:
167167
fatalError("Unknown case")

Sources/SwiftLanguageService/SyntaxHighlightingTokenParser.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ struct SyntaxHighlightingTokenParser {
172172
// therefore we don't use .parameter here (which LSP clients like
173173
// VSCode seem to interpret as variable identifiers, however
174174
// causing a 'wrong highlighting' e.g. of `x` in `f(x y: Int) {}`)
175-
return (.function, [.declaration])
175+
return (.function, [.declaration, .parameterLabel])
176176
case values.refVarStatic,
177177
values.refVarClass,
178178
values.refVarInstance:

Tests/SourceKitLSPTests/SemanticTokensTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ final class SemanticTokensTests: SourceKitLSPTestCase {
321321
expected: [
322322
TokenSpec(marker: "1️⃣", length: 4, kind: .keyword),
323323
TokenSpec(marker: "2️⃣", length: 1, kind: .identifier),
324-
TokenSpec(marker: "3️⃣", length: 1, kind: .function),
324+
TokenSpec(marker: "3️⃣", length: 1, kind: .function, modifiers: .parameterLabel),
325325
TokenSpec(marker: "4️⃣", length: 3, kind: .struct, modifiers: .defaultLibrary),
326326
TokenSpec(marker: "5️⃣", length: 1, kind: .identifier),
327327
TokenSpec(marker: "6️⃣", length: 6, kind: .struct, modifiers: .defaultLibrary),
@@ -844,7 +844,7 @@ final class SemanticTokensTests: SourceKitLSPTestCase {
844844
TokenSpec(marker: "2️⃣", length: 7, kind: .identifier),
845845
TokenSpec(marker: "3️⃣", length: 4, kind: .keyword),
846846
TokenSpec(marker: "4️⃣", length: 1, kind: .identifier),
847-
TokenSpec(marker: "5️⃣", length: 1, kind: .function),
847+
TokenSpec(marker: "5️⃣", length: 1, kind: .function, modifiers: .parameterLabel),
848848
TokenSpec(marker: "6️⃣", length: 7, kind: .actor),
849849
]
850850
)
@@ -859,10 +859,10 @@ final class SemanticTokensTests: SourceKitLSPTestCase {
859859
expected: [
860860
TokenSpec(marker: "1️⃣", length: 4, kind: .keyword),
861861
TokenSpec(marker: "2️⃣", length: 3, kind: .identifier),
862-
TokenSpec(marker: "3️⃣", length: 3, kind: .function),
862+
TokenSpec(marker: "3️⃣", length: 3, kind: .function, modifiers: .parameterLabel),
863863
TokenSpec(marker: "4️⃣", length: 3, kind: .struct, modifiers: .defaultLibrary),
864864
TokenSpec(marker: "5️⃣", length: 3, kind: .function),
865-
TokenSpec(marker: "6️⃣", length: 3, kind: .function),
865+
TokenSpec(marker: "6️⃣", length: 3, kind: .function, modifiers: .parameterLabel),
866866
TokenSpec(marker: "7️⃣", length: 1, kind: .number),
867867
]
868868
)
@@ -876,7 +876,7 @@ final class SemanticTokensTests: SourceKitLSPTestCase {
876876
expected: [
877877
TokenSpec(marker: "1️⃣", length: 4, kind: .keyword),
878878
TokenSpec(marker: "2️⃣", length: 3, kind: .identifier),
879-
TokenSpec(marker: "3️⃣", length: 3, kind: .function),
879+
TokenSpec(marker: "3️⃣", length: 3, kind: .function, modifiers: .parameterLabel),
880880
TokenSpec(marker: "4️⃣", length: 12, kind: .identifier),
881881
TokenSpec(marker: "5️⃣", length: 3, kind: .struct, modifiers: .defaultLibrary),
882882
]

0 commit comments

Comments
 (0)