src/vs/platform/undoRedo/common/undoRedo.ts

190 LOC · 188 covered · 2 uncovered · 9 ranges · 1742 concepts · 4 introducers · 866 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 > /*--------------------------------------------------------------------------------------------- undoRedo.ts ×4
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 { IDisposable } from '../../../base/common/lifecycle.js';
7 > import { URI } from '../../../base/common/uri.js';
8 > import { createDecorator } from '../../instantiation/common/instantiation.js';
9 >
10 > export const IUndoRedoService = createDecorator<IUndoRedoService>('undoRedoService');
11 >
12 > export const enum UndoRedoElementType {
13 > Resource,
14 > Workspace
15 > }
16 >
17 > export interface IResourceUndoRedoElement {
18 > readonly type: UndoRedoElementType.Resource;
19 > /**
20 > * The resource impacted by this element.
21 > */
22 > readonly resource: URI;
23 > /**
24 > * A user presentable label. May be localized.
25 > */
26 > readonly label: string;
27 > /**
28 > * A code describing the operation. Will not be localized.
29 > */
30 > readonly code: string;
31 > /**
32 > * Show a message to the user confirming when trying to undo this element
33 > */
34 > readonly confirmBeforeUndo?: boolean;
35 > undo(): Promise<void> | void;
36 > redo(): Promise<void> | void;
37 > }
38 >
39 > export interface IWorkspaceUndoRedoElement {
40 > readonly type: UndoRedoElementType.Workspace;
41 > /**
42 > * The resources impacted by this element.
43 > */
44 > readonly resources: readonly URI[];
45 > /**
46 > * A user presentable label. May be localized.
47 > */
48 > readonly label: string;
49 > /**
50 > * A code describing the operation. Will not be localized.
51 > */
52 > readonly code: string;
53 > /**
54 > * Show a message to the user confirming when trying to undo this element
55 > */
56 > readonly confirmBeforeUndo?: boolean;
57 > undo(): Promise<void> | void;
58 > redo(): Promise<void> | void;
59 >
60 > /**
61 > * If implemented, indicates that this undo/redo element can be split into multiple per resource elements.
62 > */
63 > split?(): IResourceUndoRedoElement[];
64 >
65 > /**
66 > * If implemented, will be invoked before calling `undo()` or `redo()`.
67 > * This is a good place to prepare everything such that the calls to `undo()` or `redo()` are synchronous.
68 > * If a disposable is returned, it will be invoked to clean things up.
69 > */
70 > prepareUndoRedo?(): Promise<IDisposable> | IDisposable | void;
71 > }
72 >
73 > export type IUndoRedoElement = IResourceUndoRedoElement | IWorkspaceUndoRedoElement;
74 >
75 > export interface IPastFutureElements {
76 > past: IUndoRedoElement[];
77 > future: IUndoRedoElement[];
78 > }
79 >
80 > export interface UriComparisonKeyComputer {
81 > getComparisonKey(uri: URI): string;
82 > }
83 >
84 > export class ResourceEditStackSnapshot {
85 > constructor(
86 > public readonly resource: URI, undoRedoService.ts ×2
87 > public readonly elements: number[]
88 > ) { }
90 >
91 > export class UndoRedoGroup {
92 > private static _ID = 0;
93 >
94 > public readonly id: number;
95 > private order: number;
96 >
97 > constructor() {
98 > this.id = UndoRedoGroup._ID++;
99 > this.order = 1;
100 > }
101 >
102 > public nextOrder(): number {
103 > if (this.id === 0) { undoRedo.ts ×2
104 > return 0;
105 > }
106 return this.order++;
107 > } undoRedo.ts ×2
109 > public static None = new UndoRedoGroup();
110 > }
111 >
112 > export class UndoRedoSource {
113 > private static _ID = 0;
114 >
115 > public readonly id: number;
116 > private order: number;
117 >
118 > constructor() {
119 > this.id = UndoRedoSource._ID++;
120 > this.order = 1;
121 > }
122 >
123 > public nextOrder(): number {
124 > if (this.id === 0) { undoRedoService.ts ×9
125 > return 0;
126 > }
127 return this.order++;
130 > public static None = new UndoRedoSource();
131 > }
132 >
133 > export interface IUndoRedoService {
134 > readonly _serviceBrand: undefined;
135 >
136 > /**
137 > * Register an URI -> string hasher.
138 > * This is useful for making multiple URIs share the same undo-redo stack.
139 > */
140 > registerUriComparisonKeyComputer(scheme: string, uriComparisonKeyComputer: UriComparisonKeyComputer): IDisposable;
141 >
142 > /**
143 > * Get the hash used internally for a certain URI.
144 > * This uses any registered `UriComparisonKeyComputer`.
145 > */
146 > getUriComparisonKey(resource: URI): string;
147 >
148 > /**
149 > * Add a new element to the `undo` stack.
150 > * This will destroy the `redo` stack.
151 > */
152 > pushElement(element: IUndoRedoElement, group?: UndoRedoGroup, source?: UndoRedoSource): void;
153 >
154 > /**
155 > * Get the last pushed element for a resource.
156 > * If the last pushed element has been undone, returns null.
157 > */
158 > getLastElement(resource: URI): IUndoRedoElement | null;
159 >
160 > /**
161 > * Get all the elements associated with a resource.
162 > * This includes the past and the future.
163 > */
164 > getElements(resource: URI): IPastFutureElements;
165 >
166 > /**
167 > * Validate or invalidate stack elements associated with a resource.
168 > */
169 > setElementsValidFlag(resource: URI, isValid: boolean, filter: (element: IUndoRedoElement) => boolean): void;
170 >
171 > /**
172 > * Remove elements that target `resource`.
173 > */
174 > removeElements(resource: URI): void;
175 >
176 > /**
177 > * Create a snapshot of the current elements on the undo-redo stack for a resource.
178 > */
179 > createSnapshot(resource: URI): ResourceEditStackSnapshot;
180 > /**
181 > * Attempt (as best as possible) to restore a certain snapshot previously created with `createSnapshot` for a resource.
182 > */
183 > restoreSnapshot(snapshot: ResourceEditStackSnapshot): void;
184 >
185 > canUndo(resource: URI | UndoRedoSource): boolean;
186 > undo(resource: URI | UndoRedoSource): Promise<void> | void;
187 >
188 > canRedo(resource: URI | UndoRedoSource): boolean;
189 > redo(resource: URI | UndoRedoSource): Promise<void> | void;
190 > }