Skip to content
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
26 changes: 23 additions & 3 deletions Sources/SwiftDiagnostics/DiagnosticsFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ public struct DiagnosticsFormatter {
var isFreeOfAnnotations: Bool {
return diagnostics.isEmpty && suffixText.isEmpty
}

/// Converts a UTF-8 column index into an index that considers each character as a single column, not each UTF-8
/// byte.
///
/// For example the 👨‍👩‍👧‍👦 character is considered as a single character, not 25 bytes.
///
/// Both the input and the output column are 1-based.
func characterColumn(ofUtf8Column utf8Column: Int) -> Int {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume you don't think this should be part of the location converter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I think it’s quite specific to this use case.

let index =
sourceString.utf8.index(
sourceString.utf8.startIndex,
offsetBy: utf8Column - 1,
limitedBy: sourceString.utf8.endIndex
) ?? sourceString.utf8.endIndex
return sourceString.distance(from: sourceString.startIndex, to: index) + 1
}
}

/// Number of lines which should be printed before and after the diagnostic message
Expand Down Expand Up @@ -139,7 +155,7 @@ public struct DiagnosticsFormatter {

let endColumn: Int
if endLine > lineNumber {
endColumn = annotatedLine.sourceString.count
endColumn = annotatedLine.sourceString.utf8.count
} else if endLine == lineNumber {
endColumn = endLoc.column
} else {
Expand Down Expand Up @@ -274,9 +290,13 @@ public struct DiagnosticsFormatter {
annotatedSource.append("\n")
}

let columnsWithDiagnostics = Set(annotatedLine.diagnostics.map { $0.location(converter: slc).column })
let columnsWithDiagnostics = Set(
annotatedLine.diagnostics.map {
annotatedLine.characterColumn(ofUtf8Column: $0.location(converter: slc).column)
}
)
let diagsPerColumn = Dictionary(grouping: annotatedLine.diagnostics) { diag in
diag.location(converter: slc).column
annotatedLine.characterColumn(ofUtf8Column: diag.location(converter: slc).column)
}.sorted { lhs, rhs in
lhs.key > rhs.key
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,37 @@ final class ParserDiagnosticsFormatterIntegrationTests: XCTestCase {

assertStringsEqualWithDiff(annotate(source: source), expectedOutput)
}

func testDontCrashIfFullLineHighlightContainsEmoji() {
let source = """
func o() {
}👨‍👩‍👧‍👦}
}
"""

let expectedOutput = """
1 │ func o() {
2 │ }👨‍👩‍👧‍👦}
│ │╰─ error: extraneous braces at top level
│ ╰─ error: consecutive statements on a line must be separated by newline or ';'
3 │ }

"""

assertStringsEqualWithDiff(annotate(source: source), expectedOutput)
}

func testEmojiInSourceCode() {
let source = """
let 👨‍👩‍👧‍👦 = ;
"""

let expectedOutput = """
1 │ let 👨‍👩‍👧‍👦 = ;
│ ╰─ error: expected expression in variable

"""

assertStringsEqualWithDiff(annotate(source: source), expectedOutput)
}
}