-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCsSourceFormatter.cs
More file actions
125 lines (111 loc) · 4.41 KB
/
CsSourceFormatter.cs
File metadata and controls
125 lines (111 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Collections.Generic;
using System.Text;
namespace CodeFactory.Formatting.CSharp
{
/// <summary>
/// Helper class that adds indent levels correctly for source code formatting.
/// </summary>
public class CsSourceFormatter
{
/// <summary>
/// Field that holds the indent statement to be used in code blocks
/// </summary>
private readonly string _indentStatement;
/// <summary>
/// The string builder used to format the source code.
/// </summary>
readonly StringBuilder _codeFormatter = new StringBuilder();
/// <summary>
/// Creates a new instance of the <see cref="CsSourceFormatter"/>
/// </summary>
/// <param name="indentStatement">Optional parameter that allows you to set the target type of indent that will occur with each code statement.</param>
public CsSourceFormatter(string indentStatement = "\t")
{
_indentStatement = indentStatement;
}
/// <summary>
/// Appends code to the end of the current line in the formatter.
/// </summary>
/// <param name="code">The code to append.</param>
public void AppendCode(string code)
{
if(string.IsNullOrEmpty(code))return;
_codeFormatter.Append(code);
}
/// <summary>
/// Appends a new line of code to the formatter.
/// </summary>
/// <param name="indentLevel">The number of indent levels to add to the source code.</param>
/// <param name="code">The code to add to the formatter.</param>
public void AppendCodeLine(int indentLevel, string code)
{
AppendCodeLine(indentLevel);
//Bug fix update for Issue #13
if (!string.IsNullOrEmpty(code)) _codeFormatter.Append(code);
}
/// <summary>
/// Appends a new line of code to the formatter.
/// </summary>
/// <param name="indentLevel">The number of indent levels to add to the source code.</param>
public void AppendCodeLine(int indentLevel)
{
_codeFormatter.AppendLine();
if (indentLevel > 0)
{
int currentLevel = 0;
while (currentLevel < indentLevel)
{
_codeFormatter.Append(_indentStatement);
currentLevel++;
}
}
}
/// <summary>
/// Appends a target indent level to a already formatted block of code.
/// </summary>
/// <param name="indentLevel">The target indent level to be added to the existing code block.</param>
/// <param name="codeBlock">The block of code to append to.</param>
public void AppendCodeBlock(int indentLevel, string codeBlock)
{
if(string.IsNullOrEmpty(codeBlock)) return;
//Split the existing documentation into individual lines to be processed.
var codeLines = codeBlock.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
//iterate over each document line and confirm it can be formatted for C# xml documentation.
foreach (string codeLine in codeLines)
{
AppendCodeLine(indentLevel,codeLine);
}
}
/// <summary>
/// Appends a target indent level to a already formatted block of code.
/// </summary>
/// <param name="indentLevel">The target indent level to be added to the existing code block.</param>
/// <param name="codeBlock">The block of code to append to.</param>
public void AppendCodeBlock(int indentLevel, IEnumerable<string> codeBlock)
{
if (codeBlock == null) return;
//iterate over each document line and confirm it can be formatted for C# xml documentation.
foreach (string codeLine in codeBlock)
{
AppendCodeLine(indentLevel, codeLine);
}
}
/// <summary>
/// Returns the formatted source code.
/// </summary>
/// <returns>Formatted SourceCode.</returns>
public string ReturnSource()
{
return _codeFormatter.ToString();
}
/// <summary>
/// Clears the formatter to be reused.
/// </summary>
public void ResetFormatter()
{
_codeFormatter.Clear();
}
}
}