diff --git a/CHANGELOG.md b/CHANGELOG.md index 27e8203..212628f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,13 @@ All notable changes to the "sqlfluff" extension will be documented in this file. +## [2.4.1] - 2023-07-27 + +- Fix bugs with `SQLFluff Format Selection`. + ## [2.4.0] - 2023-07-26 -- Fix bug with - `SQLFluff Format Selection` now can be placed in the context menu. +- Fix bug with `SQLFluff Format Selection` now can be placed in the context menu. ## [2.3.9] - 2023-07-26 diff --git a/package.json b/package.json index 128cab5..c6743f9 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "vscode-sqlfluff", "displayName": "sqlfluff", - "version": "2.4.0", + "version": "2.4.1", "description": "A linter and auto-formatter for SQLfluff, a popular linting tool for SQL and dbt.", "publisher": "dorzey", "icon": "images/icon.png", diff --git a/src/features/providers/formatter/helper.ts b/src/features/providers/formatter/helper.ts index 548edcf..8096280 100644 --- a/src/features/providers/formatter/helper.ts +++ b/src/features/providers/formatter/helper.ts @@ -38,9 +38,13 @@ export default class FormatHelper { let linesWithWhitespace: string[] = []; if (formatSettings?.preserveLeadingWhitespace) { + if (lines[0].startsWith(" ")) { + return lines; + } + lines.forEach((line) => { const emptySpace = new Array(leadingWhitespace).join(" "); - const whitespaceLine = line === "" ? line : emptySpace.concat(line); + const whitespaceLine = !/\S/.test(line) ? line : emptySpace.concat(line); linesWithWhitespace.push(whitespaceLine); }) } else { diff --git a/src/features/providers/formatter/rangeFormat.ts b/src/features/providers/formatter/rangeFormat.ts index 2d950ba..8db2de8 100644 --- a/src/features/providers/formatter/rangeFormat.ts +++ b/src/features/providers/formatter/rangeFormat.ts @@ -32,10 +32,15 @@ export class FormatSelectionProvider { const workingDirectory = Configuration.workingDirectory(rootPath); const textEdits: vscode.TextEdit[] = []; - const endCharacter = document.lineAt(range.end.line).range.end.character; + // Do not format the last line if it is only whitespace + const endLine = !/\S/.test( + document.lineAt(range.end.line).text.slice(0, range.end.character), + ) ? range.end.line - 1 + : range.end.line; + const endCharacter = document.lineAt(endLine).range.end.character; const lineRange = new vscode.Range( new vscode.Position(range.start.line, 0), - new vscode.Position(range.end.line, endCharacter), + new vscode.Position(endLine, endCharacter), ); if (workingDirectory?.includes("${")) { @@ -85,7 +90,7 @@ export class FormatSelectionProvider { let lines = FormatHelper.parseLines(result.lines); - const leadingWhitespace = document.lineAt(range.start.line).firstNonWhitespaceCharacterIndex; + const leadingWhitespace = document.lineAt(range.start.line).firstNonWhitespaceCharacterIndex + 1; lines = lines ? FormatHelper.addLeadingWhitespace(lines, document.languageId, leadingWhitespace) : undefined; if (lines === undefined) return []; diff --git a/test/.vscode/settings.json b/test/.vscode/settings.json index 57c6187..8e146fd 100644 --- a/test/.vscode/settings.json +++ b/test/.vscode/settings.json @@ -6,11 +6,12 @@ "sqlfluff.codeActions.excludeRules.workspace": false, "sqlfluff.config": "${workspaceFolder}/.sqlfluff", "sqlfluff.dialect": "postgres", + "sqlfluff.format.arguments": [], "sqlfluff.format.languages": [ "sql", { "language": "markdown" , - "contextMenuFormatOptions": false, + "contextMenuFormatOptions": true, "preserveLeadingWhitespace": true } ],