[go: nahoru, domu]

Skip to content

Commit

Permalink
Added commands to share and acquire ports on hosts and guests
Browse files Browse the repository at this point in the history
  • Loading branch information
quoc-ho committed Jul 2, 2024
1 parent 9367c9d commit 7451b05
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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%",
Expand Down
20 changes: 20 additions & 0 deletions src/core/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 12 additions & 5 deletions src/liveshare/liveshare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -49,22 +49,29 @@ export class LiveShare {
await this.getHostServerPort()
}

public async getHostServerPort(): Promise<number> {
if (this.hostServerPort !== undefined && this.hostServerPort !== null) {
public async getHostServerPort(reset: boolean = false): Promise<number> {
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()
}
}
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down

0 comments on commit 7451b05

Please sign in to comment.