Skip to content

Commit d4f304e

Browse files
committed
Uppercase first letter of diagnostics
sourcekitd returns diagnostics with the first letter lowercase. Xcode, for example, shows the messages with the first letter uppercases. I think that looks nicer and we should also uppercase the first letter in sourcekit-lsp.
1 parent e0173c8 commit d4f304e

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

Sources/SourceKitLSP/Swift/Diagnostic.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,19 @@ extension TextEdit {
125125
}
126126
}
127127

128+
fileprivate extension String {
129+
/// Returns this string with the first letter uppercased.
130+
///
131+
/// If the string does not start with a letter, no change is made to it.
132+
func withFirstLetterUppercased() -> String {
133+
if let firstLetter = self.first {
134+
return firstLetter.uppercased() + self.dropFirst()
135+
} else {
136+
return self
137+
}
138+
}
139+
}
140+
128141
extension Diagnostic {
129142

130143
/// Creates a diagnostic from a sourcekitd response dictionary.
@@ -138,7 +151,7 @@ extension Diagnostic {
138151
let keys = diag.sourcekitd.keys
139152
let values = diag.sourcekitd.values
140153

141-
guard let message: String = diag[keys.description] else { return nil }
154+
guard let message: String = diag[keys.description]?.withFirstLetterUppercased() else { return nil }
142155

143156
var range: Range<Position>? = nil
144157
if let line: Int = diag[keys.line],
@@ -309,7 +322,7 @@ extension DiagnosticRelatedInformation {
309322
return nil
310323
}
311324

312-
guard let message: String = diag[keys.description] else { return nil }
325+
guard let message: String = diag[keys.description]?.withFirstLetterUppercased() else { return nil }
313326

314327
var actions: [CodeAction]? = nil
315328
if let skfixits: SKDResponseArray = diag[keys.fixits],

Tests/SourceKitLSPTests/LocalSwiftTests.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ final class LocalSwiftTests: XCTestCase {
447447
XCTAssertEqual(
448448
fixit,
449449
CodeAction(
450-
title: "chain the optional using '?' to access member 'bigEndian' only for non-'nil' base values",
450+
title: "Chain the optional using '?' to access member 'bigEndian' only for non-'nil' base values",
451451
kind: .quickFix,
452452
diagnostics: nil,
453453
edit: WorkspaceEdit(changes: [uri: [expectedTextEdit]], documentChanges: nil),
@@ -469,7 +469,7 @@ final class LocalSwiftTests: XCTestCase {
469469
XCTAssertEqual(
470470
fixit,
471471
CodeAction(
472-
title: "force-unwrap using '!' to abort execution if the optional value contains 'nil'",
472+
title: "Force-unwrap using '!' to abort execution if the optional value contains 'nil'",
473473
kind: .quickFix,
474474
diagnostics: nil,
475475
edit: WorkspaceEdit(changes: [uri: [expectedTextEdit]], documentChanges: nil),
@@ -627,19 +627,19 @@ final class LocalSwiftTests: XCTestCase {
627627

628628
for fixit in quickFixes {
629629
if fixit.title.contains("!") {
630-
XCTAssert(fixit.title.starts(with: "force-unwrap using '!'"))
630+
XCTAssert(fixit.title.starts(with: "Force-unwrap using '!'"))
631631
expectedTextEdit.newText = "!"
632632
XCTAssertEqual(fixit.edit, WorkspaceEdit(changes: [uri: [expectedTextEdit]], documentChanges: nil))
633633
} else {
634-
XCTAssert(fixit.title.starts(with: "chain the optional using '?'"))
634+
XCTAssert(fixit.title.starts(with: "Chain the optional using '?'"))
635635
expectedTextEdit.newText = "?"
636636
XCTAssertEqual(fixit.edit, WorkspaceEdit(changes: [uri: [expectedTextEdit]], documentChanges: nil))
637637
}
638638
XCTAssertEqual(fixit.kind, .quickFix)
639639
XCTAssertEqual(fixit.diagnostics?.count, 1)
640640
XCTAssertEqual(fixit.diagnostics?.first?.severity, .error)
641641
XCTAssertEqual(fixit.diagnostics?.first?.range, Range(Position(line: 1, utf16index: 6)))
642-
XCTAssert(fixit.diagnostics?.first?.message.starts(with: "value of optional type") == true)
642+
XCTAssert(fixit.diagnostics?.first?.message.starts(with: "Value of optional type") == true)
643643
}
644644
}
645645

@@ -729,7 +729,7 @@ final class LocalSwiftTests: XCTestCase {
729729
XCTAssertEqual(quickFixes.count, 1)
730730
guard let fixit = quickFixes.first else { return }
731731

732-
XCTAssertEqual(fixit.title, "use 'new(_:hotness:)' instead")
732+
XCTAssertEqual(fixit.title, "Use 'new(_:hotness:)' instead")
733733
XCTAssertEqual(fixit.diagnostics?.count, 1)
734734
XCTAssert(fixit.diagnostics?.first?.message.contains("is deprecated") == true)
735735
XCTAssertEqual(
@@ -1627,7 +1627,7 @@ final class LocalSwiftTests: XCTestCase {
16271627

16281628
let diagnostic = try await testClient.nextDiagnosticsNotification()
16291629
let diag = try XCTUnwrap(diagnostic.diagnostics.first)
1630-
XCTAssertEqual(diag.message, "cannot find 'bar' in scope")
1630+
XCTAssertEqual(diag.message, "Cannot find 'bar' in scope")
16311631

16321632
// Ensure that we don't get a second `PublishDiagnosticsNotification`
16331633
await assertThrowsError(try await testClient.nextDiagnosticsNotification(timeout: 2))

Tests/SourceKitLSPTests/PullDiagnosticsTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ final class PullDiagnosticsTests: XCTestCase {
101101
// toolchains that don't contain the change yet.
102102
XCTAssert(
103103
[
104-
"add stubs for conformance",
105-
"do you want to add protocol stubs?",
104+
"Add stubs for conformance",
105+
"Do you want to add protocol stubs?",
106106
].contains(action.title)
107107
)
108108
}

Tests/SourceKitLSPTests/SourceKitTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ final class SKTests: XCTestCase {
287287
// FIXME: The error message for the missing module is misleading on Darwin
288288
// https://github.com/apple/swift-package-manager/issues/5925
289289
XCTAssert(
290-
diagnostic.message.contains("could not build Objective-C module")
291-
|| diagnostic.message.contains("no such module"),
290+
diagnostic.message.contains("Could not build Objective-C module")
291+
|| diagnostic.message.contains("No such module"),
292292
"expected module import error but found \"\(diagnostic.message)\""
293293
)
294294
}

0 commit comments

Comments
 (0)