Skip to content

Commit

Permalink
Include Source Localization in doc comments for accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
liamnichols committed May 16, 2024
1 parent 80979c4 commit 524d3a4
Show file tree
Hide file tree
Showing 10 changed files with 394 additions and 7 deletions.
19 changes: 12 additions & 7 deletions Sources/StringGenerator/StringGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1180,16 +1180,21 @@ extension Resource {
}

var leadingTrivia: Trivia {
var trivia: Trivia = .init(pieces: [])
var docComponents: [String] = []

if let commentLines = comment?.components(separatedBy: .newlines), !commentLines.isEmpty {
for line in commentLines {
trivia = trivia.appending(Trivia.docLineComment("/// \(line)"))
trivia = trivia.appending(.newline)
}
if let comment {
docComponents.append(comment)
}

return trivia
docComponents.append("""
### Source Localization
```
\(sourceLocalization)
```
""")

return Trivia(docComment: docComponents.joined(separator: "\n\n"))
}

func statements(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ extension String {

extension String.FormatSpecifiers {
/// %@ should convert to a String argument
///
/// ### Source Localization
///
/// ```
/// Test %@
/// ```
internal static func at(_ arg1: String) -> Self {
Self (
key: "at",
Expand All @@ -71,6 +77,12 @@ extension String.FormatSpecifiers {
}

/// %d should convert to an Int argument
///
/// ### Source Localization
///
/// ```
/// Test %d
/// ```
internal static func d(_ arg1: Int) -> Self {
Self (
key: "d",
Expand All @@ -84,6 +96,12 @@ extension String.FormatSpecifiers {
}

/// %lld should covert to an Int
///
/// ### Source Localization
///
/// ```
/// Test %lld
/// ```
internal static func d_length(_ arg1: Int) -> Self {
Self (
key: "d_length",
Expand All @@ -97,6 +115,12 @@ extension String.FormatSpecifiers {
}

/// %f should convert to a Double argument
///
/// ### Source Localization
///
/// ```
/// Test %f
/// ```
internal static func f(_ arg1: Double) -> Self {
Self (
key: "f",
Expand All @@ -110,6 +134,12 @@ extension String.FormatSpecifiers {
}

/// %.2f should convert to a Double argument
///
/// ### Source Localization
///
/// ```
/// Test %.2f
/// ```
internal static func f_precision(_ arg1: Double) -> Self {
Self (
key: "f_precision",
Expand All @@ -123,6 +153,12 @@ extension String.FormatSpecifiers {
}

/// %i should convert to an Int argument
///
/// ### Source Localization
///
/// ```
/// Test %i
/// ```
internal static func i(_ arg1: Int) -> Self {
Self (
key: "i",
Expand All @@ -136,6 +172,12 @@ extension String.FormatSpecifiers {
}

/// %o should convert to a UInt argument
///
/// ### Source Localization
///
/// ```
/// Test %o
/// ```
internal static func o(_ arg1: UInt) -> Self {
Self (
key: "o",
Expand All @@ -149,6 +191,12 @@ extension String.FormatSpecifiers {
}

/// % should not be converted to an argument
///
/// ### Source Localization
///
/// ```
/// Test %
/// ```
internal static var percentage: Self {
Self (
key: "percentage",
Expand All @@ -160,6 +208,12 @@ extension String.FormatSpecifiers {
}

/// %% should not be converted to an argument
///
/// ### Source Localization
///
/// ```
/// Test %%
/// ```
internal static var percentage_escaped: Self {
Self (
key: "percentage_escaped",
Expand All @@ -171,6 +225,12 @@ extension String.FormatSpecifiers {
}

/// %% should not be converted to an argument
///
/// ### Source Localization
///
/// ```
/// Test 50%% off
/// ```
internal static var percentage_escaped_space_o: Self {
Self (
key: "percentage_escaped_space_o",
Expand All @@ -182,6 +242,12 @@ extension String.FormatSpecifiers {
}

/// '% o' should not be converted to an argument
///
/// ### Source Localization
///
/// ```
/// Test 50% off
/// ```
internal static var percentage_space_o: Self {
Self (
key: "percentage_space_o",
Expand All @@ -193,6 +259,12 @@ extension String.FormatSpecifiers {
}

/// %u should convert to a UInt argument
///
/// ### Source Localization
///
/// ```
/// Test %u
/// ```
internal static func u(_ arg1: UInt) -> Self {
Self (
key: "u",
Expand All @@ -206,6 +278,12 @@ extension String.FormatSpecifiers {
}

/// %x should convert to a UInt argument
///
/// ### Source Localization
///
/// ```
/// Test %x
/// ```
internal static func x(_ arg1: UInt) -> Self {
Self (
key: "x",
Expand Down Expand Up @@ -302,66 +380,144 @@ extension LocalizedStringResource {
/// - Note: Using ``LocalizedStringResource.FormatSpecifiers`` requires iOS 16/macOS 13 or later. See ``String.FormatSpecifiers`` for an iOS 15/macOS 12 compatible API.
internal struct FormatSpecifiers {
/// %@ should convert to a String argument
///
/// ### Source Localization
///
/// ```
/// Test %@
/// ```
internal func at(_ arg1: String) -> LocalizedStringResource {
LocalizedStringResource(formatSpecifiers: .at(arg1))
}

/// %d should convert to an Int argument
///
/// ### Source Localization
///
/// ```
/// Test %d
/// ```
internal func d(_ arg1: Int) -> LocalizedStringResource {
LocalizedStringResource(formatSpecifiers: .d(arg1))
}

/// %lld should covert to an Int
///
/// ### Source Localization
///
/// ```
/// Test %lld
/// ```
internal func d_length(_ arg1: Int) -> LocalizedStringResource {
LocalizedStringResource(formatSpecifiers: .d_length(arg1))
}

/// %f should convert to a Double argument
///
/// ### Source Localization
///
/// ```
/// Test %f
/// ```
internal func f(_ arg1: Double) -> LocalizedStringResource {
LocalizedStringResource(formatSpecifiers: .f(arg1))
}

/// %.2f should convert to a Double argument
///
/// ### Source Localization
///
/// ```
/// Test %.2f
/// ```
internal func f_precision(_ arg1: Double) -> LocalizedStringResource {
LocalizedStringResource(formatSpecifiers: .f_precision(arg1))
}

/// %i should convert to an Int argument
///
/// ### Source Localization
///
/// ```
/// Test %i
/// ```
internal func i(_ arg1: Int) -> LocalizedStringResource {
LocalizedStringResource(formatSpecifiers: .i(arg1))
}

/// %o should convert to a UInt argument
///
/// ### Source Localization
///
/// ```
/// Test %o
/// ```
internal func o(_ arg1: UInt) -> LocalizedStringResource {
LocalizedStringResource(formatSpecifiers: .o(arg1))
}

/// % should not be converted to an argument
///
/// ### Source Localization
///
/// ```
/// Test %
/// ```
internal var percentage: LocalizedStringResource {
LocalizedStringResource(formatSpecifiers: .percentage)
}

/// %% should not be converted to an argument
///
/// ### Source Localization
///
/// ```
/// Test %%
/// ```
internal var percentage_escaped: LocalizedStringResource {
LocalizedStringResource(formatSpecifiers: .percentage_escaped)
}

/// %% should not be converted to an argument
///
/// ### Source Localization
///
/// ```
/// Test 50%% off
/// ```
internal var percentage_escaped_space_o: LocalizedStringResource {
LocalizedStringResource(formatSpecifiers: .percentage_escaped_space_o)
}

/// '% o' should not be converted to an argument
///
/// ### Source Localization
///
/// ```
/// Test 50% off
/// ```
internal var percentage_space_o: LocalizedStringResource {
LocalizedStringResource(formatSpecifiers: .percentage_space_o)
}

/// %u should convert to a UInt argument
///
/// ### Source Localization
///
/// ```
/// Test %u
/// ```
internal func u(_ arg1: UInt) -> LocalizedStringResource {
LocalizedStringResource(formatSpecifiers: .u(arg1))
}

/// %x should convert to a UInt argument
///
/// ### Source Localization
///
/// ```
/// Test %x
/// ```
internal func x(_ arg1: UInt) -> LocalizedStringResource {
LocalizedStringResource(formatSpecifiers: .x(arg1))
}
Expand Down
Loading

0 comments on commit 524d3a4

Please sign in to comment.