diff --git a/README.md b/README.md index e49a6f5..7eaadfa 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,28 @@ # SSMS Executor -SQL Server Management Studio (SSMS) extension for executing the current statement based on the cursor position. +SQL Server Management Studio (SSMS) extension for executing the current statement based on the cursor position — no need to highlight text first. -If your script contains multiple statements, position the cursor within the desired statement and press `ctrl+shift+e` to execute it. +## Features + +### Execute Current Statement + +If your script contains multiple SQL statements, place your cursor anywhere within the statement you want to run and press `Ctrl+Shift+E`. The extension automatically detects the statement boundaries and executes only that statement, leaving the rest of your script untouched. Your original cursor position and any text selection are restored after execution. + +This is available from the **Tools** menu (**Execute Statement**) or from the toolbar. + +### Execute Inner Statement + +When working with compound or nested SQL blocks (such as `BEGIN...END`), the **Execute Inner Statement** command lets you run just the innermost statement at the cursor position rather than the entire block. This is useful for debugging or testing individual statements inside stored procedures, `IF` blocks, or transaction wrappers. + +This is available from the **Tools** menu (**Execute Inner Statement**). + +### Options + +You can configure the extension under **Tools > Options > SSMS Executor**: + +| Option | Description | +|---|---| +| **Execute inner statements** | When enabled, the default `Ctrl+Shift+E` shortcut will execute the inner statement instead of the full block. | # Documentation @@ -17,5 +37,5 @@ If your script contains multiple statements, position the cursor within the desi You can download the extension from the [Releases section](https://github.com/tkwj/ssms-executor/releases) or build it yourself using the provided source code. -This version only supports SSMS 21 & 22 -For prior SSMS versions, please consult the original version here https://github.com/devvcat/ssms-executor/ +This version only supports SSMS 21 & 22. +For prior SSMS versions, please consult the original version here: https://github.com/devvcat/ssms-executor/ diff --git a/SSMSExecutor/Executor.cs b/SSMSExecutor/Executor.cs index 7315829..444c2f3 100644 --- a/SSMSExecutor/Executor.cs +++ b/SSMSExecutor/Executor.cs @@ -22,6 +22,8 @@ public Executor(DTE2 dte) _document = dte.GetDocument(); + if (_document == null) throw new InvalidOperationException("No active document."); + SaveActiveAndAnchorPoints(); } @@ -124,7 +126,7 @@ private bool IsCaretInsideStatement(TSqlStatement statement, VirtualPoint caret) if (caret.Line >= ft.Line && caret.Line <= lt.Line) { - var isBeforeFirstToken = caret.Line == ft.Line && caret.LineCharOffset < ft.Column; + var isBeforeFirstToken = caret.Line == ft.Line && caret.LineCharOffset < ft.Column + 1; var isAfterLastToken = caret.Line == lt.Line && caret.LineCharOffset > lt.Column + lt.Text.Length; if (!(isBeforeFirstToken || isAfterLastToken)) @@ -146,13 +148,13 @@ private TextBlock GetTextBlockFromStatement(TSqlStatement statement) StartPoint = new VirtualPoint { Line = ft.Line, - LineCharOffset = ft.Column + LineCharOffset = ft.Column + 1 }, EndPoint = new VirtualPoint { Line = lt.Line, - LineCharOffset = lt.Column + lt.Text.Length + LineCharOffset = lt.Column + lt.Text.Length + 1 } }; } diff --git a/SSMSExecutor/ExecutorPackage.cs b/SSMSExecutor/ExecutorPackage.cs index e900108..2b5a90a 100644 --- a/SSMSExecutor/ExecutorPackage.cs +++ b/SSMSExecutor/ExecutorPackage.cs @@ -1,4 +1,5 @@ -using Microsoft.VisualStudio; +using Devvcat.SSMS.Options; +using Microsoft.VisualStudio; using Microsoft.VisualStudio.Shell; using Microsoft.Win32; using System; diff --git a/SSMSExecutor/Helpers.cs b/SSMSExecutor/Helpers.cs index 9376490..01e0864 100644 --- a/SSMSExecutor/Helpers.cs +++ b/SSMSExecutor/Helpers.cs @@ -6,11 +6,16 @@ static class Helpers { public static bool HasActiveDocument(this DTE2 dte) { - if (dte != null && dte.ActiveDocument != null) + try { - var doc = (dte.ActiveDocument.DTE)?.ActiveDocument; - return doc != null; + if (dte != null && dte.ActiveDocument != null) + { + var doc = (dte.ActiveDocument.DTE)?.ActiveDocument; + return doc != null; + } } + catch + { } return false; } diff --git a/SSMSExecutor/SSMSExecutor.csproj b/SSMSExecutor/SSMSExecutor.csproj index e9c773b..5d98646 100644 --- a/SSMSExecutor/SSMSExecutor.csproj +++ b/SSMSExecutor/SSMSExecutor.csproj @@ -36,7 +36,7 @@ prompt 4 True - C:\Program Files\Microsoft SQL Server Management Studio 21\Release\Common7\IDE\Extensions\SSMSExecutor + C:\temp\SSMSExecutor False @@ -89,12 +89,15 @@ - 170.128.0 + 180.6.0 compile; build; native; contentfiles; analyzers; buildtransitive - + + runtime; build; native; contentfiles; analyzers; buildtransitive + all +