Skip to content

Add CodeAction support to show PSSA rule documentation #1600

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

Merged
merged 1 commit into from
Nov 15, 2018
Merged
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
31 changes: 27 additions & 4 deletions src/features/CodeActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import vscode = require("vscode");
import { LanguageClient } from "vscode-languageclient";
import Window = vscode.window;
import { IFeature } from "../feature";
import { ILogger } from "../logging";

export class CodeActionsFeature implements IFeature {
private command: vscode.Disposable;
private applyEditsCommand: vscode.Disposable;
private showDocumentationCommand: vscode.Disposable;
private languageClient: LanguageClient;

constructor() {
this.command = vscode.commands.registerCommand("PowerShell.ApplyCodeActionEdits", (edit: any) => {
constructor(private log: ILogger) {
this.applyEditsCommand = vscode.commands.registerCommand("PowerShell.ApplyCodeActionEdits", (edit: any) => {
Window.activeTextEditor.edit((editBuilder) => {
editBuilder.replace(
new vscode.Range(
Expand All @@ -23,13 +25,34 @@ export class CodeActionsFeature implements IFeature {
edit.Text);
});
});

this.showDocumentationCommand =
vscode.commands.registerCommand("PowerShell.ShowCodeActionDocumentation", (ruleName: any) => {
this.showRuleDocumentation(ruleName);
});
}

public dispose() {
this.command.dispose();
this.applyEditsCommand.dispose();
this.showDocumentationCommand.dispose();
}

public setLanguageClient(languageclient: LanguageClient) {
this.languageClient = languageclient;
}

public showRuleDocumentation(ruleId: string) {
const pssaDocBaseURL = "https://github.com/PowerShell/PSScriptAnalyzer/blob/development/RuleDocumentation";

if (!ruleId) {
this.log.writeWarning("Cannot show documentation for code action, no ruleName was supplied.");
return;
}

if (ruleId.startsWith("PS")) {
ruleId = ruleId.substr(2);
}

vscode.commands.executeCommand("vscode.open", vscode.Uri.parse(pssaDocBaseURL + `/${ruleId}.md`));
}
}
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export function activate(context: vscode.ExtensionContext): void {
new PesterTestsFeature(sessionManager),
new ExtensionCommandsFeature(logger),
new SelectPSSARulesFeature(logger),
new CodeActionsFeature(),
new CodeActionsFeature(logger),
new NewFileOrProjectFeature(),
new DocumentFormatterFeature(logger, documentSelector),
new RemoteFilesFeature(),
Expand Down