Skip to content

Commit

Permalink
v0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbie Ostermann committed Sep 17, 2022
1 parent ab22181 commit 218630b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 42 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to the "sqlfluff" extension will be documented in this file.

Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.

## [0.1.1] - 2022-09-12

- Add quickfix option to exclude the rule from the workspace settings instead of the global settings.

## [0.1.0] - 2022-09-12

- Add a hover provider to view the documentation from the SQLFluff site.
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vscode-sqlfluff",
"displayName": "sqlfluff",
"version": "0.1.0",
"version": "0.1.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 Expand Up @@ -75,7 +75,7 @@
"sqlfluff.excludeRules": {
"type": "array",
"default": [],
"description": "Exclude specific rules."
"description": "Exclude specific rules. The excludedRules from the global settings and workspace settings are combined."
},
"sqlfluff.ignoreLocalConfig": {
"type": "boolean",
Expand All @@ -90,7 +90,7 @@
"sqlfluff.rules": {
"type": "array",
"default": [],
"description": "Narrow the search to only specific rules."
"description": "Narrow the search to only specific rules. The rules from the global settings and workspace settings are combined."
},
"sqlfluff.workingDirectory": {
"type": "string",
Expand Down
24 changes: 21 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from "vscode";

import { EXCLUDE_RULE, FormattingEditProvider, HoverProvider, LinterProvider, QuickFixProvider, VIEW_DOCUMENTATION } from "./features/linter";
import { EXCLUDE_RULE, EXCLUDE_RULE_WORKSPACE, FormattingEditProvider, HoverProvider, LinterProvider, QuickFixProvider, VIEW_DOCUMENTATION } from "./features/linter";

export const activate = (context: vscode.ExtensionContext) => {
new LinterProvider().activate(context.subscriptions);
Expand All @@ -25,6 +25,7 @@ export const activate = (context: vscode.ExtensionContext) => {
);

context.subscriptions.push(vscode.commands.registerCommand(EXCLUDE_RULE, toggleRule));
context.subscriptions.push(vscode.commands.registerCommand(EXCLUDE_RULE_WORKSPACE, toggleRuleWorkspace));
context.subscriptions.push(vscode.commands.registerCommand(VIEW_DOCUMENTATION, showDocumentation));
};

Expand All @@ -33,19 +34,36 @@ export const deactivate: any = () => { };

function toggleRule(rule: string) {
const configuration = vscode.workspace.getConfiguration("sqlfluff");
const excludeRulesArray: string[] = configuration.get("excludeRules");
const excludeRules: any = configuration.inspect("excludeRules");
const excludeRulesArray = excludeRules.globalValue ?? [];

if (!excludeRulesArray.includes(rule)) {
excludeRulesArray.push(rule);
}

excludeRulesArray.sort((x, y) => {
excludeRulesArray.sort((x: string, y: string) => {
return parseInt(x.substring(1)) - parseInt(y.substring(1));
});

return configuration.update("excludeRules", excludeRulesArray, vscode.ConfigurationTarget.Global);
}

function toggleRuleWorkspace(rule: string) {
const configuration = vscode.workspace.getConfiguration("sqlfluff");
const excludeRules: any = configuration.inspect("excludeRules");
const excludeRulesArray = excludeRules.workspaceValue ?? [];

if (!excludeRulesArray.includes(rule)) {
excludeRulesArray.push(rule);
}

excludeRulesArray.sort((x: string, y: string) => {
return parseInt(x.substring(1)) - parseInt(y.substring(1));
});

return configuration.update("excludeRules", excludeRulesArray, vscode.ConfigurationTarget.Workspace);
}

function showDocumentation(rule: string) {
const path = `https://docs.sqlfluff.com/en/stable/rules.html#sqlfluff.rules.Rule_${rule}`;

Expand Down
16 changes: 12 additions & 4 deletions src/features/helper/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ export class Configuration {
}

private static excludeRules(): string[] {
const excludeRulesArray: string[] = vscode.workspace
const excludeRulesConfiguration: any = vscode.workspace
.getConfiguration("sqlfluff")
.get("excludeRules");
.inspect("excludeRules");

const excludeRulesGlobalArray = excludeRulesConfiguration.globalValue ?? [];
const excludeRulesWorkspaceArray = excludeRulesConfiguration.workspaceValue ?? [];
const excludeRulesArray = [...excludeRulesGlobalArray, ...excludeRulesWorkspaceArray];

const excludeRules = excludeRulesArray.join(",");
return excludeRules ? ["--exclude-rules", excludeRules] : [];
Expand All @@ -59,9 +63,13 @@ export class Configuration {
}

private static rules(): string[] {
const rulesArray: string[] = vscode.workspace
const rulesConfiguration: any = vscode.workspace
.getConfiguration("sqlfluff")
.get("rules");
.inspect("rules");

const rulesGlobalArray = rulesConfiguration.globalValue ?? [];
const rulesWorkspaceArray = rulesConfiguration.workspaceValue ?? [];
const rulesArray = [...rulesGlobalArray, ...rulesWorkspaceArray];

const rules = rulesArray.join(",");
return rules ? ["--rules", rules] : [];
Expand Down
46 changes: 14 additions & 32 deletions src/features/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { normalize } from "./helper/utilities";
import { FormattingProvider } from "./providers/format";
import { Linter, LintingProvider } from "./providers/lint";

export const EXCLUDE_RULE = "sqlfluff.quickfix.command";
export const EXCLUDE_RULE = "sqlfluff.quickfix.excludeRule";
export const EXCLUDE_RULE_WORKSPACE = "sqlfluff.quickfix.excludeRuleWorkspace";
export const VIEW_DOCUMENTATION = "sqlfluff.quickfix.viewDocumentation";

export class LinterProvider implements Linter {
Expand Down Expand Up @@ -95,55 +96,36 @@ export class QuickFixProvider implements vscode.CodeActionProvider {
_token: vscode.CancellationToken
): vscode.CodeAction[] {
// for each diagnostic entry that has the matching `code`, create a code action command
const codeActions = context.diagnostics.map((diagnostic) =>
this.createCodeAction(diagnostic)
const excludeRulesGlobal = context.diagnostics.map((diagnostic) =>
this.createCodeAction(diagnostic, true)
);

return codeActions;
// const documentationActions = context.diagnostics.map((diagnostic) =>
// this.createDocumentationAction(diagnostic)
// );
const excludeRulesWorkspace = context.diagnostics.map((diagnostic) =>
this.createCodeAction(diagnostic, false)
);

// return [...codeActions, ...documentationActions];
return [...excludeRulesGlobal, ...excludeRulesWorkspace];
}

private createCodeAction(
diagnostic: vscode.Diagnostic
diagnostic: vscode.Diagnostic,
global: boolean
): vscode.CodeAction {
const action = new vscode.CodeAction(
`Exclude Rule ${diagnostic.code}`,
`Exclude Rule ${diagnostic.code} ${global ? "from Global Settings" : "from Workspace Settings"}`,
vscode.CodeActionKind.QuickFix
);
action.command = {
command: EXCLUDE_RULE,
title: `Exclude Rule ${diagnostic.code}`,
tooltip: `This will exclude the rule ${diagnostic.code} in settings.json`,
command: global ? EXCLUDE_RULE : EXCLUDE_RULE_WORKSPACE,
title: `Exclude Rule ${diagnostic.code} ${global ? "from Global Settings" : "from Workspace Settings"}`,
tooltip: `This will exclude the rule ${diagnostic.code} in the ${global ? "Global" : "Workspace"} Settings`,
arguments: [diagnostic.code]
};
action.diagnostics = [diagnostic];
action.isPreferred = true;

return action;
}

private createDocumentationAction(
diagnostic: vscode.Diagnostic
): vscode.CodeAction {
const action = new vscode.CodeAction(
`View Documentation for Rule ${diagnostic.code}`,
vscode.CodeActionKind.QuickFix
);
action.command = {
command: VIEW_DOCUMENTATION,
title: `View Documentation for Rule ${diagnostic.code}`,
tooltip: `This will open the SQLFluff documentation for rule ${diagnostic.code}`,
arguments: [diagnostic.code]
};
action.diagnostics = [diagnostic];
action.isPreferred = false;

return action;
}
}

export class HoverProvider implements vscode.HoverProvider {
Expand Down

0 comments on commit 218630b

Please sign in to comment.