Skip to content

Commit

Permalink
Don't render just spaces in a line
Browse files Browse the repository at this point in the history
Motivation:

If a blank line is render, the text based renderer will include the
current indentation level resulting in lines with just spaces.

Modifications:

- Don't include indentation if the line to render is otherwise empty

Result:

Less trailing whitespace.
  • Loading branch information
glbrntt committed Nov 13, 2024
1 parent c3f09df commit e0b1b12
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
3 changes: 3 additions & 0 deletions Sources/GRPCCodeGen/Internal/Renderer/TextBasedRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ final class StringCodeWriter {
if nextWriteAppendsToLastLine && !lines.isEmpty {
let existingLine = lines.removeLast()
newLine = existingLine + line
} else if line.isEmpty {
// Skip indentation to avoid trailing whitespace on blank lines.
newLine = line
} else {
let indentation = Array(repeating: " ", count: self.indentation * level).joined()
newLine = indentation + line
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
public struct NamespaceA_ServiceA_Client: NamespaceA_ServiceA.ClientProtocol {
private let client: GRPCCore.GRPCClient
public init(wrapping client: GRPCCore.GRPCClient) {
self.client = client
}
/// Documentation for MethodA
public func methodA<R>(
request: GRPCCore.ClientRequest<NamespaceA_ServiceARequest>,
Expand Down Expand Up @@ -205,11 +205,11 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
public struct NamespaceA_ServiceA_Client: NamespaceA_ServiceA.ClientProtocol {
private let client: GRPCCore.GRPCClient
public init(wrapping client: GRPCCore.GRPCClient) {
self.client = client
}
/// Documentation for MethodA
public func methodA<R>(
request: GRPCCore.StreamingClientRequest<NamespaceA_ServiceARequest>,
Expand Down Expand Up @@ -308,11 +308,11 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
public struct NamespaceA_ServiceA_Client: NamespaceA_ServiceA.ClientProtocol {
private let client: GRPCCore.GRPCClient
public init(wrapping client: GRPCCore.GRPCClient) {
self.client = client
}
/// Documentation for MethodA
public func methodA<R>(
request: GRPCCore.ClientRequest<NamespaceA_ServiceARequest>,
Expand Down Expand Up @@ -409,11 +409,11 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
public struct NamespaceA_ServiceA_Client: NamespaceA_ServiceA.ClientProtocol {
private let client: GRPCCore.GRPCClient
public init(wrapping client: GRPCCore.GRPCClient) {
self.client = client
}
/// Documentation for MethodA
public func methodA<R>(
request: GRPCCore.StreamingClientRequest<NamespaceA_ServiceARequest>,
Expand Down Expand Up @@ -477,7 +477,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
options: GRPCCore.CallOptions,
_ body: @Sendable @escaping (GRPCCore.ClientResponse<NamespaceA_ServiceAResponse>) async throws -> R
) async throws -> R where R: Sendable
/// Documentation for MethodB
func methodB<R>(
request: GRPCCore.ClientRequest<NamespaceA_ServiceARequest>,
Expand All @@ -504,7 +504,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
body
)
}
package func methodB<R>(
request: GRPCCore.ClientRequest<NamespaceA_ServiceARequest>,
options: GRPCCore.CallOptions = .defaults,
Expand Down Expand Up @@ -540,7 +540,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
handleResponse
)
}
/// Documentation for MethodB
package func methodB<Result>(
_ message: NamespaceA_ServiceARequest,
Expand All @@ -563,11 +563,11 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
package struct NamespaceA_ServiceA_Client: NamespaceA_ServiceA.ClientProtocol {
private let client: GRPCCore.GRPCClient
package init(wrapping client: GRPCCore.GRPCClient) {
self.client = client
}
/// Documentation for MethodA
package func methodA<R>(
request: GRPCCore.StreamingClientRequest<NamespaceA_ServiceARequest>,
Expand All @@ -587,7 +587,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
handler: body
)
}
/// Documentation for MethodB
package func methodB<R>(
request: GRPCCore.ClientRequest<NamespaceA_ServiceARequest>,
Expand Down Expand Up @@ -688,11 +688,11 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
internal struct ServiceA_Client: ServiceA.ClientProtocol {
private let client: GRPCCore.GRPCClient
internal init(wrapping client: GRPCCore.GRPCClient) {
self.client = client
}
/// Documentation for MethodA
internal func methodA<R>(
request: GRPCCore.ClientRequest<ServiceARequest>,
Expand Down Expand Up @@ -758,7 +758,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
public struct NamespaceA_ServiceA_Client: NamespaceA_ServiceA.ClientProtocol {
private let client: GRPCCore.GRPCClient
public init(wrapping client: GRPCCore.GRPCClient) {
self.client = client
}
Expand All @@ -780,7 +780,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
public struct ServiceB_Client: ServiceB.ClientProtocol {
private let client: GRPCCore.GRPCClient
public init(wrapping client: GRPCCore.GRPCClient) {
self.client = client
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ final class ServerCodeTranslatorSnippetBasedTests: XCTestCase {
request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
context: GRPCCore.ServerContext
) async throws -> GRPCCore.StreamingServerResponse<NamespaceA_ServiceAResponse>
/// Documentation for outputStreamingMethod
func outputStreaming(
request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
Expand Down Expand Up @@ -452,7 +452,7 @@ final class ServerCodeTranslatorSnippetBasedTests: XCTestCase {
request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
context: GRPCCore.ServerContext
) async throws -> GRPCCore.ServerResponse<NamespaceA_ServiceAResponse>
/// Documentation for outputStreamingMethod
func outputStreaming(
request: GRPCCore.ServerRequest<NamespaceA_ServiceARequest>,
Expand All @@ -472,7 +472,7 @@ final class ServerCodeTranslatorSnippetBasedTests: XCTestCase {
)
return GRPCCore.StreamingServerResponse(single: response)
}
internal func outputStreaming(
request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
context: GRPCCore.ServerContext
Expand Down

0 comments on commit e0b1b12

Please sign in to comment.