src/vs/platform/userDataSync/common/settingsMerge.ts
656 LOC · 631 covered · 25 uncovered · 221 ranges · 735 concepts · 118 introducers · 419 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
settingsMerge.ts ×19
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { distinct } from '../../../base/common/arrays.js';
import { IStringDictionary } from '../../../base/common/collections.js';
import { JSONVisitor, parse, visit } from '../../../base/common/json.js';
import { applyEdits, setProperty, withFormatting } from '../../../base/common/jsonEdit.js';
import { Edit, FormattingOptions, getEOL } from '../../../base/common/jsonFormatter.js';
import * as objects from '../../../base/common/objects.js';
import { IConfigurationService } from '../../configuration/common/configuration.js';
import * as contentUtil from './content.js';
import { getDisallowedIgnoredSettings, IConflictSetting } from './userDataSync.js';
export interface IMergeResult {
localContent: string | null;
remoteContent: string | null;
hasConflicts: boolean;
conflictsSettings: IConflictSetting[];
}
export function getIgnoredSettings(defaultIgnoredSettings: string[], configurationService: IConfigurationService, settingsContent?: string): string[] {
if (settingsContent) {
value = getIgnoredSettingsFromConfig(configurationService);
}
const added: string[] = [], removed: string[] = [...getDisallowedIgnoredSettings()];
if (Array.isArray(value)) {
for (const key of value) {
removed.push(key.substring(1));
added.push(key);
}
}
return distinct([...defaultIgnoredSettings, ...added,].filter(setting => !removed.includes(setting)));
}
function getIgnoredSettingsFromConfig(configurationService: IConfigurationService): ReadonlyArray<string> {
settingsSync.ts ×10
let userValue = configurationService.inspect<string[]>('settingsSync.ignoredSettings').userValue;
if (userValue !== undefined) {
}
userValue = configurationService.inspect<string[]>('sync.ignoredSettings').userValue;
settingsMerge.ts ×2
if (userValue !== undefined) {
return userValue;
}
return configurationService.getValue<string[]>('settingsSync.ignoredSettings') || [];
settingsMerge.ts ×2
const parsed = parse(settingsContent);
return parsed ? parsed['settingsSync.ignoredSettings'] || parsed['sync.ignoredSettings'] || [] : [];
}
export function removeComments(content: string, formattingOptions: FormattingOptions): string {
const source = parse(content) || {};
let result = '{}';
for (const key of Object.keys(source)) {
const edits = setProperty(result, [key], source[key], formattingOptions);
result = applyEdits(result, edits);
}
return result;
}
export function updateIgnoredSettings(targetContent: string, sourceContent: string, ignoredSettings: string[], formattingOptions: FormattingOptions): string {
const source = parse(sourceContent) || {};
const target = parse(targetContent);
if (!target) {
}
for (const key of ignoredSettings) {
const sourceValue = source[key];
const targetValue = target[key];
// Remove in target
if (sourceValue === undefined) {
targetContent = contentUtil.edit(targetContent, [key], undefined, formattingOptions);
settingsMerge.ts ×1
}
// Update in target
else if (targetValue !== undefined) {
targetContent = contentUtil.edit(targetContent, [key], sourceValue, formattingOptions);
settingsMerge.ts ×1
}
else {
settingsToAdd.push(findSettingNode(key, sourceTree)!);
}
settingsToAdd.sort((a, b) => a.startOffset - b.startOffset);
settingsToAdd.forEach(s => targetContent = addSetting(s.setting!.key, sourceContent, targetContent, formattingOptions));
}
}
export function merge(originalLocalContent: string, originalRemoteContent: string, baseContent: string | null, ignoredSettings: string[], resolvedConflicts: { key: string; value: any | undefined }[], formattingOptions: FormattingOptions): IMergeResult {
const localContentWithoutIgnoredSettings = updateIgnoredSettings(originalLocalContent, originalRemoteContent, ignoredSettings, formattingOptions);
const localForwarded = baseContent !== localContentWithoutIgnoredSettings;
const remoteForwarded = baseContent !== originalRemoteContent;
/* no changes */
if (!localForwarded && !remoteForwarded) {
return { conflictsSettings: [], localContent: null, remoteContent: null, hasConflicts: false };
settingsMerge.ts ×1
}
/* local has changed and remote has not */
return { conflictsSettings: [], localContent: null, remoteContent: localContentWithoutIgnoredSettings, hasConflicts: false };
settingsMerge.ts ×1
}
/* remote has changed and local has not */
return { conflictsSettings: [], localContent: updateIgnoredSettings(originalRemoteContent, originalLocalContent, ignoredSettings, formattingOptions), remoteContent: null, hasConflicts: false };
settingsMerge.ts ×1
}
/* local is empty and not synced before */
const localContent = areSame(originalLocalContent, originalRemoteContent, ignoredSettings) ? null : updateIgnoredSettings(originalRemoteContent, originalLocalContent, ignoredSettings, formattingOptions);
settingsMerge.ts ×1
return { conflictsSettings: [], localContent, remoteContent: null, hasConflicts: false };
}
/* remote and local has changed */
let localContent = originalLocalContent;
let remoteContent = originalRemoteContent;
const local = parse(originalLocalContent);
const remote = parse(originalRemoteContent);
const ignored = ignoredSettings.reduce((set, key) => { set.add(key); return set; }, new Set<string>());
const localToRemote = compare(local, remote, ignored);
const baseToLocal = compare(base, local, ignored);
const baseToRemote = compare(base, remote, ignored);
const conflicts: Map<string, IConflictSetting> = new Map<string, IConflictSetting>();
const handledConflicts: Set<string> = new Set<string>();
const handleConflict = (conflictKey: string): void => {
const resolvedConflict = resolvedConflicts.filter(({ key }) => key === conflictKey)[0];
if (resolvedConflict) {
localContent = contentUtil.edit(localContent, [conflictKey], resolvedConflict.value, formattingOptions);
settingsMerge.ts ×1
remoteContent = contentUtil.edit(remoteContent, [conflictKey], resolvedConflict.value, formattingOptions);
conflicts.set(conflictKey, { key: conflictKey, localValue: local[conflictKey], remoteValue: remote[conflictKey] });
}
};
// Removed settings in Local
for (const key of baseToLocal.removed.values()) {
if (baseToRemote.updated.has(key)) {
}
else {
remoteContent = contentUtil.edit(remoteContent, [key], undefined, formattingOptions);
}
// Removed settings in Remote
for (const key of baseToRemote.removed.values()) {
continue;
}
if (baseToLocal.updated.has(key)) {
}
else {
localContent = contentUtil.edit(localContent, [key], undefined, formattingOptions);
}
// Updated settings in Local
for (const key of baseToLocal.updated.values()) {
}
if (baseToRemote.updated.has(key)) {
if (localToRemote.updated.has(key)) {
}
remoteContent = contentUtil.edit(remoteContent, [key], local[key], formattingOptions);
settingsMerge.ts ×1
}
// Updated settings in Remote
for (const key of baseToRemote.updated.values()) {
}
if (baseToLocal.updated.has(key)) {
if (localToRemote.updated.has(key)) {
handleConflict(key);
}
localContent = contentUtil.edit(localContent, [key], remote[key], formattingOptions);
settingsMerge.ts ×1
}
// Added settings in Local
for (const key of baseToLocal.added.values()) {
continue;
}
if (baseToRemote.added.has(key)) {
if (localToRemote.updated.has(key)) {
}
remoteContent = addSetting(key, localContent, remoteContent, formattingOptions);
settingsMerge.ts ×1
}
// Added settings in remote
for (const key of baseToRemote.added.values()) {
}
if (baseToLocal.added.has(key)) {
if (localToRemote.updated.has(key)) {
handleConflict(key);
}
localContent = addSetting(key, remoteContent, localContent, formattingOptions);
settingsMerge.ts ×1
}
const hasConflicts = conflicts.size > 0 || !areSame(localContent, remoteContent, ignoredSettings);
const hasLocalChanged = hasConflicts || !areSame(localContent, originalLocalContent, []);
settingsMerge.ts ×7
const hasRemoteChanged = hasConflicts || !areSame(remoteContent, originalRemoteContent, []);
return { localContent: hasLocalChanged ? localContent : null, remoteContent: hasRemoteChanged ? remoteContent : null, conflictsSettings: [...conflicts.values()], hasConflicts };
}
function areSame(localContent: string, remoteContent: string, ignoredSettings: string[]): boolean {
settingsMerge.ts ×1
if (localContent === remoteContent) {
}
const local = parse(localContent);
const remote = parse(remoteContent);
const ignored = ignoredSettings.reduce((set, key) => { set.add(key); return set; }, new Set<string>());
const localTree = parseSettings(localContent).filter(node => !(node.setting && ignored.has(node.setting.key)));
const remoteTree = parseSettings(remoteContent).filter(node => !(node.setting && ignored.has(node.setting.key)));
if (localTree.length !== remoteTree.length) {
}
for (let index = 0; index < localTree.length; index++) {
const remoteNode = remoteTree[index];
if (localNode.setting && remoteNode.setting) {
}
if (!objects.equals(local[localNode.setting.key], remote[localNode.setting.key])) {
settingsMerge.ts ×1
return false;
}
return false;
}
} else {
return false;
}
return true;
}
export function isEmpty(content: string): boolean {
const nodes = parseSettings(content);
return nodes.length === 0;
}
return true;
}
function compare(from: IStringDictionary<any> | null, to: IStringDictionary<any>, ignored: Set<string>): { added: Set<string>; removed: Set<string>; updated: Set<string> } {
settingsMerge.ts ×9
const fromKeys = from ? Object.keys(from).filter(key => !ignored.has(key)) : [];
const toKeys = Object.keys(to).filter(key => !ignored.has(key));
const added = toKeys.filter(key => !fromKeys.includes(key)).reduce((r, key) => { r.add(key); return r; }, new Set<string>());
const removed = fromKeys.filter(key => !toKeys.includes(key)).reduce((r, key) => { r.add(key); return r; }, new Set<string>());
const updated: Set<string> = new Set<string>();
if (from) {
for (const key of fromKeys) {
}
const value2 = to[key];
if (!objects.equals(value1, value2)) {
}
return { added, removed, updated };
}
export function addSetting(key: string, sourceContent: string, targetContent: string, formattingOptions: FormattingOptions): string {
const sourceTree = parseSettings(sourceContent);
const targetTree = parseSettings(targetContent);
const insertLocation = getInsertLocation(key, sourceTree, targetTree);
return insertAtLocation(targetContent, key, source[key], insertLocation, targetTree, formattingOptions);
}
interface InsertLocation {
index: number;
insertAfter: boolean;
}
function getInsertLocation(key: string, sourceTree: INode[], targetTree: INode[]): InsertLocation {
settingsMerge.ts ×5
const sourceNodeIndex = sourceTree.findIndex(node => node.setting?.key === key);
const sourcePreviousNode: INode = sourceTree[sourceNodeIndex - 1];
if (sourcePreviousNode) {
Previous node in source is a setting.
Find the same setting in the target.
Insert it after that setting
*/
if (sourcePreviousNode.setting) {
const targetPreviousSetting = findSettingNode(sourcePreviousNode.setting.key, targetTree);
settingsMerge.ts ×2
if (targetPreviousSetting) {
return { index: targetTree.indexOf(targetPreviousSetting), insertAfter: true };
}
else {
const sourcePreviousSettingNode = findPreviousSettingNode(sourceNodeIndex, sourceTree);
/*
Source has a setting defined before the setting to be added.
Find the same previous setting in the target.
If found, insert before its next setting so that comments are retrieved.
Otherwise, insert at the end.
*/
if (sourcePreviousSettingNode) {
const targetPreviousSetting = findSettingNode(sourcePreviousSettingNode.setting!.key, targetTree);
settingsMerge.ts ×3
if (targetPreviousSetting) {
const targetNextSetting = findNextSettingNode(targetTree.indexOf(targetPreviousSetting), targetTree);
settingsMerge.ts ×3
const sourceCommentNodes = findNodesBetween(sourceTree, sourcePreviousSettingNode, sourceTree[sourceNodeIndex]);
if (targetNextSetting) {
const targetCommentNodes = findNodesBetween(targetTree, targetPreviousSetting, targetNextSetting);
settingsMerge.ts ×2
const targetCommentNode = findLastMatchingTargetCommentNode(sourceCommentNodes, targetCommentNodes);
if (targetCommentNode) {
return { index: targetTree.indexOf(targetCommentNode), insertAfter: true }; /* Insert after comment */
settingsMerge.ts ×1
return { index: targetTree.indexOf(targetNextSetting), insertAfter: false }; /* Insert before target next setting */
settingsMerge.ts ×1
}
const targetCommentNodes = findNodesBetween(targetTree, targetPreviousSetting, targetTree[targetTree.length - 1]);
settingsMerge.ts ×3
const targetCommentNode = findLastMatchingTargetCommentNode(sourceCommentNodes, targetCommentNodes);
if (targetCommentNode) {
return { index: targetTree.indexOf(targetCommentNode), insertAfter: true }; /* Insert after comment */
settingsMerge.ts ×1
return { index: targetTree.length - 1, insertAfter: true }; /* Insert at the end */
settingsMerge.ts ×1
}
const sourceNextNode = sourceTree[sourceNodeIndex + 1];
if (sourceNextNode) {
Next node in source is a setting.
Find the same setting in the target.
Insert it before that setting
*/
if (sourceNextNode.setting) {
const targetNextSetting = findSettingNode(sourceNextNode.setting.key, targetTree);
settingsMerge.ts ×1
if (targetNextSetting) {
/* Insert before target's next setting */
return { index: targetTree.indexOf(targetNextSetting), insertAfter: false };
}
}
else {
const sourceNextSettingNode = findNextSettingNode(sourceNodeIndex, sourceTree);
/*
Source has a setting defined after the setting to be added.
Find the same next setting in the target.
If found, insert after its previous setting so that comments are retrieved.
Otherwise, insert at the beginning.
*/
if (sourceNextSettingNode) {
const targetNextSetting = findSettingNode(sourceNextSettingNode.setting!.key, targetTree);
if (targetNextSetting) {
const targetPreviousSetting = findPreviousSettingNode(targetTree.indexOf(targetNextSetting), targetTree);
const sourceCommentNodes = findNodesBetween(sourceTree, sourceTree[sourceNodeIndex], sourceNextSettingNode);
if (targetPreviousSetting) {
const targetCommentNodes = findNodesBetween(targetTree, targetPreviousSetting, targetNextSetting);
settingsMerge.ts ×2
const targetCommentNode = findLastMatchingTargetCommentNode(sourceCommentNodes.reverse(), targetCommentNodes.reverse());
if (targetCommentNode) {
return { index: targetTree.indexOf(targetCommentNode), insertAfter: false }; /* Insert before comment */
settingsMerge.ts ×1
return { index: targetTree.indexOf(targetPreviousSetting), insertAfter: true }; /* Insert after target previous setting */
settingsMerge.ts ×1
}
const targetCommentNodes = findNodesBetween(targetTree, targetTree[0], targetNextSetting);
settingsMerge.ts ×3
const targetCommentNode = findLastMatchingTargetCommentNode(sourceCommentNodes.reverse(), targetCommentNodes.reverse());
if (targetCommentNode) {
return { index: targetTree.indexOf(targetCommentNode), insertAfter: false }; /* Insert before comment */
settingsMerge.ts ×1
}
}
}
return { index: targetTree.length - 1, insertAfter: true };
}
function insertAtLocation(content: string, key: string, value: any, location: InsertLocation, tree: INode[], formattingOptions: FormattingOptions): string {
settingsMerge.ts ×5
let edits: Edit[];
/* Insert at the end */
if (location.index === -1) {
edits = getEditToInsertAtLocation(content, key, value, location, tree, formattingOptions).map(edit => withFormatting(content, edit, formattingOptions)[0]);
settingsMerge.ts ×3
}
}
function getEditToInsertAtLocation(content: string, key: string, value: any, location: InsertLocation, tree: INode[], formattingOptions: FormattingOptions): Edit[] {
settingsMerge.ts ×3
const newProperty = `${JSON.stringify(key)}: ${JSON.stringify(value)}`;
const eol = getEOL(formattingOptions, content);
const node = tree[location.index];
if (location.insertAfter) {
const edits: Edit[] = [];
/* Insert after a setting */
if (node.setting) {
edits.push({ offset: node.endOffset, length: 0, content: ',' + newProperty });
settingsMerge.ts ×1
}
/* Insert after a comment */
else {
const nextSettingNode = findNextSettingNode(location.index, tree);
const previousSettingNode = findPreviousSettingNode(location.index, tree);
const previousSettingCommaOffset = previousSettingNode?.setting?.commaOffset;
/* If there is a previous setting and it does not has comma then add it */
if (previousSettingNode && previousSettingCommaOffset === undefined) {
edits.push({ offset: previousSettingNode.endOffset, length: 0, content: ',' });
jsonEdit.ts ×2
}
const isPreviouisSettingIncludesComment = previousSettingCommaOffset !== undefined && previousSettingCommaOffset > node.endOffset;
edits.push({
offset: isPreviouisSettingIncludesComment ? previousSettingCommaOffset + 1 : node.endOffset,
length: 0,
content: nextSettingNode ? eol + newProperty + ',' : eol + newProperty
});
}
return edits;
}
else {
/* Insert before a setting */
if (node.setting) {
return [{ offset: node.startOffset, length: 0, content: newProperty + ',' }];
settingsMerge.ts ×1
}
/* Insert before a comment */
const content = (tree[location.index - 1] && !tree[location.index - 1].setting /* previous node is comment */ ? eol : '')
settingsMerge.ts ×2
+ newProperty
+ (findNextSettingNode(location.index, tree) ? ',' : '')
+ eol;
return [{ offset: node.startOffset, length: 0, content }];
}
}
return tree.filter(node => node.setting?.key === key)[0];
}
function findPreviousSettingNode(index: number, tree: INode[]): INode | undefined {
settingsMerge.ts ×2
for (let i = index - 1; i >= 0; i--) {
if (tree[i].setting) {
}
}
function findNextSettingNode(index: number, tree: INode[]): INode | undefined {
settingsMerge.ts ×1
for (let i = index + 1; i < tree.length; i++) {
}
}
function findNodesBetween(nodes: INode[], from: INode, till: INode): INode[] {
settingsMerge.ts ×4
const fromIndex = nodes.indexOf(from);
const tillIndex = nodes.indexOf(till);
return nodes.filter((node, index) => fromIndex < index && index < tillIndex);
}
function findLastMatchingTargetCommentNode(sourceComments: INode[], targetComments: INode[]): INode | undefined {
settingsMerge.ts ×4
if (sourceComments.length && targetComments.length) {
for (; index < targetComments.length && index < sourceComments.length; index++) {
if (sourceComments[index].value !== targetComments[index].value) {
}
}
}
interface INode {
readonly startOffset: number;
readonly endOffset: number;
readonly value: string;
readonly setting?: {
readonly key: string;
readonly commaOffset: number | undefined;
};
readonly comment?: string;
}
const nodes: INode[] = [];
let hierarchyLevel = -1;
let startOffset: number;
let key: string;
const visitor: JSONVisitor = {
onObjectBegin: (offset: number) => {
},
// this is setting key
startOffset = offset;
key = name;
}
},
if (hierarchyLevel === 0) {
startOffset,
endOffset: offset + length,
value: content.substring(startOffset, offset + length),
setting: {
key,
commaOffset: undefined
}
});
}
},
if (hierarchyLevel === 0) {
startOffset,
endOffset: offset + length,
value: content.substring(startOffset, offset + length),
setting: {
key,
commaOffset: undefined
}
});
}
nodes.push({
startOffset,
endOffset: offset + length,
value: content.substring(startOffset, offset + length),
setting: {
key,
commaOffset: undefined
}
});
}
},
if (sep === ',') {
for (; index >= 0; index--) {
if (nodes[index].setting) {
break;
}
}
const node = nodes[index];
if (node) {
nodes.splice(index, 1, {
startOffset: node.startOffset,
endOffset: node.endOffset,
value: node.value,
setting: {
key: node.setting!.key,
commaOffset: offset
}
});
}
}
},
nodes.push({
startOffset: offset,
endOffset: offset + length,
value: content.substring(offset, offset + length),
});
}
}
visit(content, visitor);
return nodes;
}