Skip to content

feat: add functionality to run single SQL statement at cursor (g+s)#89

Merged
Maxteabag merged 3 commits into
Maxteabag:mainfrom
LVG77:feat/run-single-query
Jan 31, 2026
Merged

feat: add functionality to run single SQL statement at cursor (g+s)#89
Maxteabag merged 3 commits into
Maxteabag:mainfrom
LVG77:feat/run-single-query

Conversation

@LVG77
Copy link
Copy Markdown
Contributor

@LVG77 LVG77 commented Jan 18, 2026

What

Adds functionality to execute only the statement at the cursor position using g+s in Normal mode. Includes semicolon-aware parsing logic and unit tests.

Why

I often work on complex queries with multiple CTEs. In my workflow it is quite useful to run separately sections of the full query for testing or just prototype on the side a subquery before integrating it. In those cases, I have each query separated by ";" so that I can run them one by one in any arbitrary order. This is the main reason why I integrated this feature. I decided to reuse the "g" menu which already existed and only integrate a single option inside the execute group.

How

Here is a high level breakdown of the main changes:

  1. SQL Parsing Logic (multi_statement.py)
    I added two new functions to handle the "single statement" detection:
  • _get_statement_ranges: This tracks the exact character start and end positions for every SQL statement in the editor. It should ignore semicolons inside strings. It supports semicolon-delimited queries.
  • find_statement_at_cursor: This takes current cursor's (row, col) position, converts it to a character offset, and finds which statement range encompasses that offset.
  1. Execution Action (query_execution.py)
    The actual execution logic:
  • action_execute_single_statement: This method fetches the current text and cursor location, calls the parsing logic to find the specific statement, and then executes the query in the same way as in _execute_query_common.

Repository owner deleted a comment from PeterAdams-ecitsolutions Jan 31, 2026
@Maxteabag
Copy link
Copy Markdown
Owner

Nice! Thanks.

I found it confusing that blank lines didn't separate SQL statements.

So if were to do
"Select 1 from X

Select 2 from Y"

And have cursor on line 1, both queries would run.

Other than some duplicate code, great work. Good stuff, I'm patching the PR.

_get_statement_ranges() now handles blank-line separated statements,
matching the behavior of split_statements(). Previously, g+s would
run the entire buffer when statements were separated by blank lines
instead of semicolons.
The quote-tracking state machine was duplicated 5 times. Now uses
a single _iter_sql_chars() generator that handles escape sequences
and string literal detection, reducing the file by ~100 lines.
@Maxteabag Maxteabag merged commit 4318107 into Maxteabag:main Jan 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants