in src/formatter-settings/index.ts [72:148]
public async resolveCustomTextEditor(document: vscode.TextDocument, webviewPanel: vscode.WebviewPanel, _token: vscode.CancellationToken): Promise<void> {
// restrict one webviewpanel only
if (this.webviewPanel) {
vscode.commands.executeCommand("vscode.open", document.uri);
webviewPanel.dispose();
return;
} else {
this.webviewPanel = webviewPanel;
}
this.webviewPanel.webview.options = {
enableScripts: true,
enableCommandUris: true,
};
this.webviewPanel.onDidDispose(() => {
this.webviewPanel = undefined;
});
this.webviewPanel.webview.html = this.getHtmlForWebview(path.join(this.context.extensionPath, "out", "assets", "formatter-settings", "index.js"));
this.webviewPanel.webview.onDidReceiveMessage(async (e) => {
switch (e.command) {
case "onWillInitialize":
if (!await this.initialize(document)) {
this.webviewPanel?.dispose();
}
break;
case "onWillChangeExampleKind":
if (this.exampleKind !== e.exampleKind) {
sendInfo("", { formatterExample: e.exampleKind });
this.exampleKind = e.exampleKind;
this.format();
}
break;
case "onWillChangeSetting":
const settingValue: string | undefined = FormatterConverter.webView2ProfileConvert(e.id, e.value.toString());
sendInfo("", { formatterSetting: e.id });
// "" represents an empty inputbox, we regard it as a valid value.
if (settingValue === undefined) {
return;
}
if (SupportedSettings.indentationSettings.includes(e.id)) {
const config = vscode.workspace.getConfiguration(undefined, { languageId: "java" });
if (e.id === SupportedSettings.TABULATION_CHAR) {
const targetValue = (settingValue === "tab") ? false : true;
await config.update(VSCodeSettings.INSERT_SPACES, targetValue, undefined, true);
} else if (e.id === SupportedSettings.TABULATION_SIZE) {
await config.update(VSCodeSettings.TAB_SIZE, (settingValue === "") ? "" : Number(settingValue), undefined, true);
}
this.profileSettings.set(e.id, settingValue);
} else if (e.id === VSCodeSettings.DETECT_INDENTATION) {
const config = vscode.workspace.getConfiguration(undefined, { languageId: "java" });
await config.update(VSCodeSettings.DETECT_INDENTATION, (settingValue === "true"), undefined, true);
} else {
await this.modifyProfile(e.id, settingValue, document);
}
break;
case "onWillDownloadAndUse": {
const settingsUrl = vscode.workspace.getConfiguration("java").get<string>(JavaConstants.SETTINGS_URL_KEY);
if (!settingsUrl || !isRemote(settingsUrl)) {
vscode.window.showErrorMessage("The active formatter profile does not exist or is not remote, please check it in the Settings and try again.",
"Open Settings").then((result) => {
if (result === "Open Settings") {
openFormatterSettings();
}
});
return;
}
this.webviewPanel?.dispose();
await this.downloadAndUse(settingsUrl);
break;
}
default:
break;
}
});
await this.checkRequirement();
}