default settings are stored in json file

This commit is contained in:
Till Salinger 2018-04-21 19:23:31 +02:00
Родитель d3d7abccf9
Коммит 0b500d3350
7 изменённых файлов: 57 добавлений и 31 удалений

18
settings/defaults.json Normal file
Просмотреть файл

@ -0,0 +1,18 @@
[
[
"editor.multiCursorModifier",
"ctrlCmd"
],
[
"editor.snippetSuggestions",
"top"
],
[
"editor.formatOnPaste",
true
],
[
"workbench.colorTheme",
"Monokai"
]
]

Просмотреть файл

Просмотреть файл

@ -110,7 +110,9 @@ function setting2QuickPickItem(setting: VscodeSetting, sublimeName?: string): IS
detail: setting.overwritesValue
? `${icons.exclamationPoint} Overwrites existing value: '${setting.oldValue}' with '${setting.value}'`
: '',
label: sublimeName ? `${sublimeName} ${icons.arrowRight} ${setting.name}` : `{Sublime Default} ${icons.arrowRight} ${setting.name}: ${setting.value}`,
label: sublimeName
? `${sublimeName} ${icons.arrowRight} ${setting.name}`
: `{Sublime Default} ${icons.arrowRight} ${setting.name}: ${setting.value}`,
picked: !setting.overwritesValue,
setting,
};

Просмотреть файл

