Skip to content

Commit

Permalink
Merge pull request #1419 from iusildra/add-hot-code-reload
Browse files Browse the repository at this point in the history
Add hot code reload
  • Loading branch information
tgodzik authored Feb 22, 2024
2 parents 7ce76d6 + 3630fa0 commit 4dbfd24
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
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

0 comments on commit 4dbfd24

Please sign in to comment.