From 96320e648ee82016142c4c1f86ae7c673a741e41 Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Sun, 4 Nov 2018 16:52:25 -0700 Subject: [PATCH] Add CodeAction support to show PSSA rule documentation This corresponds to PSES PR: https://github.com/PowerShell/PowerShellEditorServices/pull/789 Currently this points to the PSSA development branch. Maybe that should point to the master branch. Could be a setting I suppose but that seems like overkill. --- src/features/CodeActions.ts | 31 +++++++++++++++++++++++++++---- src/main.ts | 2 +- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/features/CodeActions.ts b/src/features/CodeActions.ts index 871773b64a..53f18fd1db 100644 --- a/src/features/CodeActions.ts +++ b/src/features/CodeActions.ts @@ -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( @@ -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`)); + } } diff --git a/src/main.ts b/src/main.ts index 86482a05af..8c78193a32 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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(),