Repository navigation
Expand file tree
/
Copy pathCodeWriter.cs
More file actions
100 lines (88 loc) · 2.42 KB
/
Copy pathCodeWriter.cs
File metadata and controls
100 lines (88 loc) · 2.42 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
using System;
using System.Collections.Generic;
using System.Text;
namespace CodeMinion.Core.Helpers
{
public class CodeWriter
{
public CodeWriter() { }
public CodeWriter(StringBuilder s)
{
StringBuilder = s;
}
public StringBuilder StringBuilder { get; set; } = new StringBuilder();
public int IndentSpaces { get; set; } = 4;
private int _level = 0;
/// <summary>
/// Write a line (with automatic indentation), line break is appended
/// </summary>
public void Out(string line, Action block=null)
{
if (_level > 0)
StringBuilder.Append(new String(' ', IndentSpaces * _level));
StringBuilder.AppendLine(line);
if (block==null)
return;
Block(block);
}
/// <summary>
/// Alias of Out
/// </summary>
public void AppendLine(string s="")
{
Out(s);
}
/// <summary>
/// Insert an empty line
/// </summary>
public void Break()
{
Out("");
}
/// <summary>
/// Increase the level of indentation
/// </summary>
public void Indent()
{
_level++;
}
/// <summary>
/// Decrease the level of indentation
/// </summary>
public void Outdent()
{
_level--;
}
/// <summary>
/// Increase the level of indentation for the generator action, then outdent
/// </summary>
public void Indent(Action a)
{
_level++;
try
{
a();
}
finally
{
_level--;
}
}
/// <summary>
/// Generate an indented code block
/// </summary>
/// <param name="a"></param>
/// <param name="opening_brace"></param>
/// <param name="closing_brace"></param>
public void Block(Action a, string opening_brace = "{", string closing_brace = "}")
{
Out(opening_brace);
Indent(a);
Out(closing_brace);
}
public override string ToString()
{
return StringBuilder.ToString();
}
}
}