Skip to content

Commit 96320e6

Browse files
committed
Add CodeAction support to show PSSA rule documentation
This corresponds to PSES PR: PowerShell/PowerShellEditorServices#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.
1 parent d02adc5 commit 96320e6

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/features/CodeActions.ts

+27-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import vscode = require("vscode");
66
import { LanguageClient } from "vscode-languageclient";
77
import Window = vscode.window;
88
import { IFeature } from "../feature";
9+
import { ILogger } from "../logging";
910

1011
export class CodeActionsFeature implements IFeature {
11-
private command: vscode.Disposable;
12+
private applyEditsCommand: vscode.Disposable;
13+
private showDocumentationCommand: vscode.Disposable;
1214
private languageClient: LanguageClient;
1315

14-
constructor() {
15-
this.command = vscode.commands.registerCommand("PowerShell.ApplyCodeActionEdits", (edit: any) => {
16+
constructor(private log: ILogger) {
17+
this.applyEditsCommand = vscode.commands.registerCommand("PowerShell.ApplyCodeActionEdits", (edit: any) => {
1618
Window.activeTextEditor.edit((editBuilder) => {
1719
editBuilder.replace(
1820
new vscode.Range(
@@ -23,13 +25,34 @@ export class CodeActionsFeature implements IFeature {
2325
edit.Text);
2426
});
2527
});
28+
29+
this.showDocumentationCommand =
30+
vscode.commands.registerCommand("PowerShell.ShowCodeActionDocumentation", (ruleName: any) => {
31+
this.showRuleDocumentation(ruleName);
32+
});
2633
}
2734

2835
public dispose() {
29-
this.command.dispose();
36+
this.applyEditsCommand.dispose();
37+
this.showDocumentationCommand.dispose();
3038
}
3139

3240
public setLanguageClient(languageclient: LanguageClient) {
3341
this.languageClient = languageclient;
3442
}
43+
44+
public showRuleDocumentation(ruleId: string) {
45+
const pssaDocBaseURL = "https://github.com/PowerShell/PSScriptAnalyzer/blob/development/RuleDocumentation";
46+
47+
if (!ruleId) {
48+
this.log.writeWarning("Cannot show documentation for code action, no ruleName was supplied.");
49+
return;
50+
}
51+
52+
if (ruleId.startsWith("PS")) {
53+
ruleId = ruleId.substr(2);
54+
}
55+
56+
vscode.commands.executeCommand("vscode.open", vscode.Uri.parse(pssaDocBaseURL + `/${ruleId}.md`));
57+
}
3558
}

src/main.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export function activate(context: vscode.ExtensionContext): void {
128128
new PesterTestsFeature(sessionManager),
129129
new ExtensionCommandsFeature(logger),
130130
new SelectPSSARulesFeature(logger),
131-
new CodeActionsFeature(),
131+
new CodeActionsFeature(logger),
132132
new NewFileOrProjectFeature(),
133133
new DocumentFormatterFeature(logger, documentSelector),
134134
new RemoteFilesFeature(),

0 commit comments

Comments
 (0)