Skip to content

Commit e52cc41

Browse files
committed
Add syntax highlighting for parameter labels
1 parent cf49214 commit e52cc41

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

Release Notes/511.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@
7272
- Pull request: https://github.com/apple/swift-syntax/pull/2272
7373
- Migration steps: If necessary, change type annotations from the tuple to the `IncrementalParseResult` type.
7474

75+
- `SyntaxClassification` gained a new case: `argumentLabel`
76+
- The new classification case covers the first names of parameters in function-like declarations and the label of arguments in function-like calls.
77+
- Pull request: https://github.com/apple/swift-syntax/pull/2375
78+
- Migration steps: In exhaustive switches over `SyntaxClassification`, cover the new case.
79+
7580
## Template
7681

7782
- *Affected API or two word description*

Sources/SwiftIDEUtils/SyntaxClassification.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public enum SyntaxClassification {
4747
case stringLiteral
4848
/// An identifier referring to a type.
4949
case type
50+
/// The label of a function parameter or a function call argument.
51+
case argumentLabel
5052
}
5153

5254
extension SyntaxClassification {
@@ -83,6 +85,10 @@ extension SyntaxClassification {
8385
return (.keyword, false)
8486
case \SimpleTypeIdentifierSyntax.name:
8587
return (.type, false)
88+
case \FunctionParameterSyntax.firstName:
89+
return (.argumentLabel, false)
90+
case \LabeledExprSyntax.label:
91+
return (.argumentLabel, false)
8692
default:
8793
return nil
8894
}

Tests/SwiftIDEUtilsTest/ClassificationTests.swift

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ public class ClassificationTests: XCTestCase {
130130
expected: [
131131
ClassificationSpec(source: "func", kind: .keyword),
132132
ClassificationSpec(source: "foo", kind: .identifier),
133-
ClassificationSpec(source: "x", kind: .identifier),
133+
ClassificationSpec(source: "x", kind: .argumentLabel),
134134
ClassificationSpec(source: "Int", kind: .type),
135-
ClassificationSpec(source: "y", kind: .identifier),
135+
ClassificationSpec(source: "y", kind: .argumentLabel),
136136
ClassificationSpec(source: "Int", kind: .type),
137137
ClassificationSpec(source: "Int", kind: .type),
138138
ClassificationSpec(source: "return", kind: .keyword),
@@ -283,13 +283,13 @@ public class ClassificationTests: XCTestCase {
283283
"#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)",
284284
expected: [
285285
ClassificationSpec(source: "colorLiteral", kind: .identifier),
286-
ClassificationSpec(source: "red", kind: .identifier),
286+
ClassificationSpec(source: "red", kind: .argumentLabel),
287287
ClassificationSpec(source: "0", kind: .integerLiteral),
288-
ClassificationSpec(source: "green", kind: .identifier),
288+
ClassificationSpec(source: "green", kind: .argumentLabel),
289289
ClassificationSpec(source: "0", kind: .integerLiteral),
290-
ClassificationSpec(source: "blue", kind: .identifier),
290+
ClassificationSpec(source: "blue", kind: .argumentLabel),
291291
ClassificationSpec(source: "0", kind: .integerLiteral),
292-
ClassificationSpec(source: "alpha", kind: .identifier),
292+
ClassificationSpec(source: "alpha", kind: .argumentLabel),
293293
ClassificationSpec(source: "1", kind: .integerLiteral),
294294
]
295295
)
@@ -300,7 +300,7 @@ public class ClassificationTests: XCTestCase {
300300
"#imageLiteral(resourceName: \"cloud.png\")",
301301
expected: [
302302
ClassificationSpec(source: "imageLiteral", kind: .identifier),
303-
ClassificationSpec(source: "resourceName", kind: .identifier),
303+
ClassificationSpec(source: "resourceName", kind: .argumentLabel),
304304
ClassificationSpec(source: "\"cloud.png\"", kind: .stringLiteral),
305305
]
306306
)
@@ -311,7 +311,7 @@ public class ClassificationTests: XCTestCase {
311311
"#fileLiteral(resourceName: \"cloud.png\")",
312312
expected: [
313313
ClassificationSpec(source: "fileLiteral", kind: .identifier),
314-
ClassificationSpec(source: "resourceName", kind: .identifier),
314+
ClassificationSpec(source: "resourceName", kind: .argumentLabel),
315315
ClassificationSpec(source: "\"cloud.png\"", kind: .stringLiteral),
316316
]
317317
)
@@ -530,10 +530,10 @@ public class ClassificationTests: XCTestCase {
530530
ClassificationSpec(source: "keywordInCaseAndLocalArgLabel", kind: .identifier),
531531
ClassificationSpec(source: "for", kind: .identifier),
532532
ClassificationSpec(source: "Int", kind: .type),
533-
ClassificationSpec(source: "for", kind: .identifier),
533+
ClassificationSpec(source: "for", kind: .argumentLabel),
534534
ClassificationSpec(source: "in", kind: .identifier),
535535
ClassificationSpec(source: "Int", kind: .type),
536-
ClassificationSpec(source: "class", kind: .identifier),
536+
ClassificationSpec(source: "class", kind: .argumentLabel),
537537
ClassificationSpec(source: "Int", kind: .type),
538538
]
539539
)
@@ -577,4 +577,29 @@ public class ClassificationTests: XCTestCase {
577577
]
578578
)
579579
}
580+
581+
public func testargumentLabel() {
582+
assertClassification(
583+
"""
584+
func foo(arg: Int) {}
585+
""",
586+
expected: [
587+
ClassificationSpec(source: "func", kind: .keyword),
588+
ClassificationSpec(source: "foo", kind: .identifier),
589+
ClassificationSpec(source: "arg", kind: .argumentLabel),
590+
ClassificationSpec(source: "Int", kind: .type),
591+
]
592+
)
593+
594+
assertClassification(
595+
"""
596+
foo(arg: 1)
597+
""",
598+
expected: [
599+
ClassificationSpec(source: "foo", kind: .identifier),
600+
ClassificationSpec(source: "arg", kind: .argumentLabel),
601+
ClassificationSpec(source: "1", kind: .integerLiteral),
602+
]
603+
)
604+
}
580605
}

0 commit comments

Comments
 (0)