Skip to content

Commit e0b1b12

Browse files
committed
Don't render just spaces in a line
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.
1 parent c3f09df commit e0b1b12

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

Sources/GRPCCodeGen/Internal/Renderer/TextBasedRenderer.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ final class StringCodeWriter {
7070
if nextWriteAppendsToLastLine && !lines.isEmpty {
7171
let existingLine = lines.removeLast()
7272
newLine = existingLine + line
73+
} else if line.isEmpty {
74+
// Skip indentation to avoid trailing whitespace on blank lines.
75+
newLine = line
7376
} else {
7477
let indentation = Array(repeating: " ", count: self.indentation * level).joined()
7578
newLine = indentation + line

Tests/GRPCCodeGenTests/Internal/Translator/ClientCodeTranslatorSnippetBasedTests.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
9898
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
9999
public struct NamespaceA_ServiceA_Client: NamespaceA_ServiceA.ClientProtocol {
100100
private let client: GRPCCore.GRPCClient
101-
101+
102102
public init(wrapping client: GRPCCore.GRPCClient) {
103103
self.client = client
104104
}
105-
105+
106106
/// Documentation for MethodA
107107
public func methodA<R>(
108108
request: GRPCCore.ClientRequest<NamespaceA_ServiceARequest>,
@@ -205,11 +205,11 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
205205
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
206206
public struct NamespaceA_ServiceA_Client: NamespaceA_ServiceA.ClientProtocol {
207207
private let client: GRPCCore.GRPCClient
208-
208+
209209
public init(wrapping client: GRPCCore.GRPCClient) {
210210
self.client = client
211211
}
212-
212+
213213
/// Documentation for MethodA
214214
public func methodA<R>(
215215
request: GRPCCore.StreamingClientRequest<NamespaceA_ServiceARequest>,
@@ -308,11 +308,11 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
308308
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
309309
public struct NamespaceA_ServiceA_Client: NamespaceA_ServiceA.ClientProtocol {
310310
private let client: GRPCCore.GRPCClient
311-
311+
312312
public init(wrapping client: GRPCCore.GRPCClient) {
313313
self.client = client
314314
}
315-
315+
316316
/// Documentation for MethodA
317317
public func methodA<R>(
318318
request: GRPCCore.ClientRequest<NamespaceA_ServiceARequest>,
@@ -409,11 +409,11 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
409409
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
410410
public struct NamespaceA_ServiceA_Client: NamespaceA_ServiceA.ClientProtocol {
411411
private let client: GRPCCore.GRPCClient
412-
412+
413413
public init(wrapping client: GRPCCore.GRPCClient) {
414414
self.client = client
415415
}
416-
416+
417417
/// Documentation for MethodA
418418
public func methodA<R>(
419419
request: GRPCCore.StreamingClientRequest<NamespaceA_ServiceARequest>,
@@ -477,7 +477,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
477477
options: GRPCCore.CallOptions,
478478
_ body: @Sendable @escaping (GRPCCore.ClientResponse<NamespaceA_ServiceAResponse>) async throws -> R
479479
) async throws -> R where R: Sendable
480-
480+
481481
/// Documentation for MethodB
482482
func methodB<R>(
483483
request: GRPCCore.ClientRequest<NamespaceA_ServiceARequest>,
@@ -504,7 +504,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
504504
body
505505
)
506506
}
507-
507+
508508
package func methodB<R>(
509509
request: GRPCCore.ClientRequest<NamespaceA_ServiceARequest>,
510510
options: GRPCCore.CallOptions = .defaults,
@@ -540,7 +540,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
540540
handleResponse
541541
)
542542
}
543-
543+
544544
/// Documentation for MethodB
545545
package func methodB<Result>(
546546
_ message: NamespaceA_ServiceARequest,
@@ -563,11 +563,11 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
563563
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
564564
package struct NamespaceA_ServiceA_Client: NamespaceA_ServiceA.ClientProtocol {
565565
private let client: GRPCCore.GRPCClient
566-
566+
567567
package init(wrapping client: GRPCCore.GRPCClient) {
568568
self.client = client
569569
}
570-
570+
571571
/// Documentation for MethodA
572572
package func methodA<R>(
573573
request: GRPCCore.StreamingClientRequest<NamespaceA_ServiceARequest>,
@@ -587,7 +587,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
587587
handler: body
588588
)
589589
}
590-
590+
591591
/// Documentation for MethodB
592592
package func methodB<R>(
593593
request: GRPCCore.ClientRequest<NamespaceA_ServiceARequest>,
@@ -688,11 +688,11 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
688688
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
689689
internal struct ServiceA_Client: ServiceA.ClientProtocol {
690690
private let client: GRPCCore.GRPCClient
691-
691+
692692
internal init(wrapping client: GRPCCore.GRPCClient) {
693693
self.client = client
694694
}
695-
695+
696696
/// Documentation for MethodA
697697
internal func methodA<R>(
698698
request: GRPCCore.ClientRequest<ServiceARequest>,
@@ -758,7 +758,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
758758
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
759759
public struct NamespaceA_ServiceA_Client: NamespaceA_ServiceA.ClientProtocol {
760760
private let client: GRPCCore.GRPCClient
761-
761+
762762
public init(wrapping client: GRPCCore.GRPCClient) {
763763
self.client = client
764764
}
@@ -780,7 +780,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
780780
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
781781
public struct ServiceB_Client: ServiceB.ClientProtocol {
782782
private let client: GRPCCore.GRPCClient
783-
783+
784784
public init(wrapping client: GRPCCore.GRPCClient) {
785785
self.client = client
786786
}

Tests/GRPCCodeGenTests/Internal/Translator/ServerCodeTranslatorSnippetBasedTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ final class ServerCodeTranslatorSnippetBasedTests: XCTestCase {
408408
request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
409409
context: GRPCCore.ServerContext
410410
) async throws -> GRPCCore.StreamingServerResponse<NamespaceA_ServiceAResponse>
411-
411+
412412
/// Documentation for outputStreamingMethod
413413
func outputStreaming(
414414
request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
@@ -452,7 +452,7 @@ final class ServerCodeTranslatorSnippetBasedTests: XCTestCase {
452452
request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
453453
context: GRPCCore.ServerContext
454454
) async throws -> GRPCCore.ServerResponse<NamespaceA_ServiceAResponse>
455-
455+
456456
/// Documentation for outputStreamingMethod
457457
func outputStreaming(
458458
request: GRPCCore.ServerRequest<NamespaceA_ServiceARequest>,
@@ -472,7 +472,7 @@ final class ServerCodeTranslatorSnippetBasedTests: XCTestCase {
472472
)
473473
return GRPCCore.StreamingServerResponse(single: response)
474474
}
475-
475+
476476
internal func outputStreaming(
477477
request: GRPCCore.StreamingServerRequest<NamespaceA_ServiceARequest>,
478478
context: GRPCCore.ServerContext

0 commit comments

Comments
 (0)