This commit is contained in:
andreamah 2023-05-30 09:37:28 -07:00
Родитель 9f3ae774a8
Коммит 69cc552933
5 изменённых файлов: 158 добавлений и 3 удалений

Просмотреть файл

@ -1,4 +1,5 @@
{
"enabledApiProposals": ["portsAttributes"],
"name": "live-server",
"displayName": "Live Preview",
"description": "Hosts a local server in your workspace for you to preview your webpages on.",

Просмотреть файл

@ -43,12 +43,13 @@ export class Connection extends Disposable {
new vscode.EventEmitter<string>()
);
public readonly onShouldResetInitHost = this._onShouldResetInitHost.event;
private readonly _portAttributes: serverPortAttributesProvider;
constructor(
private readonly _workspace: vscode.WorkspaceFolder | undefined,
private _rootPrefix: string,
public httpPort: number,
public wsPort: number,
private _httpPort: number,
private _wsPort: number,
public host: string
) {
super();
@ -61,8 +62,29 @@ export class Connection extends Disposable {
})
);
this._portAttributes = this._register(new serverPortAttributesProvider());
}
set httpPort(port: number) {
this._httpPort = port;
this._portAttributes.httpPort = port;
}
get httpPort(): number {
return this._httpPort;
}
set wsPort(port: number) {
this._wsPort = port;
this._portAttributes.wsPort = port;
}
get wsPort(): number {
return this._wsPort;
}
/**
* Called by the server manager to inform this object that a connection has been successful.
* @param httpPort HTTP server port number
@ -181,3 +203,30 @@ export class Connection extends Disposable {
: false;
}
}
export class serverPortAttributesProvider
extends Disposable
implements vscode.PortAttributesProvider
{
public wsPort = 0;
public httpPort = 0;
constructor(
) {
super();
vscode.workspace.registerPortAttributesProvider({}, this);
}
providePortAttributes(
port: number,
pid: number | undefined,
commandLine: string | undefined,
token: vscode.CancellationToken
): vscode.ProviderResult<vscode.PortAttributes> {
if (port == this.wsPort || port == this.httpPort) {
return new vscode.PortAttributes(
vscode.PortAutoForwardAction.Silent
);
}
return undefined;
}
}

Просмотреть файл

@ -35,7 +35,7 @@ export class HttpServer extends Disposable {
_extensionUri: vscode.Uri,
private readonly _reporter: TelemetryReporter,
private readonly _endpointManager: EndpointManager,
private readonly _connection: Connection
private readonly _connection: Connection,
) {
super();
this._contentLoader = this._register(
@ -337,3 +337,4 @@ export class HttpServer extends Disposable {
});
}
}

Просмотреть файл

@ -347,3 +347,4 @@ export class ServerGrouping extends Disposable {
this.closeServer();
}
}

103
src/typings/vscode.proposed.d.ts поставляемый Normal file
Просмотреть файл

@ -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<PortAttributes>;
}
/**
* 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;
}
}