From 0031da05b670de2869ef23e76026a6cc5d499d44 Mon Sep 17 00:00:00 2001 From: Allison Chou Date: Wed, 24 Mar 2021 01:58:42 -0700 Subject: [PATCH 01/13] Add support for tabs vs. spaces settings for generated files --- .../src/CodeGeneration/CodeWriter.cs | 54 ++++++++++++++++--- .../CodeGeneration/CodeWriterExtensions.cs | 32 ++--------- .../CodeGeneration/DefaultDocumentWriter.cs | 4 +- .../src/PublicAPI.Unshipped.txt | 4 ++ 4 files changed, 57 insertions(+), 37 deletions(-) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs index 5b44fbc7fc3a..43c6272d9b60 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs @@ -18,9 +18,16 @@ public sealed class CodeWriter private int _currentLineIndex; private int _currentLineCharacterIndex; - public CodeWriter() + public CodeWriter() : this(Environment.NewLine, 4, false) { - NewLine = Environment.NewLine; + } + + public CodeWriter(string newLine, int tabSize, bool indentWithTabs) + { + NewLine = newLine; + + TabSize = tabSize; + IndentWithTabs = indentWithTabs; _builder = new StringBuilder(); } @@ -47,6 +54,10 @@ public string NewLine } } + public int TabSize { get; } + + public bool IndentWithTabs { get; } + public SourceLocation Location => new SourceLocation(_absoluteIndex, _currentLineIndex, _currentLineCharacterIndex); public char this[int index] @@ -62,17 +73,44 @@ public char this[int index] } } - // Internal for testing. - internal CodeWriter Indent(int size) + public CodeWriter Indent(int size) { - if (Length == 0 || this[Length - 1] == '\n') + if (size == 0 && Length != 0 && this[Length - 1] != '\n') + { + return this; + } + + var actualSize = 0; + if (IndentWithTabs) { - _builder.Append(' ', size); + // Avoid writing directly to the StringBuilder here, that will throw off the manual indexing + // done by the base class. + var tabs = size / TabSize; + actualSize += tabs; + for (var i = 0; i < tabs; i++) + { + _builder.Append("\t"); + } - _currentLineCharacterIndex += size; - _absoluteIndex += size; + var spaces = size % TabSize; + actualSize += spaces; + for (var i = 0; i < spaces; i++) + { + _builder.Append(" "); + } + } + else + { + actualSize = size; + for (var i = 0; i < size; i++) + { + _builder.Append(" "); + } } + _currentLineCharacterIndex += actualSize; + _absoluteIndex += actualSize; + return this; } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs index eee9f37927cb..227f8d2ae3a4 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; @@ -48,29 +48,7 @@ public static CodeWriter WritePadding(this CodeWriter writer, int offset, Source var basePadding = CalculatePadding(); var resolvedPadding = Math.Max(basePadding - offset, 0); - if (context.Options.IndentWithTabs) - { - // Avoid writing directly to the StringBuilder here, that will throw off the manual indexing - // done by the base class. - var tabs = resolvedPadding / context.Options.IndentSize; - for (var i = 0; i < tabs; i++) - { - writer.Write("\t"); - } - - var spaces = resolvedPadding % context.Options.IndentSize; - for (var i = 0; i < spaces; i++) - { - writer.Write(" "); - } - } - else - { - for (var i = 0; i < resolvedPadding; i++) - { - writer.Write(" "); - } - } + writer.Indent(resolvedPadding); return writer; @@ -86,7 +64,7 @@ int CalculatePadding() } else if (@char == '\t') { - spaceCount += context.Options.IndentSize; + spaceCount += writer.TabSize; } else { @@ -569,11 +547,11 @@ public struct CSharpCodeWritingScope : IDisposable private int _tabSize; private int _startIndent; - public CSharpCodeWritingScope(CodeWriter writer, int tabSize = 4, bool autoSpace = true) + public CSharpCodeWritingScope(CodeWriter writer, bool autoSpace = true) { _writer = writer; _autoSpace = autoSpace; - _tabSize = tabSize; + _tabSize = writer.TabSize; _startIndent = -1; // Set in WriteStartScope WriteStartScope(); diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultDocumentWriter.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultDocumentWriter.cs index c5fb4de90e25..a86385f248ea 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultDocumentWriter.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultDocumentWriter.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; @@ -32,7 +32,7 @@ public override RazorCSharpDocument WriteDocument(RazorCodeDocument codeDocument } var context = new DefaultCodeRenderingContext( - new CodeWriter(), + new CodeWriter(Environment.NewLine, _options.IndentSize, _options.IndentWithTabs), _codeTarget.CreateNodeWriter(), codeDocument, documentNode, diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/PublicAPI.Unshipped.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/PublicAPI.Unshipped.txt index 15ad64837f8a..c51cc2b0d254 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/PublicAPI.Unshipped.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/PublicAPI.Unshipped.txt @@ -1,6 +1,10 @@ #nullable enable +Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.IndentWithTabs.get -> bool +Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.TabSize.get -> int Microsoft.AspNetCore.Razor.Language.Intermediate.CascadingGenericTypeParameter Microsoft.AspNetCore.Razor.Language.Intermediate.CascadingGenericTypeParameter.CascadingGenericTypeParameter() -> void +~Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.CodeWriter(string newLine, int tabSize, bool indentWithTabs) -> void +~Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.Indent(int size) -> Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter ~Microsoft.AspNetCore.Razor.Language.Intermediate.CascadingGenericTypeParameter.GenericTypeNames.get -> System.Collections.Generic.IReadOnlyCollection ~Microsoft.AspNetCore.Razor.Language.Intermediate.CascadingGenericTypeParameter.GenericTypeNames.set -> void ~Microsoft.AspNetCore.Razor.Language.Intermediate.ComponentIntermediateNode.ProvidesCascadingGenericTypes.get -> System.Collections.Generic.Dictionary From 4adb5dacd421421085e3248cf477b385fbe0e469 Mon Sep 17 00:00:00 2001 From: Allison Chou Date: Wed, 24 Mar 2021 13:17:29 -0700 Subject: [PATCH 02/13] Add tests --- .../src/CodeGeneration/CodeWriter.cs | 2 +- .../CodeGeneration/CSharpCodeWriterTest.cs | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs index 43c6272d9b60..5fb42f6b28a4 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs @@ -75,7 +75,7 @@ public char this[int index] public CodeWriter Indent(int size) { - if (size == 0 && Length != 0 && this[Length - 1] != '\n') + if (size == 0 || (Length != 0 && this[Length - 1] != '\n')) { return this; } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs index cb7973df37f0..39b0c103b245 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs @@ -359,5 +359,35 @@ public void WriteAutoPropertyDeclaration_WithModifiers_WritesPropertyDeclaration var output = writer.GenerateCode(); Assert.Equal("public static global::System.String MyString { get; set; }" + Environment.NewLine, output); } + + [Fact] + public void CSharpCodeWriter_RespectTabSetting() + { + // Arrange + var writer = new CodeWriter(Environment.NewLine, tabSize: 4, indentWithTabs: true); + + // Act + writer.BuildClassDeclaration(Array.Empty(), "C", "", Array.Empty(), Array.Empty()); + writer.WriteField(Array.Empty(), Array.Empty(), "int", "f"); + + // Assert + var output = writer.GenerateCode(); + Assert.Equal("class C" + Environment.NewLine + "{" + Environment.NewLine + "\tint f;" + Environment.NewLine, output); + } + + [Fact] + public void CSharpCodeWriter_RespectSpaceSetting() + { + // Arrange + var writer = new CodeWriter(Environment.NewLine, tabSize: 4, indentWithTabs: false); + + // Act + writer.BuildClassDeclaration(Array.Empty(), "C", "", Array.Empty(), Array.Empty()); + writer.WriteField(Array.Empty(), Array.Empty(), "int", "f"); + + // Assert + var output = writer.GenerateCode(); + Assert.Equal("class C" + Environment.NewLine + "{" + Environment.NewLine + " int f;" + Environment.NewLine, output); + } } } From 62ab96b879ca5c93255311b4da97b778f08c6602 Mon Sep 17 00:00:00 2001 From: Allison Chou Date: Wed, 24 Mar 2021 13:52:28 -0700 Subject: [PATCH 03/13] Cleanup --- .../src/CodeGeneration/CodeWriter.cs | 12 ++++++------ .../CodeGeneration/CodeWriterExtensions.cs | 10 +++++----- .../CodeGeneration/DefaultDocumentWriter.cs | 2 +- .../src/PublicAPI.Unshipped.txt | 4 ++-- .../CodeGeneration/CSharpCodeWriterTest.cs | 19 +++++++++++++++++-- 5 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs index 5fb42f6b28a4..e63d252b4fc5 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs @@ -18,15 +18,15 @@ public sealed class CodeWriter private int _currentLineIndex; private int _currentLineCharacterIndex; - public CodeWriter() : this(Environment.NewLine, 4, false) + public CodeWriter() : this(Environment.NewLine, false, 4) { } - public CodeWriter(string newLine, int tabSize, bool indentWithTabs) + public CodeWriter(string newLine, bool indentWithTabs, int indentSize) { NewLine = newLine; - TabSize = tabSize; + IndentSize = indentSize; IndentWithTabs = indentWithTabs; _builder = new StringBuilder(); } @@ -54,7 +54,7 @@ public string NewLine } } - public int TabSize { get; } + public int IndentSize { get; } public bool IndentWithTabs { get; } @@ -85,14 +85,14 @@ public CodeWriter Indent(int size) { // Avoid writing directly to the StringBuilder here, that will throw off the manual indexing // done by the base class. - var tabs = size / TabSize; + var tabs = size / IndentSize; actualSize += tabs; for (var i = 0; i < tabs; i++) { _builder.Append("\t"); } - var spaces = size % TabSize; + var spaces = size % IndentSize; actualSize += spaces; for (var i = 0; i < spaces; i++) { diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs index 227f8d2ae3a4..298c82ab442a 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs @@ -64,7 +64,7 @@ int CalculatePadding() } else if (@char == '\t') { - spaceCount += writer.TabSize; + spaceCount += writer.IndentSize; } else { @@ -544,14 +544,14 @@ public struct CSharpCodeWritingScope : IDisposable { private CodeWriter _writer; private bool _autoSpace; - private int _tabSize; + private int _indentSize; private int _startIndent; public CSharpCodeWritingScope(CodeWriter writer, bool autoSpace = true) { _writer = writer; _autoSpace = autoSpace; - _tabSize = writer.TabSize; + _indentSize = writer.IndentSize; _startIndent = -1; // Set in WriteStartScope WriteStartScope(); @@ -567,7 +567,7 @@ private void WriteStartScope() TryAutoSpace(" "); _writer.WriteLine("{"); - _writer.CurrentIndent += _tabSize; + _writer.CurrentIndent += _indentSize; _startIndent = _writer.CurrentIndent; } @@ -578,7 +578,7 @@ private void WriteEndScope() // Ensure the scope hasn't been modified if (_writer.CurrentIndent == _startIndent) { - _writer.CurrentIndent -= _tabSize; + _writer.CurrentIndent -= _indentSize; } _writer.WriteLine("}"); diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultDocumentWriter.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultDocumentWriter.cs index a86385f248ea..ed8afc8d93b2 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultDocumentWriter.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultDocumentWriter.cs @@ -32,7 +32,7 @@ public override RazorCSharpDocument WriteDocument(RazorCodeDocument codeDocument } var context = new DefaultCodeRenderingContext( - new CodeWriter(Environment.NewLine, _options.IndentSize, _options.IndentWithTabs), + new CodeWriter(Environment.NewLine, _options.IndentWithTabs, _options.IndentSize), _codeTarget.CreateNodeWriter(), codeDocument, documentNode, diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/PublicAPI.Unshipped.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/PublicAPI.Unshipped.txt index ebef04064f77..a2bb17ca6af6 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/PublicAPI.Unshipped.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/PublicAPI.Unshipped.txt @@ -1,9 +1,9 @@ #nullable enable +Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.IndentSize.get -> int Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.IndentWithTabs.get -> bool -Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.TabSize.get -> int Microsoft.AspNetCore.Razor.Language.Intermediate.CascadingGenericTypeParameter Microsoft.AspNetCore.Razor.Language.Intermediate.CascadingGenericTypeParameter.CascadingGenericTypeParameter() -> void -~Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.CodeWriter(string newLine, int tabSize, bool indentWithTabs) -> void +~Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.CodeWriter(string newLine, bool indentWithTabs, int indentSize) -> void ~Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.Indent(int size) -> Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter ~Microsoft.AspNetCore.Razor.Language.Intermediate.CascadingGenericTypeParameter.GenericTypeNames.get -> System.Collections.Generic.IReadOnlyCollection ~Microsoft.AspNetCore.Razor.Language.Intermediate.CascadingGenericTypeParameter.GenericTypeNames.set -> void diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs index 39b0c103b245..17a785e657d5 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs @@ -364,7 +364,7 @@ public void WriteAutoPropertyDeclaration_WithModifiers_WritesPropertyDeclaration public void CSharpCodeWriter_RespectTabSetting() { // Arrange - var writer = new CodeWriter(Environment.NewLine, tabSize: 4, indentWithTabs: true); + var writer = new CodeWriter(Environment.NewLine, indentSize: 4, indentWithTabs: true); // Act writer.BuildClassDeclaration(Array.Empty(), "C", "", Array.Empty(), Array.Empty()); @@ -379,7 +379,7 @@ public void CSharpCodeWriter_RespectTabSetting() public void CSharpCodeWriter_RespectSpaceSetting() { // Arrange - var writer = new CodeWriter(Environment.NewLine, tabSize: 4, indentWithTabs: false); + var writer = new CodeWriter(Environment.NewLine, indentSize: 4, indentWithTabs: false); // Act writer.BuildClassDeclaration(Array.Empty(), "C", "", Array.Empty(), Array.Empty()); @@ -389,5 +389,20 @@ public void CSharpCodeWriter_RespectSpaceSetting() var output = writer.GenerateCode(); Assert.Equal("class C" + Environment.NewLine + "{" + Environment.NewLine + " int f;" + Environment.NewLine, output); } + + [Fact] + public void CSharpCodeWriter_RespectSpaceSetting_ModifyIndentSize() + { + // Arrange + var writer = new CodeWriter(Environment.NewLine, indentSize: 8, indentWithTabs: false); + + // Act + writer.BuildClassDeclaration(Array.Empty(), "C", "", Array.Empty(), Array.Empty()); + writer.WriteField(Array.Empty(), Array.Empty(), "int", "f"); + + // Assert + var output = writer.GenerateCode(); + Assert.Equal("class C" + Environment.NewLine + "{" + Environment.NewLine + " int f;" + Environment.NewLine, output); + } } } From 2b13803ebdc55b642447f8e94cb2c1b4ef5ef32f Mon Sep 17 00:00:00 2001 From: Allison Chou Date: Wed, 24 Mar 2021 14:19:55 -0700 Subject: [PATCH 04/13] Cleanup --- .../src/CodeGeneration/CodeWriter.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs index e63d252b4fc5..2e2ca695d662 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs @@ -25,9 +25,8 @@ public CodeWriter() : this(Environment.NewLine, false, 4) public CodeWriter(string newLine, bool indentWithTabs, int indentSize) { NewLine = newLine; - - IndentSize = indentSize; IndentWithTabs = indentWithTabs; + IndentSize = indentSize; _builder = new StringBuilder(); } @@ -54,10 +53,10 @@ public string NewLine } } - public int IndentSize { get; } - public bool IndentWithTabs { get; } + public int IndentSize { get; } + public SourceLocation Location => new SourceLocation(_absoluteIndex, _currentLineIndex, _currentLineCharacterIndex); public char this[int index] From 75621aa97242c2a7a95d58dd197e0e860a434d60 Mon Sep 17 00:00:00 2001 From: Allison Chou Date: Wed, 24 Mar 2021 15:15:32 -0700 Subject: [PATCH 05/13] Add field --- .../src/CodeGeneration/CodeWriter.cs | 11 +++++---- .../CodeGeneration/CodeWriterExtensions.cs | 2 +- .../CodeGeneration/DefaultDocumentWriter.cs | 2 +- .../src/DefaultRazorCodeGenerationOptions.cs | 6 ++++- ...efaultRazorCodeGenerationOptionsBuilder.cs | 3 +++ .../src/PublicAPI.Unshipped.txt | 6 ++++- .../src/RazorCodeGenerationOptions.cs | 4 ++++ .../src/RazorCodeGenerationOptionsBuilder.cs | 2 ++ .../CodeGeneration/CSharpCodeWriterTest.cs | 23 +++++++++++++++---- 9 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs index 2e2ca695d662..d6df60ffa7a1 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs @@ -18,14 +18,15 @@ public sealed class CodeWriter private int _currentLineIndex; private int _currentLineCharacterIndex; - public CodeWriter() : this(Environment.NewLine, false, 4) + public CodeWriter() : this(Environment.NewLine, false, 4, 4) { } - public CodeWriter(string newLine, bool indentWithTabs, int indentSize) + public CodeWriter(string newLine, bool indentWithTabs, int tabSize, int indentSize) { NewLine = newLine; IndentWithTabs = indentWithTabs; + TabSize = tabSize; IndentSize = indentSize; _builder = new StringBuilder(); } @@ -55,6 +56,8 @@ public string NewLine public bool IndentWithTabs { get; } + public int TabSize { get; } + public int IndentSize { get; } public SourceLocation Location => new SourceLocation(_absoluteIndex, _currentLineIndex, _currentLineCharacterIndex); @@ -84,14 +87,14 @@ public CodeWriter Indent(int size) { // Avoid writing directly to the StringBuilder here, that will throw off the manual indexing // done by the base class. - var tabs = size / IndentSize; + var tabs = size / TabSize; actualSize += tabs; for (var i = 0; i < tabs; i++) { _builder.Append("\t"); } - var spaces = size % IndentSize; + var spaces = size % TabSize; actualSize += spaces; for (var i = 0; i < spaces; i++) { diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs index 298c82ab442a..9f58a8815341 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs @@ -64,7 +64,7 @@ int CalculatePadding() } else if (@char == '\t') { - spaceCount += writer.IndentSize; + spaceCount += writer.TabSize; } else { diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultDocumentWriter.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultDocumentWriter.cs index ed8afc8d93b2..3b5cdba94a0c 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultDocumentWriter.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultDocumentWriter.cs @@ -32,7 +32,7 @@ public override RazorCSharpDocument WriteDocument(RazorCodeDocument codeDocument } var context = new DefaultCodeRenderingContext( - new CodeWriter(Environment.NewLine, _options.IndentWithTabs, _options.IndentSize), + new CodeWriter(Environment.NewLine, _options.IndentWithTabs, _options.TabSize, _options.IndentSize), _codeTarget.CreateNodeWriter(), codeDocument, documentNode, diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorCodeGenerationOptions.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorCodeGenerationOptions.cs index 0b1441b945ac..4aba675b59b3 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorCodeGenerationOptions.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorCodeGenerationOptions.cs @@ -6,7 +6,8 @@ namespace Microsoft.AspNetCore.Razor.Language internal class DefaultRazorCodeGenerationOptions : RazorCodeGenerationOptions { public DefaultRazorCodeGenerationOptions( - bool indentWithTabs, + bool indentWithTabs, + int tabSize, int indentSize, bool designTime, string rootNamespace, @@ -17,6 +18,7 @@ public DefaultRazorCodeGenerationOptions( bool omitMinimizedComponentAttributeValues) { IndentWithTabs = indentWithTabs; + TabSize = tabSize; IndentSize = indentSize; DesignTime = designTime; RootNamespace = rootNamespace; @@ -31,6 +33,8 @@ public DefaultRazorCodeGenerationOptions( public override bool IndentWithTabs { get; } + public override int TabSize { get; } + public override int IndentSize { get; } public override string RootNamespace { get; } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorCodeGenerationOptionsBuilder.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorCodeGenerationOptionsBuilder.cs index 2d16ab6b5ab8..5592b75c88c2 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorCodeGenerationOptionsBuilder.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorCodeGenerationOptionsBuilder.cs @@ -31,6 +31,8 @@ public DefaultRazorCodeGenerationOptionsBuilder(bool designTime) public override string FileKind { get; } + public override int TabSize { get; set; } = 4; + public override int IndentSize { get; set; } = 4; public override bool IndentWithTabs { get; set; } @@ -45,6 +47,7 @@ public override RazorCodeGenerationOptions Build() { return new DefaultRazorCodeGenerationOptions( IndentWithTabs, + TabSize, IndentSize, DesignTime, RootNamespace, diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/PublicAPI.Unshipped.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/PublicAPI.Unshipped.txt index a2bb17ca6af6..44c6e05a394c 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/PublicAPI.Unshipped.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/PublicAPI.Unshipped.txt @@ -1,9 +1,13 @@ #nullable enable Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.IndentSize.get -> int Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.IndentWithTabs.get -> bool +Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.TabSize.get -> int Microsoft.AspNetCore.Razor.Language.Intermediate.CascadingGenericTypeParameter Microsoft.AspNetCore.Razor.Language.Intermediate.CascadingGenericTypeParameter.CascadingGenericTypeParameter() -> void -~Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.CodeWriter(string newLine, bool indentWithTabs, int indentSize) -> void +abstract Microsoft.AspNetCore.Razor.Language.RazorCodeGenerationOptions.TabSize.get -> int +abstract Microsoft.AspNetCore.Razor.Language.RazorCodeGenerationOptionsBuilder.TabSize.get -> int +abstract Microsoft.AspNetCore.Razor.Language.RazorCodeGenerationOptionsBuilder.TabSize.set -> void +~Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.CodeWriter(string newLine, bool indentWithTabs, int tabSize, int indentSize) -> void ~Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.Indent(int size) -> Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter ~Microsoft.AspNetCore.Razor.Language.Intermediate.CascadingGenericTypeParameter.GenericTypeNames.get -> System.Collections.Generic.IReadOnlyCollection ~Microsoft.AspNetCore.Razor.Language.Intermediate.CascadingGenericTypeParameter.GenericTypeNames.set -> void diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorCodeGenerationOptions.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorCodeGenerationOptions.cs index 0bd804a21d53..130ecd1dc139 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorCodeGenerationOptions.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorCodeGenerationOptions.cs @@ -11,6 +11,7 @@ public static RazorCodeGenerationOptions CreateDefault() { return new DefaultRazorCodeGenerationOptions( indentWithTabs: false, + tabSize: 4, indentSize: 4, designTime: false, suppressChecksum: false, @@ -25,6 +26,7 @@ public static RazorCodeGenerationOptions CreateDesignTimeDefault() { return new DefaultRazorCodeGenerationOptions( indentWithTabs: false, + tabSize: 4, indentSize: 4, designTime: true, rootNamespace: null, @@ -71,6 +73,8 @@ public static RazorCodeGenerationOptions CreateDesignTime(Action diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorCodeGenerationOptionsBuilder.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorCodeGenerationOptionsBuilder.cs index d8896d3050d5..c394ebea4d26 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorCodeGenerationOptionsBuilder.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorCodeGenerationOptionsBuilder.cs @@ -11,6 +11,8 @@ public abstract class RazorCodeGenerationOptionsBuilder public virtual string FileKind => null; + public abstract int TabSize { get; set; } + public abstract int IndentSize { get; set; } public abstract bool IndentWithTabs { get; set; } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs index 17a785e657d5..7dce9183f171 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs @@ -364,7 +364,7 @@ public void WriteAutoPropertyDeclaration_WithModifiers_WritesPropertyDeclaration public void CSharpCodeWriter_RespectTabSetting() { // Arrange - var writer = new CodeWriter(Environment.NewLine, indentSize: 4, indentWithTabs: true); + var writer = new CodeWriter(Environment.NewLine, indentWithTabs: true, tabSize: 4, indentSize: 4); // Act writer.BuildClassDeclaration(Array.Empty(), "C", "", Array.Empty(), Array.Empty()); @@ -375,11 +375,26 @@ public void CSharpCodeWriter_RespectTabSetting() Assert.Equal("class C" + Environment.NewLine + "{" + Environment.NewLine + "\tint f;" + Environment.NewLine, output); } + [Fact] + public void CSharpCodeWriter_RespectTabSetting_ModifyTabSize() + { + // Arrange + var writer = new CodeWriter(Environment.NewLine, indentWithTabs: true, tabSize: 2, indentSize: 4); + + // Act + writer.BuildClassDeclaration(Array.Empty(), "C", "", Array.Empty(), Array.Empty()); + writer.WriteField(Array.Empty(), Array.Empty(), "int", "f"); + + // Assert + var output = writer.GenerateCode(); + Assert.Equal("class C" + Environment.NewLine + "{" + Environment.NewLine + "\t\tint f;" + Environment.NewLine, output); + } + [Fact] public void CSharpCodeWriter_RespectSpaceSetting() { // Arrange - var writer = new CodeWriter(Environment.NewLine, indentSize: 4, indentWithTabs: false); + var writer = new CodeWriter(Environment.NewLine, indentWithTabs: false, tabSize: 4, indentSize: 4); // Act writer.BuildClassDeclaration(Array.Empty(), "C", "", Array.Empty(), Array.Empty()); @@ -394,7 +409,7 @@ public void CSharpCodeWriter_RespectSpaceSetting() public void CSharpCodeWriter_RespectSpaceSetting_ModifyIndentSize() { // Arrange - var writer = new CodeWriter(Environment.NewLine, indentSize: 8, indentWithTabs: false); + var writer = new CodeWriter(Environment.NewLine, indentWithTabs: false, tabSize: 4, indentSize: 7); // Act writer.BuildClassDeclaration(Array.Empty(), "C", "", Array.Empty(), Array.Empty()); @@ -402,7 +417,7 @@ public void CSharpCodeWriter_RespectSpaceSetting_ModifyIndentSize() // Assert var output = writer.GenerateCode(); - Assert.Equal("class C" + Environment.NewLine + "{" + Environment.NewLine + " int f;" + Environment.NewLine, output); + Assert.Equal("class C" + Environment.NewLine + "{" + Environment.NewLine + " int f;" + Environment.NewLine, output); } } } From a45cab57a43717c6f03f596e62532f1674c601fc Mon Sep 17 00:00:00 2001 From: Allison Chou Date: Wed, 24 Mar 2021 15:44:18 -0700 Subject: [PATCH 06/13] Cleanup --- global.json | 4 +-- .../src/CodeGeneration/CodeWriter.cs | 7 ++-- .../CodeGeneration/CodeWriterExtensions.cs | 2 +- .../CodeGeneration/DefaultDocumentWriter.cs | 2 +- .../src/DefaultRazorCodeGenerationOptions.cs | 4 --- ...efaultRazorCodeGenerationOptionsBuilder.cs | 3 -- .../src/PublicAPI.Unshipped.txt | 6 +--- .../src/RazorCodeGenerationOptions.cs | 4 --- .../src/RazorCodeGenerationOptionsBuilder.cs | 2 -- .../CodeGeneration/CSharpCodeWriterTest.cs | 34 ++----------------- 10 files changed, 9 insertions(+), 59 deletions(-) diff --git a/global.json b/global.json index 5c7f2bf20d5c..0be8668cd4ed 100644 --- a/global.json +++ b/global.json @@ -1,9 +1,9 @@ { "sdk": { - "version": "6.0.100-preview.3.21168.19" + "version": "6.0.100-preview.2.21155.3" }, "tools": { - "dotnet": "6.0.100-preview.3.21168.19", + "dotnet": "6.0.100-preview.2.21155.3", "runtimes": { "dotnet/x64": [ "2.1.25", diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs index d6df60ffa7a1..292bd8bb0322 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs @@ -18,16 +18,15 @@ public sealed class CodeWriter private int _currentLineIndex; private int _currentLineCharacterIndex; - public CodeWriter() : this(Environment.NewLine, false, 4, 4) + public CodeWriter() : this(Environment.NewLine, false, 4) { } - public CodeWriter(string newLine, bool indentWithTabs, int tabSize, int indentSize) + public CodeWriter(string newLine, bool indentWithTabs, int tabSize) { NewLine = newLine; IndentWithTabs = indentWithTabs; TabSize = tabSize; - IndentSize = indentSize; _builder = new StringBuilder(); } @@ -58,8 +57,6 @@ public string NewLine public int TabSize { get; } - public int IndentSize { get; } - public SourceLocation Location => new SourceLocation(_absoluteIndex, _currentLineIndex, _currentLineCharacterIndex); public char this[int index] diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs index 9f58a8815341..fd4561b0307b 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs @@ -551,7 +551,7 @@ public CSharpCodeWritingScope(CodeWriter writer, bool autoSpace = true) { _writer = writer; _autoSpace = autoSpace; - _indentSize = writer.IndentSize; + _indentSize = writer.TabSize; _startIndent = -1; // Set in WriteStartScope WriteStartScope(); diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultDocumentWriter.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultDocumentWriter.cs index 3b5cdba94a0c..ed8afc8d93b2 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultDocumentWriter.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultDocumentWriter.cs @@ -32,7 +32,7 @@ public override RazorCSharpDocument WriteDocument(RazorCodeDocument codeDocument } var context = new DefaultCodeRenderingContext( - new CodeWriter(Environment.NewLine, _options.IndentWithTabs, _options.TabSize, _options.IndentSize), + new CodeWriter(Environment.NewLine, _options.IndentWithTabs, _options.IndentSize), _codeTarget.CreateNodeWriter(), codeDocument, documentNode, diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorCodeGenerationOptions.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorCodeGenerationOptions.cs index 4aba675b59b3..03b56f309168 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorCodeGenerationOptions.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorCodeGenerationOptions.cs @@ -7,7 +7,6 @@ internal class DefaultRazorCodeGenerationOptions : RazorCodeGenerationOptions { public DefaultRazorCodeGenerationOptions( bool indentWithTabs, - int tabSize, int indentSize, bool designTime, string rootNamespace, @@ -18,7 +17,6 @@ public DefaultRazorCodeGenerationOptions( bool omitMinimizedComponentAttributeValues) { IndentWithTabs = indentWithTabs; - TabSize = tabSize; IndentSize = indentSize; DesignTime = designTime; RootNamespace = rootNamespace; @@ -33,8 +31,6 @@ public DefaultRazorCodeGenerationOptions( public override bool IndentWithTabs { get; } - public override int TabSize { get; } - public override int IndentSize { get; } public override string RootNamespace { get; } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorCodeGenerationOptionsBuilder.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorCodeGenerationOptionsBuilder.cs index 5592b75c88c2..2d16ab6b5ab8 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorCodeGenerationOptionsBuilder.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorCodeGenerationOptionsBuilder.cs @@ -31,8 +31,6 @@ public DefaultRazorCodeGenerationOptionsBuilder(bool designTime) public override string FileKind { get; } - public override int TabSize { get; set; } = 4; - public override int IndentSize { get; set; } = 4; public override bool IndentWithTabs { get; set; } @@ -47,7 +45,6 @@ public override RazorCodeGenerationOptions Build() { return new DefaultRazorCodeGenerationOptions( IndentWithTabs, - TabSize, IndentSize, DesignTime, RootNamespace, diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/PublicAPI.Unshipped.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/PublicAPI.Unshipped.txt index 44c6e05a394c..fa3d0497e7d6 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/PublicAPI.Unshipped.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/PublicAPI.Unshipped.txt @@ -1,13 +1,9 @@ #nullable enable -Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.IndentSize.get -> int Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.IndentWithTabs.get -> bool Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.TabSize.get -> int Microsoft.AspNetCore.Razor.Language.Intermediate.CascadingGenericTypeParameter Microsoft.AspNetCore.Razor.Language.Intermediate.CascadingGenericTypeParameter.CascadingGenericTypeParameter() -> void -abstract Microsoft.AspNetCore.Razor.Language.RazorCodeGenerationOptions.TabSize.get -> int -abstract Microsoft.AspNetCore.Razor.Language.RazorCodeGenerationOptionsBuilder.TabSize.get -> int -abstract Microsoft.AspNetCore.Razor.Language.RazorCodeGenerationOptionsBuilder.TabSize.set -> void -~Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.CodeWriter(string newLine, bool indentWithTabs, int tabSize, int indentSize) -> void +~Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.CodeWriter(string newLine, bool indentWithTabs, int tabSize) -> void ~Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.Indent(int size) -> Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter ~Microsoft.AspNetCore.Razor.Language.Intermediate.CascadingGenericTypeParameter.GenericTypeNames.get -> System.Collections.Generic.IReadOnlyCollection ~Microsoft.AspNetCore.Razor.Language.Intermediate.CascadingGenericTypeParameter.GenericTypeNames.set -> void diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorCodeGenerationOptions.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorCodeGenerationOptions.cs index 130ecd1dc139..0bd804a21d53 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorCodeGenerationOptions.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorCodeGenerationOptions.cs @@ -11,7 +11,6 @@ public static RazorCodeGenerationOptions CreateDefault() { return new DefaultRazorCodeGenerationOptions( indentWithTabs: false, - tabSize: 4, indentSize: 4, designTime: false, suppressChecksum: false, @@ -26,7 +25,6 @@ public static RazorCodeGenerationOptions CreateDesignTimeDefault() { return new DefaultRazorCodeGenerationOptions( indentWithTabs: false, - tabSize: 4, indentSize: 4, designTime: true, rootNamespace: null, @@ -73,8 +71,6 @@ public static RazorCodeGenerationOptions CreateDesignTime(Action diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorCodeGenerationOptionsBuilder.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorCodeGenerationOptionsBuilder.cs index c394ebea4d26..d8896d3050d5 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorCodeGenerationOptionsBuilder.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/RazorCodeGenerationOptionsBuilder.cs @@ -11,8 +11,6 @@ public abstract class RazorCodeGenerationOptionsBuilder public virtual string FileKind => null; - public abstract int TabSize { get; set; } - public abstract int IndentSize { get; set; } public abstract bool IndentWithTabs { get; set; } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs index 7dce9183f171..9d6cbf71dca4 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs @@ -364,7 +364,7 @@ public void WriteAutoPropertyDeclaration_WithModifiers_WritesPropertyDeclaration public void CSharpCodeWriter_RespectTabSetting() { // Arrange - var writer = new CodeWriter(Environment.NewLine, indentWithTabs: true, tabSize: 4, indentSize: 4); + var writer = new CodeWriter(Environment.NewLine, indentWithTabs: true, tabSize: 4); // Act writer.BuildClassDeclaration(Array.Empty(), "C", "", Array.Empty(), Array.Empty()); @@ -375,26 +375,11 @@ public void CSharpCodeWriter_RespectTabSetting() Assert.Equal("class C" + Environment.NewLine + "{" + Environment.NewLine + "\tint f;" + Environment.NewLine, output); } - [Fact] - public void CSharpCodeWriter_RespectTabSetting_ModifyTabSize() - { - // Arrange - var writer = new CodeWriter(Environment.NewLine, indentWithTabs: true, tabSize: 2, indentSize: 4); - - // Act - writer.BuildClassDeclaration(Array.Empty(), "C", "", Array.Empty(), Array.Empty()); - writer.WriteField(Array.Empty(), Array.Empty(), "int", "f"); - - // Assert - var output = writer.GenerateCode(); - Assert.Equal("class C" + Environment.NewLine + "{" + Environment.NewLine + "\t\tint f;" + Environment.NewLine, output); - } - [Fact] public void CSharpCodeWriter_RespectSpaceSetting() { // Arrange - var writer = new CodeWriter(Environment.NewLine, indentWithTabs: false, tabSize: 4, indentSize: 4); + var writer = new CodeWriter(Environment.NewLine, indentWithTabs: false, tabSize: 4); // Act writer.BuildClassDeclaration(Array.Empty(), "C", "", Array.Empty(), Array.Empty()); @@ -404,20 +389,5 @@ public void CSharpCodeWriter_RespectSpaceSetting() var output = writer.GenerateCode(); Assert.Equal("class C" + Environment.NewLine + "{" + Environment.NewLine + " int f;" + Environment.NewLine, output); } - - [Fact] - public void CSharpCodeWriter_RespectSpaceSetting_ModifyIndentSize() - { - // Arrange - var writer = new CodeWriter(Environment.NewLine, indentWithTabs: false, tabSize: 4, indentSize: 7); - - // Act - writer.BuildClassDeclaration(Array.Empty(), "C", "", Array.Empty(), Array.Empty()); - writer.WriteField(Array.Empty(), Array.Empty(), "int", "f"); - - // Assert - var output = writer.GenerateCode(); - Assert.Equal("class C" + Environment.NewLine + "{" + Environment.NewLine + " int f;" + Environment.NewLine, output); - } } } From 4d2614df5fe5aec1aa4fed1187345069ea38b4c1 Mon Sep 17 00:00:00 2001 From: Allison Chou Date: Wed, 24 Mar 2021 15:47:19 -0700 Subject: [PATCH 07/13] Revert changes to global.json --- global.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/global.json b/global.json index 0be8668cd4ed..5c7f2bf20d5c 100644 --- a/global.json +++ b/global.json @@ -1,9 +1,9 @@ { "sdk": { - "version": "6.0.100-preview.2.21155.3" + "version": "6.0.100-preview.3.21168.19" }, "tools": { - "dotnet": "6.0.100-preview.2.21155.3", + "dotnet": "6.0.100-preview.3.21168.19", "runtimes": { "dotnet/x64": [ "2.1.25", From 305dec93f7db3d9f44ff6166a0ce3bc85d3f1aa1 Mon Sep 17 00:00:00 2001 From: Allison Chou Date: Wed, 24 Mar 2021 15:48:05 -0700 Subject: [PATCH 08/13] fix naming --- .../src/CodeGeneration/CodeWriterExtensions.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs index fd4561b0307b..227f8d2ae3a4 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs @@ -544,14 +544,14 @@ public struct CSharpCodeWritingScope : IDisposable { private CodeWriter _writer; private bool _autoSpace; - private int _indentSize; + private int _tabSize; private int _startIndent; public CSharpCodeWritingScope(CodeWriter writer, bool autoSpace = true) { _writer = writer; _autoSpace = autoSpace; - _indentSize = writer.TabSize; + _tabSize = writer.TabSize; _startIndent = -1; // Set in WriteStartScope WriteStartScope(); @@ -567,7 +567,7 @@ private void WriteStartScope() TryAutoSpace(" "); _writer.WriteLine("{"); - _writer.CurrentIndent += _indentSize; + _writer.CurrentIndent += _tabSize; _startIndent = _writer.CurrentIndent; } @@ -578,7 +578,7 @@ private void WriteEndScope() // Ensure the scope hasn't been modified if (_writer.CurrentIndent == _startIndent) { - _writer.CurrentIndent -= _indentSize; + _writer.CurrentIndent -= _tabSize; } _writer.WriteLine("}"); From 5c5c254008edb586d02e62b5e5a0e1136529c918 Mon Sep 17 00:00:00 2001 From: Allison Chou Date: Thu, 25 Mar 2021 17:04:24 -0700 Subject: [PATCH 09/13] Refactor and fix failing tests --- global.json | 4 ++-- .../src/CodeGeneration/CodeWriter.cs | 15 +++------------ .../TestComponent.codegen.cs | 2 +- .../TestComponent.mappings.txt | 14 +++++++------- .../TestComponent.codegen.cs | 2 +- .../TestComponent.mappings.txt | 2 +- 6 files changed, 15 insertions(+), 24 deletions(-) diff --git a/global.json b/global.json index 5c7f2bf20d5c..0be8668cd4ed 100644 --- a/global.json +++ b/global.json @@ -1,9 +1,9 @@ { "sdk": { - "version": "6.0.100-preview.3.21168.19" + "version": "6.0.100-preview.2.21155.3" }, "tools": { - "dotnet": "6.0.100-preview.3.21168.19", + "dotnet": "6.0.100-preview.2.21155.3", "runtimes": { "dotnet/x64": [ "2.1.25", diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs index 292bd8bb0322..77281f1b74d6 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs @@ -86,25 +86,16 @@ public CodeWriter Indent(int size) // done by the base class. var tabs = size / TabSize; actualSize += tabs; - for (var i = 0; i < tabs; i++) - { - _builder.Append("\t"); - } + _builder.Append('\t', tabs); var spaces = size % TabSize; actualSize += spaces; - for (var i = 0; i < spaces; i++) - { - _builder.Append(" "); - } + _builder.Append(' ', spaces); } else { actualSize = size; - for (var i = 0; i < size; i++) - { - _builder.Append(" "); - } + _builder.Append(' ', size); } _currentLineCharacterIndex += actualSize; diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithCssScope/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithCssScope/TestComponent.codegen.cs index 95b48f43d907..a38bd26fe387 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithCssScope/TestComponent.codegen.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithCssScope/TestComponent.codegen.cs @@ -76,7 +76,7 @@ protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Renderin #line default #line hidden #nullable disable - + __o = Microsoft.AspNetCore.Components.BindConverter.FormatValue( #nullable restore diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithCssScope/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithCssScope/TestComponent.mappings.txt index f3d9de1293b1..0fa1e7a5b834 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithCssScope/TestComponent.mappings.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithCssScope/TestComponent.mappings.txt @@ -35,19 +35,19 @@ Generated Location: (2126:73,38 [18] ) Source Location: (557:12,84 [6] x:\dir\subdir\Test\TestComponent.cshtml) | | -Generated Location: (2353:78,96 [6] ) +Generated Location: (2341:78,84 [6] ) | | Source Location: (589:13,30 [10] x:\dir\subdir\Test\TestComponent.cshtml) |myVariable| -Generated Location: (2540:83,30 [10] ) +Generated Location: (2528:83,30 [10] ) |myVariable| Source Location: (637:13,78 [3] x:\dir\subdir\Test\TestComponent.cshtml) | }| -Generated Location: (2905:92,78 [3] ) +Generated Location: (2893:92,78 [3] ) | }| @@ -62,7 +62,7 @@ Source Location: (651:16,7 [245] x:\dir\subdir\Test\TestComponent.cshtml) for (var i = 0; i < 10; i++) { | -Generated Location: (3087:102,7 [245] ) +Generated Location: (3075:102,7 [245] ) | ElementReference myElementReference; TemplatedComponent myComponentReference; @@ -76,12 +76,12 @@ Generated Location: (3087:102,7 [245] ) Source Location: (912:25,28 [1] x:\dir\subdir\Test\TestComponent.cshtml) |i| -Generated Location: (3499:119,28 [1] ) +Generated Location: (3487:119,28 [1] ) |i| Source Location: (925:25,41 [1] x:\dir\subdir\Test\TestComponent.cshtml) |i| -Generated Location: (3675:127,41 [1] ) +Generated Location: (3663:127,41 [1] ) |i| Source Location: (931:25,47 [166] x:\dir\subdir\Test\TestComponent.cshtml) @@ -93,7 +93,7 @@ Source Location: (931:25,47 [166] x:\dir\subdir\Test\TestComponent.cshtml) System.GC.KeepAlive(myVariable); } | -Generated Location: (3847:134,47 [166] ) +Generated Location: (3835:134,47 [166] ) | } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SingleLineControlFlowStatements_InCodeBlock/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SingleLineControlFlowStatements_InCodeBlock/TestComponent.codegen.cs index 4b5f6a8517b4..c72365f1eb5f 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SingleLineControlFlowStatements_InCodeBlock/TestComponent.codegen.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SingleLineControlFlowStatements_InCodeBlock/TestComponent.codegen.cs @@ -45,7 +45,7 @@ protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Renderin #line default #line hidden #nullable disable - + } #pragma warning restore 1998 diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SingleLineControlFlowStatements_InCodeBlock/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SingleLineControlFlowStatements_InCodeBlock/TestComponent.mappings.txt index 34c80001e6a7..fe1a4a0917e6 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SingleLineControlFlowStatements_InCodeBlock/TestComponent.mappings.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SingleLineControlFlowStatements_InCodeBlock/TestComponent.mappings.txt @@ -24,7 +24,7 @@ Generated Location: (1301:42,16 [6] ) Source Location: (216:6,26 [2] x:\dir\subdir\Test\TestComponent.cshtml) | | -Generated Location: (1398:47,38 [2] ) +Generated Location: (1386:47,26 [2] ) | | From 503499f530fd40d65f08704f410d363c176d56ce Mon Sep 17 00:00:00 2001 From: Allison Chou Date: Thu, 25 Mar 2021 17:06:01 -0700 Subject: [PATCH 10/13] Revert global.json changes --- global.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/global.json b/global.json index 0be8668cd4ed..c0b7cd56c94d 100644 --- a/global.json +++ b/global.json @@ -1,9 +1,9 @@ { "sdk": { - "version": "6.0.100-preview.2.21155.3" + "version": "6.0.100-preview.3.21168.19" }, "tools": { - "dotnet": "6.0.100-preview.2.21155.3", + "dotnet": "6.0.100-preview.3.21168.19", "runtimes": { "dotnet/x64": [ "2.1.25", @@ -33,4 +33,4 @@ "Microsoft.DotNet.Arcade.Sdk": "6.0.0-beta.21167.3", "Microsoft.DotNet.Helix.Sdk": "6.0.0-beta.21167.3" } -} +} \ No newline at end of file From d2fdfdc51cbddc02608d869f4ff0a130830b19cf Mon Sep 17 00:00:00 2001 From: Allison Chou Date: Thu, 25 Mar 2021 21:46:06 -0700 Subject: [PATCH 11/13] Update src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs Co-authored-by: Pranav K --- .../src/CodeGeneration/CodeWriter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs index 77281f1b74d6..5a386bb98c8c 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs @@ -18,7 +18,7 @@ public sealed class CodeWriter private int _currentLineIndex; private int _currentLineCharacterIndex; - public CodeWriter() : this(Environment.NewLine, false, 4) + public CodeWriter() : this(Environment.NewLine, indentWithTabs: false, tabSize: 4) { } From e70d65fee2483900b4cbf074ae1d6fba56d63b60 Mon Sep 17 00:00:00 2001 From: Allison Chou Date: Mon, 29 Mar 2021 22:20:03 -0700 Subject: [PATCH 12/13] Revert global.json changes --- global.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/global.json b/global.json index c0b7cd56c94d..5c7f2bf20d5c 100644 --- a/global.json +++ b/global.json @@ -33,4 +33,4 @@ "Microsoft.DotNet.Arcade.Sdk": "6.0.0-beta.21167.3", "Microsoft.DotNet.Helix.Sdk": "6.0.0-beta.21167.3" } -} \ No newline at end of file +} From e70ae71257d4bd6d14378b8e8b7c690e2c134ab9 Mon Sep 17 00:00:00 2001 From: Allison Chou Date: Mon, 29 Mar 2021 22:30:48 -0700 Subject: [PATCH 13/13] Modify constructor --- .../src/CodeGeneration/CodeWriter.cs | 8 ++++---- .../src/CodeGeneration/DefaultDocumentWriter.cs | 2 +- .../src/PublicAPI.Unshipped.txt | 2 +- .../test/CodeGeneration/CSharpCodeWriterTest.cs | 16 ++++++++++++++-- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs index 5a386bb98c8c..59f46fea939e 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriter.cs @@ -18,15 +18,15 @@ public sealed class CodeWriter private int _currentLineIndex; private int _currentLineCharacterIndex; - public CodeWriter() : this(Environment.NewLine, indentWithTabs: false, tabSize: 4) + public CodeWriter() : this(Environment.NewLine, RazorCodeGenerationOptions.CreateDefault()) { } - public CodeWriter(string newLine, bool indentWithTabs, int tabSize) + public CodeWriter(string newLine, RazorCodeGenerationOptions options) { NewLine = newLine; - IndentWithTabs = indentWithTabs; - TabSize = tabSize; + IndentWithTabs = options.IndentWithTabs; + TabSize = options.IndentSize; _builder = new StringBuilder(); } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultDocumentWriter.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultDocumentWriter.cs index ed8afc8d93b2..e23a3030e940 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultDocumentWriter.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultDocumentWriter.cs @@ -32,7 +32,7 @@ public override RazorCSharpDocument WriteDocument(RazorCodeDocument codeDocument } var context = new DefaultCodeRenderingContext( - new CodeWriter(Environment.NewLine, _options.IndentWithTabs, _options.IndentSize), + new CodeWriter(Environment.NewLine, _options), _codeTarget.CreateNodeWriter(), codeDocument, documentNode, diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/PublicAPI.Unshipped.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/PublicAPI.Unshipped.txt index fa3d0497e7d6..8f38fcd0813c 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/PublicAPI.Unshipped.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/PublicAPI.Unshipped.txt @@ -3,7 +3,7 @@ Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.IndentWithTabs.get Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.TabSize.get -> int Microsoft.AspNetCore.Razor.Language.Intermediate.CascadingGenericTypeParameter Microsoft.AspNetCore.Razor.Language.Intermediate.CascadingGenericTypeParameter.CascadingGenericTypeParameter() -> void -~Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.CodeWriter(string newLine, bool indentWithTabs, int tabSize) -> void +~Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.CodeWriter(string newLine, Microsoft.AspNetCore.Razor.Language.RazorCodeGenerationOptions options) -> void ~Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter.Indent(int size) -> Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriter ~Microsoft.AspNetCore.Razor.Language.Intermediate.CascadingGenericTypeParameter.GenericTypeNames.get -> System.Collections.Generic.IReadOnlyCollection ~Microsoft.AspNetCore.Razor.Language.Intermediate.CascadingGenericTypeParameter.GenericTypeNames.set -> void diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs index 9d6cbf71dca4..88f4447eda61 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs @@ -364,7 +364,13 @@ public void WriteAutoPropertyDeclaration_WithModifiers_WritesPropertyDeclaration public void CSharpCodeWriter_RespectTabSetting() { // Arrange - var writer = new CodeWriter(Environment.NewLine, indentWithTabs: true, tabSize: 4); + var options = RazorCodeGenerationOptions.Create(o => + { + o.IndentWithTabs = true; + o.IndentSize = 4; + }); + + var writer = new CodeWriter(Environment.NewLine, options); // Act writer.BuildClassDeclaration(Array.Empty(), "C", "", Array.Empty(), Array.Empty()); @@ -379,7 +385,13 @@ public void CSharpCodeWriter_RespectTabSetting() public void CSharpCodeWriter_RespectSpaceSetting() { // Arrange - var writer = new CodeWriter(Environment.NewLine, indentWithTabs: false, tabSize: 4); + var options = RazorCodeGenerationOptions.Create(o => + { + o.IndentWithTabs = false; + o.IndentSize = 4; + }); + + var writer = new CodeWriter(Environment.NewLine, options); // Act writer.BuildClassDeclaration(Array.Empty(), "C", "", Array.Empty(), Array.Empty());