Skip to content

Commit 564911c

Browse files
authored
fix: Add validation for profile (#651)
1 parent d674df0 commit 564911c

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

src/formatter-settings/index.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ export class JavaFormatterSettingsEditorProvider implements vscode.CustomTextEdi
4949
if (!this.settingsUrl || e.document.uri.toString() !== vscode.Uri.file(this.profilePath).toString()) {
5050
return;
5151
}
52-
await this.parseProfileAndUpdate(e.document);
52+
if (!await this.parseProfileAndUpdate(e.document)) {
53+
this.webviewPanel?.dispose();
54+
}
5355
});
5456
}
5557

@@ -168,23 +170,27 @@ export class JavaFormatterSettingsEditorProvider implements vscode.CustomTextEdi
168170
}
169171

170172
private async initialize(document: vscode.TextDocument): Promise<boolean> {
171-
if (!await this.checkRequirement()) {
172-
return false;
173-
}
174-
if (!await this.checkProfileSettings()) {
173+
if (!await this.checkRequirement() || !await this.checkProfileSettings() || !await this.parseProfileAndUpdate(document)) {
175174
return false;
176175
}
177176
this.exampleKind = ExampleKind.INDENTATION_EXAMPLE;
178-
await this.parseProfileAndUpdate(document);
179177
this.webviewPanel?.webview.postMessage({
180178
command: "changeReadOnlyState",
181179
value: this.readOnly,
182180
});
183181
return true;
184182
}
185183

186-
private async parseProfileAndUpdate(document: vscode.TextDocument): Promise<void> {
184+
private async parseProfileAndUpdate(document: vscode.TextDocument): Promise<boolean> {
187185
const content: ProfileContent = parseProfile(document);
186+
if (!content.isValid) {
187+
vscode.window.showErrorMessage("The current profile is invalid, please check it in the Settings and try again.", "Open Settings").then((anwser) => {
188+
if (anwser === "Open Settings") {
189+
openFormatterSettings();
190+
}
191+
})
192+
return false;
193+
}
188194
this.diagnosticCollection.set(document.uri, content.diagnostics);
189195
if (this.webviewPanel) {
190196
this.profileElements = content.profileElements || this.profileElements;
@@ -200,6 +206,7 @@ export class JavaFormatterSettingsEditorProvider implements vscode.CustomTextEdi
200206
await this.updateVSCodeSettings();
201207
this.format();
202208
}
209+
return true;
203210
}
204211

205212
private onChangeProfileSettings(): void {

src/formatter-settings/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export interface DOMAttr extends Attr {
5656
}
5757

5858
export interface ProfileContent {
59+
isValid: boolean,
5960
settingsVersion: string,
6061
diagnostics: vscode.Diagnostic[],
6162
profileElements?: Map<string, DOMElement>,

src/formatter-settings/utils.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export function parseProfile(document: vscode.TextDocument): ProfileContent {
7272
const profileSettings = new Map<string, string>();
7373
let lastElement = undefined;
7474
const diagnostics: vscode.Diagnostic[] = [];
75+
let settingsVersion = JavaConstants.CURRENT_FORMATTER_SETTINGS_VERSION;
7576
const documentDOM = new DOMParser({
7677
locator: {}, errorHandler: (_level, msg) => {
7778
const bracketExp: RegExp = new RegExp("\\[line:(\\d*),col:(\\d*)\\]", "g");
@@ -81,11 +82,13 @@ export function parseProfile(document: vscode.TextDocument): ProfileContent {
8182
}
8283
}
8384
}).parseFromString(document.getText());
84-
let settingsVersion = documentDOM.documentElement.getAttribute("version") || JavaConstants.CURRENT_FORMATTER_SETTINGS_VERSION;
85+
if (!documentDOM) {
86+
return { isValid: false, settingsVersion, diagnostics };
87+
}
88+
settingsVersion = documentDOM.documentElement.getAttribute("version") || settingsVersion;
8589
const profiles = documentDOM.documentElement.getElementsByTagName("profile");
8690
if (!profiles || profiles.length === 0) {
87-
diagnostics.push(new vscode.Diagnostic(new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0)), "No valid profiles found."));
88-
return { settingsVersion, diagnostics };
91+
return { isValid: false, settingsVersion, diagnostics };
8992
}
9093
const settingsProfileName: string | undefined = vscode.workspace.getConfiguration("java").get<string>(JavaConstants.SETTINGS_PROFILE_KEY);
9194
for (let i = 0; i < profiles.length; i++) {
@@ -119,8 +122,7 @@ export function parseProfile(document: vscode.TextDocument): ProfileContent {
119122
}
120123
}
121124
if (!profileElements.size) {
122-
diagnostics.push(new vscode.Diagnostic(new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0)), "No valid settings found in the profile."));
123-
return { settingsVersion, diagnostics };
125+
return { isValid: false, settingsVersion, diagnostics };
124126
}
125127
const supportedProfileSettings = getSupportedProfileSettings(Number(settingsVersion));
126128
for (const setting of supportedProfileSettings.values()) {
@@ -146,6 +148,7 @@ export function parseProfile(document: vscode.TextDocument): ProfileContent {
146148
setting.value = webViewValue;
147149
}
148150
return {
151+
isValid: true,
149152
settingsVersion,
150153
diagnostics,
151154
profileElements,

0 commit comments

Comments
 (0)