Skip to content

Commit 0aa33d1

Browse files
committed
Downgrade editor placeholder in source file from error to warning.
Fixes #526.
1 parent 61e4cf5 commit 0aa33d1

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

Sources/SwiftFormat/Parsing.swift

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,14 @@ func parseAndEmitDiagnostics(
4848
SourceLocationConverter(file: url?.path ?? "<unknown>", tree: sourceFile)
4949
for diagnostic in diagnostics {
5050
let location = diagnostic.location(converter: expectedConverter)
51-
parsingDiagnosticHandler(diagnostic, location)
51+
52+
// Downgrade editor placeholders to warnings, because it is useful to support formatting
53+
// in-progress files that contain those.
54+
if diagnostic.diagnosticID == StaticTokenError.editorPlaceholder.diagnosticID {
55+
parsingDiagnosticHandler(downgradedToWarning(diagnostic), location)
56+
} else {
57+
parsingDiagnosticHandler(diagnostic, location)
58+
}
5259
}
5360
}
5461

@@ -58,3 +65,29 @@ func parseAndEmitDiagnostics(
5865

5966
return restoringLegacyTriviaBehavior(sourceFile)
6067
}
68+
69+
// Wraps a `DiagnosticMessage` but forces its severity to be that of a warning instead of an error.
70+
struct DowngradedDiagnosticMessage: DiagnosticMessage {
71+
var originalDiagnostic: DiagnosticMessage
72+
73+
var message: String { originalDiagnostic.message }
74+
75+
var diagnosticID: SwiftDiagnostics.MessageID { originalDiagnostic.diagnosticID }
76+
77+
var severity: DiagnosticSeverity { .warning }
78+
}
79+
80+
/// Returns a new `Diagnostic` that is identical to the given diagnostic, except that its severity
81+
/// has been downgraded to a warning.
82+
func downgradedToWarning(_ diagnostic: Diagnostic) -> Diagnostic {
83+
// `Diagnostic` is immutable, so create a new one with the same values except for the
84+
// severity-downgraded message.
85+
return Diagnostic(
86+
node: diagnostic.node,
87+
position: diagnostic.position,
88+
message: DowngradedDiagnosticMessage(originalDiagnostic: diagnostic.diagMessage),
89+
highlights: diagnostic.highlights,
90+
notes: diagnostic.notes,
91+
fixIts: diagnostic.fixIts
92+
)
93+
}

0 commit comments

Comments
 (0)