1
>
/*---------------------------------------------------------------------------------------------
fileEditDiff.ts
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 { URI } from '../../../base/common/uri.js';
7
>
import { isEqual } from '../../../base/common/resources.js';
8
>
import type { FileEdit } from './state/protocol/state.js';
9
>
import { FileEditKind } from './state/sessionState.js';
10
>
11
>
/**
12
>
* A {@link FileEdit} decoded into parsed URIs and a resolved {@link FileEditKind}.
13
>
*
14
>
* The create/delete/rename/edit detection and the "primary resource" rule
15
>
* (after-URI for create/edit/rename, before-URI for delete) are subtle and
16
>
* were historically duplicated across several adapters. {@link normalizeFileEdit}
17
>
* centralizes them so every consumer derives the same shape from a protocol
18
>
* {@link FileEdit}.
19
>
*/
20
>
export interface INormalizedFileEdit {
21
>
/** The kind of file operation. */
22
>
readonly kind: FileEditKind;
23
>
/** Primary file URI: after-URI for create/edit/rename, before-URI for delete. */
24
>
readonly resource: URI;
25
>
/** The before-state file URI, when present (absent for creates). */
26
>
readonly beforeUri?: URI;
27
>
/** The after-state file URI, when present (absent for deletes). */
28
>
readonly afterUri?: URI;
29
>
/** URI from which the before-content can be read (absent for creates). */
30
>
readonly beforeContentUri?: URI;
31
>
/** URI from which the after-content can be read (absent for deletes). */
32
>
readonly afterContentUri?: URI;
33
>
}
34
>
35
>
/**
36
>
* Decodes a protocol {@link FileEdit} into parsed URIs and a resolved
37
>
* {@link FileEditKind}. Returns `undefined` when the edit carries no usable
38
>
* file URI (neither `before` nor `after`).
39
>
*/
40
>
export function normalizeFileEdit(edit: FileEdit): INormalizedFileEdit | undefined {
41
>
const beforeUri = edit.before ? URI.parse(edit.before.uri) : undefined;
42
>
const afterUri = edit.after ? URI.parse(edit.after.uri) : undefined;
43
>
44
>
const resource = afterUri ?? beforeUri;
45
>
if (!resource) {
46
return undefined;
47
}
48
49
let kind: FileEditKind;
51
kind = FileEditKind.Create;
52
} else if (beforeUri && !afterUri) {