Skip to content

Commit

Permalink
First Connect
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinGrote committed Oct 29, 2024
1 parent d617194 commit 69e3209
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 26 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1011,12 +1011,12 @@
"verbose"
],
"default": "off",
"markdownDescription": "Traces the communication between VS Code and the PowerShell Editor Services [LSP Server](https://microsoft.github.io/language-server-protocol/). **only for extension developers and issue troubleshooting!**"
"markdownDescription": "Traces the communication between VS Code and the PowerShell Editor Services [LSP Server](https://microsoft.github.io/language-server-protocol/). **Only for extension developers and issue troubleshooting!**"
},
"powershell.trace.dap": {
"type": "boolean",
"default": false,
"markdownDescription": "Traces the communication between VS Code and the PowerShell Editor Services [DAP Server](https://microsoft.github.io/debug-adapter-protocol/). **This setting is only meant for extension developers and issue troubleshooting!**"
"markdownDescription": "Traces the communication between VS Code and the PowerShell Editor Services [DAP Server](https://microsoft.github.io/debug-adapter-protocol/). **Only meant for extension developers and issue troubleshooting!**"
}
}
},
Expand Down
32 changes: 18 additions & 14 deletions src/features/DebugSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { PowerShellProcess } from "../process";
import { IEditorServicesSessionDetails, SessionManager } from "../session";
import { getSettings } from "../settings";
import path from "path";
import { checkIfFileExists } from "../utils";
import { checkIfFileExists, onPowerShellSettingChange } from "../utils";

export const StartDebuggerNotificationType =
new NotificationType<void>("powerShell/startDebugger");
Expand Down Expand Up @@ -608,17 +608,9 @@ class PowerShellDebugAdapterTrackerFactory implements DebugAdapterTrackerFactory
disposables: Disposable[] = [];
dapLogEnabled: boolean = workspace.getConfiguration("powershell").get<boolean>("trace.dap") ?? false;
constructor(private adapterName = "PowerShell") {
this.disposables.push(workspace.onDidChangeConfiguration(change => {
if (
change.affectsConfiguration("powershell.trace.dap")
) {
this.dapLogEnabled = workspace.getConfiguration("powershell").get<boolean>("trace.dap") ?? false;
if (this.dapLogEnabled) {
// Trigger the output pane to appear. This gives the user time to position it before starting a debug.
this.log?.show(true);
}
}
}));
this.disposables.push(
onPowerShellSettingChange("trace.dap", (visible:boolean | undefined) => {this.setLogVisibility(visible);})
);
}

/* We want to use a shared output log for separate debug sessions as usually only one is running at a time and we
Expand All @@ -630,10 +622,20 @@ class PowerShellDebugAdapterTrackerFactory implements DebugAdapterTrackerFactory
_log: LogOutputChannel | undefined;
get log(): LogOutputChannel | undefined {
if (this.dapLogEnabled && this._log === undefined) {
this._log = window.createOutputChannel(`${this.adapterName} Trace - DAP`, { log: true });
this._log = window.createOutputChannel(`${this.adapterName}: Trace DAP`, { log: true });
this.disposables.push(this._log);
}
return this.dapLogEnabled ? this._log : undefined;
return this._log;
}

private setLogVisibility(visible?: boolean): void {
if (this.log !== undefined) {
if (visible) {
this.log.show(true);
} else {
this.log.hide();
}
}
}

createDebugAdapterTracker(session: DebugSession): DebugAdapterTracker {
Expand Down Expand Up @@ -663,6 +665,8 @@ class PowerShellDebugAdapterTrackerFactory implements DebugAdapterTrackerFactory
};
}



dispose(): void {
this.disposables.forEach(d => d.dispose());
}
Expand Down
25 changes: 23 additions & 2 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ export class SessionManager implements Middleware {
private suppressRestartPrompt = false;
private versionDetails: IPowerShellVersionDetails | undefined;

private _outputChannel?: vscode.LogOutputChannel;
/** Omnisharp and PSES messages sent via LSP are surfaced here. */
private get outputChannel(): vscode.LogOutputChannel {
return this._outputChannel
??= vscode.window.createOutputChannel("PowerShell: Editor Services", { log: true });
}

private _traceOutputChannel?: vscode.LogOutputChannel;
/** The LanguageClient LSP message trace is surfaced here. */
private get traceOutputChannel(): vscode.LogOutputChannel {
return this._traceOutputChannel
??= vscode.window.createOutputChannel("PowerShell: Trace LSP", { log: true });
}

