Skip to content
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

Add hot code reload #1419

Merged
merged 5 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/metals-vscode/icons/hot_code_replace.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions packages/metals-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,14 @@
}
},
"commands": [
{
"command": "metals.debug.hotCodeReplace",
"title": "Hot Code Replace",
"icon": {
"light": "icons/hot_code_replace.svg",
"dark": "icons/hot_code_replace.svg"
}
},
{
"command": "metals.reveal-active-file",
"category": "Metals",
Expand Down Expand Up @@ -695,6 +703,13 @@
"when": "view == metalsPackages"
}
],
"debug/toolBar": [
{
"command": "metals.debug.hotCodeReplace",
"group": "navigation@100",
"when": "scalaHotReloadOn"
}
],
"commandPalette": [
{
"command": "metals.show-tasty",
Expand Down
74 changes: 74 additions & 0 deletions packages/metals-vscode/src/debugger/hotCodeReplace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

// Adapted from https://github.com/microsoft/vscode-java-debug/blob/main/src/hotCodeReplace.ts

import * as vscode from "vscode";

import { DebugSession, commands } from "vscode";

const HCR_ACTIVE = "scalaHotReloadOn";

export function initializeHotCodeReplace() {
vscode.debug.onDidStartDebugSession((session) => {
if (session?.configuration.noDebug && !vscode.debug.activeDebugSession) {
vscode.commands.executeCommand("setContext", HCR_ACTIVE, false);
}
});
vscode.debug.onDidChangeActiveDebugSession((session) => {
vscode.commands.executeCommand(
"setContext",
HCR_ACTIVE,
session && !session.configuration.noDebug
);
});
}

export async function applyHCR() {
const debugSession: DebugSession | undefined =
vscode.debug.activeDebugSession;
if (!debugSession) {
return;
}

if (debugSession.configuration.noDebug) {
vscode.window
.showWarningMessage(
"Failed to apply the changes because hot code replace is not supported by run mode, " +
"would you like to restart the program?"
)
.then((res) => {
if (res === "Yes") {
vscode.commands.executeCommand("workbench.action.debug.restart");
}
});

return;
}

await commands.executeCommand("workbench.action.files.save");
const redefineRequest = debugSession.customRequest("redefineClasses");
vscode.window.setStatusBarMessage(
"$(sync~spin) Applying code changes...",
redefineRequest
);
const response = await redefineRequest;

if (response?.errorMessage) {
vscode.window.showErrorMessage(response.errorMessage);
return;
}

if (!response?.changedClasses?.length) {
vscode.window.showWarningMessage(
"No classes were reloaded, please check the logs"
);
return;
}

const changed = response.changedClasses.length;
vscode.window.setStatusBarMessage(
`$(check) ${changed} Class${changed > 1 ? "es" : ""} reloaded`,
5 * 1000
);
}
5 changes: 5 additions & 0 deletions packages/metals-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ import {
SCALA_LANGID,
} from "./consts";
import { ScalaCodeLensesParams } from "./debugger/types";
import { applyHCR, initializeHotCodeReplace } from "./debugger/hotCodeReplace";

const outputChannel = window.createOutputChannel("Metals");
const downloadJava = "Download Java";
Expand Down Expand Up @@ -1188,6 +1189,10 @@ function launchMetals(
}
);
context.subscriptions.push(decorationsRangesDidChangeDispoasable);
registerCommand("metals.debug.hotCodeReplace", () => {
applyHCR();
});
initializeHotCodeReplace();
},
(reason) => {
if (reason instanceof Error) {
Expand Down
Loading