Skip to content

Commit d60457d

Browse files
committed
Allow specifying a custom position for diagnostics
1 parent cec860e commit d60457d

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

Sources/SwiftDiagnostics/Diagnostic.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,21 @@ public struct Diagnostic: CustomDebugStringConvertible {
1919
/// The node at whose start location the message should be displayed.
2020
public let node: Syntax
2121

22+
/// The position at which the location should be anchored.
23+
/// By default, this is the start location of `node`.
24+
public let position: AbsolutePosition
25+
2226
/// Nodes that should be highlighted in the source code.
2327
public let highlights: [Syntax]
2428

2529
/// Fix-Its that can be applied to resolve this diagnostic.
2630
/// Each Fix-It offers a different way to resolve the diagnostic. Usually, there's only one.
2731
public let fixIts: [FixIt]
2832

29-
public init(node: Syntax, message: DiagnosticMessage, highlights: [Syntax] = [], fixIts: [FixIt] = []) {
30-
self.diagMessage = message
33+
public init(node: Syntax, position: AbsolutePosition? = nil, message: DiagnosticMessage, highlights: [Syntax] = [], fixIts: [FixIt] = []) {
3134
self.node = node
35+
self.position = position ?? node.positionAfterSkippingLeadingTrivia
36+
self.diagMessage = message
3237
self.highlights = highlights
3338
self.fixIts = fixIts
3439
}
@@ -46,7 +51,7 @@ public struct Diagnostic: CustomDebugStringConvertible {
4651

4752
/// The location at which the diagnostic should be displayed.
4853
public func location(converter: SourceLocationConverter) -> SourceLocation {
49-
return node.startLocation(converter: converter)
54+
return converter.location(for: position)
5055
}
5156

5257
public var debugDescription: String {

Sources/SwiftParser/Diagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
6767
// MARK: - Private helper functions
6868

6969
/// Produce a diagnostic.
70-
private func addDiagnostic<T: SyntaxProtocol>(_ node: T, _ message: DiagnosticMessage, highlights: [Syntax] = [], fixIts: [FixIt] = [], handledNodes: [SyntaxIdentifier] = []) {
70+
private func addDiagnostic<T: SyntaxProtocol>(_ node: T, position: AbsolutePosition? = nil, _ message: DiagnosticMessage, highlights: [Syntax] = [], fixIts: [FixIt] = [], handledNodes: [SyntaxIdentifier] = []) {
7171
diagnostics.removeAll(where: { handledNodes.contains($0.node.id) })
72-
diagnostics.append(Diagnostic(node: Syntax(node), message: message, highlights: highlights, fixIts: fixIts))
72+
diagnostics.append(Diagnostic(node: Syntax(node), position: position, message: message, highlights: highlights, fixIts: fixIts))
7373
self.handledNodes.append(contentsOf: handledNodes)
7474
}
7575

7676
/// Produce a diagnostic.
77-
private func addDiagnostic<T: SyntaxProtocol>(_ node: T, _ message: StaticParserError, highlights: [Syntax] = [], fixIts: [FixIt] = [], handledNodes: [SyntaxIdentifier] = []) {
78-
addDiagnostic(node, message as DiagnosticMessage, highlights: highlights, fixIts: fixIts, handledNodes: handledNodes)
77+
private func addDiagnostic<T: SyntaxProtocol>(_ node: T,position: AbsolutePosition? = nil, _ message: StaticParserError, highlights: [Syntax] = [], fixIts: [FixIt] = [], handledNodes: [SyntaxIdentifier] = []) {
78+
addDiagnostic(node, position: position, message as DiagnosticMessage, highlights: highlights, fixIts: fixIts, handledNodes: handledNodes)
7979
}
8080

8181
/// Whether the node should be skipped for diagnostic emission.

0 commit comments

Comments
 (0)