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

feat: add doctor visibility provider & extract doctor #930

Merged
merged 2 commits into from
Mar 31, 2022
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@
},
"dependencies": {
"ansicolor": "^1.1.100",
"metals-languageclient": "0.5.12",
"metals-languageclient": "0.5.14",
"promisify-child-process": "4.1.1",
"vscode-languageclient": "7.0.0"
},
Expand Down
73 changes: 73 additions & 0 deletions src/doctor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { ClientCommands, ServerCommands } from "metals-languageclient";
import { ViewColumn, WebviewPanel, window } from "vscode";
import {
Disposable,
ExecuteCommandParams,
ExecuteCommandRequest,
LanguageClient,
NotificationType,
} from "vscode-languageclient/node";

export interface DoctorVisibilityDidChangeParams {
visible: boolean;
}

const doctorEndpoint = "metals/doctorVisibilityDidChange";
const doctorNotification =
new NotificationType<DoctorVisibilityDidChangeParams>(doctorEndpoint);

export class DoctorProvider implements Disposable {
doctor: WebviewPanel | undefined;
/**
* Although in protocol there is `visible`, currently in vscode, we are cheating a little bit.
* We treat doctor to be visible if webview is opened, no matter if it is focused or not.
* We should take focus into account when doctor caching mechanism will be implemented on the server.
*/
isOpened = false;

constructor(private client: LanguageClient) {}

dispose(): void {
this.doctor?.dispose();
}

private getDoctorPanel(isReload: boolean): WebviewPanel {
if (!this.doctor) {
this.doctor = window.createWebviewPanel(
"metals-doctor",
"Metals Doctor",
ViewColumn.Active,
{ enableCommandUris: true }
);
this.isOpened = true;

this.doctor.onDidDispose(() => {
this.client.sendNotification(doctorNotification, { visible: false });
this.isOpened = false;
this.doctor = undefined;
});
} else if (!isReload) {
this.doctor.reveal();
}
return this.doctor;
}

reloadOrRefreshDoctor(params: ExecuteCommandParams): void {
const isRun = params.command === ClientCommands.RunDoctor;
const isReload = params.command === ClientCommands.ReloadDoctor;
if (isRun || (this.doctor && isReload)) {
const html = params.arguments && params.arguments[0];
if (typeof html === "string") {
const panel = this.getDoctorPanel(isReload);
panel.webview.html = html;
}
}
}

async runDoctor(): Promise<void> {
await this.client.sendRequest(ExecuteCommandRequest.type, {
command: ServerCommands.DoctorRun,
});
this.isOpened = true;
}
}
41 changes: 10 additions & 31 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ import { BuildTargetUpdate } from "./testExplorer/types";
import * as workbenchCommands from "./workbenchCommands";
import { getServerVersion } from "./getServerVersion";
import { getCoursierMirrorPath } from "./mirrors";
import { DoctorProvider } from "./doctor";

const outputChannel = window.createOutputChannel("Metals");
const openSettingsAction = "Open settings";
Expand Down Expand Up @@ -237,6 +238,7 @@ function fetchAndLaunchMetals(

outputChannel.appendLine(`Metals version: ${serverVersion}`);

/* eslint-disable @typescript-eslint/no-non-null-assertion */
const serverProperties = config.get<string[]>("serverProperties")!;
const customRepositories = config.get<string[]>("customRepositories")!;
/* eslint-enable @typescript-eslint/no-non-null-assertion */
Expand Down Expand Up @@ -359,6 +361,7 @@ function launchMetals(
treeViewProvider: true,
testExplorerProvider: true,
commandInHtmlFormat: "vscode",
doctorVisibilityProvider: true,
};

const clientOptions: LanguageClientOptions = {
Expand Down Expand Up @@ -512,27 +515,9 @@ function launchMetals(

return client.onReady().then(
() => {
let doctor: WebviewPanel | undefined;
const doctorProvider = new DoctorProvider(client);
let stacktrace: WebviewPanel | undefined;

function getDoctorPanel(isReload: boolean): WebviewPanel {
if (!doctor) {
doctor = window.createWebviewPanel(
"metals-doctor",
"Metals Doctor",
ViewColumn.Active,
{ enableCommandUris: true }
);
context.subscriptions.push(doctor);
doctor.onDidDispose(() => {
doctor = undefined;
});
} else if (!isReload) {
doctor.reveal();
}
return doctor;
}

function getStacktracePanel(): WebviewPanel {
if (!stacktrace) {
stacktrace = window.createWebviewPanel(
Expand All @@ -558,7 +543,6 @@ function launchMetals(
ServerCommands.GenerateBspConfig,
ServerCommands.BspSwitch,
ServerCommands.SourcesScan,
ServerCommands.DoctorRun,
ServerCommands.CascadeCompile,
ServerCommands.CleanCompile,
ServerCommands.CancelCompilation,
Expand All @@ -570,6 +554,10 @@ function launchMetals(
);
});

registerCommand(`metals.${ServerCommands.DoctorRun}`, async () => {
await doctorProvider.runDoctor();
});

function displayBuildTarget(target: string): void {
const workspaceRoots = workspace.workspaceFolders;
if (workspaceRoots && workspaceRoots.length > 0) {
Expand Down Expand Up @@ -718,18 +706,9 @@ function launchMetals(
break;
}
case ClientCommands.RunDoctor:
case ClientCommands.ReloadDoctor: {
const isRun = params.command === ClientCommands.RunDoctor;
const isReload = params.command === ClientCommands.ReloadDoctor;
if (isRun || (doctor && isReload)) {
const html = params.arguments && params.arguments[0];
if (typeof html === "string") {
const panel = getDoctorPanel(isReload);
panel.webview.html = html;
}
}
case ClientCommands.ReloadDoctor:
doctorProvider.reloadOrRefreshDoctor(params);
break;
}
case ClientCommands.FocusDiagnostics:
commands.executeCommand(ClientCommands.FocusDiagnostics);
break;
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1407,10 +1407,10 @@ merge2@^1.3.0, merge2@^1.4.1:
resolved "https://registry.yarnpkg.com/merge2/-/merge2-1.4.1.tgz#4368892f885e907455a6fd7dc55c0c9d404990ae"
integrity sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==

metals-languageclient@0.5.12:
version "0.5.12"
resolved "https://registry.yarnpkg.com/metals-languageclient/-/metals-languageclient-0.5.12.tgz#60060d5fd52660c5b9ee77100b79188d8120d663"
integrity sha512-FnUXgES0/GQUskqGqA5bv9Mci6q7WAfKdyA7KjATzMg+SvfN5xnxfvBZqjTWr5cvIViWHZ/uOoKXAxc+PrGhlA==
metals-languageclient@0.5.14:
version "0.5.14"
resolved "https://registry.yarnpkg.com/metals-languageclient/-/metals-languageclient-0.5.14.tgz#3f9b27885d5baacfb1f8419d30e3fa1269d4c765"
integrity sha512-e0GJwSc3OFzWlCHUXnHfkLb54vNGOTuGQdmfLIIi0T8MsJA6KEpzJrxBMi/Pmw+I4wvHCqng0Ouzh8+64REdPQ==
dependencies:
"@viperproject/locate-java-home" "^1.1.5"
fp-ts "^2.4.1"
Expand Down