Repository navigation
Conversation
Turn identifiers as written into names with one function, pgls_catalog::identifier_name, like Postgres' scanner: unquoted identifiers are downcased (ASCII only), quoted ones lose their quotes and unescape "". Hover, completions, the tree-sitter context, inlay hints and identifierTooLong use it, and Snapshot's find_* lookups now take names. Completions no longer lowercase the whole statement, so quoted names keep their case, and the grammar reads "a""b" as one identifier.
Completions inserted table, column, function and schema names as they are stored, so MyTable, names with spaces or quotes, and reserved keywords were read by Postgres as another name, or not at all. They are now quoted, with " escaped as "", exactly where Postgres needs it: pgls_catalog::quote_identifier follows scan.l's identifier rules and the keywords that gram.y accepts at each position.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #976.
Completions inserted names exactly as they are stored. Postgres downcases unquoted identifiers and rejects most keywords as names, so the inserted text often meant a different name:
What changed
pgls_catalog::quote_identifier(name, position)andneeds_quotes, next toidentifier_name, of which they are the inverse. A name stays unquoted only if it's an identifier byscan.l's rules that downcasing leaves unchanged, and if it isn't a keyword the grammar rejects at that position (name classification ingram.y):ColId(the first part of a table, column or schema name) rejects reserved keywords and those that name types or functions.type_function_name(an unqualified function) rejects reserved keywords and those that name columns."escaped as"". This is more precise than Postgres' ownquote_identifier, which quotes every keyword that isn't unreserved.pgls_lexer, which already generates them fromkwlist.h. It now has a publickeyword_category(word), andpgls_catalogdepends onpgls_lexer.with_schema_or_aliastakes the position of the item:ColIdand the item after the dot as a label."|or"|"), only"is escaped, and no second quote is added.from users "pr"now insertspr.emailinstead of"pr".email. Both refer to the same alias.select * from "|insertsprivate".Snapshot changes
pg_catalog.RI_FKey_cascade_del()is nowpg_catalog."RI_FKey_cascade_del"()."pr".emailis nowpr.email.Tests: unit tests for
needs_quotes/quote_identifier, covering the four keyword categories, uppercase, spaces,", a leading digit,$, non-ASCII, and the round trip throughidentifier_name. A completions test covers tables, schemas, schema-qualified tables, the opening-quote case, columns (keywords quoted alone but not after a dot), and functions with and without a schema.pgls_catalog,pgls_lexer,pgls_completions,pgls_workspace --liband clippy pass.Not done: the pretty printer keeps its own
needs_quotinginpgls_pretty_print/src/nodes/string.rs. It uses its codegen's keyword table, and sharing the predicate would mean a new dependency forpgls_pretty_printand changes instring.rswhile its PR stack is open. It can switch topgls_catalog::needs_quotes, or the predicate can move intopgls_lexer, once that stack has landed. Completing while typing inside an unterminated quote ("My|) still returns nothing, as on main. Policies and roles are still inserted unquoted.Update: Merged main, now that #976 has landed (the merge only takes main's version of the #976 changes; this PR's own diff is unchanged).
cargo clippy --workspace --all-targets -- -D warningsand the tests ofpgls_catalog,pgls_lexerandpgls_completionspass on the merge. #998 is stacked on this PR and switches expand star toquote_identifier.