Skip to content

Commit 6a7e148

Browse files
authored
Improve default diagnostic formatter (#535)
* Improve default diagnostic formatter * Fix suggestion display ordering * Highlight output only when connected to termnial * Highlight diagnostic summary only when connected to terminal * Fix exisiting tests to use tools output formatting option * Add default diagnostic formatting tests * Relocate default diagnostic formatting test to a separate file * Remove URL force unwrapping * Refactor formatted description for diagnostic * Refactor suggestion grouping * Remove redundant ANSIAnnotation color reset * Improve suggestion vertical alignment prefix * Revert public initializer changes To make it easier to fix the formatter abstraction later, we keep the previous public initializer unchanged and add a new internal initializer that accept all 4 arguments. * Clean up source lines after writing the diagnostics Since the sourceLines could potentially be big if there were diagnostics in many large files, we remove the cached lines after the console writer finished writing the diagnostics. * Improve inline documentation * Clean up source lines cache after all diagnostics have been formatted Introduces an API to inform the formatter that all diagnostics have been formatted. * Improve C import The previous import solution was working only for Darwin platforms, it needed to be update to support more platforms. * Revert whitespace change A trailing whitespace is added in `TestTutorial.tutorial:42`, this is expected and should not have been removed. * Revert "Revert public initializer changes" This reverts commit 6e1d037.
1 parent 09ad847 commit 6a7e148

15 files changed

+795
-73
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2023 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
import Foundation
12+
13+
struct ANSIAnnotation {
14+
enum Color: UInt8 {
15+
case normal = 0
16+
case red = 31
17+
case green = 32
18+
case yellow = 33
19+
case `default` = 39
20+
}
21+
22+
enum Trait: UInt8 {
23+
case normal = 0
24+
case bold = 1
25+
case italic = 3
26+
}
27+
28+
private var color: Color
29+
private var trait: Trait
30+
31+
/// The textual representation of the annotation.
32+
private var code: String {
33+
"\u{001B}[\(trait.rawValue);\(color.rawValue)m"
34+
}
35+
36+
init(color: Color, trait: Trait = .normal) {
37+
self.color = color
38+
self.trait = trait
39+
}
40+
41+
func applied(to message: String) -> String {
42+
"\(code)\(message)\(ANSIAnnotation.normal.code)"
43+
}
44+
45+
static var normal: ANSIAnnotation {
46+
self.init(color: .normal, trait: .normal)
47+
}
48+
49+
/// Annotation used for highlighting source text.
50+
static var sourceHighlight: ANSIAnnotation {
51+
ANSIAnnotation(color: .green, trait: .bold)
52+
}
53+
/// Annotation used for highlighting source suggestion.
54+
static var sourceSuggestionHighlight: ANSIAnnotation {
55+
ANSIAnnotation(color: .default, trait: .bold)
56+
}
57+
}

0 commit comments

Comments
 (0)