Skip to content

Commit 188677f

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 8cb76ba commit 188677f

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

Sources/SourceKitLSP/Swift/Diagnostic.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,19 @@ extension TextEdit {
113113
}
114114
}
115115

116+
fileprivate extension String {
117+
/// Returns this string with the first letter uppercased.
118+
///
119+
/// If the string does not start with a letter, no change is made to it.
120+
func withFirstLetterUppercased() -> String {
121+
if let firstLetter = self.first {
122+
return firstLetter.uppercased() + self.dropFirst()
123+
} else {
124+
return self
125+
}
126+
}
127+
}
128+
116129
extension Diagnostic {
117130

118131
/// Creates a diagnostic from a sourcekitd response dictionary.
@@ -124,7 +137,7 @@ extension Diagnostic {
124137
let keys = diag.sourcekitd.keys
125138
let values = diag.sourcekitd.values
126139

127-
guard let message: String = diag[keys.description] else { return nil }
140+
guard let message: String = diag[keys.description]?.withFirstLetterUppercased() else { return nil }
128141

129142
var range: Range<Position>? = nil
130143
if let line: Int = diag[keys.line],
@@ -248,7 +261,7 @@ extension DiagnosticRelatedInformation {
248261
return nil
249262
}
250263

251-
guard let message: String = diag[keys.description] else { return nil }
264+
guard let message: String = diag[keys.description]?.withFirstLetterUppercased() else { return nil }
252265

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

Tests/SourceKitLSPTests/LocalSwiftTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ final class LocalSwiftTests: XCTestCase {
535535
// Expected Fix-it: Replace `let a` with `_` because it's never used
536536
let expectedTextEdit = TextEdit(range: Position(line: 1, utf16index: 7)..<Position(line: 1, utf16index: 7), newText: "?")
537537
XCTAssertEqual(fixit, CodeAction(
538-
title: "chain the optional using '?' to access member 'bigEndian' only for non-'nil' base values",
538+
title: "Chain the optional using '?' to access member 'bigEndian' only for non-'nil' base values",
539539
kind: .quickFix,
540540
diagnostics: nil,
541541
edit: WorkspaceEdit(changes: [uri: [expectedTextEdit]], documentChanges: nil),
@@ -550,7 +550,7 @@ final class LocalSwiftTests: XCTestCase {
550550
// Expected Fix-it: Replace `let a` with `_` because it's never used
551551
let expectedTextEdit = TextEdit(range: Position(line: 1, utf16index: 7)..<Position(line: 1, utf16index: 7), newText: "!")
552552
XCTAssertEqual(fixit, CodeAction(
553-
title: "force-unwrap using '!' to abort execution if the optional value contains 'nil'",
553+
title: "Force-unwrap using '!' to abort execution if the optional value contains 'nil'",
554554
kind: .quickFix,
555555
diagnostics: nil,
556556
edit: WorkspaceEdit(changes: [uri: [expectedTextEdit]], documentChanges: nil),
@@ -691,19 +691,19 @@ final class LocalSwiftTests: XCTestCase {
691691

692692
for fixit in quickFixes {
693693
if fixit.title.contains("!") {
694-
XCTAssert(fixit.title.starts(with: "force-unwrap using '!'"))
694+
XCTAssert(fixit.title.starts(with: "Force-unwrap using '!'"))
695695
expectedTextEdit.newText = "!"
696696
XCTAssertEqual(fixit.edit, WorkspaceEdit(changes: [uri: [expectedTextEdit]], documentChanges: nil))
697697
} else {
698-
XCTAssert(fixit.title.starts(with: "chain the optional using '?'"))
698+
XCTAssert(fixit.title.starts(with: "Chain the optional using '?'"))
699699
expectedTextEdit.newText = "?"
700700
XCTAssertEqual(fixit.edit, WorkspaceEdit(changes: [uri: [expectedTextEdit]], documentChanges: nil))
701701
}
702702
XCTAssertEqual(fixit.kind, .quickFix)
703703
XCTAssertEqual(fixit.diagnostics?.count, 1)
704704
XCTAssertEqual(fixit.diagnostics?.first?.severity, .error)
705705
XCTAssertEqual(fixit.diagnostics?.first?.range, Range(Position(line: 1, utf16index: 6)))
706-
XCTAssert(fixit.diagnostics?.first?.message.starts(with: "value of optional type") == true)
706+
XCTAssert(fixit.diagnostics?.first?.message.starts(with: "Value of optional type") == true)
707707
}
708708
}
709709

@@ -788,7 +788,7 @@ final class LocalSwiftTests: XCTestCase {
788788
XCTAssertEqual(quickFixes.count, 1)
789789
guard let fixit = quickFixes.first else { return }
790790

791-
XCTAssertEqual(fixit.title, "use 'new(_:hotness:)' instead")
791+
XCTAssertEqual(fixit.title, "Use 'new(_:hotness:)' instead")
792792
XCTAssertEqual(fixit.diagnostics?.count, 1)
793793
XCTAssert(fixit.diagnostics?.first?.message.contains("is deprecated") == true)
794794
XCTAssertEqual(fixit.edit?.changes?[uri], [

Tests/SourceKitLSPTests/PullDiagnosticsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,6 @@ final class PullDiagnosticsTests: XCTestCase {
118118

119119
XCTAssertEqual(actions.count, 1)
120120
let action = try XCTUnwrap(actions.first)
121-
XCTAssertEqual(action.title, "do you want to add protocol stubs?")
121+
XCTAssertEqual(action.title, "Do you want to add protocol stubs?")
122122
}
123123
}

Tests/SourceKitLSPTests/SourceKitTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ final class SKTests: XCTestCase {
220220
// Semantic analysis: expect module import error.
221221
XCTAssertEqual(note.params.diagnostics.count, 1)
222222
if let diagnostic = note.params.diagnostics.first {
223-
XCTAssert(diagnostic.message.contains("no such module"),
223+
XCTAssert(diagnostic.message.contains("No such module"),
224224
"expected module import error but found \"\(diagnostic.message)\"")
225225
}
226226
startExpectation.fulfill()

0 commit comments

Comments
 (0)