Skip to content

Commit 50a047e

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

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/extension.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ 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 =
7+
/(?<token>r(?<hashes>#*)?"|"""|"|'''|'|`)--\s*sql/;
78

89
async function checkRange(
910
log: vscode.OutputChannel,
@@ -60,6 +61,7 @@ export async function refreshDiagnostics(
6061
let startRangePosition = -1;
6162
let sqlStringBound = '';
6263
let sqlStartLineIndex = -1;
64+
let hashes = ''; // For Rust raw strings
6365

6466
if (
6567
configuration.get<boolean>('lintSQLFiles') &&
@@ -88,15 +90,29 @@ export async function refreshDiagnostics(
8890
if (sqlStartLineIndex === -1) {
8991
if ((match = SQL_START_REGEX.exec(lineOfText)) !== null) {
9092
startRangePosition = match.index + match.groups!.token.length;
91-
sqlStringBound = match.groups!.token;
9293
sqlStartLineIndex = lineIndex;
94+
if (match.groups!.hashes !== undefined) {
95+
hashes = match.groups!.hashes;
96+
sqlStringBound = `"${hashes}`;
97+
} else {
98+
sqlStringBound = match.groups!.token;
99+
}
93100
} else if ((phpPatternStart = lineOfText.indexOf(PHP_SQL)) !== -1) {
94101
startRangePosition = phpPatternStart + PHP_SQL.length;
95102
sqlStringBound = 'SQL;';
96103
sqlStartLineIndex = lineIndex;
97104
}
98-
} else if (sqlStringBound !== '') {
99-
let endSqlIndex = lineOfText.indexOf(sqlStringBound);
105+
}
106+
if (sqlStringBound !== '') {
107+
let endSqlIndex = -1;
108+
if (lineIndex === sqlStartLineIndex) {
109+
endSqlIndex = lineOfText.indexOf(
110+
sqlStringBound,
111+
startRangePosition,
112+
);
113+
} else {
114+
endSqlIndex = lineOfText.indexOf(sqlStringBound);
115+
}
100116
if (endSqlIndex !== -1) {
101117
sqlStringCnt += 1;
102118
const range = new vscode.Range(
@@ -109,6 +125,7 @@ export async function refreshDiagnostics(
109125
diagnostics.push(...subDiagnostics);
110126
sqlStartLineIndex = -1;
111127
sqlStringBound = '';
128+
hashes = '';
112129
}
113130
}
114131
}

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)