Welcome to mirror list, hosted at ThFree Co, Russian Federation.

settingsResource.ts « common « userDataProfile « services « workbench « vs « src - github.com/microsoft/vscode.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 88b1b25733ed2dadd170547dea16813f5add2db1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { VSBuffer } from 'vs/base/common/buffer';
import { ConfigurationScope, Extensions, IConfigurationRegistry } from 'vs/platform/configuration/common/configurationRegistry';
import { IFileService } from 'vs/platform/files/common/files';
import { ILogService } from 'vs/platform/log/common/log';
import { Registry } from 'vs/platform/registry/common/platform';
import { IUserDataProfileService, IProfileResource, ProfileCreationOptions } from 'vs/workbench/services/userDataProfile/common/userDataProfile';
import { removeComments, updateIgnoredSettings } from 'vs/platform/userDataSync/common/settingsMerge';
import { IUserDataSyncUtilService } from 'vs/platform/userDataSync/common/userDataSync';

interface ISettingsContent {
	settings: string;
}

export class SettingsResource implements IProfileResource {

	constructor(
		@IFileService private readonly fileService: IFileService,
		@IUserDataProfileService private readonly userDataProfileService: IUserDataProfileService,
		@IUserDataSyncUtilService private readonly userDataSyncUtilService: IUserDataSyncUtilService,
		@ILogService private readonly logService: ILogService,
	) {
	}

	async getContent(options?: ProfileCreationOptions): Promise<string> {
		const ignoredSettings = this.getIgnoredSettings();
		const formattingOptions = await this.userDataSyncUtilService.resolveFormattingOptions(this.userDataProfileService.currentProfile.settingsResource);
		const localContent = await this.getLocalFileContent();
		let settings = updateIgnoredSettings(localContent || '{}', '{}', ignoredSettings, formattingOptions);
		if (options?.skipComments) {
			settings = removeComments(settings, formattingOptions);
		}
		const settingsContent: ISettingsContent = { settings };
		return JSON.stringify(settingsContent);
	}

	async apply(content: string): Promise<void> {
		const settingsContent: ISettingsContent = JSON.parse(content);
		this.logService.trace(`Profile: Applying settings...`);
		const localSettingsContent = await this.getLocalFileContent();
		const formattingOptions = await this.userDataSyncUtilService.resolveFormattingOptions(this.userDataProfileService.currentProfile.settingsResource);
		const contentToUpdate = updateIgnoredSettings(settingsContent.settings, localSettingsContent || '{}', this.getIgnoredSettings(), formattingOptions);
		await this.fileService.writeFile(this.userDataProfileService.currentProfile.settingsResource, VSBuffer.fromString(contentToUpdate));
		this.logService.info(`Profile: Applied settings`);
	}

	private getIgnoredSettings(): string[] {
		const allSettings = Registry.as<IConfigurationRegistry>(Extensions.Configuration).getConfigurationProperties();
		const ignoredSettings = Object.keys(allSettings).filter(key => allSettings[key]?.scope === ConfigurationScope.MACHINE || allSettings[key]?.scope === ConfigurationScope.MACHINE_OVERRIDABLE);
		return ignoredSettings;
	}

	private async getLocalFileContent(): Promise<string | null> {
		try {
			const content = await this.fileService.readFile(this.userDataProfileService.currentProfile.settingsResource);
			return content.value.toString();
		} catch (error) {
			return null;
		}
	}

}