aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Horizon.Generators/CodeGenerator.cs
blob: 59052499e82d0c4c6aab36393f821372c1cf77fe (plain) (blame)
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
using System.Text;

namespace Ryujinx.Horizon.Generators
{
    class CodeGenerator
    {
        private const int IndentLength = 4;

        private readonly StringBuilder _sb;
        private int _currentIndentCount;

        public CodeGenerator()
        {
            _sb = new StringBuilder();
        }

        public void EnterScope(string header = null)
        {
            if (header != null)
            {
                AppendLine(header);
            }

            AppendLine("{");
            IncreaseIndentation();
        }

        public void LeaveScope(string suffix = "")
        {
            DecreaseIndentation();
            AppendLine($"}}{suffix}");
        }

        public void IncreaseIndentation()
        {
            _currentIndentCount++;
        }

        public void DecreaseIndentation()
        {
            if (_currentIndentCount - 1 >= 0)
            {
                _currentIndentCount--;
            }
        }

        public void AppendLine()
        {
            _sb.AppendLine();
        }

        public void AppendLine(string text)
        {
            _sb.Append(' ', IndentLength * _currentIndentCount);
            _sb.AppendLine(text);
        }

        public override string ToString()
        {
            return _sb.ToString();
        }
    }
}