supabase-community / postgres_lsp

A Language Server for Postgres
https://supabase.com
MIT License
3.24k stars 61 forks source link

Add `SiblingToken`s back to the parser #54

Open psteinroe opened 9 months ago

psteinroe commented 9 months ago

Sibling tokens are tokens that should end up at the same depth in the CST, such as ( and ). Right now, that is not the case. Take for example the following statement:

ALTER TABLE products ALTER COLUMN price TYPE numeric(10,2);

The brackets of numeric are not on the same level:

AlterTableStmt@0..59
        Alter@0..5 "ALTER"
        Whitespace@5..6 " "
        Table@6..11 "TABLE"
        Whitespace@11..12 " "
        RangeVar@12..20
          Ident@12..20 "products"
        Whitespace@20..21 " "
        AlterTableCmd@21..57
          Alter@21..26 "ALTER"
          Whitespace@26..27 " "
          Column@27..33 "COLUMN"
          Whitespace@33..34 " "
          Ident@34..39 "price"
          Whitespace@39..40 " "
          TypeP@40..44 "TYPE"
          Whitespace@44..45 " "
          ColumnDef@45..57
            TypeName@45..57
              Numeric@45..52 "numeric"
              Ascii40@52..53 "("  <-- this is deeper than
              AConst@53..55
                Iconst@53..55 "10"
              Ascii44@55..56 ","
              AConst@56..57
                Iconst@56..57 "2"
        Ascii41@57..58 ")" <-- this
        Ascii59@58..59 ";"

To fix this, we have to keep track the depth at which tokens are "opened". If the closing token is encountered, we apply it at the same depth as its sibling.

A SiblingToken struct already exists from past implementations. It should be reused.