Skip to content

Commit ea0cf3a

Browse files
bergmeisterChristoph BergmeisterTylerLeonhardt
authored
🧹 Refactor classes that do not need a language client to not inherit from IFeature (#2685)
* Refactor classes that do not need a language client to not inherit from IFeature * Use base class LanguageClientConsumer * Remove IFeature by making languageClientConsumer abstract * Provide base implementation for setLanguageClient to remove repeated code in some implementations * Adapt ExternalApi.ts and PowerShellNotebooks.ts * newline Co-authored-by: Christoph Bergmeister <[email protected]> Co-authored-by: Tyler James Leonhardt <[email protected]>
1 parent 4f52afa commit ea0cf3a

24 files changed

+117
-184
lines changed

‎src/feature.ts

-12
This file was deleted.

‎src/features/CodeActions.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@
33
*--------------------------------------------------------*/
44

55
import vscode = require("vscode");
6-
import { LanguageClient } from "vscode-languageclient";
76
import Window = vscode.window;
8-
import { IFeature } from "../feature";
97
import { ILogger } from "../logging";
108

11-
export class CodeActionsFeature implements IFeature {
9+
export class CodeActionsFeature implements vscode.Disposable {
1210
private applyEditsCommand: vscode.Disposable;
1311
private showDocumentationCommand: vscode.Disposable;
14-
private languageClient: LanguageClient;
1512

1613
constructor(private log: ILogger) {
1714
this.applyEditsCommand = vscode.commands.registerCommand("PowerShell.ApplyCodeActionEdits", (edit: any) => {
@@ -37,10 +34,6 @@ export class CodeActionsFeature implements IFeature {
3734
this.showDocumentationCommand.dispose();
3835
}
3936

40-
public setLanguageClient(languageclient: LanguageClient) {
41-
this.languageClient = languageclient;
42-
}
43-
4437
public showRuleDocumentation(ruleId: string) {
4538
const pssaDocBaseURL = "https://github.com/PowerShell/PSScriptAnalyzer/blob/master/RuleDocumentation";
4639

‎src/features/Console.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import vscode = require("vscode");
66
import { LanguageClient, NotificationType, RequestType } from "vscode-languageclient";
77
import { ICheckboxQuickPickItem, showCheckboxQuickPick } from "../controls/checkboxQuickPick";
8-
import { IFeature } from "../feature";
98
import { Logger } from "../logging";
109
import Settings = require("../settings");
10+
import { LanguageClientConsumer } from "../languageClientConsumer";
1111

1212
export const EvaluateRequestType = new RequestType<IEvaluateRequestArguments, void, void, void>("evaluate");
1313
export const OutputNotificationType = new NotificationType<IOutputNotificationBody, void>("output");
@@ -197,19 +197,14 @@ function onInputEntered(responseText: string): IShowInputPromptResponseBody {
197197
}
198198
}
199199

200-
export class ConsoleFeature implements IFeature {
200+
export class ConsoleFeature extends LanguageClientConsumer {
201201
private commands: vscode.Disposable[];
202-
private languageClient: LanguageClient;
203202
private resolveStatusBarPromise: (value?: {} | PromiseLike<{}>) => void;
204203

205204
constructor(private log: Logger) {
205+
super();
206206
this.commands = [
207207
vscode.commands.registerCommand("PowerShell.RunSelection", async () => {
208-
if (this.languageClient === undefined) {
209-
this.log.writeAndShowError(`<${ConsoleFeature.name}>: ` +
210-
"Unable to instantiate; language client undefined.");
211-
return;
212-
}
213208

214209
if (vscode.window.activeTerminal &&
215210
vscode.window.activeTerminal.name !== "PowerShell Integrated Console") {
@@ -257,7 +252,6 @@ export class ConsoleFeature implements IFeature {
257252

258253
public setLanguageClient(languageClient: LanguageClient) {
259254
this.languageClient = languageClient;
260-
261255
this.languageClient.onRequest(
262256
ShowChoicePromptRequestType,
263257
(promptDetails) => showChoicePrompt(promptDetails, this.languageClient));

‎src/features/CustomViews.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
import * as path from "path";
66
import * as vscode from "vscode";
77
import { LanguageClient, RequestType } from "vscode-languageclient";
8-
import { IFeature } from "../feature";
8+
import { LanguageClientConsumer } from "../languageClientConsumer";
99

10-
export class CustomViewsFeature implements IFeature {
10+
export class CustomViewsFeature extends LanguageClientConsumer {
1111

1212
private commands: vscode.Disposable[] = [];
13-
private languageClient: LanguageClient;
1413
private contentProvider: PowerShellContentProvider;
1514

1615
constructor() {
16+
super();
1717
this.contentProvider = new PowerShellContentProvider();
1818
this.commands.push(
1919
vscode.workspace.registerTextDocumentContentProvider(
@@ -65,8 +65,6 @@ export class CustomViewsFeature implements IFeature {
6565
args.id,
6666
args.appendedHtmlBodyContent);
6767
});
68-
69-
this.languageClient = languageClient;
7068
}
7169
}
7270

‎src/features/DebugSession.ts

+9-12
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,28 @@ import vscode = require("vscode");
66
import { CancellationToken, DebugConfiguration, DebugConfigurationProvider,
77
ExtensionContext, WorkspaceFolder } from "vscode";
88
import { LanguageClient, NotificationType, RequestType } from "vscode-languageclient";
9-
import { IFeature } from "../feature";
109
import { getPlatformDetails, OperatingSystem } from "../platform";
1110
import { PowerShellProcess} from "../process";
1211
import { SessionManager, SessionStatus } from "../session";
1312
import Settings = require("../settings");
1413
import utils = require("../utils");
1514
import { NamedPipeDebugAdapter } from "../debugAdapter";
1615
import { Logger } from "../logging";
16+
import { LanguageClientConsumer } from "../languageClientConsumer";
1717

1818
export const StartDebuggerNotificationType =
1919
new NotificationType<void, void>("powerShell/startDebugger");
2020

21-
export class DebugSessionFeature implements IFeature, DebugConfigurationProvider, vscode.DebugAdapterDescriptorFactory {
21+
export class DebugSessionFeature extends LanguageClientConsumer
22+
implements DebugConfigurationProvider, vscode.DebugAdapterDescriptorFactory {
2223

2324
private sessionCount: number = 1;
2425
private command: vscode.Disposable;
2526
private tempDebugProcess: PowerShellProcess;
2627
private tempSessionDetails: utils.IEditorServicesSessionDetails;
2728

2829
constructor(context: ExtensionContext, private sessionManager: SessionManager, private logger: Logger) {
30+
super();
2931
// Register a debug configuration provider
3032
context.subscriptions.push(vscode.debug.registerDebugConfigurationProvider("PowerShell", this));
3133
context.subscriptions.push(vscode.debug.registerDebugAdapterDescriptorFactory("PowerShell", this))
@@ -325,10 +327,9 @@ export class DebugSessionFeature implements IFeature, DebugConfigurationProvider
325327
}
326328
}
327329

328-
export class SpecifyScriptArgsFeature implements IFeature {
330+
export class SpecifyScriptArgsFeature implements vscode.Disposable {
329331

330332
private command: vscode.Disposable;
331-
private languageClient: LanguageClient;
332333
private context: vscode.ExtensionContext;
333334

334335
constructor(context: vscode.ExtensionContext) {
@@ -340,10 +341,6 @@ export class SpecifyScriptArgsFeature implements IFeature {
340341
});
341342
}
342343

343-
public setLanguageClient(languageclient: LanguageClient) {
344-
this.languageClient = languageclient;
345-
}
346-
347344
public dispose() {
348345
this.command.dispose();
349346
}
@@ -391,14 +388,14 @@ interface IGetPSHostProcessesResponseBody {
391388
hostProcesses: IPSHostProcessInfo[];
392389
}
393390

394-
export class PickPSHostProcessFeature implements IFeature {
391+
export class PickPSHostProcessFeature extends LanguageClientConsumer {
395392

396393
private command: vscode.Disposable;
397-
private languageClient: LanguageClient;
398394
private waitingForClientToken: vscode.CancellationTokenSource;
399395
private getLanguageClientResolve: (value?: LanguageClient | Thenable<LanguageClient>) => void;
400396

401397
constructor() {
398+
super();
402399

403400
this.command =
404401
vscode.commands.registerCommand("PowerShell.PickPSHostProcess", () => {
@@ -522,14 +519,14 @@ interface IRunspace {
522519
export const GetRunspaceRequestType =
523520
new RequestType<any, IRunspace[], string, void>("powerShell/getRunspace");
524521

525-
export class PickRunspaceFeature implements IFeature {
522+
export class PickRunspaceFeature extends LanguageClientConsumer {
526523

527524
private command: vscode.Disposable;
528-
private languageClient: LanguageClient;
529525
private waitingForClientToken: vscode.CancellationTokenSource;
530526
private getLanguageClientResolve: (value?: LanguageClient | Thenable<LanguageClient>) => void;
531527

532528
constructor() {
529+
super();
533530
this.command =
534531
vscode.commands.registerCommand("PowerShell.PickRunspace", (processId) => {
535532
return this.getLanguageClient()

‎src/features/Examples.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
import path = require("path");
66
import vscode = require("vscode");
7-
import { LanguageClient } from "vscode-languageclient";
8-
import { IFeature } from "../feature";
97

10-
export class ExamplesFeature implements IFeature {
8+
export class ExamplesFeature implements vscode.Disposable {
119
private command: vscode.Disposable;
1210
private examplesPath: string;
1311

@@ -21,10 +19,6 @@ export class ExamplesFeature implements IFeature {
2119
});
2220
}
2321

24-
public setLanguageClient(languageclient: LanguageClient) {
25-
// Eliminate tslint warning
26-
}
27-
2822
public dispose() {
2923
this.command.dispose();
3024
}

‎src/features/ExpandAlias.ts

+4-13
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,18 @@
44

55
import vscode = require("vscode");
66
import Window = vscode.window;
7-
import { LanguageClient, NotificationType, RequestType } from "vscode-languageclient";
8-
import { IFeature } from "../feature";
7+
import { RequestType } from "vscode-languageclient";
98
import { Logger } from "../logging";
9+
import { LanguageClientConsumer } from "../languageClientConsumer";
1010

1111
export const ExpandAliasRequestType = new RequestType<any, any, void, void>("powerShell/expandAlias");
1212

13-
export class ExpandAliasFeature implements IFeature {
13+
export class ExpandAliasFeature extends LanguageClientConsumer {
1414
private command: vscode.Disposable;
15-
private languageClient: LanguageClient;
1615

1716
constructor(private log: Logger) {
17+
super();
1818
this.command = vscode.commands.registerCommand("PowerShell.ExpandAlias", () => {
19-
if (this.languageClient === undefined) {
20-
this.log.writeAndShowError(`<${ExpandAliasFeature.name}>: ` +
21-
"Unable to instantiate; language client undefined.");
22-
return;
23-
}
2419

2520
const editor = Window.activeTextEditor;
2621
const document = editor.document;
@@ -50,8 +45,4 @@ export class ExpandAliasFeature implements IFeature {
5045
public dispose() {
5146
this.command.dispose();
5247
}
53-
54-
public setLanguageClient(languageclient: LanguageClient) {
55-
this.languageClient = languageclient;
56-
}
5748
}

‎src/features/ExtensionCommands.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import * as path from "path";
88
import * as vscode from "vscode";
99
import { LanguageClient, NotificationType, NotificationType0,
1010
Position, Range, RequestType } from "vscode-languageclient";
11-
import { IFeature } from "../feature";
1211
import { Logger } from "../logging";
1312
import Settings = require("../settings");
13+
import { LanguageClientConsumer } from "../languageClientConsumer";
1414

1515
export interface IExtensionCommand {
1616
name: string;
@@ -173,20 +173,15 @@ interface IInvokeRegisteredEditorCommandParameter {
173173
commandName: string;
174174
}
175175

176-
export class ExtensionCommandsFeature implements IFeature {
176+
export class ExtensionCommandsFeature extends LanguageClientConsumer {
177177

178178
private command: vscode.Disposable;
179179
private command2: vscode.Disposable;
180-
private languageClient: LanguageClient;
181180
private extensionCommands: IExtensionCommand[] = [];
182181

183182
constructor(private log: Logger) {
183+
super();
184184
this.command = vscode.commands.registerCommand("PowerShell.ShowAdditionalCommands", () => {
185-
if (this.languageClient === undefined) {
186-
this.log.writeAndShowError(`<${ExtensionCommandsFeature.name}>: ` +
187-
"Unable to instantiate; language client undefined.");
188-
return;
189-
}
190185

191186
const editor = vscode.window.activeTextEditor;
192187
let start = editor.selection.start;

‎src/features/ExternalApi.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import * as vscode from "vscode";
55
import { v4 as uuidv4 } from 'uuid';
66
import { LanguageClient } from "vscode-languageclient";
7-
import { IFeature } from "../feature";
7+
import { LanguageClientConsumer } from "../languageClientConsumer";
88
import { Logger } from "../logging";
99
import { SessionManager } from "../session";
1010

@@ -15,12 +15,12 @@ export interface IExternalPowerShellDetails {
1515
architecture: string;
1616
}
1717

18-
export class ExternalApiFeature implements IFeature {
18+
export class ExternalApiFeature extends LanguageClientConsumer {
1919
private commands: vscode.Disposable[];
20-
private languageClient: LanguageClient;
2120
private static readonly registeredExternalExtension: Map<string, IExternalExtension> = new Map<string, IExternalExtension>();
2221

2322
constructor(private sessionManager: SessionManager, private log: Logger) {
23+
super();
2424
this.commands = [
2525
/*
2626
DESCRIPTION:
@@ -141,10 +141,6 @@ export class ExternalApiFeature implements IFeature {
141141
command.dispose();
142142
}
143143
}
144-
145-
public setLanguageClient(languageclient: LanguageClient) {
146-
this.languageClient = languageclient;
147-
}
148144
}
149145

150146
interface IExternalExtension {

‎src/features/FindModule.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,23 @@
33
*--------------------------------------------------------*/
44

55
import vscode = require("vscode");
6-
import { LanguageClient, NotificationType, RequestType } from "vscode-languageclient";
7-
import Window = vscode.window;
8-
import { IFeature } from "../feature";
6+
import { RequestType } from "vscode-languageclient";
97
import QuickPickItem = vscode.QuickPickItem;
8+
import { LanguageClientConsumer } from "../languageClientConsumer";
109

1110
export const FindModuleRequestType =
1211
new RequestType<any, any, void, void>("powerShell/findModule");
1312

1413
export const InstallModuleRequestType =
1514
new RequestType<string, void, void, void>("powerShell/installModule");
1615

17-
export class FindModuleFeature implements IFeature {
16+
export class FindModuleFeature extends LanguageClientConsumer {
1817

1918
private command: vscode.Disposable;
20-
private languageClient: LanguageClient;
2119
private cancelFindToken: vscode.CancellationTokenSource;
2220

2321
constructor() {
22+
super();
2423
this.command = vscode.commands.registerCommand("PowerShell.PowerShellFindModule", () => {
2524
// It takes a while to get the list of PowerShell modules, display some UI to let user know
2625
this.cancelFindToken = new vscode.CancellationTokenSource();
@@ -53,10 +52,6 @@ export class FindModuleFeature implements IFeature {
5352
});
5453
}
5554

56-
public setLanguageClient(languageclient: LanguageClient) {
57-
this.languageClient = languageclient;
58-
}
59-
6055
public dispose() {
6156
this.command.dispose();
6257
}

‎src/features/GenerateBugReport.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import os = require("os");
66
import vscode = require("vscode");
7-
import { IFeature, LanguageClient } from "../feature";
87
import { SessionManager } from "../session";
98
import Settings = require("../settings");
109

@@ -26,7 +25,7 @@ const extensions =
2625
return 0;
2726
});
2827

29-
export class GenerateBugReportFeature implements IFeature {
28+
export class GenerateBugReportFeature implements vscode.Disposable {
3029

3130
private command: vscode.Disposable;
3231

@@ -81,10 +80,6 @@ ${this.generateExtensionTable(extensions)}
8180
this.command.dispose();
8281
}
8382

84-
public setLanguageClient(languageclient: LanguageClient) {
85-
// Eliminate tslint warning.
86-
}
87-
8883
private generateExtensionTable(installedExtensions): string {
8984
if (!installedExtensions.length) {
9085
return "none";

0 commit comments

Comments
 (0)