Skip to content

[Diagnostic formatter] Add ":<line>" to filename header. #1402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Sources/SwiftDiagnostics/DiagnosticsFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,13 @@ public struct DiagnosticsFormatter {
// If there was a filename, add it first.
if let fileName = fileName {
let header = colorizeBufferOutline("===")
annotatedSource.append("\(indentString)\(header) \(fileName) \(header)\n")
let firstLine =
1
+ (annotatedSourceLines.enumerated().first { (lineIndex, sourceLine) in
!sourceLine.isFreeOfAnnotations
}?.offset ?? 0)

annotatedSource.append("\(indentString)\(header) \(fileName):\(firstLine) \(header)\n")
}

/// Keep track if a line missing char should be printed
Expand Down
15 changes: 10 additions & 5 deletions Tests/SwiftDiagnosticsTest/GroupDiagnosticsFormatterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ final class GroupedDiagnosticsFormatterTests: XCTestCase {
// Main source file.
let (mainSourceID, mainSourceMarkers) = group.addTestFile(
"""


// test
let pi = 3.14159
1️⃣#myAssert(pi == 3)
print("hello"
Expand Down Expand Up @@ -111,9 +114,11 @@ final class GroupedDiagnosticsFormatterTests: XCTestCase {
AssertStringsEqualWithDiff(
annotated,
"""
=== main.swift ===
1 │ let pi = 3.14159
2 │ #myAssert(pi == 3)
=== main.swift:5 ===
3 │ // test
4 │ let pi = 3.14159
5 │ #myAssert(pi == 3)
│ ╰─ note: in expansion of macro 'myAssert' here
╭─── #myAssert ───────────────────────────────────────────────────────
│1 │ let __a = pi
Expand All @@ -123,7 +128,7 @@ final class GroupedDiagnosticsFormatterTests: XCTestCase {
│4 │ fatalError("assertion failed: pi != 3")
│5 │ }
╰─────────────────────────────────────────────────────────────────────
3 │ print("hello"
6 │ print("hello"
│ ╰─ error: expected ')' to end function call

"""
Expand Down Expand Up @@ -180,7 +185,7 @@ final class GroupedDiagnosticsFormatterTests: XCTestCase {
AssertStringsEqualWithDiff(
annotated,
"""
=== main.swift ===
=== main.swift:2 ===
1 │ let pi = 3.14159
2 │ #myAssert(pi == 3)
│ ╰─ note: in expansion of macro 'myAssert' here
Expand Down