Skip to content

Commit

Permalink
v2.4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertOstermann committed Jul 27, 2023
1 parent 1f38d6b commit 7e1ff7d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 5 additions & 1 deletion src/features/providers/formatter/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 8 additions & 3 deletions src/features/providers/formatter/rangeFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("${")) {
Expand Down Expand Up @@ -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 [];
Expand Down
3 changes: 2 additions & 1 deletion test/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
],
Expand Down

0 comments on commit 7e1ff7d

Please sign in to comment.