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

Provide feedback on malformed JSON of argv.json #151659

Merged
merged 6 commits into from Jun 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -5,7 +5,7 @@

import { language } from 'vs/base/common/platform';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { INotificationService, Severity } from 'vs/platform/notification/common/notification';
import { IJSONEditingService } from 'vs/workbench/services/configuration/common/jsonEditing';
import { ILocaleService } from 'vs/workbench/contrib/localization/common/locale';
import { ILanguagePackItem, ILanguagePackService } from 'vs/platform/languagePacks/common/languagePacks';
Expand All @@ -15,6 +15,10 @@ import { ViewContainerLocation } from 'vs/workbench/common/views';
import { IExtensionManagementService } from 'vs/platform/extensionManagement/common/extensionManagement';
import { IProgressService, ProgressLocation } from 'vs/platform/progress/common/progress';
import { localize } from 'vs/nls';
import { toAction } from 'vs/base/common/actions';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { stripComments } from 'vs/base/common/stripComments';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';

export class NativeLocaleService implements ILocaleService {
_serviceBrand: undefined;
Expand All @@ -26,11 +30,43 @@ export class NativeLocaleService implements ILocaleService {
@ILanguagePackService private readonly languagePackService: ILanguagePackService,
@IPaneCompositePartService private readonly paneCompositePartService: IPaneCompositePartService,
@IExtensionManagementService private readonly extensionManagementService: IExtensionManagementService,
@IProgressService private readonly progressService: IProgressService
@IProgressService private readonly progressService: IProgressService,
@ITextFileService private readonly textFileService: ITextFileService,
@IEditorService private readonly editorService: IEditorService
) { }

private async writeLocaleValue(locale: string | undefined): Promise<void> {
private async validateLocaleFile(): Promise<boolean> {
try {
const content = await this.textFileService.read(this.environmentService.argvResource, { encoding: 'utf8' });
Copy link
Member

Choose a reason for hiding this comment

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

This is more 💄 , but reading this method writeLocalValue I might be confused at first as to why we read the file. How about moving this new block of code into a private method validateLocaleFile (or similar) so that the logic becomes clearer as to why we read the file before writing?

Copy link
Member Author

Choose a reason for hiding this comment

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

addressed this.


// This is the same logic that we do where argv.json is parsed so mirror that:
// https://github.com/microsoft/vscode/blob/32d40cf44e893e87ac33ac4f08de1e5f7fe077fc/src/main.js#L238-L246
JSON.parse(stripComments(content.value));
} catch (error) {
this.notificationService.notify({
severity: Severity.Error,
message: localize('argvInvalid', 'Unable to write display language. Please open the runtime settings, correct errors/warnings in it and try again.'),
actions: {
primary: [
toAction({
id: 'openArgv',
label: localize('openArgv', "Open Runtime Settings"),
run: () => this.editorService.openEditor({ resource: this.environmentService.argvResource })
})
]
}
});
return false;
}
return true;
}

private async writeLocaleValue(locale: string | undefined): Promise<boolean> {
if (!(await this.validateLocaleFile())) {
return false;
}
await this.jsonEditingService.write(this.environmentService.argvResource, [{ path: ['locale'], value: locale }], true);
return true;
}

async setLocale(languagePackItem: ILanguagePackItem): Promise<boolean> {
Expand Down Expand Up @@ -66,8 +102,7 @@ export class NativeLocaleService implements ILocaleService {
);
}

await this.writeLocaleValue(locale);
return true;
return await this.writeLocaleValue(locale);
} catch (err) {
this.notificationService.error(err);
return false;
Expand All @@ -79,8 +114,7 @@ export class NativeLocaleService implements ILocaleService {
return false;
}
try {
await this.writeLocaleValue(undefined);
return true;
return await this.writeLocaleValue(undefined);
} catch (err) {
this.notificationService.error(err);
return false;
Expand Down