Five things I said were still wrong, fixed
The follow-up to the procedure formatting post: the five bugs I listed as still wrong, fixed, with what each did before and does now.
The last post ended with a list of five things I knew were still broken in how SQLly formats and completes procedures. This one is me going back and fixing the list. All five, plus one I found on the way.
Why I'm writing about bugs twice
When I shipped the CREATE statement clean-up I put a "what's still wrong" section at the bottom. Partly honesty, partly a to-do list I couldn't quietly lose. It worked. The list nagged at me, so here is what each item was, what it does now, and why I think the new behavior is the right one rather than just a different one.
1. The popup that blinked while you typed a type
Type CREATE TABLE t (Id in and the suggestion popup, which had been happily
showing INT, INTEGER and friends after Id i, closed.
Type the t and it came back. A one-keystroke blink.
The cause is a rule I still agree with: when the word under the caret is a whole keyword,
the automatic popup stays shut. If you've typed FROM, you don't want a list of
things that start with FROM, and you really don't want Enter to accept one instead of giving
you a new line. The problem is that in is a keyword, and it's also the first two
letters of INT.
The fix is narrow. In a slot that takes a data type, a column name or a parameter, the
keyword rule only closes the popup if the slot has nothing to offer beyond the word itself.
Id in keeps showing types. Id INT NOT shows NOT NULL.
CREATE TABLE on its own still closes, because the only thing it could offer is
TABLE again, and Enter should insert a newline there. Outside DDL nothing changed:
WHERE OrderId in still goes quiet, as before.
2. RETURN knew nothing about your variables
Inside a procedure, RETURN followed by a space offered the generic query
keywords. SELECT, FROM, GROUP BY. Nothing you would ever put after RETURN. If you typed
@ you got your variables, which was the workaround, but you shouldn't need one.
Now the variables and parameters in scope come first, in the order they appear, and then the few words RETURN takes on your engine:
CREATE PROCEDURE dbo.Reconcile @BatchId INT
AS
BEGIN
DECLARE @Count INT;
...
RETURN ▏ -- @BatchId, @Count, NULL
This isn't a T-SQL-only trick. In a PL/pgSQL function it reads the parameter list and the
DECLARE section, so you get a, r, then
QUERY, NEXT and NULL. MySQL routines and PL/SQL
declaration sections work the same way. Two places deliberately don't get this treatment:
the RETURN that opens an inline table function (RETURNS TABLE AS RETURN
(SELECT) and the RETURN NUMBER in a PL/SQL header, which is a type slot
and still offers types.
3. FROM and GROUP BY right after AS
The list after a procedure's AS was the general-purpose one, which includes the
query clauses. FROM, JOIN, WHERE, GROUP BY, ORDER BY. None of them can start a statement, so
none of them belong there. Same after BEGIN, and same after a statement that has
clearly finished.
These spots now list statement starters only: BEGIN first after AS, then DECLARE, SET, SELECT,
INSERT, UPDATE, DELETE, IF, WHILE, EXEC, RETURN and the rest. Triggers get the same treatment
after their AS, where the old list was even stranger (it thought it was inside a condition and
offered AND, ASC and BETWEEN). PostgreSQL's AS $$ counts as the same spot.
The part that needed care was not breaking the opposite case. If you write a SELECT, press Enter, and start the next line, that line probably continues the query, and T-SQL doesn't make you type a semicolon to say so. There the clauses stay, WHERE first. The rule only removes them where a statement certainly starts: after a routine's AS, after BEGIN, ELSE, TRY or CATCH, or when nothing precedes the caret in its statement. A line that merely looks like a new statement keeps the old, generous list.
While I was in there: WITH EXECUTE AS OWNER in a procedure header was being read
as an EXEC statement, so the body position after it offered procedure names.
Fixed.
4. SELECT 1; in two lines
This was the one in the trigger screenshot last time:
BEGIN
SET NOCOUNT ON;
SELECT
1;
END
Formatted on its own, select 1; stayed on one line. Inside a block it broke in
two. I called it consistent with how lists get formatted, which was true and also a bit of a
cop-out. The real reason was a rule that said "keep short queries on one line" only applied
when the whole script was a single statement. Put two statements in a file and both got the
full treatment, which is why select 1; select 2; turned into four lines.
The new rule: a SELECT with nothing after its list is a constant or an assignment, not a query
to lay out. SELECT 1;, SELECT @x = 1, @y = 2;,
SELECT @@ROWCOUNT; stay on one line wherever they sit, as long as they fit the
short-query length you've configured. Anything with a FROM, a WHERE or a set operator is still
laid out clause by clause, in a block or out of one.
BEGIN
SET NOCOUNT ON;
SELECT @x = 1, @y = 2;
SELECT
COUNT(*)
FROM
t;
IF @x = 1
BEGIN
SELECT 1
END
END
5. Index options in whatever case you typed
CREATE INDEX ... WITH (fillfactor = 80, online = on, pad_index = on) came back
as WITH (FILLFACTOR = 80, online = ON, pad_index = ON). FILLFACTOR happens to be
a reserved word in T-SQL, so it got cased. ONLINE and PAD_INDEX aren't reserved anywhere
(you can name a column online), so the dialect tables leave them alone, and so
did the formatter.
They're keywords by position now. Inside a WITH ( ... ) that belongs to an index,
a constraint or a table, the word before an = is an option name, and the fixed
values after it (ON, OFF, PAGE, ROW, NONE, COLUMNSTORE, the MINUTES in MAX_DURATION = 1
MINUTES) are values. Both follow your keyword casing. Nested option groups like
ONLINE = ON (WAIT_AT_LOW_PRIORITY (...)) are covered, PostgreSQL's
WITH (fillfactor = 80, deduplicate_items = off) is covered, and
ALTER INDEX ... REBUILD finally capitalizes REBUILD. A column called
online in a SELECT keeps your spelling, because it isn't inside that kind of
parenthesis.
The test rule, again
Same rule as last time: no existing IntelliSense test gets to break without me looking at it. The full suite is 1,514 tests now and all of them pass, the original 1,507 unchanged. The new behavior has its own tests: seven for RETURN across T-SQL, PL/pgSQL, MySQL and PL/SQL, three for statement starts and the popup, and whole-script formatting checks for a filtered, option-heavy index on SQL Server and a concurrent one on PostgreSQL.
Six formatter tests did change, and I want to be upfront about which. Five were ones I wrote
last week that expected SELECT and 1 on separate lines inside a
block. They were encoding the bug. The sixth expected raiserror('no', 16, 1) in
lowercase inside a trigger; the formatter had always meant to case RAISERROR as the statement
it is, and the parenthesis after it was tricking it into treating it as a function. Same
trick, by the way, kept WITH (nolock) lowercase. Both are cased now.
And one IntelliSense test changed, with me looking at it personally, as promised. It typed
S as the first thing in a procedure body and expected exactly SELECT and INSERT.
Now it also gets SET, because SET NOCOUNT ON and SET @x = ... are about as common
a first line as SELECT is. I'd rather the list be right than the test be untouched.
What's still wrong
Shorter list this time. A filtered index with a lot of options still lays out oddly: the
WHERE gets its own line like a query clause and the WITH (...) trails
the condition instead of starting a new line. And a PostgreSQL function body written inside
$$ quotes is left exactly as you wrote it, which is safe but means Format Document
doesn't help you there. Both are on the list. Send me the shape of DDL that comes back worse
than it went in and it becomes a test, same as these did.