constructor(
private extensionContext: vscode.ExtensionContext,
private sessionSettings: Settings,
Expand Down Expand Up @@ -131,13 +145,16 @@ export class SessionManager implements Middleware {
this.HostVersion = this.HostVersion.split("-")[0];

this.registerCommands();

}


public async dispose(): Promise<void> {
await this.stop(); // A whole lot of disposals.

this.languageStatusItem.dispose();


for (const handler of this.registeredHandlers) {
handler.dispose();
}
Expand Down Expand Up @@ -622,6 +639,7 @@ export class SessionManager implements Middleware {
});
});
};

const clientOptions: LanguageClientOptions = {
documentSelector: this.documentSelector,
synchronize: {
Expand All @@ -645,9 +663,11 @@ export class SessionManager implements Middleware {
// hangs up (ECONNRESET errors).
error: (_error: Error, _message: Message, _count: number): ErrorHandlerResult => {
// TODO: Is there any error worth terminating on?
this.logger.writeError(`${_error.name}: ${_error.message} ${_error.cause}`);
return { action: ErrorAction.Continue };
},
closed: (): CloseHandlerResult => {
this.logger.write("Language service connection closed.");
// We have our own restart experience
return {
action: CloseAction.DoNotRestart,
Expand All @@ -656,8 +676,8 @@ export class SessionManager implements Middleware {
},
},
middleware: this,
traceOutputChannel: vscode.window.createOutputChannel("PowerShell Trace - LSP", {log: true}),
outputChannel: vscode.window.createOutputChannel("PowerShell Editor Services", {log: true}),
traceOutputChannel: this.traceOutputChannel,
outputChannel: this.outputChannel
};

const languageClient = new LanguageClient("powershell", "PowerShell Editor Services Client", connectFunc, clientOptions);
Expand All @@ -666,6 +686,7 @@ export class SessionManager implements Middleware {
// TODO: We should only turn this on in preview.
languageClient.registerProposedFeatures();


// NOTE: We don't currently send any events from PSES, but may again in
// the future so we're leaving this side wired up.
languageClient.onTelemetry((event) => {
Expand Down
41 changes: 33 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,11 @@ export function sleep(ms: number): Promise<void> {
}

/**
* Invokes the specified action when a PowerShell setting changes
* @param setting a string representation of the setting you wish to evaluate
* Invokes the specified action when a setting changes
* @param setting a string representation of the setting you wish to evaluate, e.g. `trace.server`
* @param action the action to take when the setting changes
* @param scope the scope in which the vscode setting should be evaluated. Defaults to
* @param section the section of the vscode settings to evaluate. Defaults to `powershell`
* @param scope the scope in which the vscode setting should be evaluated.
* @param workspace
* @param onDidChangeConfiguration
* @example onPowerShellSettingChange("settingName", (newValue) => console.log(newValue));
Expand All @@ -99,19 +100,43 @@ export function sleep(ms: number): Promise<void> {

// Because we actually do use the constraint in the callback
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
export function onPowerShellSettingChange<T>(
export function onSettingChange<T>(
section: string,
setting: string,
listener: (newValue: T | undefined) => void,
section: "powershell",
action: (newValue: T | undefined) => void,
scope?: vscode.ConfigurationScope,
): vscode.Disposable {
const settingPath = `${section}.${setting}`;
return vscode.workspace.onDidChangeConfiguration(e => {
if (!e.affectsConfiguration(section, scope)) { return; }
if (!e.affectsConfiguration(settingPath, scope)) { return; }
const value = vscode.workspace.getConfiguration(section, scope).get<T>(setting);
listener(value);
action(value);
});
}

/**
* Invokes the specified action when a PowerShell setting changes. Convenience function for `onSettingChange`
* @param setting a string representation of the setting you wish to evaluate, e.g. `trace.server`
* @param action the action to take when the setting changes
* @param section the section of the vscode settings to evaluate. Defaults to `powershell`
* @param scope the scope in which the vscode setting should be evaluated.
* @param workspace
* @param onDidChangeConfiguration
* @example onPowerShellSettingChange("settingName", (newValue) => console.log(newValue));
* @returns a Disposable object that can be used to stop listening for changes
*/

// Because we actually do use the constraint in the callback
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
export function onPowerShellSettingChange<T>(
setting: string,
action: (newValue: T | undefined) => void,
scope?: vscode.ConfigurationScope,
): vscode.Disposable {
const section = "powershell";
return onSettingChange(section, setting, action, scope);
}

export const isMacOS: boolean = process.platform === "darwin";
export const isWindows: boolean = process.platform === "win32";
export const isLinux: boolean = !isMacOS && !isWindows;

0 comments on commit 69e3209

Please sign in to comment.