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 1 commit
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
68 changes: 68 additions & 0 deletions src/doctor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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;
isVisible = 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.isVisible = true;

this.doctor.onDidDispose(() => {
this.client.sendNotification(doctorNotification, { visible: false });
this.isVisible = false;
this.doctor = undefined;
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also an option to change visibility if this tab isn't focused.

      this.doctor.onDidChangeViewState(() => {
        this.doctor.visible
      })

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onDidChangeViewState fires when webview isn't focused or stops being visible?

I assumed that if the webview is open then we should always treat it as "visible" - user may come to it anytime and we should display them actual status.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed that if the webview is open then we should always treat it as "visible" - user may come to it anytime and we should display them actual status.

Haha. Actually, this visible word usage attracted my attention to that and I decided to check if it really about visibility or not.

Won't it be equal to the just opening a doctor page?
I think if we introduce some kind of cache it won't take a lot of time to refresh it after focusing on it back.

} 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.isVisible = 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