fix(editor): format SQL scopes to selection when text is selected#1661
Conversation
|
Preview deployment for your docs. Learn more about Mintlify Previews.
💡 Tip: Enable Workflows to automatically generate PRs for you. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f92368a03e
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| ) | ||
| let fullRange = NSRange(location: 0, length: (textView.string as NSString).length) | ||
| textView.replaceCharacters(in: fullRange, with: result.formattedSQL) | ||
| textView.replaceCharacters(in: scope.range, with: result.formattedSQL) |
There was a problem hiding this comment.
Preserve boundary newlines when formatting a selection
When the active selection includes the line break that separates it from unselected SQL, this replacement drops that separator because SQLTokenFormatter.format trims leading/trailing whitespace from result.formattedSQL. For example, selecting select 1;\n at the start of select 1;\nselect 2; replaces it with SELECT 1;, leaving SELECT 1;select 2; and changing the query text. This is a common selection shape from selecting whole lines, so the selected-range path needs to preserve boundary whitespace or adjust the range before replacing.
Useful? React with 👍 / 👎.
Fixes #1656.
What
Format SQL (menu, editor toolbar, or right-click) now formats only the selected text when a selection is active. With no selection it formats the whole document, same as before. Undo reverts exactly the range that was formatted.
Root cause
SQLEditorCoordinator.performFormatSQL()readtextView.selectedRange()only as a cursor hint, then unconditionally formattedtextView.stringand replaced the full document range. The selection was available at the call site and simply never used for scoping. The undo half of #1656 was already fixed by #1658:TextView.replaceCharactersregisters a singleTextMutationinCEUndoManager. This change keeps that exact path, so a selection-scoped format is a single undo step covering only the selection.Design
Matches the documented DataGrip / VS Code / SSMS convention: selection formats the selection, empty selection formats the whole document. No statement-at-cursor mode: no mainstream DB client formats the statement under the cursor on an empty selection, and adopting it would surprise users. One command, selection-aware; no new menu items or shortcuts.
FormatScopeResolver, a pure helper that resolves(fullText, selectedRange)to the range and substring to format. Selections that extend past the document fall back to the full document instead of crashing insubstring(with:).NSRange, covered by a dedicated Unicode test.reapplyBoundaryWhitespacere-attaches the boundary whitespace around the formatted text; whole-document formatting keeps the existing trim behavior.Testing
FormatScopeResolverTests(7 cases).selectionReturnsSubrangeis the regression test: it fails against the old behavior. Edge cases covered: cursor-only, NSNotFound, selection == whole document, out-of-bounds selection, UTF-16 offsets with emoji.SQLFormatterServiceTestssuite still passes.CHANGELOG entry added under Unreleased > Fixed; keyboard-shortcuts doc updated.