src/vs/platform/userDataSync/common/promptsSync/promptsMerge.ts

177 LOC · 167 covered · 10 uncovered · 57 ranges · 585 concepts · 29 introducers · 348 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.

1 > /*--------------------------------------------------------------------------------------------- abstractSynchronizer.ts ×49
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { IStringDictionary } from '../../../../base/common/collections.js';
7 >
8 > export interface IMergeResult {
9 > local: {
10 > added: IStringDictionary<string>;
11 > updated: IStringDictionary<string>;
12 > removed: string[];
13 > };
14 > remote: {
15 > added: IStringDictionary<string>;
16 > updated: IStringDictionary<string>;
17 > removed: string[];
18 > };
19 > conflicts: string[];
20 > }
21 >
22 > export function merge(local: IStringDictionary<string>, remote: IStringDictionary<string> | null, base: IStringDictionary<string> | null): IMergeResult {
23 > const localAdded: IStringDictionary<string> = {}; promptsSync.ts ×16
24 > const localUpdated: IStringDictionary<string> = {};
25 > const localRemoved: Set<string> = new Set<string>();
26 >
27 > if (!remote) {
28 > return {
29 > local: { added: localAdded, updated: localUpdated, removed: [...localRemoved.values()] },
30 > remote: { added: local, updated: {}, removed: [] },
31 > conflicts: []
32 > };
33 > }
35 > const localToRemote = compare(local, remote);
36 > if (localToRemote.added.size === 0 && localToRemote.removed.size === 0 && localToRemote.updated.size === 0) { promptsSync.ts ×16
37 > // No changes found between local and remote. promptsMerge.ts ×1
38 > return {
39 > local: { added: localAdded, updated: localUpdated, removed: [...localRemoved.values()] },
40 > remote: { added: {}, updated: {}, removed: [] },
41 > conflicts: []
42 > };
43 > }
45 > const baseToLocal = compare(base, local);
46 > const baseToRemote = compare(base, remote);
47 >
48 > const remoteAdded: IStringDictionary<string> = {};
49 > const remoteUpdated: IStringDictionary<string> = {};
50 > const remoteRemoved: Set<string> = new Set<string>();
51 >
52 > const conflicts: Set<string> = new Set<string>();
53 >
54 > // Removed prompts in Local
55 > for (const key of baseToLocal.removed.values()) {
56 > // Conflict - Got updated in remote. promptsMerge.ts ×2
57 > if (baseToRemote.updated.has(key)) {
58 > // Add to local promptsMerge.ts ×1
59 > localAdded[key] = remote[key];
60 > }
61 > // Remove it in remote promptsSync.ts ×2
62 > else {
63 > remoteRemoved.add(key);
64 > }
67 > // Removed prompts in Remote
68 > for (const key of baseToRemote.removed.values()) {
69 > if (conflicts.has(key)) { promptsMerge.ts ×3
70 continue;
71 }
72 > // Conflict - Got updated in local promptsMerge.ts ×3
73 > if (baseToLocal.updated.has(key)) {
74 > conflicts.add(key); promptsMerge.ts ×2
75 > }
76 > // Also remove in Local promptsMerge.ts ×1
77 > else {
78 > localRemoved.add(key);
79 > }
82 > // Updated prompts in Local
83 > for (const key of baseToLocal.updated.values()) {
84 > if (conflicts.has(key)) { promptsMerge.ts ×2
85 > continue; promptsMerge.ts ×2
86 > }
87 > // Got updated in remote promptsMerge.ts ×2
88 > if (baseToRemote.updated.has(key)) {
89 > // Has different value promptsMerge.ts ×2
90 > if (localToRemote.updated.has(key)) {
91 > conflicts.add(key);
92 > }
93 > } else { promptsMerge.ts ×2
94 > remoteUpdated[key] = local[key];
95 > }
98 > // Updated prompts in Remote
99 > for (const key of baseToRemote.updated.values()) {
100 > if (conflicts.has(key)) { promptsMerge.ts ×2
101 > continue; promptsMerge.ts ×2
102 > }
103 > // Got updated in local promptsMerge.ts ×2
104 > if (baseToLocal.updated.has(key)) {
105 // Has different value
106 if (localToRemote.updated.has(key)) {
107 conflicts.add(key);
108 }
109 > } else if (local[key] !== undefined) { promptsMerge.ts ×2
110 > localUpdated[key] = remote[key]; promptsMerge.ts ×1
111 > }
114 > // Added prompts in Local
115 > for (const key of baseToLocal.added.values()) {
116 > if (conflicts.has(key)) { promptsMerge.ts ×4
117 continue;
118 }
119 > // Got added in remote promptsMerge.ts ×4
120 > if (baseToRemote.added.has(key)) {
121 > // Has different value promptsMerge.ts ×1
122 > if (localToRemote.updated.has(key)) {
123 > conflicts.add(key); promptsMerge.ts ×2
124 > }
125 > } else { promptsMerge.ts ×4
126 > remoteAdded[key] = local[key]; promptsMerge.ts ×1
127 > }
130 > // Added prompts in remote
131 > for (const key of baseToRemote.added.values()) {
132 > if (conflicts.has(key)) { promptsMerge.ts ×2
133 > continue; promptsMerge.ts ×2
134 > }
135 > // Got added in local promptsMerge.ts ×2
136 > if (baseToLocal.added.has(key)) {
137 > // Has different value promptsMerge.ts ×1
138 > if (localToRemote.updated.has(key)) {
139 conflicts.add(key);
140 }
141 > } else { promptsMerge.ts ×2
142 > localAdded[key] = remote[key]; promptsSync.ts ×2
143 > }
146 > return {
147 > local: { added: localAdded, removed: [...localRemoved.values()], updated: localUpdated },
148 > remote: { added: remoteAdded, removed: [...remoteRemoved.values()], updated: remoteUpdated },
149 > conflicts: [...conflicts.values()],
150 > };
151 > }
153 > function compare(from: IStringDictionary<string> | null, to: IStringDictionary<string> | null): { added: Set<string>; removed: Set<string>; updated: Set<string> } { promptsSync.ts ×4
154 > const fromKeys = from ? Object.keys(from) : [];
155 > const toKeys = to ? Object.keys(to) : [];
156 > const added = toKeys.filter(key => !fromKeys.includes(key)).reduce((r, key) => { r.add(key); return r; }, new Set<string>());
157 > const removed = fromKeys.filter(key => !toKeys.includes(key)).reduce((r, key) => { r.add(key); return r; }, new Set<string>());
158 > const updated: Set<string> = new Set<string>();
159 >
160 > for (const key of fromKeys) {
161 > if (removed.has(key)) { promptsMerge.ts ×3
162 > continue; promptsMerge.ts ×1
163 > }
164 > const fromPrompt = from![key]; promptsMerge.ts ×3
165 > const toPrompt = to![key];
166 > if (fromPrompt !== toPrompt) {
167 > updated.add(key); promptsMerge.ts ×1
168 > }
171 > return { added, removed, updated };
172 > }
174 > export function areSame(a: IStringDictionary<string>, b: IStringDictionary<string>): boolean {
175 > const { added, removed, updated } = compare(a, b); promptsSync.ts ×4
176 > return added.size === 0 && removed.size === 0 && updated.size === 0;
177 > }