Skip to content

Uppercase first letter of diagnostics #784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 1, 2023
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
17 changes: 15 additions & 2 deletions Sources/SourceKitLSP/Swift/Diagnostic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ extension TextEdit {
}
}

fileprivate extension String {
/// Returns this string with the first letter uppercased.
///
/// If the string does not start with a letter, no change is made to it.
func withFirstLetterUppercased() -> String {
if let firstLetter = self.first {
return firstLetter.uppercased() + self.dropFirst()
} else {
return self
}
}
}

extension Diagnostic {

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

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

var range: Range<Position>? = nil
if let line: Int = diag[keys.line],
Expand Down Expand Up @@ -309,7 +322,7 @@ extension DiagnosticRelatedInformation {
return nil
}

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

var actions: [CodeAction]? = nil
if let skfixits: SKDResponseArray = diag[keys.fixits],
Expand Down
14 changes: 7 additions & 7 deletions Tests/SourceKitLSPTests/LocalSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ final class LocalSwiftTests: XCTestCase {
XCTAssertEqual(
fixit,
CodeAction(
title: "chain the optional using '?' to access member 'bigEndian' only for non-'nil' base values",
title: "Chain the optional using '?' to access member 'bigEndian' only for non-'nil' base values",
kind: .quickFix,
diagnostics: nil,
edit: WorkspaceEdit(changes: [uri: [expectedTextEdit]], documentChanges: nil),
Expand All @@ -469,7 +469,7 @@ final class LocalSwiftTests: XCTestCase {
XCTAssertEqual(
fixit,
CodeAction(
title: "force-unwrap using '!' to abort execution if the optional value contains 'nil'",
title: "Force-unwrap using '!' to abort execution if the optional value contains 'nil'",
kind: .quickFix,
diagnostics: nil,
edit: WorkspaceEdit(changes: [uri: [expectedTextEdit]], documentChanges: nil),
Expand Down Expand Up @@ -627,19 +627,19 @@ final class LocalSwiftTests: XCTestCase {

for fixit in quickFixes {
if fixit.title.contains("!") {
XCTAssert(fixit.title.starts(with: "force-unwrap using '!'"))
XCTAssert(fixit.title.starts(with: "Force-unwrap using '!'"))
expectedTextEdit.newText = "!"
XCTAssertEqual(fixit.edit, WorkspaceEdit(changes: [uri: [expectedTextEdit]], documentChanges: nil))
} else {
XCTAssert(fixit.title.starts(with: "chain the optional using '?'"))
XCTAssert(fixit.title.starts(with: "Chain the optional using '?'"))
expectedTextEdit.newText = "?"
XCTAssertEqual(fixit.edit, WorkspaceEdit(changes: [uri: [expectedTextEdit]], documentChanges: nil))
}
XCTAssertEqual(fixit.kind, .quickFix)
XCTAssertEqual(fixit.diagnostics?.count, 1)
XCTAssertEqual(fixit.diagnostics?.first?.severity, .error)
XCTAssertEqual(fixit.diagnostics?.first?.range, Range(Position(line: 1, utf16index: 6)))
XCTAssert(fixit.diagnostics?.first?.message.starts(with: "value of optional type") == true)
XCTAssert(fixit.diagnostics?.first?.message.starts(with: "Value of optional type") == true)
}
}

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

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

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

// Ensure that we don't get a second `PublishDiagnosticsNotification`
await assertThrowsError(try await testClient.nextDiagnosticsNotification(timeout: 2))
Expand Down
4 changes: 2 additions & 2 deletions Tests/SourceKitLSPTests/PullDiagnosticsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ final class PullDiagnosticsTests: XCTestCase {
// toolchains that don't contain the change yet.
XCTAssert(
[
"add stubs for conformance",
"do you want to add protocol stubs?",
"Add stubs for conformance",
"Do you want to add protocol stubs?",
].contains(action.title)
)
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/SourceKitLSPTests/SourceKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ final class SKTests: XCTestCase {
// FIXME: The error message for the missing module is misleading on Darwin
// https://github.com/apple/swift-package-manager/issues/5925
XCTAssert(
diagnostic.message.contains("could not build Objective-C module")
|| diagnostic.message.contains("no such module"),
diagnostic.message.contains("Could not build Objective-C module")
|| diagnostic.message.contains("No such module"),
"expected module import error but found \"\(diagnostic.message)\""
)
}
Expand Down