Repository navigation
update vscode extension to support hotreload #993
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -110,3 +110,38 @@ export class DynamicDebugConfigProvider | |
| return results; | ||
| } | ||
| } | ||
|
|
||
| export class JasprDebugAdapterTrackerFactory | ||
| implements vscode.DebugAdapterTrackerFactory | ||
| { | ||
| createDebugAdapterTracker( | ||
| session: vscode.DebugSession | ||
| ): vscode.ProviderResult<vscode.DebugAdapterTracker> { | ||
| const processId = session.configuration._processId; | ||
| if (!processId) { | ||
| return undefined; | ||
| } | ||
| const daemon = JasprServeDaemon.getDaemonByProcessId(processId); | ||
| if (!daemon) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return { | ||
| onWillReceiveMessage(message: any) { | ||
| if (message?.type === "request") { | ||
| if (message.command === "hotReload") { | ||
| daemon.reload(); | ||
|
Comment on lines
+132
to
+133
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does seem accurate. |
||
| } else if ( | ||
| message.command === "restart" || | ||
| message.command === "hotRestart" || | ||
| ((message.command === "disconnect" || | ||
| message.command === "terminate") && | ||
| Boolean(message.arguments?.restart)) | ||
| ) { | ||
| daemon.restart(); | ||
| } | ||
| } | ||
| }, | ||
| }; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,40 @@ export class JasprServeDaemon implements vscode.Disposable { | |
| private randomId: number = 0; | ||
| private runName: string = ""; | ||
|
|
||
| private static _activeDaemons: Map<number, JasprServeDaemon> = new Map(); | ||
| public static activeDaemon: JasprServeDaemon | undefined; | ||
|
|
||
| public static getDaemonByProcessId( | ||
| processId: number | ||
| ): JasprServeDaemon | undefined { | ||
| return this._activeDaemons.get(processId); | ||
| } | ||
|
|
||
| public static getActiveDaemon(): JasprServeDaemon | undefined { | ||
| if (this.activeDaemon && this.activeDaemon.status === "running") { | ||
| return this.activeDaemon; | ||
| } | ||
| const activeSession = vscode.debug.activeDebugSession; | ||
| if (activeSession?.configuration?._processId) { | ||
| const daemon = this.getDaemonByProcessId( | ||
| activeSession.configuration._processId | ||
| ); | ||
| if (daemon && daemon.status === "running") { | ||
| return daemon; | ||
| } | ||
| } | ||
|
Comment on lines
+45
to
+56
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems valid. |
||
| for (const daemon of this._activeDaemons.values()) { | ||
| if (daemon.status === "running") { | ||
| return daemon; | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| private nextRequestId: number = 1; | ||
| private lastReloadTime: number = 0; | ||
| private lastRestartTime: number = 0; | ||
|
|
||
| async start( | ||
| context: vscode.ExtensionContext, | ||
| folder?: vscode.WorkspaceFolder, | ||
|
|
@@ -46,6 +80,10 @@ export class JasprServeDaemon implements vscode.Disposable { | |
| this.randomId = Math.floor(Math.random() * 1000000); | ||
| this.runName = debugConfiguration?.name ?? "Jaspr"; | ||
|
|
||
| JasprServeDaemon.activeDaemon = this; | ||
| JasprServeDaemon._activeDaemons.set(this.randomId, this); | ||
| void vscode.commands.executeCommand("setContext", "jaspr:isRunning", true); | ||
|
|
||
| this.statusBarItem = await vscode.window.createStatusBarItem( | ||
| vscode.StatusBarAlignment.Left, | ||
| 2 | ||
|
|
@@ -114,6 +152,15 @@ export class JasprServeDaemon implements vscode.Disposable { | |
| } | ||
| }) | ||
| ); | ||
|
|
||
| this._disposables.push( | ||
| vscode.debug.onDidTerminateDebugSession((session) => { | ||
| const index = this.sessions.indexOf(session); | ||
| if (index !== -1) { | ||
| this.sessions.splice(index, 1); | ||
| } | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| async _startTerminal(context: vscode.ExtensionContext) { | ||
|
|
@@ -137,6 +184,15 @@ export class JasprServeDaemon implements vscode.Disposable { | |
| if (data.length === 1 && data.charCodeAt(0) === 3) { | ||
| // Ctrl+C | ||
| this.stop(); | ||
| return; | ||
| } | ||
|
|
||
| if (data === "r") { | ||
| this.reload(); | ||
| } else if (data === "R") { | ||
| this.restart(); | ||
| } else if (data === "q") { | ||
| this.stop(); | ||
| } | ||
| }, | ||
| open: () => resolve(terminal), | ||
|
|
@@ -198,7 +254,41 @@ export class JasprServeDaemon implements vscode.Disposable { | |
| }); | ||
| } | ||
|
|
||
| public reload(): void { | ||
| const now = Date.now(); | ||
| if (now - this.lastReloadTime < 250) { | ||
| return; | ||
| } | ||
| this.lastReloadTime = now; | ||
| this.sendRequest("client.restart", { fullRestart: false }); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems valid. Would be unfortunate to drop support for earlier versions already though. Perhaps can use and check the |
||
| } | ||
|
|
||
| public restart(): void { | ||
| const now = Date.now(); | ||
| if (now - this.lastRestartTime < 250) { | ||
| return; | ||
| } | ||
| this.lastRestartTime = now; | ||
| this.sendRequest("client.restart", { fullRestart: true }); | ||
| } | ||
|
|
||
| public sendRequest(method: string, params?: any): void { | ||
| if (this.status !== "running" || !this.process) { | ||
| return; | ||
| } | ||
|
Comment on lines
+276
to
+278
|
||
| const id = `${++this.nextRequestId}`; | ||
| const request = [{ method, params, id }]; | ||
| this.process.stdin.write(JSON.stringify(request) + "\n"); | ||
| } | ||
|
|
||
| async stop(): Promise<void> { | ||
| if (JasprServeDaemon.activeDaemon === this) { | ||
| JasprServeDaemon.activeDaemon = undefined; | ||
| } | ||
| JasprServeDaemon._activeDaemons.delete(this.randomId); | ||
| if (JasprServeDaemon._activeDaemons.size === 0) { | ||
| void vscode.commands.executeCommand("setContext", "jaspr:isRunning", false); | ||
| } | ||
| this.process?.stdin.write('[{"method":"daemon.shutdown", "id": "0"}]\n'); | ||
| this.statusBarCloseItem?.hide(); | ||
| const sess = this.sessions; | ||
|
|
@@ -210,6 +300,13 @@ export class JasprServeDaemon implements vscode.Disposable { | |
| } | ||
|
|
||
| async dispose() { | ||
| if (JasprServeDaemon.activeDaemon === this) { | ||
| JasprServeDaemon.activeDaemon = undefined; | ||
| } | ||
| JasprServeDaemon._activeDaemons.delete(this.randomId); | ||
| if (JasprServeDaemon._activeDaemons.size === 0) { | ||
| void vscode.commands.executeCommand("setContext", "jaspr:isRunning", false); | ||
| } | ||
| this.stop(); | ||
|
|
||
| this.statusBarItem?.dispose(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should these conditions be
inputFocusinstead ofeditorTextFocus, so they don't trigger in other inputs like "Find"?