-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
144 lines (127 loc) · 3.99 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace ResharperCodeTemplateToVSCodeSnippet
{
class Program
{
static void Main(string[] args)
{
if (args.Length != 1 || !File.Exists(args[0])) {
Console.WriteLine(@"Usage: .\T2S.exe ExportedTemplates.DotSettings");
return;
}
try {
ProcessFile(args[0]);
}
catch {
Console.WriteLine("Unreconized DotSettings file.");
}
}
class DataModel
{
public string Shortcut;
public string Description;
public string[] Body;
public override string ToString()
{
return $"Shortcut: {Shortcut ?? "(null)"}";
}
}
static void ProcessFile(string sourceFile)
{
const string LeadingKeyString = "/Default/PatternsAndTemplates/LiveTemplates/Template/=";
var document = XDocument.Load(sourceFile);
if (document.Root == null) {
throw new NotSupportedException();
}
//Get and organize content from xaml
var content = new Dictionary<string, DataModel>();
foreach (var rootItem in document.Root.Elements().Where(item => item.Name.LocalName == "String" && item.FirstAttribute?.Name.LocalName == "Key")) {
var key = rootItem.FirstAttribute.Value;
var value = rootItem.Value;
if (key.StartsWith(LeadingKeyString) && key.Length > LeadingKeyString.Length + 32) {
var id = key.Substring(LeadingKeyString.Length, 32);
var item = content.GetOrAdd(id);
var type = key.Substring(LeadingKeyString.Length + 32 + 1).Split('/');
switch (type[0]) {
case "Shortcut":
item.Shortcut = value;
break;
case "Description":
item.Description = value;
break;
case "Text":
item.Body = value.Replace("\r", "").Split('\n');
break;
}
}
}
//Filter and sort
var codeTemplates = content.Values.Where(item => !string.IsNullOrEmpty(item.Shortcut) && item.Body?.Length > 0 && !string.IsNullOrEmpty(item.Body[0])).OrderBy(item => item.Shortcut).ToList();
//Convert all $XXX$ indicators
var placeHolder = new Dictionary<string, int>();
foreach (var template in codeTemplates) {
placeHolder.Clear();
foreach (var line in template.Body) {
int currentIndex = 0;
while (true) {
int startIndex = line.IndexOf('$', currentIndex);
if (startIndex == -1) {
break;;
}
int endIndex = line.IndexOf('$', startIndex + 1);
if (endIndex == -1) {
break;;
}
currentIndex = endIndex + 1;
string name = line.Substring(startIndex + 1, endIndex - startIndex - 1);
if (name != "END") {
if (!placeHolder.TryGetValue(name, out var number)) {
number = placeHolder.Count + 1;
placeHolder.Add(name, number);
}
}
}
}
for (int index = 0; index < template.Body.Length; ++index) {
template.Body[index] = template.Body[index].Replace("$END$", "$0");
foreach (var item in placeHolder) {
template.Body[index] = template.Body[index].Replace($"${item.Key}$", $"${{{item.Value}:{item.Key}}}");
}
}
}
//Generate
var builder = new StringBuilder(4096);
builder.Append("{\n");
foreach (var template in codeTemplates) {
builder.Append($"\t\"{template.Shortcut}\":{{\n");
{
builder.Append($"\t\t\"prefix\": \"{template.Shortcut}\",\n");
if (!string.IsNullOrEmpty(template.Description)) {
builder.Append($"\t\t\"description\": \"{template.Description}\",\n");
}
builder.Append("\t\t\"body\": ");
if (template.Body.Length == 1) {
builder.Append($"\"{template.Body[0]}\"\n");
}
else {
builder.Append("[\n");
foreach (var line in template.Body) {
builder.Append($"\t\t\t\"{line.Replace("\t", "\\t")}\",\n");
}
builder.Append("\t\t]\n");
}
}
builder.Append("\t},\n");
}
builder.Append("}");
//Output
var targetFile = Path.Combine(Path.GetDirectoryName(sourceFile), "csharp.json");
File.WriteAllText(targetFile, builder.ToString());
}
}
}