diff --git a/package.json b/package.json index 2c04f1c19..6f93bde2a 100644 --- a/package.json +++ b/package.json @@ -285,6 +285,16 @@ } ], "commands": [ + { + "command": "latex-workshop.acquireHostPort", + "title": "Acquire Live Share host port", + "category": "LaTeX Workshop" + }, + { + "command": "latex-workshop.shareHostPort", + "title": "Share Live Share host port", + "category": "LaTeX Workshop" + }, { "command": "latex-workshop.navigate-envpair", "title": "%command.navigate-envpair%", diff --git a/src/core/commands.ts b/src/core/commands.ts index a835e3d95..42b8a7eda 100644 --- a/src/core/commands.ts +++ b/src/core/commands.ts @@ -5,6 +5,26 @@ import { getSurroundingMacroRange, stripText } from '../utils/utils' const logger = lw.log('Commander') +export async function acquireHostPort() { + logger.log('ACQUIREHOSTPORT command invoked.') + if (lw.liveshare.isGuest) { + await lw.liveshare.getHostServerPort(true) + } + else { + void vscode.window.showInformationMessage('This command only works for guests in a Live Share session.') + } +} + +export async function shareHostPort() { + logger.log('SHAREHOSTPORT command invoked.') + if (lw.liveshare.isHost) { + await lw.liveshare.shareServer() + } + else { + void vscode.window.showInformationMessage('This command only works for a host in a Live Share session.') + } +} + export async function build(skipSelection: boolean = false, rootFile: string | undefined = undefined, languageId: string | undefined = undefined, recipe: string | undefined = undefined) { logger.log('BUILD command invoked.') await lw.compile.build(skipSelection, rootFile, languageId, recipe) diff --git a/src/liveshare/liveshare.ts b/src/liveshare/liveshare.ts index 2ab7efd19..496f2673f 100644 --- a/src/liveshare/liveshare.ts +++ b/src/liveshare/liveshare.ts @@ -5,8 +5,8 @@ import * as url from 'url' export class LiveShare { private role: vsls.Role = vsls.Role.None - private hostServerPort: number | undefined | null + private shareServerDisposable: vscode.Disposable | undefined liveshare: vsls.LiveShare | undefined | null @@ -49,22 +49,29 @@ export class LiveShare { await this.getHostServerPort() } - public async getHostServerPort(): Promise { - if (this.hostServerPort !== undefined && this.hostServerPort !== null) { + public async getHostServerPort(reset: boolean = false): Promise { + if (!reset && this.hostServerPort !== undefined && this.hostServerPort !== null) { return this.hostServerPort } else { const savedClipboard = await vscode.env.clipboard.readText() await vscode.commands.executeCommand('liveshare.listServers') const hostUrl = await vscode.env.clipboard.readText() - await vscode.env.clipboard.writeText(savedClipboard) const hostServerPort = Number(url.parse(hostUrl).port) this.hostServerPort = hostServerPort + await vscode.env.clipboard.writeText(savedClipboard) return hostServerPort } } + public async shareServer() { + if (this.shareServerDisposable !== null) { + this.shareServerDisposable?.dispose() + } + this.shareServerDisposable = await this.liveshare?.shareServer({ port: lw.server.getPort(), displayName: 'latex-workshop-server' }) + } + private async initHost() { - await this.liveshare?.shareServer({ port: lw.server.getPort(), displayName: 'latex-workshop-server' }) + await this.shareServer() } } diff --git a/src/main.ts b/src/main.ts index 6e34d2f0c..65c869a24 100644 --- a/src/main.ts +++ b/src/main.ts @@ -156,6 +156,8 @@ export function activate(extensionContext: vscode.ExtensionContext) { function registerLatexWorkshopCommands(extensionContext: vscode.ExtensionContext) { extensionContext.subscriptions.push( + vscode.commands.registerCommand('latex-workshop.acquireHostPort', () => lw.commands.acquireHostPort()), + vscode.commands.registerCommand('latex-workshop.shareHostPort', () => lw.commands.shareHostPort()), vscode.commands.registerCommand('latex-workshop.saveWithoutBuilding', () => lw.commands.saveActive()), vscode.commands.registerCommand('latex-workshop.build', () => lw.commands.build()), vscode.commands.registerCommand('latex-workshop.recipes', (recipe: string | undefined) => lw.commands.recipes(recipe)),