Skip to content

Commit 405c26c

Browse files
committed
feat: Support raw string s in Rust
1 parent 22148f4 commit 405c26c

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/extension.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import sqlLint from 'sql-lint';
33
import * as configuration from './configuration';
44

55
export const PHP_SQL = '<<<SQL';
6-
export const SQL_START_REGEX = /(?<token>"""|"|'''|'|`)--\s*sql/;
6+
export const SQL_START_REGEX = /(?<token>r(?<hashes>#*)?"|"""|"|'''|'|`)--\s*sql/;
77

88
async function checkRange(
99
log: vscode.OutputChannel,
@@ -60,6 +60,7 @@ export async function refreshDiagnostics(
6060
let startRangePosition = -1;
6161
let sqlStringBound = '';
6262
let sqlStartLineIndex = -1;
63+
let hashes = ''; // For Rust raw strings
6364

6465
if (
6566
configuration.get<boolean>('lintSQLFiles') &&
@@ -88,15 +89,26 @@ export async function refreshDiagnostics(
8889
if (sqlStartLineIndex === -1) {
8990
if ((match = SQL_START_REGEX.exec(lineOfText)) !== null) {
9091
startRangePosition = match.index + match.groups!.token.length;
91-
sqlStringBound = match.groups!.token;
9292
sqlStartLineIndex = lineIndex;
93+
if (match.groups!.hashes !== undefined) {
94+
hashes = match.groups!.hashes;
95+
sqlStringBound = `"${hashes}`;
96+
} else {
97+
sqlStringBound = match.groups!.token;
98+
}
9399
} else if ((phpPatternStart = lineOfText.indexOf(PHP_SQL)) !== -1) {
94100
startRangePosition = phpPatternStart + PHP_SQL.length;
95101
sqlStringBound = 'SQL;';
96102
sqlStartLineIndex = lineIndex;
97103
}
98-
} else if (sqlStringBound !== '') {
99-
let endSqlIndex = lineOfText.indexOf(sqlStringBound);
104+
}
105+
if (sqlStringBound !== '') {
106+
let endSqlIndex = -1;
107+
if (lineIndex === sqlStartLineIndex) {
108+
endSqlIndex = lineOfText.indexOf(sqlStringBound, startRangePosition);
109+
} else {
110+
endSqlIndex = lineOfText.indexOf(sqlStringBound);
111+
}
100112
if (endSqlIndex !== -1) {
101113
sqlStringCnt += 1;
102114
const range = new vscode.Range(
@@ -109,6 +121,7 @@ export async function refreshDiagnostics(
109121
diagnostics.push(...subDiagnostics);
110122
sqlStartLineIndex = -1;
111123
sqlStringBound = '';
124+
hashes = '';
112125
}
113126
}
114127
}

test/testdata/multiline.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ select * from book;
44
"#;
55

66
let _another = r#"--sql; select * from book;"#;
7+
let _another = r##"--sql; select * from "book";"##;
78
}

0 commit comments

Comments
 (0)