Skip to content

Update vscode-languageclient and refactor (a lot of TLC) #4131

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 5 commits into from
Aug 12, 2022
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
2,118 changes: 1,034 additions & 1,084 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"publisher": "ms-vscode",
"description": "(Preview) Develop PowerShell modules, commands and scripts in Visual Studio Code!",
"engines": {
"vscode": "^1.59.0"
"vscode": "^1.65.0"
},
"license": "SEE LICENSE IN LICENSE.txt",
"homepage": "https://github.com/PowerShell/vscode-powershell/blob/main/README.md",
Expand Down Expand Up @@ -45,26 +45,26 @@
"onView:PowerShellCommands"
],
"dependencies": {
"@vscode/extension-telemetry": "~0.6.2",
"@vscode/extension-telemetry": "^0.6.2",
"node-fetch": "~2.6.7",
"semver": "~7.3.7",
"uuid": "~8.3.2",
"vscode-languageclient": "~7.0.0",
"vscode-languageclient": "~8.0.2",
"vscode-languageserver-protocol": "~3.17.2"
},
"devDependencies": {
"@types/glob": "~7.2.0",
"@types/mocha": "~9.1.1",
"@types/mock-fs": "~4.13.1",
"@types/node": "~14.17.4",
"@types/node-fetch": "~2.6.1",
"@types/node-fetch": "~2.6.2",
"@types/rewire": "~2.5.28",
"@types/semver": "~7.3.11",
"@types/semver": "~7.3.12",
"@types/sinon": "~10.0.13",
"@types/uuid": "~8.3.4",
"@types/vscode": "~1.59.0",
"@types/vscode": "~1.65.0",
"@vscode/test-electron": "~2.1.5",
"esbuild": "^0.15.0",
"esbuild": "^0.15.1",
"glob": "^8.0.3",
"mocha": "~10.0.0",
"mocha-multi-reporters": "~1.5.1",
Expand Down
9 changes: 5 additions & 4 deletions src/controls/checkboxQuickPick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ function getQuickPickItems(items: ICheckboxQuickPickItem[]): vscode.QuickPickIte
const quickPickItems: vscode.QuickPickItem[] = [];
quickPickItems.push({ label: confirmItemLabel, description: "" });

items.forEach((item) =>
for (const item of items) {
quickPickItems.push({
label: convertToCheckBox(item),
description: item.description,
}));
label: convertToCheckBox(item),
description: item.description,
});
}

return quickPickItems;
}
Expand Down
114 changes: 56 additions & 58 deletions src/features/Console.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

"use strict";

import vscode = require("vscode");
import { NotificationType, RequestType } from "vscode-languageclient";
import { LanguageClient } from "vscode-languageclient/node";
Expand All @@ -16,11 +18,11 @@ export const ExecutionStatusChangedNotificationType =

export const ShowChoicePromptRequestType =
new RequestType<IShowChoicePromptRequestArgs,
IShowChoicePromptResponseBody, string>("powerShell/showChoicePrompt");
IShowChoicePromptResponseBody, string>("powerShell/showChoicePrompt");

export const ShowInputPromptRequestType =
new RequestType<IShowInputPromptRequestArgs,
IShowInputPromptResponseBody, string>("powerShell/showInputPrompt");
IShowInputPromptResponseBody, string>("powerShell/showInputPrompt");

export interface IEvaluateRequestArguments {
expression: string;
Expand Down Expand Up @@ -127,30 +129,22 @@ function showChoicePrompt(
});

// Select the defaults
promptDetails.defaultChoices.forEach((choiceIndex) => {
checkboxQuickPickItems[choiceIndex].isSelected = true;
});
for (const choice of promptDetails.defaultChoices) {
checkboxQuickPickItems[choice].isSelected = true;
};

resultThenable =
showCheckboxQuickPick(
checkboxQuickPickItems,
{ confirmPlaceHolder: promptDetails.message })
checkboxQuickPickItems,
{ confirmPlaceHolder: promptDetails.message })
.then(onItemsSelected);
}

return resultThenable;
}

