forked from thedeemon/gep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemplatesForm.cs
147 lines (127 loc) · 4.06 KB
/
TemplatesForm.cs
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace gep
{
partial class TemplatesForm : Form
{
public TemplatesForm()
{
InitializeComponent();
}
private void OnLoad(object sender, EventArgs e)
{
int i = languageCombo.Items.Add(new CodeGenCPP());
languageCombo.Items.Add(new CodeGenCS());
languageCombo.SelectedIndex = i;
}
private void OnSelChange(object sender, EventArgs e)
{
CodeSnippet snp = (CodeSnippet)snippetsList.SelectedItem;
if (snp == null) return;
templateText.Text = snp.Text;
variablesText.Text = snp.Description;
exampleText.Text = snp.Generate();
}
private void OnTemplateChanged(object sender, EventArgs e)
{
CodeSnippet snp = (CodeSnippet)snippetsList.SelectedItem;
if (snp == null) return;
snp.Text = templateText.Text;
exampleText.Text = snp.Generate();
}
private void OnOK(object sender, EventArgs e)
{
foreach (object o in languageCombo.Items)
{
CodeGenBase cg = (CodeGenBase)o;
cg.SaveTemplates();
}
Close();
}
private void OnCancel(object sender, EventArgs e)
{
Close();
}
private void OnRestoreDefaults(object sender, EventArgs e)
{
foreach (object o in snippetsList.Items)
{
CodeSnippet snp = (CodeSnippet)o;
snp.RestoreDefault();
}
OnSelChange(null, null);
}
private void OnLangChange(object sender, EventArgs e)
{
CodeGenBase cg = (CodeGenBase)languageCombo.SelectedItem;
if (cg == null) return;
snippetsList.Items.Clear();
foreach (CodeSnippet snp in cg.snippets)
snippetsList.Items.Add(snp);
templateText.Clear();
variablesText.Clear();
exampleText.Clear();
}
}
class CodeSnippet
{
string caption;
string default_text;
string text;
string description;
string codename;
Dictionary<string, string> vars = new Dictionary<string, string>();
public CodeSnippet(string _caption, string _codename, string _deftext, string _descr)
{
caption = _caption; text = default_text = _deftext; description = _descr;
codename = _codename;
}
public override string ToString()
{
return caption;
}
public string Text
{
get { return text; }
set { text = value; }
}
public string Caption { get { return caption; } }
public string Description { get { return description; } }
public string Codename { get { return codename; } }
public void SetVar(string var, string val)
{
vars[var] = val;
}
public void Clear()
{
vars.Clear();
}
public void SetVars(string[] vrs)
{
for (int i = 0; i < vrs.Length / 2; i++)
SetVar(vrs[i * 2], vrs[i * 2 + 1]);
}
public void RestoreDefault()
{
text = default_text;
}
public string Generate()
{
string s = text;
foreach (string var in vars.Keys)
s = s.Replace(var, vars[var]);
return s;
}
public string GenerateWith(string[] vrs)
{
return Translate(text, vrs);
}
public static string Translate(string s, string[] vrs)
{
for (int i = 0; i < vrs.Length / 2; i++)
s = s.Replace(vrs[i * 2], vrs[i * 2 + 1]);
return s;
}
}
}