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

feat: Support raw strings in Rust #121

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import sqlLint from 'sql-lint';
import * as configuration from './configuration';

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

async function checkRange(
log: vscode.OutputChannel,
Expand Down Expand Up @@ -60,6 +61,7 @@ export async function refreshDiagnostics(
let startRangePosition = -1;
let sqlStringBound = '';
let sqlStartLineIndex = -1;
let hashes = ''; // For Rust raw strings

if (
configuration.get<boolean>('lintSQLFiles') &&
Expand Down Expand Up @@ -88,15 +90,29 @@ export async function refreshDiagnostics(
if (sqlStartLineIndex === -1) {
if ((match = SQL_START_REGEX.exec(lineOfText)) !== null) {
startRangePosition = match.index + match.groups!.token.length;
sqlStringBound = match.groups!.token;
sqlStartLineIndex = lineIndex;
if (match.groups!.hashes !== undefined) {
hashes = match.groups!.hashes;
sqlStringBound = `"${hashes}`;
} else {
sqlStringBound = match.groups!.token;
}
} else if ((phpPatternStart = lineOfText.indexOf(PHP_SQL)) !== -1) {
startRangePosition = phpPatternStart + PHP_SQL.length;
sqlStringBound = 'SQL;';
sqlStartLineIndex = lineIndex;
}
} else if (sqlStringBound !== '') {
let endSqlIndex = lineOfText.indexOf(sqlStringBound);
}
if (sqlStringBound !== '') {
let endSqlIndex = -1;
if (lineIndex === sqlStartLineIndex) {
endSqlIndex = lineOfText.indexOf(
sqlStringBound,
startRangePosition,
);
} else {
endSqlIndex = lineOfText.indexOf(sqlStringBound);
}
if (endSqlIndex !== -1) {
sqlStringCnt += 1;
const range = new vscode.Range(
Expand All @@ -109,6 +125,7 @@ export async function refreshDiagnostics(
diagnostics.push(...subDiagnostics);
sqlStartLineIndex = -1;
sqlStringBound = '';
hashes = '';
}
}
}
Expand Down
1 change: 1 addition & 0 deletions test/testdata/multiline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ select * from book;
"#;

let _another = r#"--sql; select * from book;"#;
let _another = r##"--sql; select * from "book";"##;
}
Loading