Skip to content

Commit

Permalink
Ctrl+Enter to execute the surrounding statement
Browse files Browse the repository at this point in the history
  • Loading branch information
Rekkonnect committed May 28, 2023
1 parent aeb5d5f commit 2883213
Show file tree
Hide file tree
Showing 14 changed files with 495 additions and 328 deletions.
2 changes: 1 addition & 1 deletion AboutForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions ColorComboBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ You should have received a copy of the GNU General Public License
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Windows.Forms;

Expand Down Expand Up @@ -96,8 +97,8 @@ public static void SetectColor(ComboBox comboBox, Color color)

private static void Init()
{
grayDotPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
blackDotPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
grayDotPen.DashStyle = DashStyle.Dot;
blackDotPen.DashStyle = DashStyle.Dot;

var colors = Enum.GetValues(typeof(KnownColor))
.Cast<KnownColor>()
Expand Down
16 changes: 15 additions & 1 deletion CompactView.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
<Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.SqlServer.Compact.4.0.8876.1\lib\net40\System.Data.SqlServerCe.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
Expand Down Expand Up @@ -112,13 +115,16 @@
<Compile Include="OptionsForm.Designer.cs">
<DependentUpon>OptionsForm.cs</DependentUpon>
</Compile>
<Compile Include="PrintType.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Settings.cs" />
<Compile Include="RichTextBoxExtension.cs" />
<Compile Include="RichTextBoxExtensions.cs" />
<Compile Include="RichTextBoxHelper.cs" />
<Compile Include="Settings.cs" />
<Compile Include="SqlCeBase.cs" />
<Compile Include="SqlCeDb.cs" />
<Compile Include="SqlCeNativeErrors.cs" />
<Compile Include="SqlParser.cs" />
<Compile Include="ToolsForm.cs">
<SubType>Form</SubType>
Expand Down Expand Up @@ -162,6 +168,7 @@
<None Include="CHANGE.LOG" />
<None Include="CompactView_TemporaryKey.pfx" />
<None Include="COPYING" />
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down Expand Up @@ -273,6 +280,13 @@
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>
if not exist "$(TargetDir)x86" md "$(TargetDir)x86"
xcopy /s /y "$(SolutionDir)packages\Microsoft.SqlServer.Compact.4.0.8876.1\NativeBinaries\x86\*.*" "$(TargetDir)x86"
if not exist "$(TargetDir)amd64" md "$(TargetDir)amd64"
xcopy /s /y "$(SolutionDir)packages\Microsoft.SqlServer.Compact.4.0.8876.1\NativeBinaries\amd64\*.*" "$(TargetDir)amd64"</PostBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
2 changes: 1 addition & 1 deletion GlobalText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private static void FillEnglish()
{ "Paste", "Paste" },
{ "LoadFromFile", "Load from file" },
{ "SaveToFile", "Save to file" },
{ "Querys", "Querys" },
{ "Queries", "Queries" },
{ "Print", "Print" },
{ "File", "File" },
{ "RecentFiles", "RecentFiles" },
Expand Down
3 changes: 2 additions & 1 deletion MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 81 additions & 7 deletions MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ You should have received a copy of the GNU General Public License
using System;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
Expand Down Expand Up @@ -419,17 +421,32 @@ private void dataGrid_CellDoubleClick(object sender, DataGridViewCellEventArgs e

private void mainForm_KeyDown(object sender, KeyEventArgs e)
{
// On F5 key press, execute the query
if (e.KeyData == Keys.F5 && rtbQuery.Focused)
btnExecute.PerformClick();
switch (e.KeyData)
{
// On F5 key press, execute the query
case Keys.F5:
if (rtbQuery.Focused)
{
ExecuteQuery(true);
}
break;
}
}

private void btnExecute_Click(object sender, EventArgs e)
{
if (rtbQuery.Text.Trim().Length == 0)
ExecuteQuery(true);
}

private void ExecuteQuery(bool shouldWarnPartialSelection)
{
bool isEmptySql = string.IsNullOrWhiteSpace(rtbQuery.Text);
if (isEmptySql)
return;
bool partial = rtbQuery.SelectedText.Trim().Length > 0;
if (partial)

bool partial = !string.IsNullOrWhiteSpace(rtbQuery.SelectedText);
shouldWarnPartialSelection &= partial;
if (shouldWarnPartialSelection)
{
DialogResult result = MessageBox.Show(GlobalText.GetValue("SelectedTextQuery"), GlobalText.GetValue("Confirm"),
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
Expand All @@ -453,7 +470,7 @@ private void btnExecute_Click(object sender, EventArgs e)
if (resultSet != null || string.IsNullOrEmpty(db.LastError))
{
lbResult.ForeColor = Color.Black;
lbResult.Text = $"{db.QueryCount} {GlobalText.GetValue("Querys")}, {dataGrid.RowCount} {GlobalText.GetValue("Rows")}, {ms} {GlobalText.GetValue("Milliseconds")}";
lbResult.Text = $"{db.QueryCount} {GlobalText.GetValue("Queries")}, {dataGrid.RowCount} {GlobalText.GetValue("Rows")}, {ms} {GlobalText.GetValue("Milliseconds")}";
if (resultSet == null && regexCreateAlterDrop.IsMatch(regexDropQuotesAndBrackets.Replace(rtbQuery.Text, string.Empty)))
{
db.ResetDdl(); // Update DDL
Expand Down Expand Up @@ -644,6 +661,63 @@ private void rtbQuery_Enter_Leave_SelectionChanged(object sender, EventArgs e)
UpdateStatus();
}

private void rtbQuery_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyData)
{
case Keys.Enter | Keys.Control:
// Only select the text around the current statement
if (rtbQuery.Focused)
{
SelectCurrentStatementAroundCursor();
}
ExecuteQuery(false);
e.Handled = true;
break;
}
}

private void SelectCurrentStatementAroundCursor()
{
int selectedIndex = rtbQuery.SelectionStart;
if (selectedIndex >= rtbQuery.Text.Length)
selectedIndex = rtbQuery.Text.Length - 1;

var statements = db.GetSqlStatements(rtbQuery.Text).ToArray();

foreach (var statement in statements)
{
bool shouldSelect = ShouldSelectStatement(statement, selectedIndex);

if (shouldSelect)
{
SelectStatement(statement);
return;
}
}

if (statements.Length > 0)
{
var lastStatement = statements.Last();
SelectStatement(lastStatement);
}
}

private void SelectStatement(Match statement)
{
rtbQuery.Select(statement.Index, statement.Length);
}

private static bool ShouldSelectStatement(Match statement, int selectedIndex)
{
if (statement.Index > selectedIndex)
return true;

var statementEndIndex = statement.Index + statement.Length;
return statement.Index <= selectedIndex
&& selectedIndex <= statementEndIndex;
}

private void btnCut_Click(object sender, EventArgs e)
{
rtbQuery.Cut();
Expand Down
Loading

0 comments on commit 2883213

Please sign in to comment.