Skip to content

Commit d674df0

Browse files
authored
fix: If a remote profile failed to open, the user cannot open it again (#650)
1 parent 1d9d873 commit d674df0

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

src/formatter-settings/RemoteProfileProvider.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,21 @@ import { downloadFile } from "./utils";
66

77
export class RemoteProfileProvider implements vscode.TextDocumentContentProvider {
88

9-
public static scheme = "formatter";
9+
public static scheme = "java-formatter";
10+
private contentStorage: Map<string, string> = new Map<string, string>();
11+
12+
public setContent(url: string, content: string): void {
13+
this.contentStorage.set(url, content);
14+
}
1015

1116
async provideTextDocumentContent(uri: vscode.Uri): Promise<string> {
1217
const originalUri: vscode.Uri = uri.with({ scheme: "https" });
13-
return await downloadFile(originalUri.toString());
18+
return this.contentStorage.get(originalUri.toString()) || downloadFile(originalUri.toString());
1419
}
1520
}
1621

22+
export let remoteProfileProvider = new RemoteProfileProvider();
23+
1724
export function initRemoteProfileProvider(context: vscode.ExtensionContext) {
18-
const remoteProfileProvider = new RemoteProfileProvider();
1925
context.subscriptions.push(vscode.workspace.registerTextDocumentContentProvider(RemoteProfileProvider.scheme, remoteProfileProvider));
2026
}

src/formatter-settings/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { XMLSerializer } from "xmldom";
1010
import { loadTextFromFile } from "../utils";
1111
import { Example, getSupportedVSCodeSettings, JavaConstants, SupportedSettings, VSCodeSettings } from "./FormatterConstants";
1212
import { FormatterConverter } from "./FormatterConverter";
13-
import { RemoteProfileProvider } from "./RemoteProfileProvider";
13+
import { remoteProfileProvider, RemoteProfileProvider } from "./RemoteProfileProvider";
1414
import { DOMElement, ExampleKind, ProfileContent } from "./types";
1515
import { addDefaultProfile, downloadFile, getProfilePath, getAbsoluteTargetPath, getVSCodeSetting, isRemote, openFormatterSettings, parseProfile } from "./utils";
1616
export class JavaFormatterSettingsEditorProvider implements vscode.CustomTextEditorProvider {
@@ -272,13 +272,17 @@ export class JavaFormatterSettingsEditorProvider implements vscode.CustomTextEdi
272272
}
273273
}
274274

275-
private async downloadAndUse(settingsUrl: string): Promise<void> {
275+
private async downloadAndUse(settingsUrl: string): Promise<boolean> {
276276
const profilePath = await getAbsoluteTargetPath(this.context, path.basename(settingsUrl));
277277
const data = await downloadFile(settingsUrl);
278+
if (!data) {
279+
return false;
280+
}
278281
await fse.outputFile(profilePath, data);
279282
const workspaceFolders = vscode.workspace.workspaceFolders;
280283
await vscode.workspace.getConfiguration("java").update("format.settings.url", (workspaceFolders?.length ? vscode.workspace.asRelativePath(profilePath) : profilePath), !(workspaceFolders?.length));
281284
this.showFormatterSettingsEditor();
285+
return true;
282286
}
283287

284288
private checkProfileSettings = instrumentOperation("formatter.checkProfileSetting", async (operationId: string) => {
@@ -299,6 +303,11 @@ export class JavaFormatterSettingsEditorProvider implements vscode.CustomTextEdi
299303
this.checkedProfileSettings = await vscode.window.showInformationMessage("The active formatter profile is remote, do you want to open it in read-only mode or download and use it locally?",
300304
"Open in read-only mode", "Download and use it locally").then(async (result) => {
301305
if (result === "Open in read-only mode") {
306+
const content = await downloadFile(this.settingsUrl!);
307+
if (!content) {
308+
return false;
309+
}
310+
remoteProfileProvider.setContent(this.settingsUrl!, content);
302311
this.readOnly = true;
303312
return true;
304313
} else if (result === "Download and use it locally") {

src/formatter-settings/utils.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,17 @@ export function parseProfile(document: vscode.TextDocument): ProfileContent {
156156
}
157157

158158
export async function downloadFile(settingsUrl: string): Promise<string> {
159-
return (await axios.get(settingsUrl)).data;
159+
try {
160+
return (await axios.get(settingsUrl)).data;
161+
} catch (e) {
162+
const answer = await vscode.window.showErrorMessage(`Failed to get the profile content from uri: ${settingsUrl}. Do you want to retry?`, "Retry", "Open Settings");
163+
if (answer === "Retry") {
164+
return downloadFile(settingsUrl);
165+
} else if (answer === "Open Settings") {
166+
openFormatterSettings();
167+
}
168+
return "";
169+
}
160170
}
161171

162172
export function openFormatterSettings(): void {

0 commit comments

Comments
 (0)