Skip to content

Commit

Permalink
v3.1.5: Fix bug with ignoring diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertOstermann committed Aug 16, 2024
1 parent 7dfd941 commit 3085859
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 18 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All notable changes to the "sqlfluff" extension will be documented in this file.

## [3.1.5] - 2024-08-16
- Fix bug with ignoring diagnostics

## [3.1.4] - 2024-07-25

- Correctly parse the sqlfluff version number. [Issue](https://github.com/sqlfluff/vscode-sqlfluff/issues/151)
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": "3.1.4",
"version": "3.1.5",
"description": "A linter and auto-formatter for SQLfluff, a popular linting tool for SQL and dbt.",
"publisher": "dorzey",
"icon": "images/icon.png",
Expand Down
26 changes: 20 additions & 6 deletions src/features/helper/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Variables from "./types/variables";
export default class Utilities {
static outputChannel = vscode.window.createOutputChannel("SQLFluff");

public static appendHyphenatedLine(newLines = true) {
static appendHyphenatedLine(newLines = true) {
if (newLines) {
Utilities.outputChannel.appendLine("\n------------------------------------------------------------\n");
} else {
Expand All @@ -32,7 +32,7 @@ export default class Utilities {
return command;
}

public static normalizePath(path: string, allowEscapes = false): string {
static normalizePath(path: string, allowEscapes = false): string {
if (path === undefined) {
return path;
}
Expand All @@ -47,11 +47,11 @@ export default class Utilities {
return path.replace(/\\+/g, "/");
}

public static async sleep(milliseconds: number) {
static async sleep(milliseconds: number) {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
}

public static parseVersion(versionString: string): ParsedVersion {
static parseVersion(versionString: string): ParsedVersion {
const parsedVersion: ParsedVersion = {
major: 0,
minor: 0,
Expand Down Expand Up @@ -86,7 +86,21 @@ export default class Utilities {
return parsedVersion;
}

public static isNumber = (value: any, cast = true) =>
static isNumber = (value: any, cast = true) =>
typeof value === "number" || (cast ? !isNaN(Number(value)) : !isNaN(value));
public static toNumber = (value: any, fallback = 0) => (Utilities.isNumber(value) ? Number(value) : fallback);

static toNumber = (value: any, fallback = 0) => (Utilities.isNumber(value) ? Number(value) : fallback);

/**
* Get the diagnostic code without the description
* @param diagnostic - VSCode Diagnostic
* @returns Diagnostic code without description
*/
static getDiagnosticCode = (diagnostic: vscode.Diagnostic) =>
Utilities.extractBeforeColon(diagnostic.code?.toString() ?? "");

private static extractBeforeColon(input: string): string {
const match = input.match(/^(.*?):/);
return match ? match[1] : "";
}
}
7 changes: 5 additions & 2 deletions src/features/providers/linter/actions/hover.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import * as vscode from "vscode";

import Utilities from "../../../helper/utilities";

export default class HoverProvider implements vscode.HoverProvider {
public provideHover(
document: vscode.TextDocument,
position: vscode.Position,
token: vscode.CancellationToken,
token: vscode.CancellationToken
): vscode.ProviderResult<vscode.Hover> {
const editor = vscode.window.activeTextEditor;
const diagnostics = vscode.languages.getDiagnostics(document.uri);
Expand All @@ -30,7 +32,8 @@ export default class HoverProvider implements vscode.HoverProvider {
// Link to the rule permalinks so that if the rule docs move in the future that
// we don't have to update the links here. We rely on the docs internally redirecting
// us to the appropriate location.
const path = `https://docs.sqlfluff.com/en/stable/perma/rule/${diagnostic.code}.html`;
// const path = `https://docs.sqlfluff.com/en/stable/perma/rule/${Utilities.getDiagnosticCode(diagnostic)}.html`;
const path = `https://docs.sqlfluff.com/en/stable/rules.html#sqlfluff.rules.sphinx.Rule_${diagnostic.code}`;
const markdownString = new vscode.MarkdownString();

markdownString.appendMarkdown(`[View Documentation](${path}) for Rule ${diagnostic.code}.\n`);
Expand Down
19 changes: 10 additions & 9 deletions src/features/providers/linter/actions/quickFix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as vscode from "vscode";

import { EXCLUDE_RULE, EXCLUDE_RULE_WORKSPACE } from "../../../commands/excludeRules";
import Configuration from "../../../helper/configuration";
import Utilities from "../../../helper/utilities";

export default class QuickFixProvider implements vscode.CodeActionProvider {
public static readonly providedCodeActionKind = [vscode.CodeActionKind.QuickFix];
Expand All @@ -11,7 +12,7 @@ export default class QuickFixProvider implements vscode.CodeActionProvider {
document: vscode.TextDocument,
range: vscode.Range | vscode.Selection,
context: vscode.CodeActionContext,
token: vscode.CancellationToken,
token: vscode.CancellationToken
): vscode.CodeAction[] {
const noqaSingleRules: vscode.CodeAction[] = [];
const noqaAllRules: vscode.CodeAction[] = [];
Expand All @@ -21,7 +22,7 @@ export default class QuickFixProvider implements vscode.CodeActionProvider {
if (Configuration.noqaEnabled()) {
const noqaDisabledRules = Configuration.noqaDisabledRules();
context.diagnostics.forEach((diagnostic) => {
if (diagnostic.code && !noqaDisabledRules.includes(diagnostic.code.toString())) {
if (diagnostic.code && !noqaDisabledRules.includes(Utilities.getDiagnosticCode(diagnostic))) {
const singleRuleCodeAction = this.createNoqaCodeFix(document, diagnostic, false);
const allRulesCodeAction = this.createNoqaCodeFix(document, diagnostic, true);
noqaSingleRules.push(singleRuleCodeAction);
Expand All @@ -44,7 +45,7 @@ export default class QuickFixProvider implements vscode.CodeActionProvider {
private createNoqaCodeFix(
document: vscode.TextDocument,
diagnostic: vscode.Diagnostic,
allRules: boolean,
allRules: boolean
): vscode.CodeAction {
const title = allRules ? "Ignore all rules for this line" : `Ignore rule ${diagnostic.code} for this line`;
const fix = new vscode.CodeAction(title, vscode.CodeActionKind.QuickFix);
Expand All @@ -54,7 +55,7 @@ export default class QuickFixProvider implements vscode.CodeActionProvider {
const line = document.lineAt(diagnostic.range.start.line);
const endPosition = new vscode.Position(
line.range.end.line,
line.range.end.character > 0 ? line.range.end.character : 0,
line.range.end.character > 0 ? line.range.end.character : 0
);
const noqaREGEX = /\s*-- noqa(?::(\s?\w\d{3},?)*)?.*/;
const noqa = noqaREGEX.exec(line.text);
Expand All @@ -67,20 +68,20 @@ export default class QuickFixProvider implements vscode.CodeActionProvider {
if (noqa.length > 1) {
if (noqa[1]) {
if (noqa[1].endsWith(",")) {
fix.edit.insert(document.uri, endPosition, ` ${diagnostic.code}`);
fix.edit.insert(document.uri, endPosition, ` ${Utilities.getDiagnosticCode(diagnostic)}`);
} else {
fix.edit.insert(document.uri, endPosition, `, ${diagnostic.code}`);
fix.edit.insert(document.uri, endPosition, `, ${Utilities.getDiagnosticCode(diagnostic)}`);
}
} else {
fix.edit.insert(document.uri, endPosition, `: ${diagnostic.code}`);
fix.edit.insert(document.uri, endPosition, `: ${Utilities.getDiagnosticCode(diagnostic)}`);
}
}
}
} else {
if (allRules) {
fix.edit.insert(document.uri, endPosition, " -- noqa");
} else {
fix.edit.insert(document.uri, endPosition, ` -- noqa: ${diagnostic.code}`);
fix.edit.insert(document.uri, endPosition, ` -- noqa: ${Utilities.getDiagnosticCode(diagnostic)}`);
}
}

Expand All @@ -94,7 +95,7 @@ export default class QuickFixProvider implements vscode.CodeActionProvider {
command: global ? EXCLUDE_RULE : EXCLUDE_RULE_WORKSPACE,
title: title,
tooltip: `This will exclude the rule ${diagnostic.code} in the ${global ? "Global" : "Workspace"} Settings`,
arguments: [diagnostic.code],
arguments: [Utilities.getDiagnosticCode(diagnostic)],
};
action.diagnostics = [diagnostic];

Expand Down

0 comments on commit 3085859

Please sign in to comment.