未验证 提交 ce8d9ee4 编写于 作者: M Matt Bierner 提交者: GitHub

Shared more properties between TextDocument and CustomDocument (#93342)

* Expose more shared properties from TextDocument on CustomDocument

Copies some shared properties from `TextDocument` to `CustomDocument`. We may want to consider this since `CustomDocument` is intented to be subclassed, and there is the risk after shipping the custom editor api that one of these subclasses could implement a property/method such as `version` which would break if we added a `CustomDocument.version` property at a later time
上级 65003808
......@@ -1449,9 +1449,20 @@ declare module 'vscode' {
readonly uri: Uri;
/**
* Event fired when there are no more references to the `CustomDocument`.
* Is this document representing an untitled file which has never been saved yet.
*/
readonly onDidDispose: Event<void>;
readonly isUntitled: boolean;
/**
* The version number of this document (it will strictly increase after each
* change, including undo/redo).
*/
readonly version: number;
/**
* `true` if there are unpersisted changes.
*/
readonly isDirty: boolean;
/**
* List of edits from document open to the document's current state.
......@@ -1465,6 +1476,17 @@ declare module 'vscode' {
* or in front of the last entry in `appliedEdits` if the user saves and then hits undo.
*/
readonly savedEdits: ReadonlyArray<EditType>;
/**
* `true` if the document has been closed. A closed document isn't synchronized anymore
* and won't be re-used when the same resource is opened again.
*/
readonly isClosed: boolean;
/**
* Event fired when there are no more references to the `CustomDocument`.
*/
readonly onDidDispose: Event<void>;
}
/**
......
......@@ -17,6 +17,7 @@ import { RemoteAuthorityResolverErrorCode } from 'vs/platform/remote/common/remo
import type * as vscode from 'vscode';
import { Cache } from './cache';
import { assertIsDefined } from 'vs/base/common/types';
import { Schemas } from 'vs/base/common/network';
function es5ClassCompat(target: Function): any {
///@ts-ignore
......@@ -2591,22 +2592,22 @@ interface EditState {
export class CustomDocument<EditType = unknown> implements vscode.CustomDocument<EditType> {
readonly #edits = new Cache<EditType>('edits');
#editState: EditState;
readonly #viewType: string;
readonly #uri: vscode.Uri;
#editState: EditState = {
allEdits: [],
currentIndex: -1,
saveIndex: -1,
};
#isDisposed = false;
#version = 1;
constructor(viewType: string, uri: vscode.Uri) {
this.#viewType = viewType;
this.#uri = uri;
this.#editState = {
allEdits: [],
currentIndex: 0,
saveIndex: 0
};
}
//#region Public API
......@@ -2615,15 +2616,27 @@ export class CustomDocument<EditType = unknown> implements vscode.CustomDocument
public get uri(): vscode.Uri { return this.#uri; }
public get fileName(): string { return this.uri.fsPath; }
public get isUntitled() { return this.uri.scheme === Schemas.untitled; }
#onDidDispose = new Emitter<void>();
public readonly onDidDispose = this.#onDidDispose.event;
get appliedEdits() {
public get isClosed() { return this.#isDisposed; }
public get version() { return this.#version; }
public get isDirty() {
return this.#editState.currentIndex !== this.#editState.saveIndex;
}
public get appliedEdits() {
return this.#editState.allEdits.slice(0, this.#editState.currentIndex + 1)
.map(id => this._getEdit(id));
}
get savedEdits() {
public get savedEdits() {
return this.#editState.allEdits.slice(0, this.#editState.saveIndex + 1)
.map(id => this._getEdit(id));
}
......@@ -2631,11 +2644,13 @@ export class CustomDocument<EditType = unknown> implements vscode.CustomDocument
//#endregion
/** @internal */ _dispose(): void {
this.#isDisposed = true;
this.#onDidDispose.fire();
this.#onDidDispose.dispose();
}
/** @internal */ _updateEditState(state: EditState) {
++this.#version;
this.#editState = state;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册