Skip to content

Commit 67924de

Browse files
author
Benjamin Pasero
committed
only apply editor settings that changed (for microsoft#21487)
1 parent a74e4fe commit 67924de

3 files changed

Lines changed: 117 additions & 5 deletions

File tree

src/vs/base/common/objects.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,34 @@ export function safeStringify(obj: any): string {
291291
export function getOrDefault<T, R>(obj: T, fn: (obj: T) => R, defaultValue: R = null): R {
292292
const result = fn(obj);
293293
return typeof result === 'undefined' ? defaultValue : result;
294+
}
295+
296+
/**
297+
* Returns an object that has keys for each value that is different in the base object. Keys
298+
* that do not exist in the target but in the base object are not considered.
299+
*
300+
* Note: This is not a deep-diffing method, so the values are strictly taken into the resulting
301+
* object if they differ.
302+
*
303+
* @param base the object to diff against
304+
* @param obj the object to use for diffing
305+
*/
306+
export function distinct<T>(base: object, target: object): object {
307+
const result = Object.create(null);
308+
309+
if (!base || !target) {
310+
return result;
311+
}
312+
313+
const targetKeys = Object.keys(target);
314+
targetKeys.forEach(k => {
315+
const baseValue = base[k];
316+
const targetValue = target[k];
317+
318+
if (!equals(baseValue, targetValue)) {
319+
result[k] = targetValue;
320+
}
321+
});
322+
323+
return result;
294324
}

src/vs/base/test/common/objects.test.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,81 @@ suite('Objects', () => {
173173
someValue = 4;
174174
assert.strictEqual(child.getter, 4);
175175
});
176+
177+
test('distinct', function () {
178+
let base = {
179+
one: 'one',
180+
two: 2,
181+
three: {
182+
3: true
183+
},
184+
four: false
185+
};
186+
187+
let diff = objects.distinct(base, base);
188+
assert.deepEqual(diff, {});
189+
190+
let obj = {};
191+
192+
diff = objects.distinct(base, obj);
193+
assert.deepEqual(diff, {});
194+
195+
obj = {
196+
one: 'one',
197+
two: 2
198+
};
199+
200+
diff = objects.distinct(base, obj);
201+
assert.deepEqual(diff, {});
202+
203+
obj = {
204+
three: {
205+
3: true
206+
},
207+
four: false
208+
};
209+
210+
diff = objects.distinct(base, obj);
211+
assert.deepEqual(diff, {});
212+
213+
obj = {
214+
one: 'two',
215+
two: 2,
216+
three: {
217+
3: true
218+
},
219+
four: true
220+
};
221+
222+
diff = objects.distinct(base, obj);
223+
assert.deepEqual(diff, {
224+
one: 'two',
225+
four: true
226+
});
227+
228+
obj = {
229+
one: null,
230+
two: 2,
231+
three: {
232+
3: true
233+
},
234+
four: void 0
235+
};
236+
237+
diff = objects.distinct(base, obj);
238+
assert.deepEqual(diff, {
239+
one: null,
240+
four: void 0
241+
});
242+
243+
obj = {
244+
one: 'two',
245+
two: 3,
246+
three: { 3: false },
247+
four: true
248+
};
249+
250+
diff = objects.distinct(base, obj);
251+
assert.deepEqual(diff, obj);
252+
});
176253
});

src/vs/workbench/browser/parts/editor/textEditor.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,17 @@ export abstract class BaseTextEditor extends BaseEditor {
302302

303303
const editorConfiguration = this.computeConfiguration(configuration);
304304

305-
// Apply to control if it changed. We do not want to call updateOptions() with the same options
306-
// because it could be that from some other place editor options where changed dynamically (e.g.
307-
// word wrapping) and configurations can change as a matter of many things, not all editor related
308-
if (!this.lastAppliedEditorOptions || !objects.equals(editorConfiguration, this.lastAppliedEditorOptions)) {
305+
// Try to figure out the actual editor options that changed from the last time we updated the editor.
306+
// We do this so that we are not overwriting some dynamic editor settings (e.g. word wrap) that might
307+
// have been applied to the editor directly.
308+
let editorSettingsToApply = editorConfiguration;
309+
if (this.lastAppliedEditorOptions) {
310+
editorSettingsToApply = objects.distinct(this.lastAppliedEditorOptions, editorSettingsToApply);
311+
}
312+
313+
if (Object.keys(editorSettingsToApply).length > 0) {
309314
this.lastAppliedEditorOptions = editorConfiguration;
310-
this.editorControl.updateOptions(editorConfiguration);
315+
this.editorControl.updateOptions(editorSettingsToApply);
311316
}
312317
}
313318

0 commit comments

Comments
 (0)