Skip to content

Commit 6e0e1fc

Browse files
authored
Merge pull request #1441 from ahoppen/ahoppen/highlight
If no explicit highlight range was given, highlight the node the diagnostic is anchored at instead of highlighting nothing
2 parents d7180b9 + c24786d commit 6e0e1fc

File tree

4 files changed

+8
-5
lines changed

4 files changed

+8
-5
lines changed

Sources/SwiftDiagnostics/Diagnostic.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,20 @@ public struct Diagnostic: CustomDebugStringConvertible {
3333
/// Each Fix-It offers a different way to resolve the diagnostic. Usually, there's only one.
3434
public let fixIts: [FixIt]
3535

36+
/// If `highlights` is `nil` then `node` will be highlighted. This is a
37+
/// reasonable default for almost all diagnostics.
3638
public init(
3739
node: Syntax,
3840
position: AbsolutePosition? = nil,
3941
message: DiagnosticMessage,
40-
highlights: [Syntax] = [],
42+
highlights: [Syntax]? = nil,
4143
notes: [Note] = [],
4244
fixIts: [FixIt] = []
4345
) {
4446
self.node = node
4547
self.position = position ?? node.positionAfterSkippingLeadingTrivia
4648
self.diagMessage = message
47-
self.highlights = highlights
49+
self.highlights = highlights ?? [node]
4850
self.notes = notes
4951
self.fixIts = fixIts
5052
}

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,12 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
8484
// MARK: - Private helper functions
8585

8686
/// Produce a diagnostic.
87+
/// If `highlights` is `nil` the `node` will be highlighted.
8788
func addDiagnostic<T: SyntaxProtocol>(
8889
_ node: T,
8990
position: AbsolutePosition? = nil,
9091
_ message: DiagnosticMessage,
91-
highlights: [Syntax] = [],
92+
highlights: [Syntax]? = nil,
9293
notes: [Note] = [],
9394
fixIts: [FixIt] = [],
9495
handledNodes: [SyntaxIdentifier] = []

Sources/SwiftSyntaxBuilder/ValidatingSyntaxNodes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extension SyntaxProtocol {
3232
}
3333

3434
extension Trivia {
35-
/// If `trivia` has contains no unexpected trivia, return `trivia`, otherwise
35+
/// If `trivia` contains no unexpected trivia, return `trivia`, otherwise
3636
/// throw an error with diagnostics describing the unexpected trivia.
3737
public init(validating trivia: Trivia) throws {
3838
self = trivia

Tests/SwiftParserTest/translated/RecoveryTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ final class RecoveryTests: XCTestCase {
747747
protocol Multi 1️⃣ident {}
748748
""",
749749
diagnostics: [
750-
DiagnosticSpec(message: "found an unexpected second identifier in protocol")
750+
DiagnosticSpec(message: "found an unexpected second identifier in protocol", highlight: "ident")
751751
]
752752
)
753753
}

0 commit comments

Comments
 (0)