Skip to content

fix: context menu Format SQL not working (#659)#662

Merged
datlechin merged 2 commits intomainfrom
fix/context-menu-format-sql
Apr 10, 2026
Merged

fix: context menu Format SQL not working (#659)#662
datlechin merged 2 commits intomainfrom
fix/context-menu-format-sql

Conversation

@datlechin
Copy link
Copy Markdown
Collaborator

Summary

  • Fix context menu "Format SQL" doing nothing when clicked
  • Fix context menu disappearing after tab switch (destroy/recreate lifecycle)

Closes #659

Root Cause

formatQuery() is passed as a closure from QueryEditorView (a SwiftUI struct) through SQLEditorView.onFormatSQL to SQLEditorCoordinator.onFormatSQL. The closure captures the QueryEditorView struct by value at body evaluation time. After SwiftUI recreates the view, the captured @Binding queryText becomes stale and returns empty string.

The toolbar Format button works because it runs in the current SwiftUI render context where the binding is valid.

Fix

  1. Fallback formatting: When the onFormatSQL callback is stale, the context menu formats directly via the coordinator's text view reference using SQLFormatterService
  2. Context menu recreation: showContextMenu() lazily recreates the AIEditorContextMenu if it was destroyed by destroy() (tab switch lifecycle)

Test plan

  • Open SQL editor, type a query, right-click > Format SQL > query is formatted
  • Switch to another tab and back, right-click > Format SQL > still works
  • Toolbar Format button still works as before
  • Other context menu items (AI Explain, AI Optimize, Save as Favorite) still work

…#659)

Root cause: formatQuery() passed as onFormatSQL closure captures QueryEditorView
struct by value. After SwiftUI recreates the view, the captured @binding queryText
becomes stale (empty). Toolbar button works because it runs in current render context.

Fix: add fallback in context menu that formats directly via the text view when the
callback closure is stale. Also lazily recreate context menu after destroy().
Coordinator lifecycle:
- Move callback wiring from .onAppear to view body (fresh on every re-render)
- Add revive() method to re-register with EditorEventRouter after tab switch
- Add onFormatSQL = nil to destroy() cleanup
- Add databaseType property to coordinator
- Remove hacky formatSQLInTextView fallback (was hardcoding .mysql dialect)

SQL formatter:
- Fix comment placeholder order: single-pass extraction by source position
- Fix SQL-standard '' escape not handled in string literal regex
- Fix addLineBreaks ignoring uppercaseKeywords option
@datlechin datlechin merged commit 7b18241 into main Apr 10, 2026
2 checks passed
@datlechin datlechin deleted the fix/context-menu-format-sql branch April 10, 2026 12:24
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.

context menu, format query not working

1 participant