From ace0b83a0c951052349b102c3af34e92cae76767 Mon Sep 17 00:00:00 2001 From: kaizen3031593 <36202692+kaizen3031593@users.noreply.github.com> Date: Wed, 17 Nov 2021 03:41:53 -0500 Subject: [PATCH] fix(pacmak): dotnet code docs loses indentation (#3180) Currently code examples in dotnet lose their indentation styles because we call `trim()` on each line in the code example. We want that behavior for other types of xml docs but not for code. This PR makes code a special case that calls `trimRight()` on each line, preserving the indentation on the left. The only change in the snapshots is translating this example: ```ts docs const x = 12 + 44; const s1 = "string"; const s2 = "string \nwith new newlines"; // see https://github.com/aws/jsii/issues/2569 const s3 = `string with new lines`; ``` to this: ``` int x = 12 + 44; string s1 = "string"; string s2 = @"string with new newlines"; // see https://github.com/aws/jsii/issues/2569 string s3 = @"string with new lines"; ``` --- By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license]. [Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0 --- .../jsii-pacmak/lib/targets/dotnet/dotnetdocgenerator.ts | 6 ++++-- .../generated-code/__snapshots__/target-dotnet.test.ts.snap | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/jsii-pacmak/lib/targets/dotnet/dotnetdocgenerator.ts b/packages/jsii-pacmak/lib/targets/dotnet/dotnetdocgenerator.ts index 4b336c235c..e10ba5b582 100644 --- a/packages/jsii-pacmak/lib/targets/dotnet/dotnetdocgenerator.ts +++ b/packages/jsii-pacmak/lib/targets/dotnet/dotnetdocgenerator.ts @@ -205,8 +205,10 @@ export class DotNetDocGenerator { xml.att(name, value); } const xmlstring = xml.end({ allowEmpty: true, pretty: false }); - - for (const line of xmlstring.split('\n').map((x) => x.trim())) { + const trimLeft = tag !== 'code'; + for (const line of xmlstring + .split('\n') + .map((x) => (trimLeft ? x.trim() : x.trimRight()))) { this.code.line(`/// ${line}`); } } diff --git a/packages/jsii-pacmak/test/generated-code/__snapshots__/target-dotnet.test.ts.snap b/packages/jsii-pacmak/test/generated-code/__snapshots__/target-dotnet.test.ts.snap index 812245c6e1..4f5d46a03d 100644 --- a/packages/jsii-pacmak/test/generated-code/__snapshots__/target-dotnet.test.ts.snap +++ b/packages/jsii-pacmak/test/generated-code/__snapshots__/target-dotnet.test.ts.snap @@ -6235,8 +6235,8 @@ namespace Amazon.JSII.Tests.CalculatorNamespace /// string s2 = @"string /// with new newlines"; // see https://github.com/aws/jsii/issues/2569 /// string s3 = @"string - /// with - /// new lines"; + /// with + /// new lines"; /// [JsiiClass(nativeType: typeof(Amazon.JSII.Tests.CalculatorNamespace.DocumentedClass), fullyQualifiedName: "jsii-calc.DocumentedClass")] public class DocumentedClass : DeputyBase