From 877fe439edb4147c9cd1df4db88bc72f9419389b Mon Sep 17 00:00:00 2001 From: Paula Camargo Date: Wed, 31 May 2023 15:29:38 -0700 Subject: [PATCH 1/8] Add port attribute provider --- .../debugPort/portAttributesProvider.ts | 31 ++++++ src/extension/extensionInit.ts | 27 ++++- types/vscode.proposed.portsAttributes.d.ts | 103 ++++++++++++++++++ 3 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 src/extension/debugger/debugPort/portAttributesProvider.ts create mode 100644 types/vscode.proposed.portsAttributes.d.ts diff --git a/src/extension/debugger/debugPort/portAttributesProvider.ts b/src/extension/debugger/debugPort/portAttributesProvider.ts new file mode 100644 index 00000000..e9a8e23f --- /dev/null +++ b/src/extension/debugger/debugPort/portAttributesProvider.ts @@ -0,0 +1,31 @@ +/*--------------------------------------------------------- + * Copyright (C) Microsoft Corporation. All rights reserved. + *--------------------------------------------------------*/ + +import { CancellationToken, PortAttributes, PortAttributesProvider, PortAutoForwardAction, ProviderResult } from 'vscode'; + +export const enum DefaultPythonDebugPorts { + Min = 55000, + Max = 56000, +} + +export class DebugPortAttributesProvider implements PortAttributesProvider { + /** Cache of used ports (#1092) */ + private cachedResolutions: string[] = new Array(16).fill(''); + + /** Index counter for the next cached resolution index in the list */ + private cachedResolutionIndex = 0; + + public providePortAttributes( + port: number, + pid: number | undefined, + commandLine: string | undefined, + token: CancellationToken + ): ProviderResult { + if (port >= DefaultPythonDebugPorts.Min && port <= DefaultPythonDebugPorts.Max) { + return new PortAttributes(PortAutoForwardAction.OpenBrowser); + } else { + return undefined; + } + } +} diff --git a/src/extension/extensionInit.ts b/src/extension/extensionInit.ts index 5318cd23..3026ab6b 100644 --- a/src/extension/extensionInit.ts +++ b/src/extension/extensionInit.ts @@ -3,8 +3,8 @@ 'use strict'; -import { debug, DebugConfigurationProviderTriggerKind, languages, Uri, window } from 'vscode'; -import { executeCommand, getConfiguration, registerCommand, startDebugging } from './common/vscodeapi'; +import { debug, DebugConfigurationProviderTriggerKind, languages, Uri, window, workspace } from 'vscode'; +import { executeCommand, getConfiguration, registerCommand, showInformationMessage, startDebugging } from './common/vscodeapi'; import { DebuggerTypeName } from './constants'; import { DynamicPythonDebugConfigurationService } from './debugger/configuration/dynamicdebugConfigurationService'; import { IExtensionContext } from './common/types'; @@ -31,6 +31,8 @@ import { JsonLanguages, LaunchJsonCompletionProvider } from './debugger/configur import { LaunchJsonUpdaterServiceHelper } from './debugger/configuration/launch.json/updaterServiceHelper'; import { ignoreErrors } from './common/promiseUtils'; import { pickArgsInput } from './common/utils/localize'; +import { DebugPortAttributesProvider, DefaultPythonDebugPorts } from './debugger/debugPort/portAttributesProvider'; +import { createServer, IncomingMessage, ServerResponse } from 'http'; export async function registerDebugger(context: IExtensionContext): Promise { const childProcessAttachService = new ChildProcessAttachService(); @@ -125,4 +127,25 @@ export async function registerDebugger(context: IExtensionContext): Promise { + showInformationMessage('Hello World!'); + + const server = createServer((request: IncomingMessage, response: ServerResponse) => { + response.end('Hello world!'); + }); + + server.listen(55100, () => { + showInformationMessage('Message received'); + }); + }) + ); } diff --git a/types/vscode.proposed.portsAttributes.d.ts b/types/vscode.proposed.portsAttributes.d.ts new file mode 100644 index 00000000..cf130357 --- /dev/null +++ b/types/vscode.proposed.portsAttributes.d.ts @@ -0,0 +1,103 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +declare module 'vscode' { + + // https://github.com/microsoft/vscode/issues/115616 @alexr00 + + /** + * The action that should be taken when a port is discovered through automatic port forwarding discovery. + */ + export enum PortAutoForwardAction { + /** + * Notify the user that the port is being forwarded. This is the default action. + */ + Notify = 1, + /** + * Once the port is forwarded, open the browser to the forwarded port. + */ + OpenBrowser = 2, + /** + * Once the port is forwarded, open the preview browser to the forwarded port. + */ + OpenPreview = 3, + /** + * Forward the port silently. + */ + Silent = 4, + /** + * Do not forward the port. + */ + Ignore = 5, + /** + * Once the port is forwarded, open the browser to the forwarded port. Only open the browser the first time the port is forwarded in a session. + */ + OpenBrowserOnce = 6 + } + + /** + * The attributes that a forwarded port can have. + */ + export class PortAttributes { + /** + * The action to be taken when this port is detected for auto forwarding. + */ + autoForwardAction: PortAutoForwardAction; + + /** + * @deprecated + */ + constructor(port: number, autoForwardAction: PortAutoForwardAction); + /** + * Creates a new PortAttributes object + * @param port the port number + * @param autoForwardAction the action to take when this port is detected + */ + constructor(autoForwardAction: PortAutoForwardAction); + } + + /** + * A provider of port attributes. Port attributes are used to determine what action should be taken when a port is discovered. + */ + export interface PortAttributesProvider { + /** + * Provides attributes for the given port. For ports that your extension doesn't know about, simply + * return undefined. For example, if `providePortAttributes` is called with ports 3000 but your + * extension doesn't know anything about 3000 you should return undefined. + */ + providePortAttributes(port: number, pid: number | undefined, commandLine: string | undefined, token: CancellationToken): ProviderResult; + } + + /** + * A selector that will be used to filter which {@link PortAttributesProvider} should be called for each port. + */ + export interface PortAttributesSelector { + /** + * Specifying a port range will cause your provider to only be called for ports within the range. + */ + portRange?: [number, number]; + + /** + * Specifying a command pattern will cause your provider to only be called for processes whose command line matches the pattern. + */ + commandPattern?: RegExp; + } + + export namespace workspace { + /** + * If your extension listens on ports, consider registering a PortAttributesProvider to provide information + * about the ports. For example, a debug extension may know about debug ports in it's debuggee. By providing + * this information with a PortAttributesProvider the extension can tell the editor that these ports should be + * ignored, since they don't need to be user facing. + * + * The results of the PortAttributesProvider are merged with the user setting `remote.portsAttributes`. If the values conflict, the user setting takes precedence. + * + * @param portSelector It is best practice to specify a port selector to avoid unnecessary calls to your provider. + * If you don't specify a port selector your provider will be called for every port, which will result in slower port forwarding for the user. + * @param provider The {@link PortAttributesProvider PortAttributesProvider}. + */ + export function registerPortAttributesProvider(portSelector: PortAttributesSelector, provider: PortAttributesProvider): Disposable; + } +} From d5e3828f4bcfb89a252814f8af2068a81257e9bf Mon Sep 17 00:00:00 2001 From: Paula Camargo Date: Mon, 5 Jun 2023 17:50:55 -0700 Subject: [PATCH 2/8] Add message --- src/extension/extensionInit.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/extension/extensionInit.ts b/src/extension/extensionInit.ts index 3026ab6b..46f7a9a1 100644 --- a/src/extension/extensionInit.ts +++ b/src/extension/extensionInit.ts @@ -143,9 +143,7 @@ export async function registerDebugger(context: IExtensionContext): Promise { - showInformationMessage('Message received'); - }); + server.listen(55100); }) ); } From fc9630d7bedd151c73e6ec44518bc6423352f3f0 Mon Sep 17 00:00:00 2001 From: Paula Camargo Date: Tue, 12 Sep 2023 16:37:02 -0700 Subject: [PATCH 3/8] Enabled proposed api --- package.json | 3 +++ ...ts => vscode.proposed.portsAttributes.d.ts | 21 ++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) rename types/vscode.proposed.portsAttributes.d.ts => vscode.proposed.portsAttributes.d.ts (81%) diff --git a/package.json b/package.json index b9532907..c00f979f 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,9 @@ "description": "Python Debugger extension using `debugpy`.", "version": "2023.3.0-dev", "publisher": "ms-python", + "enabledApiProposals": [ + "portsAttributes" + ], "license": "MIT", "homepage": "https://github.com/Microsoft/vscode-python-debugger", "repository": { diff --git a/types/vscode.proposed.portsAttributes.d.ts b/vscode.proposed.portsAttributes.d.ts similarity index 81% rename from types/vscode.proposed.portsAttributes.d.ts rename to vscode.proposed.portsAttributes.d.ts index cf130357..e3cb4b61 100644 --- a/types/vscode.proposed.portsAttributes.d.ts +++ b/vscode.proposed.portsAttributes.d.ts @@ -16,7 +16,7 @@ declare module 'vscode' { */ Notify = 1, /** - * Once the port is forwarded, open the browser to the forwarded port. + * Once the port is forwarded, open the user's web browser to the forwarded port. */ OpenBrowser = 2, /** @@ -30,11 +30,7 @@ declare module 'vscode' { /** * Do not forward the port. */ - Ignore = 5, - /** - * Once the port is forwarded, open the browser to the forwarded port. Only open the browser the first time the port is forwarded in a session. - */ - OpenBrowserOnce = 6 + Ignore = 5 } /** @@ -46,10 +42,6 @@ declare module 'vscode' { */ autoForwardAction: PortAutoForwardAction; - /** - * @deprecated - */ - constructor(port: number, autoForwardAction: PortAutoForwardAction); /** * Creates a new PortAttributes object * @param port the port number @@ -66,8 +58,12 @@ declare module 'vscode' { * Provides attributes for the given port. For ports that your extension doesn't know about, simply * return undefined. For example, if `providePortAttributes` is called with ports 3000 but your * extension doesn't know anything about 3000 you should return undefined. + * @param port The port number of the port that attributes are being requested for. + * @param pid The pid of the process that is listening on the port. If the pid is unknown, undefined will be passed. + * @param commandLine The command line of the process that is listening on the port. If the command line is unknown, undefined will be passed. + * @param token A cancellation token that indicates the result is no longer needed. */ - providePortAttributes(port: number, pid: number | undefined, commandLine: string | undefined, token: CancellationToken): ProviderResult; + providePortAttributes(attributes: { port: number; pid?: number; commandLine?: string }, token: CancellationToken): ProviderResult; } /** @@ -76,8 +72,9 @@ declare module 'vscode' { export interface PortAttributesSelector { /** * Specifying a port range will cause your provider to only be called for ports within the range. + * The start is inclusive and the end is exclusive. */ - portRange?: [number, number]; + portRange?: [number, number] | number; /** * Specifying a command pattern will cause your provider to only be called for processes whose command line matches the pattern. From 9a33a6e5a9d39d1f0c2dc24eb93206f6442a811d Mon Sep 17 00:00:00 2001 From: Paula Camargo Date: Tue, 12 Sep 2023 16:37:40 -0700 Subject: [PATCH 4/8] Ignore all the ports --- .../debugPort/portAttributesProvider.ts | 25 +++---------------- src/extension/extensionInit.ts | 16 ++---------- 2 files changed, 6 insertions(+), 35 deletions(-) diff --git a/src/extension/debugger/debugPort/portAttributesProvider.ts b/src/extension/debugger/debugPort/portAttributesProvider.ts index e9a8e23f..e733c1bc 100644 --- a/src/extension/debugger/debugPort/portAttributesProvider.ts +++ b/src/extension/debugger/debugPort/portAttributesProvider.ts @@ -4,28 +4,11 @@ import { CancellationToken, PortAttributes, PortAttributesProvider, PortAutoForwardAction, ProviderResult } from 'vscode'; -export const enum DefaultPythonDebugPorts { - Min = 55000, - Max = 56000, -} - export class DebugPortAttributesProvider implements PortAttributesProvider { - /** Cache of used ports (#1092) */ - private cachedResolutions: string[] = new Array(16).fill(''); - - /** Index counter for the next cached resolution index in the list */ - private cachedResolutionIndex = 0; - public providePortAttributes( - port: number, - pid: number | undefined, - commandLine: string | undefined, - token: CancellationToken - ): ProviderResult { - if (port >= DefaultPythonDebugPorts.Min && port <= DefaultPythonDebugPorts.Max) { - return new PortAttributes(PortAutoForwardAction.OpenBrowser); - } else { - return undefined; - } + _attributes: { port: number; pid?: number; commandLine?: string }, + _token: CancellationToken): ProviderResult + { + return new PortAttributes(PortAutoForwardAction.Ignore); } } diff --git a/src/extension/extensionInit.ts b/src/extension/extensionInit.ts index 46f7a9a1..864ac1ec 100644 --- a/src/extension/extensionInit.ts +++ b/src/extension/extensionInit.ts @@ -31,8 +31,7 @@ import { JsonLanguages, LaunchJsonCompletionProvider } from './debugger/configur import { LaunchJsonUpdaterServiceHelper } from './debugger/configuration/launch.json/updaterServiceHelper'; import { ignoreErrors } from './common/promiseUtils'; import { pickArgsInput } from './common/utils/localize'; -import { DebugPortAttributesProvider, DefaultPythonDebugPorts } from './debugger/debugPort/portAttributesProvider'; -import { createServer, IncomingMessage, ServerResponse } from 'http'; +import { DebugPortAttributesProvider } from './debugger/debugPort/portAttributesProvider'; export async function registerDebugger(context: IExtensionContext): Promise { const childProcessAttachService = new ChildProcessAttachService(); @@ -131,19 +130,8 @@ export async function registerDebugger(context: IExtensionContext): Promise { - showInformationMessage('Hello World!'); - - const server = createServer((request: IncomingMessage, response: ServerResponse) => { - response.end('Hello world!'); - }); - - server.listen(55100); - }) - ); } From 49c36f6252dca1b9853452e82687a4bbc351bd2f Mon Sep 17 00:00:00 2001 From: Paula Camargo Date: Tue, 12 Sep 2023 16:38:29 -0700 Subject: [PATCH 5/8] Fix format --- .../debugger/debugPort/portAttributesProvider.ts | 12 +++++++++--- src/extension/extensionInit.ts | 9 ++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/extension/debugger/debugPort/portAttributesProvider.ts b/src/extension/debugger/debugPort/portAttributesProvider.ts index e733c1bc..bbbad368 100644 --- a/src/extension/debugger/debugPort/portAttributesProvider.ts +++ b/src/extension/debugger/debugPort/portAttributesProvider.ts @@ -2,13 +2,19 @@ * Copyright (C) Microsoft Corporation. All rights reserved. *--------------------------------------------------------*/ -import { CancellationToken, PortAttributes, PortAttributesProvider, PortAutoForwardAction, ProviderResult } from 'vscode'; +import { + CancellationToken, + PortAttributes, + PortAttributesProvider, + PortAutoForwardAction, + ProviderResult, +} from 'vscode'; export class DebugPortAttributesProvider implements PortAttributesProvider { public providePortAttributes( _attributes: { port: number; pid?: number; commandLine?: string }, - _token: CancellationToken): ProviderResult - { + _token: CancellationToken, + ): ProviderResult { return new PortAttributes(PortAutoForwardAction.Ignore); } } diff --git a/src/extension/extensionInit.ts b/src/extension/extensionInit.ts index 864ac1ec..fa7cfa10 100644 --- a/src/extension/extensionInit.ts +++ b/src/extension/extensionInit.ts @@ -4,7 +4,7 @@ 'use strict'; import { debug, DebugConfigurationProviderTriggerKind, languages, Uri, window, workspace } from 'vscode'; -import { executeCommand, getConfiguration, registerCommand, showInformationMessage, startDebugging } from './common/vscodeapi'; +import { executeCommand, getConfiguration, registerCommand, startDebugging } from './common/vscodeapi'; import { DebuggerTypeName } from './constants'; import { DynamicPythonDebugConfigurationService } from './debugger/configuration/dynamicdebugConfigurationService'; import { IExtensionContext } from './common/types'; @@ -128,10 +128,5 @@ export async function registerDebugger(context: IExtensionContext): Promise Date: Thu, 14 Sep 2023 16:39:25 -0700 Subject: [PATCH 6/8] fix pr comments --- .../debugger/debugPort/portAttributesProvider.ts | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/extension/debugger/debugPort/portAttributesProvider.ts b/src/extension/debugger/debugPort/portAttributesProvider.ts index bbbad368..13fea1b5 100644 --- a/src/extension/debugger/debugPort/portAttributesProvider.ts +++ b/src/extension/debugger/debugPort/portAttributesProvider.ts @@ -2,19 +2,13 @@ * Copyright (C) Microsoft Corporation. All rights reserved. *--------------------------------------------------------*/ -import { - CancellationToken, - PortAttributes, - PortAttributesProvider, - PortAutoForwardAction, - ProviderResult, -} from 'vscode'; +import { CancellationToken, PortAttributes, PortAttributesProvider, ProviderResult } from 'vscode'; export class DebugPortAttributesProvider implements PortAttributesProvider { public providePortAttributes( _attributes: { port: number; pid?: number; commandLine?: string }, _token: CancellationToken, ): ProviderResult { - return new PortAttributes(PortAutoForwardAction.Ignore); + return undefined; } } From 9e25e3e20b7ae7a4490edbd112e678e350a58b40 Mon Sep 17 00:00:00 2001 From: Paula Camargo Date: Fri, 15 Sep 2023 15:07:34 -0700 Subject: [PATCH 7/8] Add regex for terminal command --- src/extension/extensionInit.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/extension/extensionInit.ts b/src/extension/extensionInit.ts index fa7cfa10..f4075875 100644 --- a/src/extension/extensionInit.ts +++ b/src/extension/extensionInit.ts @@ -128,5 +128,10 @@ export async function registerDebugger(context: IExtensionContext): Promise Date: Fri, 15 Sep 2023 15:23:32 -0700 Subject: [PATCH 8/8] Add adapter to pattern --- src/extension/extensionInit.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extension/extensionInit.ts b/src/extension/extensionInit.ts index f4075875..35dd76b0 100644 --- a/src/extension/extensionInit.ts +++ b/src/extension/extensionInit.ts @@ -130,7 +130,7 @@ export async function registerDebugger(context: IExtensionContext): Promise