inline_sql_syntax icon indicating copy to clipboard operation
inline_sql_syntax copied to clipboard

Not highlighted with TypeScript Generics

Open ariesclark opened this issue 2 years ago • 9 comments

The following code should be properly highlighted, but is not. Removing the generic type will cause the string to be highlighted again.

const { data } = await sql<{
  date: Date;
  foo: number;
  bar: number;
}>`
  select
    toStartOfInterval(timestamp, ${interval(5, "minute")}) AS date,
    sum(_sample_interval * double2) / sum(_sample_interval) as foo,
    sum(_sample_interval * double3) / sum(_sample_interval) as bar

  from analytics

  where timestamp >= now() - ${interval(7, "day")}
    and timestamp <= now()
    and index1 = ${worldId}

  group by date
  order by date
`;

ariesclark avatar Sep 11 '23 01:09 ariesclark

+1

herenickname avatar Oct 08 '23 16:10 herenickname

Generic type arguments can be implemented (⚠️ if they are on a single line - I'll come back to this in a followup comment) - check out working versions of this feature in the following VS Code extensions:

  1. SQL tagged template literals by @frigus02 (repo: https://github.com/frigus02/vscode-sql-tagged-template-literals)
  2. Inline SQL by @notyetspecified (repo: https://github.com/notyetspecified/vscode-sql-template-literal)

In the following PR by @KristjanTammekivi, the generic type parameter feature was added to Inline SQL:

  • https://github.com/notyetspecified/vscode-sql-template-literal/pull/2

karlhorky avatar Nov 19 '23 15:11 karlhorky

The problem with the original example in this current issue by @ariesclark above is that the generic type argument spans multiple lines.

The root cause for this is a bit more of a problem, since TextMate grammars have limitations, as @sheetalkamat is familiar with:

  • https://github.com/microsoft/TypeScript-TmLanguage/issues/761#issuecomment-515186694
  • https://github.com/microsoft/TypeScript-TmLanguage/issues/949#issuecomment-1423374471
  • https://github.com/microsoft/vscode-textmate/issues/41

VS Code itself somehow gets around the limitations and can do correct syntax highlighting over multiple lines, maybe with Semantic Highlighting by @aeschli and @alexdima.

I also see some changes with regular expressions, which @sheetalkamat has also been maintaining:

  • https://github.com/microsoft/TypeScript-TmLanguage/commit/88a7e09e927ea2a135db60810d6a1fe8f589d165

So maybe there is a way for a VS Code extension to also achieve this, by also using this Semantic Highlighting / these regular expressions.

The TextMate grammar limitations have also been explored in the following SQL tagged template literals VS Code extension, but no solution has yet appeared:

  • https://github.com/frigus02/vscode-sql-tagged-template-literals/issues/20
  • https://github.com/frigus02/vscode-sql-tagged-template-literals/pull/21

karlhorky avatar Nov 19 '23 15:11 karlhorky

Workaround (/* sql */ comment)

Add a /* sql */ comment after the multi-line generic

sql<{
  value: number
-}[]>`
+}[]>/* sql */ `
  SELECT 1 value;
`

Credit: @Net in https://github.com/thebearingedge/vscode-sql-lit/issues/13#issuecomment-1550486134

karlhorky avatar Nov 19 '23 16:11 karlhorky

This actually breaks coloring for the rest of the documents with generics.

image

image

vicary avatar Apr 15 '24 17:04 vicary

This actually breaks coloring for the rest of the documents with generics.

image

image

I can't seem to replicate image

Can you give a full example? Here's mine:

const sql = <T>(q): any => {
    return {
        execute: (db) => {}
    }
}

interface F {
    now: string;
}

const db = {};

await sql<F>`SELECT NOW(6) AS 'now'`.execute(db);
const boo = true;

await sql<{ now: string }>`SELECT NOW(6) AS 'now'`.execute(db);
const foo = true;

await sql`SELECT NOW(6) AS 'now'`.execute(db);
const moo = true;


export { };


KristjanTammekivi avatar Apr 17 '24 11:04 KristjanTammekivi

@KristjanTammekivi testing with your snippet below:

With the extension enabled

image

With the extension disabled

image

vicary avatar Apr 18 '24 05:04 vicary

Ah my bad, I thought this was the same extension as I made a contribution to because I was mentioned here. I'm using this extension: image

KristjanTammekivi avatar Apr 18 '24 06:04 KristjanTammekivi

~~@KristjanTammekivi nice ad (and i'm sold) =D~~

vicary avatar Apr 18 '24 10:04 vicary