function showInputPrompt(
promptDetails: IShowInputPromptRequestArgs,
client: LanguageClient): Thenable<IShowInputPromptResponseBody> {

const resultThenable =
vscode.window.showInputBox({
placeHolder: promptDetails.name + ": ",
}).then(onInputEntered);

return resultThenable;
function showInputPrompt(promptDetails: IShowInputPromptRequestArgs): Thenable<IShowInputPromptResponseBody> {
return vscode.window.showInputBox({ placeHolder: promptDetails.name + ": " }).then(onInputEntered);
}

function onItemsSelected(chosenItems: ICheckboxQuickPickItem[]): IShowChoicePromptResponseBody {
Expand Down Expand Up @@ -199,13 +193,13 @@ function onInputEntered(responseText: string): IShowInputPromptResponseBody {

export class ConsoleFeature extends LanguageClientConsumer {
private commands: vscode.Disposable[];
private handlers: vscode.Disposable[];
private resolveStatusBarPromise: (value?: {} | PromiseLike<{}>) => void;

constructor(private log: Logger) {
super();
this.commands = [
vscode.commands.registerCommand("PowerShell.RunSelection", async () => {

if (vscode.window.activeTerminal &&
vscode.window.activeTerminal.name !== "PowerShell Extension") {
this.log.write("PowerShell Extension Terminal is not active! Running in current terminal using 'runSelectedText'");
Expand All @@ -224,15 +218,12 @@ export class ConsoleFeature extends LanguageClientConsumer {
let selectionRange: vscode.Range;

if (!editor.selection.isEmpty) {
selectionRange =
new vscode.Range(
editor.selection.start,
editor.selection.end);
selectionRange = new vscode.Range(editor.selection.start, editor.selection.end);
} else {
selectionRange = editor.document.lineAt(editor.selection.start.line).range;
}

this.languageClient.sendRequest(EvaluateRequestType, {
await this.languageClient.sendRequest(EvaluateRequestType, {
expression: editor.document.getText(selectionRange),
});

Expand All @@ -247,51 +238,58 @@ export class ConsoleFeature extends LanguageClientConsumer {
public dispose() {
// Make sure we cancel any status bar
this.clearStatusBar();
this.commands.forEach((command) => command.dispose());
for (const command of this.commands) {
command.dispose();
}
for (const handler of this.handlers) {
handler.dispose();
}
}

public setLanguageClient(languageClient: LanguageClient) {
this.languageClient = languageClient;
this.languageClient.onRequest(
ShowChoicePromptRequestType,
(promptDetails) => showChoicePrompt(promptDetails, this.languageClient));

this.languageClient.onRequest(
ShowInputPromptRequestType,
(promptDetails) => showInputPrompt(promptDetails, this.languageClient));

// Set up status bar alerts for when PowerShell is executing a script
this.languageClient.onNotification(
ExecutionStatusChangedNotificationType,
(executionStatusDetails) => {
switch (executionStatusDetails.executionStatus) {
// If execution has changed to running, make a notification
case ExecutionStatus.Running:
this.showExecutionStatus("PowerShell");
break;

// If the execution has stopped, destroy the previous notification
case ExecutionStatus.Completed:
case ExecutionStatus.Aborted:
case ExecutionStatus.Failed:
this.clearStatusBar();
break;
}
});

this.handlers = [
this.languageClient.onRequest(
ShowChoicePromptRequestType,
(promptDetails) => showChoicePrompt(promptDetails, this.languageClient)),

this.languageClient.onRequest(
ShowInputPromptRequestType,
(promptDetails) => showInputPrompt(promptDetails)),

// TODO: We're not receiving these events from the server any more.
// Set up status bar alerts for when PowerShell is executing a script.
this.languageClient.onNotification(
ExecutionStatusChangedNotificationType,
(executionStatusDetails) => {
switch (executionStatusDetails.executionStatus) {
// If execution has changed to running, make a notification
case ExecutionStatus.Running:
this.showExecutionStatus("PowerShell");
break;

// If the execution has stopped, destroy the previous notification
case ExecutionStatus.Completed:
case ExecutionStatus.Aborted:
case ExecutionStatus.Failed:
this.clearStatusBar();
break;
}
})
]
}

private showExecutionStatus(message: string) {
vscode.window.withProgress({
location: vscode.ProgressLocation.Window,
}, (progress) => {
return new Promise((resolve, reject) => {
this.clearStatusBar();
location: vscode.ProgressLocation.Window,
}, (progress) => {
return new Promise((resolve, _reject) => {
this.clearStatusBar();

this.resolveStatusBarPromise = resolve;
progress.report({ message });
});
this.resolveStatusBarPromise = resolve;
progress.report({ message });
});
});
}

private clearStatusBar() {
Expand Down
22 changes: 9 additions & 13 deletions src/features/CustomViews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export class CustomViewsFeature extends LanguageClientConsumer {
}

public dispose() {
this.commands.forEach((d) => d.dispose());
for (const command of this.commands) {
command.dispose();
}
}

public setLanguageClient(languageClient: LanguageClient) {
Expand Down Expand Up @@ -177,23 +179,17 @@ class HtmlContentView extends CustomView {
let styleTags = "";
if (this.htmlContent.styleSheetPaths &&
this.htmlContent.styleSheetPaths.length > 0) {
this.htmlContent.styleSheetPaths.forEach(
(styleSheetPath) => {
styleTags += `<link rel="stylesheet" href="${
styleSheetPath.toString().replace("file://", "vscode-resource://")
}">\n`;
});
for (const styleSheetPath of this.htmlContent.styleSheetPaths) {
styleTags += `<link rel="stylesheet" href="${styleSheetPath.toString().replace("file://", "vscode-resource://")}">\n`;
}
}

let scriptTags = "";
if (this.htmlContent.javaScriptPaths &&
this.htmlContent.javaScriptPaths.length > 0) {
this.htmlContent.javaScriptPaths.forEach(
(javaScriptPath) => {
scriptTags += `<script src="${
javaScriptPath.toString().replace("file://", "vscode-resource://")
}"></script>\n`;
});
for (const javaScriptPath of this.htmlContent.javaScriptPaths) {
scriptTags += `<script src="${javaScriptPath.toString().replace("file://", "vscode-resource://")}"></script>\n`;
}
}

// Return an HTML page with the specified content
Expand Down
37 changes: 22 additions & 15 deletions src/features/DebugSession.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

"use strict";

import vscode = require("vscode");
import { CancellationToken, DebugConfiguration, DebugConfigurationProvider,
ExtensionContext, WorkspaceFolder } from "vscode";
Expand All @@ -25,6 +27,7 @@ export class DebugSessionFeature extends LanguageClientConsumer
private sessionCount: number = 1;
private tempDebugProcess: PowerShellProcess;
private tempSessionDetails: IEditorServicesSessionDetails;
private handlers: vscode.Disposable[];

constructor(context: ExtensionContext, private sessionManager: SessionManager, private logger: Logger) {
super();
Expand All @@ -47,28 +50,32 @@ export class DebugSessionFeature extends LanguageClientConsumer
return new vscode.DebugAdapterNamedPipeServer(sessionDetails.debugServicePipeName);
}

// tslint:disable-next-line:no-empty
public dispose() {
for (const handler of this.handlers) {
handler.dispose();
}
}

public setLanguageClient(languageClient: LanguageClient) {
languageClient.onNotification(
StartDebuggerNotificationType,
// TODO: Use a named debug configuration.
() => vscode.debug.startDebugging(undefined, {
request: "launch",
type: "PowerShell",
name: "PowerShell: Interactive Session"
}));

languageClient.onNotification(
StopDebuggerNotificationType,
() => vscode.debug.stopDebugging(undefined));
this.handlers = [
languageClient.onNotification(
StartDebuggerNotificationType,
// TODO: Use a named debug configuration.
async () => await vscode.debug.startDebugging(undefined, {
request: "launch",
type: "PowerShell",
name: "PowerShell: Interactive Session"
})),

languageClient.onNotification(
StopDebuggerNotificationType,
async () => await vscode.debug.stopDebugging(undefined))
];
}

public async provideDebugConfigurations(
folder: WorkspaceFolder | undefined,
token?: CancellationToken): Promise<DebugConfiguration[]> {
_folder: WorkspaceFolder | undefined,
_token?: CancellationToken): Promise<DebugConfiguration[]> {

enum DebugConfig {
LaunchCurrentFile,
Expand Down
Loading