@ -11,34 +11,37 @@ interface IConfigCheck {
readonly existingValue: string;
}
interface IMapperSettings {
mappings: { [key: string]: any };
defaults: VscodeSetting[];
}
export class Mapper {
private settingsMappings: Promise<{}> | undefined = undefined;
constructor(settingsMappings?: Promise<{}>, private mockConfig?: any) {
this.settingsMappings = settingsMappings;
}
constructor(private settings?: IMapperSettings, private mockConfig?: any) { }
public async getMappedSettings(sublimeSettings: string): Promise<CategorizedSettings> {
const settingsMappings = await this.getSettingsMappings();
const settingsMappings = await this.getSettings();
return this.mapAllSettings(settingsMappings, rjson.parse(sublimeSettings));
}
private async getSettingsMappings(): Promise<{}> {
if (!this.settingsMappings) {
this.settingsMappings = readFileAsync(resolve(__dirname, '..', 'mappings/settings.json'), 'utf-8').then(rjson.parse);
private async getSettings(): Promise<IMapperSettings> {
if (!this.settings) {
const [mappingsFile, defaultsFile] = await Promise.all([readFileAsync(resolve(__dirname, '..', 'settings/mappings.json'), 'utf-8'), readFileAsync(resolve(__dirname, '..', 'settings/defaults.json'), 'utf-8')]);
this.settings = {
mappings: rjson.parse(mappingsFile),
defaults: (rjson.parse(defaultsFile) as [[string, any]]).map((setting) => new VscodeSetting(setting[0], setting[1])),
};
}
return this.settingsMappings;
return this.settings;
}
private mapAllSettings(mappedSettings: { [key: string]: any }, sublimeSettings: { [key: string]: any }): CategorizedSettings {
private mapAllSettings(settings: IMapperSettings, sublimeSettings: { [key: string]: any }): CategorizedSettings {
const analyzedSettings: CategorizedSettings = new CategorizedSettings();
const config = this.mockConfig || vscode.workspace.getConfiguration();
for (const sublimeKey of Object.keys(sublimeSettings)) {
const sublimeSetting = { name: sublimeKey, value: sublimeSettings[sublimeKey] };
const vscodeSetting = this.mapSetting(sublimeSetting, mappedSettings[sublimeKey]);
const vscodeSetting = this.mapSetting(sublimeSetting, settings.mappings[sublimeKey]);
if (vscodeSetting) {
const configTest = this.checkWithExistingSettings(vscodeSetting, config);
const mappedSetting = new MappedSetting(sublimeSetting, vscodeSetting);
@ -55,7 +58,7 @@ export class Mapper {
analyzedSettings.noMappings.push(sublimeSetting);
}
}
return this.appendDefaultSublimeSettings(analyzedSettings, config);
return this.appendDefaultSublimeSettings(analyzedSettings, settings.defaults, config);
}
private checkWithExistingSettings(vscodeSetting: VscodeSetting, config: vscode.WorkspaceConfiguration): IConfigCheck {
@ -71,14 +74,7 @@ export class Mapper {
return returnVal;
}
private appendDefaultSublimeSettings(settings: CategorizedSettings, config: vscode.WorkspaceConfiguration): CategorizedSettings {
const defaultSettings: VscodeSetting[] = [
new VscodeSetting('editor.multiCursorModifier', 'ctrlCmd'),
new VscodeSetting('editor.snippetSuggestions', 'top'),
new VscodeSetting('editor.formatOnPaste', true),
new VscodeSetting('workbench.colorTheme', 'Monokai'),
];
private appendDefaultSublimeSettings(settings: CategorizedSettings, defaultSettings: VscodeSetting[], config: vscode.WorkspaceConfiguration): CategorizedSettings {
const mappedAndExisting: MappedSetting[] = [...settings.mappedSettings, ...settings.alreadyExisting];
// filter out default settings that will be imported as mapped settings or already exist in the user settings
const uniqueDefaultSettings = mappedAndExisting.length

Просмотреть файл

@ -13,8 +13,7 @@ suite('Importer Tests', async () => {
]);
test('Import different types', async () => {
const testMappings = Promise.resolve(testData.testMappings);
const mapper: Mapper = new Mapper(testMappings);
const mapper: Mapper = new Mapper({ mappings: testData.testMappings, defaults: [] });
const settings: CategorizedSettings = await mapper.getMappedSettings(JSON.stringify(testData.sublimeSettings));
assert.ok(settings.mappedSettings.length === 4, `mappedSettings.length is ${settings.mappedSettings.length} instead of 4`);
expected.forEach((expSetting) => {
@ -45,13 +44,19 @@ suite('Importer Tests', async () => {
}),
};
const testMappings = Promise.resolve(testData.testMappings);
const mapper: Mapper = new Mapper(testMappings, mockConfig);
const defaultSettings = [
new VscodeSetting(testData.testMappings.tab_size$test, 6), // already exists in sublime settings and should be removed
new VscodeSetting('thisShouldStay', true),
];
const mapper: Mapper = new Mapper({ mappings: testData.testMappings, defaults: defaultSettings }, mockConfig);
const sublimeSettings = JSON.stringify({ ...testData.sublimeSettings, ...testData.sublimeSettingNoMapping, ...testData.sublimeSettingSameKeyDiffVal, ...testData.sublimeSettingSameKeyVal });
const settings: CategorizedSettings = await mapper.getMappedSettings(sublimeSettings);
assert.ok(settings.alreadyExisting.length === 1);
assert.ok(settings.noMappings.length === 1);
assert.ok(settings.mappedSettings.filter(s => s.vscode.overwritesValue).length === 1);
assert.ok(settings.defaultSettings.length === 1);
assert.ok(settings.defaultSettings[0].name === 'thisShouldStay');
});
});

Просмотреть файл

@ -1,8 +1,8 @@
export const sublimeSettings = {
"draw_white_space$test": "selection",
"word_separators$test": "./\\()\"'-:,.;<>~!@#$%^&*|+=[]{}`~?",
"tab_size$test": 12,
"word_separators$test": "./\\()\"'-:,.;<>~!@#$%^&*|+=[]{}`~?",
"ensure_newline_at_eof_on_save$test": false,
"draw_white_space$test": "selection",
};
export const sublimeSettingSameKeyVal = { "sameKeySameValue": true };
@ -27,3 +27,4 @@ export const testMappings = {
"sameKeySameValue": "editor.sameKeySameValue",
"sameKeyDiffVal": "editor.sameKeyDiffVal",
};

Просмотреть файл

@ -3,7 +3,11 @@
"extends": [
"tslint:recommended"
],
"linterOptions": {
"exclude": [
"test/testData.ts"
]
},
"jsRules": {},
"rules": {
"no-string-throw": true,
@ -39,5 +43,5 @@
"avoid-escape",
"avoid-template"
]
}
},
}