src/vs/platform/userDataSync/common/snippetsMerge.ts

177 LOC · 167 covered · 10 uncovered · 58 ranges · 621 concepts · 31 introducers · 369 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 > /*--------------------------------------------------------------------------------------------- snippetsMerge.ts ×3
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> = {}; snippetsMerge.ts ×2
24 > const localUpdated: IStringDictionary<string> = {};
25 > const localRemoved: Set<string> = new Set<string>();
26 >
27 > if (!remote) {
28 > return { snippetsSync.ts ×16
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) { snippetsMerge.ts ×2
37 > // No changes found between local and remote. snippetsMerge.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 snippets in Local
55 > for (const key of baseToLocal.removed.values()) {
56 > // Conflict - Got updated in remote. snippetsMerge.ts ×2
57 > if (baseToRemote.updated.has(key)) {
58 > // Add to local snippetsMerge.ts ×1
59 > localAdded[key] = remote[key];
60 > }
61 > // Remove it in remote snippetsMerge.ts ×1
62 > else {
63 > remoteRemoved.add(key);
64 > }
67 > // Removed snippets in Remote
68 > for (const key of baseToRemote.removed.values()) {
69 > if (conflicts.has(key)) { snippetsMerge.ts ×3
70 continue;
71 }
72 > // Conflict - Got updated in local snippetsMerge.ts ×3
73 > if (baseToLocal.updated.has(key)) {
74 > conflicts.add(key); snippetsMerge.ts ×2
75 > }
76 > // Also remove in Local snippetsMerge.ts ×1
77 > else {
78 > localRemoved.add(key);
79 > }
82 > // Updated snippets in Local
83 > for (const key of baseToLocal.updated.values()) {
84 > if (conflicts.has(key)) { snippetsMerge.ts ×2
85 > continue; snippetsMerge.ts ×2
86 > }
87 > // Got updated in remote snippetsMerge.ts ×2
88 > if (baseToRemote.updated.has(key)) {
89 > // Has different value snippetsMerge.ts ×2
90 > if (localToRemote.updated.has(key)) {
91 > conflicts.add(key);
92 > }
93 > } else { snippetsMerge.ts ×2
94 > remoteUpdated[key] = local[key];
95 > }
98 > // Updated snippets in Remote
99 > for (const key of baseToRemote.updated.values()) {
100 > if (conflicts.has(key)) { snippetsMerge.ts ×2
101 > continue; snippetsMerge.ts ×2
102 > }
103 > // Got updated in local snippetsMerge.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) { snippetsMerge.ts ×2
110 > localUpdated[key] = remote[key]; snippetsMerge.ts ×1
111 > }
114 > // Added snippets in Local
115 > for (const key of baseToLocal.added.values()) {
116 > if (conflicts.has(key)) { snippetsMerge.ts ×4
117 continue;
118 }
119 > // Got added in remote snippetsMerge.ts ×4
120 > if (baseToRemote.added.has(key)) {
121 > // Has different value snippetsMerge.ts ×1
122 > if (localToRemote.updated.has(key)) {
123 > conflicts.add(key); snippetsMerge.ts ×2
124 > }
125 > } else { snippetsMerge.ts ×4
126 > remoteAdded[key] = local[key]; snippetsMerge.ts ×1
127 > }
130 > // Added snippets in remote
131 > for (const key of baseToRemote.added.values()) {
132 > if (conflicts.has(key)) { snippetsMerge.ts ×2
133 > continue; snippetsMerge.ts ×2
134 > }
135 > // Got added in local snippetsMerge.ts ×2
136 > if (baseToLocal.added.has(key)) {
137 > // Has different value snippetsMerge.ts ×1
138 > if (localToRemote.updated.has(key)) {
139 conflicts.add(key);
140 }
141 > } else { snippetsMerge.ts ×2
142 > localAdded[key] = remote[key]; snippetsMerge.ts ×1
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> } { snippetsMerge.ts ×2
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)) { snippetsMerge.ts ×3
162 > continue; snippetsMerge.ts ×1
163 > }
164 > const fromSnippet = from![key]; snippetsMerge.ts ×3
165 > const toSnippet = to![key];
166 > if (fromSnippet !== toSnippet) {
167 > updated.add(key); snippetsMerge.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); snippetsSync.ts ×4
176 > return added.size === 0 && removed.size === 0 && updated.size === 0;
177 > }