Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Ignore leading whitespace for SQL detection #109

Open
tysonstewart opened this issue Apr 18, 2023 · 2 comments
Open

Feature request: Ignore leading whitespace for SQL detection #109

tysonstewart opened this issue Apr 18, 2023 · 2 comments

Comments

@tysonstewart
Copy link

In our code, our queries often look like this:

const query = {
	text: `
		SELECT id
		FROM my_neat_table
...

The leading whitespace is helpful for keeping the queries aligned and tidy. Unfortunately, it also prevents the extension from detecting SQL. I recognize that I can use --sql at the beginning of the query but not everyone uses this extension. My request is to use trimStart() before checking the beginning of the string constant for a matching keyword.

@RivenSkaye
Copy link

+1 for this, especially considering multiline string literals in C# must start on the next line as well as being indented up to the depth of the closing quotes

@RivenSkaye
Copy link

RivenSkaye commented Sep 11, 2023

Relevant additional information here is the difference between @"verbatim string literals" which are currently supported, and """raw string literals""" which include some compile-time processing to strip leading whitespace off lines which helps in visually aligning the query. Luckily the fix would be rather simple, albeit I haven't tested this yet with a local build:

// syntaxes/c-sharp-multiline.json
// Add this to the patterns array
        {
            "comment": "C# multi-line raw string literals",
            "begin": "(\"\"\"\\n\\s*)(SELECT |INSERT INTO |DELETE |UPDATE |CREATE TABLE |CREATE INDEX)",
            "beginCaptures": {
                "2": {
                    "name": "keyword.sql"
                }
            },
            "end": "(\"\"\")",
            "patterns": [
                {
                    "include": "source.sql"
                }
            ]
        },
        {
            "comment": "C# multi-line raw string literals",
            "begin": "(\"\"\"\\n\\s*)(--\\s*sql)",
            "beginCaptures": {
                "2": {
                    "name": "comment.sql"
                }
            },
            "end": "(\"\"\")",
            "patterns": [
                {
                    "include": "source.sql"
                }
            ]
        }

Edit: never mind, the current setup for this plugin depends on vscode.TextDocument.lineAt(int).text which means it's actively incapable of multiline matching... It'd need a rewrite of that logic, or at least a way to grab the next/previous line for additional checking. It could do this, based on the code of the SQL document mode, but it'd require a little extra effort.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants