Skip to content

Commit

Permalink
Version 0.9.4 - colorized output
Browse files Browse the repository at this point in the history
  • Loading branch information
TaoK committed May 1, 2011
1 parent 498a83c commit d6b4f7b
Show file tree
Hide file tree
Showing 13 changed files with 546 additions and 262 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.txt
@@ -1,5 +1,11 @@
Poor Man's T-SQL Formatter change log

Version 0.9.4:
--------------

- Added HTML syntax colorizing (exposed in demo app and web service)


Version 0.9.3:
--------------

Expand Down
@@ -0,0 +1,57 @@
/*
Poor Man's T-SQL Formatter - a small free Transact-SQL formatting
library for .Net 2.0, written in C#.
Copyright (C) 2011 Tao Klerks
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace PoorMansTSqlFormatterDemo.FrameworkClassReplacements
{
public class CustomContentWebBrowser : System.Windows.Forms.WebBrowser
{
// WebBrowser control, modified to allow easy setting of HTML content, based on:
//http://weblogs.asp.net/gunnarpeipman/archive/2009/08/15/displaying-custom-html-in-webbrowser-control.aspx
// Also disabling navigation sound, as per:
//https://connect.microsoft.com/VisualStudio/feedback/details/345528/webbrowser-control-in-wpf-disable-sound

private const int DISABLE_NAVIGATION_SOUNDS = 21;
private const int SET_FEATURE_ON_PROCESS = 0x00000002;

[DllImport("urlmon.dll")]
[PreserveSig]
[return: MarshalAs(UnmanagedType.Error)]
static extern int CoInternetSetFeatureEnabled(int FeatureEntry, [MarshalAs(UnmanagedType.U4)] int dwFlags, bool fEnable);

public void SetHTML(string htmlContent)
{
bool allowedNavigation = AllowNavigation;
AllowNavigation = true;
CoInternetSetFeatureEnabled(DISABLE_NAVIGATION_SOUNDS, SET_FEATURE_ON_PROCESS, true);
this.Navigate("about:blank");
if (this.Document != null)
{
this.Document.Write(string.Empty);
}
this.DocumentText = htmlContent;
CoInternetSetFeatureEnabled(DISABLE_NAVIGATION_SOUNDS, SET_FEATURE_ON_PROCESS, false);
AllowNavigation = allowedNavigation;
}
}
}
435 changes: 271 additions & 164 deletions PoorMansTSqlFormatterDemo/MainForm.Designer.cs

Large diffs are not rendered by default.

123 changes: 78 additions & 45 deletions PoorMansTSqlFormatterDemo/MainForm.cs
Expand Up @@ -32,6 +32,9 @@ public partial class MainForm : Form
PoorMansTSqlFormatterLib.Interfaces.ISqlTokenParser _parser;
PoorMansTSqlFormatterLib.Interfaces.ISqlTreeFormatter _formatter;

bool _queuedRefresh = false;
object _refreshLock = new object();

public MainForm()
{
InitializeComponent();
Expand All @@ -40,71 +43,101 @@ public MainForm()
SetFormatter();
}

private void txt_Input_Leave(object sender, EventArgs e)
{
DoFormatting();
}

private void radio_Formatting_Standard_CheckedChanged(object sender, EventArgs e)
private void SettingsControlChanged(object sender, EventArgs e)
{
SetFormatter();
DoFormatting();
TryToDoFormatting();
}

private void radio_Formatting_Identity_CheckedChanged(object sender, EventArgs e)
{
SetFormatter();
DoFormatting();
}

private void chk_ExpandParens_CheckedChanged(object sender, EventArgs e)
{
SetFormatter();
DoFormatting();
}

private void chk_TrailingCommas_CheckedChanged(object sender, EventArgs e)
private void SetFormatter()
{
SetFormatter();
DoFormatting();
if (radio_Formatting_Standard.Checked)
{
_formatter = new PoorMansTSqlFormatterLib.Formatters.TSqlStandardFormatter("\t", chk_ExpandCommaLists.Checked, chk_TrailingCommas.Checked, chk_ExpandBooleanExpressions.Checked, chk_ExpandCaseStatements.Checked, chk_UppercaseKeywords.Checked, chk_Coloring.Checked);
}
else
_formatter = new PoorMansTSqlFormatterLib.Formatters.TSqlIdentityFormatter();
}

private void chk_ExpandBooleanExpressions_CheckedChanged(object sender, EventArgs e)
private void DoFormatting()
{
SetFormatter();
DoFormatting();
var tokenizedSql = _tokenizer.TokenizeSQL(txt_Input.Text);
txt_TokenizedXml.Text = tokenizedSql.PrettyPrint();
var parsedSql = _parser.ParseSQL(tokenizedSql);
txt_ParsedXml.Text = parsedSql.OuterXml;
string formattedSql = _formatter.FormatSQLTree(parsedSql);
if (!chk_Coloring.Checked)
formattedSql = System.Web.HttpUtility.HtmlEncode(formattedSql);

string displayHtmlPage = @"
<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd""><html>
<head>
<style type=""text/css"">
.SQLCode {{
font-size: 13px;
font-weight: bold;
font-family: Consolas,'Lucida Console','DejaVu Sans Mono',monospace;;
white-space: pre;
}}
.SQLComment {{
color: #00AA00;
}}
.SQLString {{
color: #AA0000;
}}
.SQLFunction {{
color: #AA00AA;
}}
.SQLKeyword {{
color: #0000AA;
}}
.SQLOperator {{
color: #AAAAAA;
}}
</style>
</head>
<body>
<div class=""SQLCode"">{0}</div>
</body>
</html>
";
webBrowser_OutputSql.SetHTML(string.Format(displayHtmlPage, formattedSql));
}


private void chk_ExpandCaseStatements_CheckedChanged(object sender, EventArgs e)
private void TryToDoFormatting()
{
SetFormatter();
DoFormatting();
lock (_refreshLock)
{
if (timer_TextChangeDelay.Enabled)
_queuedRefresh = true;
else
{
DoFormatting();
timer_TextChangeDelay.Start();
}
}
}

private void chk_UppercaseKeywords_CheckedChanged(object sender, EventArgs e)
private void txt_Input_TextChanged(object sender, EventArgs e)
{
SetFormatter();
DoFormatting();
TryToDoFormatting();
}

private void SetFormatter()
private void timer_TextChangeDelay_Tick(object sender, EventArgs e)
{
if (radio_Formatting_Standard.Checked)
timer_TextChangeDelay.Enabled = false;
lock (_refreshLock)
{
_formatter = new PoorMansTSqlFormatterLib.Formatters.TSqlStandardFormatter("\t", chk_ExpandCommaLists.Checked, chk_TrailingCommas.Checked, chk_ExpandBooleanExpressions.Checked, chk_ExpandCaseStatements.Checked, chk_UppercaseKeywords.Checked);
if (_queuedRefresh)
{
DoFormatting();
timer_TextChangeDelay.Start();
_queuedRefresh = false;
}
}
else
_formatter = new PoorMansTSqlFormatterLib.Formatters.TSqlIdentityFormatter();
}

private void DoFormatting()
{
var tokenizedSql = _tokenizer.TokenizeSQL(txt_Input.Text);
txt_TokenizedXml.Text = tokenizedSql.PrettyPrint();
var parsedSql = _parser.ParseSQL(tokenizedSql);
txt_ParsedXml.Text = parsedSql.OuterXml;
txt_OutputSql.Text = _formatter.FormatSQLTree(parsedSql);
}

}
Expand Down
6 changes: 6 additions & 0 deletions PoorMansTSqlFormatterDemo/MainForm.resx
Expand Up @@ -117,4 +117,10 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="timer_TextChangeDelay.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>15, 5</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>34</value>
</metadata>
</root>
4 changes: 4 additions & 0 deletions PoorMansTSqlFormatterDemo/PoorMansTSqlFormatterDemo.csproj
Expand Up @@ -35,10 +35,14 @@
<Reference Include="System.Data" />
<Reference Include="System.Deployment" />
<Reference Include="System.Drawing" />
<Reference Include="System.Web" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="FrameworkClassReplacements\CustomContentWebBrowser.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="FrameworkClassReplacements\SelectableTextBox.cs">
<SubType>Component</SubType>
</Compile>
Expand Down
2 changes: 1 addition & 1 deletion PoorMansTSqlFormatterDemo/Properties/AssemblyInfo.cs
Expand Up @@ -31,4 +31,4 @@
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: AssemblyVersion("0.9.*")]
[assembly: AssemblyVersion("0.9.4.*")]

0 comments on commit d6b4f7b

Please sign in to comment.