-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLFormatterPlugin.cs
193 lines (163 loc) · 6.31 KB
/
SQLFormatterPlugin.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using System.Text;
using MsaSQLEditor; //From MsaSQLEditor.dll
using System.IO;
using System.Diagnostics;
using System.Reflection;
using System.Windows.Forms;
using System.ComponentModel;
using System.Configuration;
using System.Xml.Serialization;
namespace SQLFormatterPlugin
{
/// <summary>
/// To create a plugin, add references in your project to MsaSQLEditor.dll
/// and ScintillaNET.dll, both of which are in your Access SQL Editor installation
/// folder.
/// </summary>
public class SQLFormatterPlugin : IPlugin
{
private SQLFormatterOptions _Options = null;
/// <summary>
/// Name is property of the IPlugin interface, and must be implemented.
/// Give your plugin a descriptive name.
/// </summary>
public string Name
{
get
{
return "Poor Man's T-SQL Formatter";
}
}
/// <summary>
/// ShortcutKeys is a property from IPlugin. Provide shortcut keys
/// for your plugin, or return null to only allow access through the
/// menu.
/// </summary>
public Keys ShortcutKeys
{
get
{
return Keys.Control | Keys.D1;
}
}
/// <summary>
/// Options is a property from IPlugin. Note the return type is dynamic.
/// </summary>
public dynamic Options
{
get
{
return this._Options;
}
}
private void SaveSettings()
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(SQLFormatterOptions));
using (var memStream = new MemoryStream())
{
xmlSerializer.Serialize(memStream, _Options);
memStream.Position = 0;
using (var streamReader = new StreamReader(memStream))
{
string xml = streamReader.ReadToEnd();
xml = xml.Replace("<IndentString>", "<IndentString xml:space=\"preserve\">");
Properties.Settings.Default.SQLFormatterOptions = xml;
Properties.Settings.Default.Save();
}
}
}
private void LoadSettings()
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(SQLFormatterOptions));
using (var memStream = new MemoryStream(Encoding.UTF8.GetBytes(Properties.Settings.Default.SQLFormatterOptions)))
{
try
{
_Options = (SQLFormatterOptions)xmlSerializer.Deserialize(memStream);
_Options.SetCallbacks(this.SaveSettings, this.LoadSettings);
}
catch { }
}
}
public SQLFormatterPlugin()
{
_Options = new SQLFormatterOptions(this.SaveSettings, this.LoadSettings);
LoadSettings();
}
/// <summary>
/// PerformAction is the action that is taken when your plugin is called.
/// </summary>
/// <param name="context">Currently, context only supplies a reference to the
/// ScintillaNET editor you're using, but in the future this will be
/// expanded.</param>
public void PerformAction(IPluginContext context)
{
StringBuilder ExeArgs = new StringBuilder();
if (_Options.TrailingCommas)
ExeArgs.Append("--trailingcommas ");
if (_Options.SpaceAfterComma)
ExeArgs.Append("--spaceaftercomma ");
ExeArgs
.AppendFormat("--spacespertab {0} ", _Options.SpacesPerTab);
ExeArgs
.AppendFormat("--indentstring \"{0}\" ", _Options.IndentString);
ExeArgs
.AppendFormat("--newstatementlinebreaks {0} ", _Options.NewStatementLineBreaks);
ExeArgs
.AppendFormat("--newclauselinebreaks {0} ", _Options.NewClauseLineBreaks);
ExeArgs
.AppendFormat("--maxlinewidth {0} ", _Options.MaxLineWidth);
if (_Options.ExpandCommaLists)
ExeArgs.Append("--expandcommalists ");
if (_Options.ExpandBooleanExpressions)
ExeArgs.Append("--expandbooleanexpressions ");
if (_Options.ExpandCaseStatements)
ExeArgs.Append("--expandcasestatements ");
if (_Options.ExpandBetweenConditions)
ExeArgs.Append("--expandbetweenconditions ");
if (_Options.BreakJoinOnSections)
ExeArgs.Append("--breakjoinonsections ");
if (_Options.UppercaseKeywords)
ExeArgs.Append("--uppercasekeywords ");
if (_Options.KeywordStandardization)
ExeArgs.Append("--keywordstandardization ");
if (_Options.ExpandInLists)
ExeArgs.Append("--expandinlists ");
ProcessStartInfo pi = new ProcessStartInfo(ExePath, ExeArgs.ToString());
pi.RedirectStandardInput = true;
pi.RedirectStandardOutput = true;
pi.RedirectStandardError = true;
pi.UseShellExecute = false;
pi.ErrorDialog = false;
pi.CreateNoWindow = true;
try
{
using (Process p = new Process())
{
p.StartInfo = pi;
p.Start();
p.StandardInput.Write(Convert.ToBase64String(Encoding.UTF8.GetBytes(context.Editor.Text)));
p.StandardInput.Write((char)0x04);
string Result = p.StandardOutput.ReadToEnd();
Result = Encoding.UTF8.GetString(Convert.FromBase64String(Result));
p.WaitForExit();
context.Editor.Text = Result;
}
}
catch(Win32Exception)
{
context.Editor.Text = "-- !! Couldn't find SQLFormatter.exe!\r\n\r\n" + context.Editor.Text;
}
}
private string ExePath
{
get
{
return Path.Combine(
Directory.GetParent(Assembly.GetExecutingAssembly().Location).FullName,
"SQLFormatter.exe");
}
}
}
}