-
Notifications
You must be signed in to change notification settings - Fork 549
[Generator] Wrapper for Strings Constructed during Code Generation #20098
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
29c9805
Initial Commit of CodeBlocks
dustin-wojciechowski b2f59f8
Code Block Tests added
dustin-wojciechowski 0c96dc2
Renaming, small changes
dustin-wojciechowski f69d653
Auto-format source code
117601f
Added Print(StreamWriter) method to ICodeBlocks
dustin-wojciechowski 9a34606
Removed old Print method, created utility method for tests
dustin-wojciechowski 7175e97
Added support for multiple arguments in method signature, constructor…
dustin-wojciechowski e80f685
Added IfElse and Else to IfBlock, added more tests including a Compre…
dustin-wojciechowski 0c83dec
Auto-format source code
bc804ab
Removed unused print method
dustin-wojciechowski 05d39ff
Merge branch 'main' into dev/dustin/code-blocks
dustin-wojciechowski 65cf116
Removed currentIndent from constructor arguments
dustin-wojciechowski 730f06f
Removed unnecessary SetIndent method
dustin-wojciechowski 228740f
Added test for MethodBlock
dustin-wojciechowski 380fa57
Removed unnecessary headings
dustin-wojciechowski ef1d36d
Auto-format source code
6549373
Merge branch 'main' into dev/dustin/code-blocks
dustin-wojciechowski 6312b41
Changed namespace, changed newLine to NewLine
dustin-wojciechowski cfa59b3
Changed StringWriter to TextWriter
dustin-wojciechowski 034e6f3
Changed single character strings to chars, got rid of some string con…
dustin-wojciechowski 0ee0b6f
Added Codeblock constructors that take in param string[], modified te…
dustin-wojciechowski d122b11
Use of StringBuilder in MethodBlock
dustin-wojciechowski 3ffc2ff
Changed to use params
dustin-wojciechowski f9b5a73
Formatting of expected text
dustin-wojciechowski 4a90b3f
Auto-format source code
dfcb2d1
Merge branch 'main' into dev/dustin/code-blocks
dustin-wojciechowski f240249
Changed CodeBlock constructors to add range to Blocks versus assignment
dustin-wojciechowski be5b261
Changed one more instance of Blocks.AddRange, some IDE suggestions
dustin-wojciechowski e452a8e
Merge branch 'main' into dev/dustin/code-blocks
dustin-wojciechowski 8cef401
Easier to read API for MethodBlock
dustin-wojciechowski 3d76352
Replace AddBlock with Add
dustin-wojciechowski 257b8ef
Easier API usage
dustin-wojciechowski 6b7b141
Enable method chaining in the IfBlock, modified tests to use them
dustin-wojciechowski c60f3f9
Enabled chaining for IfBlock constructors that take in blocks
dustin-wojciechowski 9cd1a67
Rewrote ComprehensiveTest to use new api design
dustin-wojciechowski a5df70c
IfBlock now writes directly to TextWriter instead of using string con…
dustin-wojciechowski 52a06fd
Merge branch 'main' into dev/dustin/code-blocks
dustin-wojciechowski f77ade5
Auto-format source code
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| using System.IO; | ||
|
|
||
| public class BlockContainer : CodeBlock { | ||
| public override void Print (TextWriter writer) | ||
| { | ||
| foreach (ICodeBlock block in Blocks) { | ||
| block.SetIndent (CurrentIndent); | ||
| block.Print (writer); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| using System.Collections; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
|
|
||
| public class CodeBlock : ICodeBlock, IEnumerable<ICodeBlock> { | ||
| protected int CurrentIndent = 0; | ||
| protected int Indent = 4; | ||
| protected string HeaderText = string.Empty; | ||
| protected List<ICodeBlock> Blocks = new (); | ||
| protected const char StartBrace = '{'; | ||
| protected const char EndBrace = '}'; | ||
| protected const char NewLine = '\n'; | ||
|
|
||
| public CodeBlock () { } | ||
|
|
||
| public CodeBlock (string text) | ||
| { | ||
| HeaderText = text; | ||
| } | ||
|
|
||
| public CodeBlock (string text, IList<ICodeBlock> blocks) | ||
| { | ||
| Blocks.AddRange (blocks); | ||
| HeaderText = text; | ||
| } | ||
|
|
||
| public CodeBlock (IList<ICodeBlock> blocks) | ||
| { | ||
| Blocks.AddRange (blocks); | ||
| } | ||
|
|
||
| public CodeBlock (string headerText, params string [] lines) | ||
| { | ||
| HeaderText = headerText; | ||
| foreach (string line in lines) { | ||
| Blocks.Add (new LineBlock (line)); | ||
| } | ||
| } | ||
|
|
||
| public CodeBlock (params string [] lines) | ||
| { | ||
| foreach (string line in lines) { | ||
| Blocks.Add (new LineBlock (line)); | ||
| } | ||
| } | ||
|
|
||
| public void Add (ICodeBlock block) | ||
| { | ||
| block.SetIndent (CurrentIndent + Indent); | ||
| Blocks.Add (block); | ||
| } | ||
|
|
||
| public void Add (string text) | ||
| { | ||
| LineBlock line = new LineBlock (text); | ||
| line.SetIndent (CurrentIndent + Indent); | ||
| Blocks.Add (line); | ||
| } | ||
|
|
||
| public void AddLine (string text) | ||
| { | ||
| LineBlock line = new LineBlock (text); | ||
| line.SetIndent (CurrentIndent + Indent); | ||
| Blocks.Add (line); | ||
| } | ||
|
|
||
| protected void WriteIndent (TextWriter writer) | ||
| { | ||
| for (var i = 0; i < CurrentIndent; i++) | ||
| writer.Write (' '); | ||
| } | ||
|
|
||
| protected virtual void WriteHeaderText (TextWriter writer) | ||
| { | ||
| WriteIndent (writer); | ||
| writer.Write (HeaderText); | ||
| writer.Write (NewLine); | ||
| } | ||
|
|
||
| public virtual void Print (TextWriter writer) | ||
| { | ||
| if (HeaderText != string.Empty) | ||
| WriteHeaderText (writer); | ||
|
|
||
| WriteIndent (writer); | ||
| writer.Write (StartBrace); | ||
| writer.Write (NewLine); | ||
|
|
||
| SetIndent (CurrentIndent + Indent); | ||
| foreach (ICodeBlock block in Blocks) { | ||
| block.SetIndent (CurrentIndent); | ||
| block.Print (writer); | ||
| } | ||
| SetIndent (CurrentIndent - Indent); | ||
|
|
||
| WriteIndent (writer); | ||
| writer.Write (EndBrace); | ||
| writer.Write (NewLine); | ||
| } | ||
|
|
||
| public void SetIndent (int indent) | ||
| { | ||
| CurrentIndent = indent; | ||
| } | ||
|
|
||
| public IEnumerator<ICodeBlock> GetEnumerator () => Blocks.GetEnumerator (); | ||
| IEnumerator IEnumerable.GetEnumerator () => GetEnumerator (); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| using System.IO; | ||
|
|
||
| public interface ICodeBlock { | ||
| public void Print (TextWriter writer); | ||
| public void SetIndent (int indent); | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
|
|
||
| public class IfBlock : CodeBlock { | ||
| List<IfBlock> ElseIfBlocks = new (); | ||
| CodeBlock? ElseBlock = null; | ||
| public IfBlock (string condition) | ||
| { | ||
| HeaderText = condition; | ||
| } | ||
|
|
||
| public IfBlock (string condition, List<ICodeBlock> blocks) | ||
| { | ||
| HeaderText = condition; | ||
| Blocks.AddRange (blocks); | ||
| } | ||
|
|
||
| public IfBlock (string condition, params string [] lines) | ||
| { | ||
| HeaderText = condition; | ||
| Blocks.AddRange (new CodeBlock (lines)); | ||
| } | ||
|
|
||
| public IfBlock AddElseIf (string condition, List<ICodeBlock> blocks) | ||
| { | ||
| ElseIfBlocks.Add (new IfBlock (condition, blocks)); | ||
| return this; | ||
| } | ||
|
|
||
| public IfBlock AddElseIf (string condition, params string [] lines) | ||
| { | ||
| ElseIfBlocks.Add (new IfBlock (condition, lines)); | ||
| return this; | ||
| } | ||
|
|
||
| public IfBlock AddElse (List<ICodeBlock> blocks) | ||
| { | ||
| ElseBlock = new CodeBlock ("else", blocks); | ||
| return this; | ||
| } | ||
|
|
||
| public IfBlock AddElse (params string [] lines) | ||
| { | ||
| ElseBlock = new CodeBlock ("else", lines); | ||
| return this; | ||
| } | ||
|
|
||
| protected override void WriteHeaderText (TextWriter writer) | ||
| { | ||
| WriteIndent (writer); | ||
| writer.Write ("if ("); | ||
| writer.Write (HeaderText); | ||
| writer.Write (')'); | ||
| writer.Write (NewLine); | ||
| } | ||
|
|
||
| protected void WriteElseHeaderText (TextWriter writer) | ||
| { | ||
| WriteIndent (writer); | ||
| writer.Write ("else if ("); | ||
| writer.Write (HeaderText); | ||
| writer.Write (')'); | ||
| writer.Write (NewLine); | ||
| } | ||
|
|
||
| public override void Print (TextWriter writer) | ||
| { | ||
| if (HeaderText != string.Empty) | ||
| WriteHeaderText (writer); | ||
|
|
||
| WriteIndent (writer); | ||
| writer.Write (StartBrace); | ||
| writer.Write (NewLine); | ||
|
|
||
| SetIndent (CurrentIndent + Indent); | ||
| foreach (ICodeBlock block in Blocks) { | ||
| block.SetIndent (CurrentIndent); | ||
| block.Print (writer); | ||
| } | ||
| SetIndent (CurrentIndent - Indent); | ||
| WriteIndent (writer); | ||
| writer.Write (EndBrace); | ||
| writer.Write (NewLine); | ||
|
|
||
| foreach (IfBlock block in ElseIfBlocks) { | ||
| block.SetIndent (CurrentIndent); | ||
| block.WriteElseHeaderText (writer); | ||
| writer.Write (StartBrace); | ||
| writer.Write (NewLine); | ||
| foreach (ICodeBlock b in block.Blocks) { | ||
| b.SetIndent (CurrentIndent + Indent); | ||
| b.Print (writer); | ||
|
|
||
| } | ||
| writer.Write (EndBrace); | ||
| writer.Write (NewLine); | ||
| } | ||
|
|
||
| if (ElseBlock is not null) | ||
| ElseBlock.Print (writer); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| using System.IO; | ||
|
|
||
| public class LineBlock : ICodeBlock { | ||
| readonly string line; | ||
| int currentIndent = 0; | ||
| const char newLine = '\n'; | ||
|
|
||
| public LineBlock (string line) | ||
| { | ||
| this.line = line; | ||
| } | ||
|
|
||
| public void SetIndent (int indent) | ||
| { | ||
| currentIndent = indent; | ||
| } | ||
|
|
||
| public void WriteIndent (TextWriter writer) | ||
| { | ||
| for (var i = 0; i < currentIndent; i++) | ||
| writer.Write (' '); | ||
| } | ||
|
|
||
| public void Print (TextWriter writer) | ||
| { | ||
| WriteIndent (writer); | ||
| writer.Write (line); | ||
| writer.Write (newLine); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| public class MethodBlock : CodeBlock { | ||
| public MethodBlock (string methodSignature, params string [] parameters) | ||
| { | ||
| StringBuilder allParameters = new (); | ||
| for (int i = 0; i < parameters.Length; i++) { | ||
| allParameters.Append (parameters [i]); | ||
| if (i != parameters.Length - 1) { | ||
| allParameters.Append (", "); | ||
| } | ||
| } | ||
| HeaderText = methodSignature + "(" + allParameters + ")"; | ||
| } | ||
|
|
||
| public MethodBlock (string methodSignature, List<ICodeBlock> blocks, params string [] parameters) | ||
| { | ||
| StringBuilder allParameters = new (); | ||
| for (int i = 0; i < parameters.Length; i++) { | ||
| allParameters.Append (parameters [i]); | ||
| if (i != parameters.Length - 1) { | ||
| allParameters.Append (", "); | ||
| } | ||
| } | ||
| HeaderText = methodSignature + "(" + allParameters + ")"; | ||
| this.Blocks = blocks; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A lot of string concatenation in this class.